• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     static const std::string PLUGINID_OID;
36     static const std::string PLUGINID_OID_SHORT_NAME;
37     static const std::string PLUGINID_OID_LONG_NAME;
38 
39     static int ParseOwnerIdFromSignature(const ByteBuffer &sigbuffer, std::string &ownerID);
40     static int ParsePluginIdFromSignature(const ByteBuffer &sigbuffer, std::string &pluginID);
41     bool InitSignerInfo(const std::string &ownerID, X509 *cert, const EVP_MD *md, const ByteBuffer &contentData,
42                         bool carrySigningTime = false);
43     bool AddSignatureInSignerInfo(const ByteBuffer &signature);
44     uint8_t *GetDataToSign(uint32_t &len);
45     PKCS7_SIGNER_INFO *GetSignerInfo();
46     int AddOwnerID(const std::string &ownerID);
47 
48 private:
49     bool AddAttrsToSignerInfo(const std::string &ownerID, const ByteBuffer &contentData);
50     bool ComputeDigest(const ByteBuffer &data, ByteBuffer &digest);
51     int GetSignAlgorithmID(const X509 *cert);
52     int AddID(const std::string &id, int nid);
53     static int ParseIdFromSignature(const ByteBuffer &sigbuffer, std::string &id, int nid);
54 
55     PKCS7_SIGNER_INFO *p7info_ = nullptr;
56     const EVP_MD *md_ = nullptr;
57     bool carrySigningTime_ = false;
58     std::unique_ptr<ByteBuffer> unsignedData_ = nullptr;
59 };
60 }
61 }
62 }
63 #endif