• 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(std::string keyPath,uint8_t digestMethod)47     SignAlgorithm(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     virtual int32_t VerifyBuffer(const std::vector<uint8_t> &digest, const std::vector<uint8_t> &signature) = 0;
53 
54 protected:
55     std::string keyName_ {};
56     uint8_t digestMethod_ = PKG_DIGEST_TYPE_SHA256;
57 };
58 
59 class VerifyAlgorithm : public SignAlgorithm {
60 public:
VerifyAlgorithm(std::string keyPath,uint8_t digestMethod)61     VerifyAlgorithm(std::string keyPath, uint8_t digestMethod) : SignAlgorithm(keyPath, digestMethod) {}
62 
~VerifyAlgorithm()63     ~VerifyAlgorithm() override {}
64 
SignBuffer(const PkgBuffer & buffer,std::vector<uint8_t> & sign,size_t & signLen)65     int32_t SignBuffer(const PkgBuffer &buffer, std::vector<uint8_t> &sign, size_t &signLen) const override
66     {
67         UNUSED(buffer);
68         UNUSED(sign);
69         UNUSED(signLen);
70         return PKG_INVALID_SIGNATURE;
71     }
72 
73     int32_t VerifyBuffer(const std::vector<uint8_t> &digest, const std::vector<uint8_t> &signature) override;
74 
75 private:
76     bool CheckEccKey(const EC_KEY *eccKey) const;
77 
78     bool CheckRsaKey(const RSA *rsakey, int &hashLen) const;
79 
80     bool LoadPubKey(const std::string &filename, struct CertKeySt &certs) const;
81 };
82 
83 class SignAlgorithmRsa : public SignAlgorithm {
84 public:
SignAlgorithmRsa(const std::string & keyPath,uint8_t digestMethod)85     SignAlgorithmRsa(const std::string &keyPath, uint8_t digestMethod) : SignAlgorithm(keyPath, digestMethod) {}
86 
~SignAlgorithmRsa()87     ~SignAlgorithmRsa() override {}
88 
89     int32_t SignBuffer(const PkgBuffer &buffer, std::vector<uint8_t> &sign, size_t &signLen) const override;
90 
VerifyBuffer(const std::vector<uint8_t> & digest,const std::vector<uint8_t> & signature)91     int32_t VerifyBuffer(const std::vector<uint8_t> &digest, const std::vector<uint8_t> &signature) override
92     {
93         UNUSED(digest);
94         UNUSED(signature);
95         return PKG_INVALID_SIGNATURE;
96     }
97 };
98 
99 class SignAlgorithmEcc : public SignAlgorithm {
100 public:
SignAlgorithmEcc(const std::string & keyPath,uint8_t digestMethod)101     SignAlgorithmEcc(const std::string &keyPath, uint8_t digestMethod) : SignAlgorithm(keyPath, digestMethod) {}
102 
~SignAlgorithmEcc()103     ~SignAlgorithmEcc() override {}
104 
105     int32_t SignBuffer(const PkgBuffer &buffer, std::vector<uint8_t> &sign, size_t &signLen) const override;
106 
VerifyBuffer(const std::vector<uint8_t> & digest,const std::vector<uint8_t> & signature)107     int32_t VerifyBuffer(const std::vector<uint8_t> &digest, const std::vector<uint8_t> &signature) override
108     {
109         UNUSED(digest);
110         UNUSED(signature);
111         return PKG_INVALID_SIGNATURE;
112     }
113 };
114 } // namespace hpackage
115 #endif
116