• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 PKCS7_SIGNED_DATA_H
17 #define PKCS7_SIGNED_DATA_H
18 
19 #include <vector>
20 #include <openssl/pkcs7.h>
21 #include <openssl/x509.h>
22 #include "pkg_manager.h"
23 
24 namespace Hpackage {
25 struct Pkcs7SignerInfo {
26     X509_NAME *issuerName = nullptr;
27     ASN1_INTEGER *serialNumber = nullptr;
28     int32_t digestNid {};
29     int32_t digestEncryptNid {};
30     std::vector<uint8_t> digestEncryptData;
31 };
32 
33 class Pkcs7SignedData {
34 public:
Pkcs7SignedData()35     Pkcs7SignedData() : pkcs7_(nullptr), digest_(), signerInfos_() {}
36 
37     ~Pkcs7SignedData();
38 
39     int32_t GetHashFromSignBlock(const uint8_t *srcData, const size_t dataLen,
40         std::vector<uint8_t> &hash);
41 
42     int32_t ParsePkcs7Data(const uint8_t *srcData, const size_t dataLen);
43 
44     int32_t Verify() const;
45 
46 private:
47     int32_t Init(const uint8_t *sourceData, const uint32_t sourceDataLen);
48     int32_t DoParse();
49     int32_t ParseContentInfo(std::vector<uint8_t> &digestBlock) const;
50     int32_t GetDigestFromContentInfo(std::vector<uint8_t> &digestBlock);
51     int32_t SignerInfosParse();
52     int32_t SignerInfoParse(PKCS7_SIGNER_INFO *p7SignerInfo, Pkcs7SignerInfo &signerInfo);
53     int32_t Pkcs7SignleSignerVerify(const Pkcs7SignerInfo &signerInfo) const;
54     int32_t VerifyDigest(X509 *cert, const Pkcs7SignerInfo &signer) const;
55 
56 private:
57     PKCS7 *pkcs7_;
58     std::vector<uint8_t> digest_;
59     std::vector<Pkcs7SignerInfo> signerInfos_;
60 };
61 } // namespace Hpackage
62 #endif
63