• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #ifndef PKG_ALGORITHM_SIGN_H
16 #define PKG_ALGORITHM_SIGN_H
17 
18 #include <openssl/bio.h>
19 #include <openssl/bn.h>
20 #include <openssl/ec.h>
21 #include <openssl/ecdsa.h>
22 #include <openssl/evp.h>
23 #include <openssl/obj_mac.h>
24 #include <openssl/pem.h>
25 #include <openssl/rsa.h>
26 #include <openssl/sha.h>
27 #include "pkg_utils.h"
28 
29 namespace Hpackage {
30 enum KEYTYPE {
31     KEY_TYPE_RSA,
32     KEY_TYPE_EC,
33 };
34 
35 struct CertKeySt {
36     X509 *cert;
37     int hashLen;
38     KEYTYPE keyType;
39     RSA *rsa;
40     EC_KEY *ecKey;
41 };
42 
43 class SignAlgorithm {
44 public:
45     using SignAlgorithmPtr = std::shared_ptr<SignAlgorithm>;
46 
SignAlgorithm(const std::string keyPath,uint8_t digestMethod)47     SignAlgorithm(const std::string keyPath, uint8_t digestMethod) : keyName_(keyPath), digestMethod_(digestMethod) {}
48 
~SignAlgorithm()49     virtual ~SignAlgorithm() {}
50 
51     virtual int32_t SignBuffer(const PkgBuffer &buffer, std::vector<uint8_t> &sign, size_t &signLen) const = 0;
52 
53     int32_t VerifyDigest(const std::vector<uint8_t> &digest, const std::vector<uint8_t> &signature);
54 
55 protected:
56     X509 *GetPubkey() const;
57     std::string keyName_ {};
58     uint8_t digestMethod_ = PKG_DIGEST_TYPE_SHA256;
59 };
60 
61 class VerifyAlgorithm : public SignAlgorithm {
62 public:
VerifyAlgorithm(const std::string keyPath,uint8_t digestMethod)63     VerifyAlgorithm(const std::string keyPath, uint8_t digestMethod) : SignAlgorithm(keyPath, digestMethod) {}
64 
~VerifyAlgorithm()65     ~VerifyAlgorithm() override {}
66 
SignBuffer(const PkgBuffer & buffer,std::vector<uint8_t> & sign,size_t & signLen)67     int32_t SignBuffer(const PkgBuffer &buffer, std::vector<uint8_t> &sign, size_t &signLen) const override
68     {
69         UNUSED(buffer);
70         UNUSED(sign);
71         UNUSED(signLen);
72         return PKG_INVALID_SIGNATURE;
73     }
74 };
75 
76 class SignAlgorithmRsa : public SignAlgorithm {
77 public:
SignAlgorithmRsa(const std::string & keyPath,uint8_t digestMethod)78     SignAlgorithmRsa(const std::string &keyPath, uint8_t digestMethod) : SignAlgorithm(keyPath, digestMethod) {}
79 
~SignAlgorithmRsa()80     ~SignAlgorithmRsa() override {}
81 
82     int32_t SignBuffer(const PkgBuffer &buffer, std::vector<uint8_t> &sign, size_t &signLen) const override;
83 };
84 
85 class SignAlgorithmEcc : public SignAlgorithm {
86 public:
SignAlgorithmEcc(const std::string & keyPath,uint8_t digestMethod)87     SignAlgorithmEcc(const std::string &keyPath, uint8_t digestMethod) : SignAlgorithm(keyPath, digestMethod) {}
88 
~SignAlgorithmEcc()89     ~SignAlgorithmEcc() override {}
90 
91     int32_t SignBuffer(const PkgBuffer &buffer, std::vector<uint8_t> &sign, size_t &signLen) const override;
92 };
93 } // namespace Hpackage
94 #endif
95