1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef CODE_SIGN_SIGNER_INFO_H
17 #define CODE_SIGN_SIGNER_INFO_H
18 
19 #include <vector>
20 #include <string>
21 #include <openssl/evp.h>
22 #include <openssl/pkcs7.h>
23 #include <openssl/x509.h>
24 
25 #include "byte_buffer.h"
26 
27 namespace OHOS {
28 namespace Security {
29 namespace CodeSign {
30 class SignerInfo {
31 public:
32     static const std::string OWNERID_OID;
33     static const std::string OWNERID_OID_SHORT_NAME;
34     static const std::string OWNERID_OID_LONG_NAME;
35 
36     static int ParseOwnerIdFromSignature(const ByteBuffer &sigbuffer, std::string &ownerID);
37     bool InitSignerInfo(const std::string &ownerID, X509 *cert, const EVP_MD *md, const ByteBuffer &contentData,
38                         bool carrySigningTime = false);
39     bool AddSignatureInSignerInfo(const ByteBuffer &signature);
40     uint8_t *GetDataToSign(uint32_t &len);
41     PKCS7_SIGNER_INFO *GetSignerInfo();
42     int AddOwnerID(const std::string &ownerID);
43 
44 private:
45     bool AddAttrsToSignerInfo(const std::string &ownerID, const ByteBuffer &contentData);
46     bool ComputeDigest(const ByteBuffer &data, ByteBuffer &digest);
47     int GetSignAlgorithmID(const X509 *cert);
48 
49     PKCS7_SIGNER_INFO *p7info_ = nullptr;
50     const EVP_MD *md_ = nullptr;
51     bool carrySigningTime_ = false;
52     std::unique_ptr<ByteBuffer> unsignedData_ = nullptr;
53 };
54 }
55 }
56 }
57 #endif