1 /*
2  * Copyright 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <openssl/bn.h>
20 #include <openssl/ec.h>
21 #include <openssl/engine.h>
22 #include <openssl/evp.h>
23 #include <openssl/rsa.h>
24 #include <openssl/x509.h>
25 
26 #include <keymaster/UniquePtr.h>
27 #include <keymaster/authorization_set.h>
28 
29 #include <hardware/keymaster_defs.h>
30 
31 namespace keymaster {
32 
33 template <typename BlobType> struct TKeymasterBlob;
34 typedef TKeymasterBlob<keymaster_key_blob_t> KeymasterKeyBlob;
35 
36 class EvpMdCtxCleaner {
37   public:
EvpMdCtxCleaner(EVP_MD_CTX * ctx)38     explicit EvpMdCtxCleaner(EVP_MD_CTX* ctx) : ctx_(ctx) {}
~EvpMdCtxCleaner()39     ~EvpMdCtxCleaner() { EVP_MD_CTX_cleanup(ctx_); }
40 
41   private:
42     EVP_MD_CTX* ctx_;
43 };
44 
45 template <typename T, typename FreeFuncRet, FreeFuncRet (*FreeFunc)(T*)>
46 struct OpenSslObjectDeleter {
operatorOpenSslObjectDeleter47     void operator()(T* p) { FreeFunc(p); }
48 };
49 
50 #define DEFINE_OPENSSL_OBJECT_POINTER(name)                                                        \
51     typedef OpenSslObjectDeleter<name, decltype(name##_free(nullptr)), name##_free> name##_Delete; \
52     typedef UniquePtr<name, name##_Delete> name##_Ptr;
53 
54 DEFINE_OPENSSL_OBJECT_POINTER(ASN1_BIT_STRING)
55 DEFINE_OPENSSL_OBJECT_POINTER(ASN1_INTEGER)
56 DEFINE_OPENSSL_OBJECT_POINTER(ASN1_OBJECT)
57 DEFINE_OPENSSL_OBJECT_POINTER(ASN1_OCTET_STRING)
58 DEFINE_OPENSSL_OBJECT_POINTER(ASN1_TIME)
59 DEFINE_OPENSSL_OBJECT_POINTER(BN_CTX)
60 DEFINE_OPENSSL_OBJECT_POINTER(EC_GROUP)
61 DEFINE_OPENSSL_OBJECT_POINTER(EC_KEY)
62 DEFINE_OPENSSL_OBJECT_POINTER(EC_POINT)
63 DEFINE_OPENSSL_OBJECT_POINTER(ENGINE)
64 DEFINE_OPENSSL_OBJECT_POINTER(EVP_PKEY)
65 DEFINE_OPENSSL_OBJECT_POINTER(EVP_PKEY_CTX)
66 DEFINE_OPENSSL_OBJECT_POINTER(PKCS8_PRIV_KEY_INFO)
67 DEFINE_OPENSSL_OBJECT_POINTER(RSA)
68 DEFINE_OPENSSL_OBJECT_POINTER(X509)
69 DEFINE_OPENSSL_OBJECT_POINTER(X509_EXTENSION)
70 DEFINE_OPENSSL_OBJECT_POINTER(X509_NAME)
71 
72 typedef OpenSslObjectDeleter<BIGNUM, void, BN_free> BIGNUM_Delete;
73 typedef UniquePtr<BIGNUM, BIGNUM_Delete> BIGNUM_Ptr;
74 
75 keymaster_error_t ec_get_group_size(const EC_GROUP* group, size_t* key_size_bits);
76 EC_GROUP* ec_get_group(keymaster_ec_curve_t curve);
77 
78 /**
79  * Many OpenSSL APIs take ownership of an argument on success but don't free the argument on
80  * failure. This means we need to tell our scoped pointers when we've transferred ownership, without
81  * triggering a warning by not using the result of release().
82  */
83 template <typename T, typename Delete_T>
release_because_ownership_transferred(UniquePtr<T,Delete_T> & p)84 inline void release_because_ownership_transferred(UniquePtr<T, Delete_T>& p) {
85     T* val __attribute__((unused)) = p.release();
86 }
87 
88 keymaster_error_t convert_pkcs8_blob_to_evp(const uint8_t* key_data, size_t key_length,
89                                             keymaster_algorithm_t expected_algorithm,
90                                             UniquePtr<EVP_PKEY, EVP_PKEY_Delete>* pkey);
91 
92 keymaster_error_t KeyMaterialToEvpKey(keymaster_key_format_t key_format,
93                                       const KeymasterKeyBlob& key_material,
94                                       keymaster_algorithm_t expected_algorithm,
95                                       UniquePtr<EVP_PKEY, EVP_PKEY_Delete>* evp_pkey);
96 
97 keymaster_error_t EvpKeyToKeyMaterial(const EVP_PKEY* evp_pkey, KeymasterKeyBlob* key_blob);
98 
99 keymaster_error_t GetEcdsa256KeyFromCert(const keymaster_blob_t* km_cert, uint8_t* x_coord,
100                                          size_t x_length, uint8_t* y_coord, size_t y_length);
101 
102 size_t ec_group_size_bits(EC_KEY* ec_key);
103 
104 keymaster_error_t GenerateRandom(uint8_t* buf, size_t length);
105 
IsCertSigningKey(const AuthorizationSet & key_description)106 inline bool IsCertSigningKey(const AuthorizationSet& key_description) {
107     return key_description.Contains(TAG_PURPOSE, KM_PURPOSE_SIGN) ||
108            key_description.Contains(TAG_PURPOSE, KM_PURPOSE_ATTEST_KEY);
109 }
110 
KmDigestToEvpDigest(keymaster_digest_t digest)111 inline const EVP_MD* KmDigestToEvpDigest(keymaster_digest_t digest) {
112     switch (digest) {
113     case KM_DIGEST_MD5:
114         return EVP_md5();
115     case KM_DIGEST_SHA1:
116         return EVP_sha1();
117     case KM_DIGEST_SHA_2_224:
118         return EVP_sha224();
119     case KM_DIGEST_SHA_2_256:
120         return EVP_sha256();
121     case KM_DIGEST_SHA_2_384:
122         return EVP_sha384();
123     case KM_DIGEST_SHA_2_512:
124         return EVP_sha512();
125     default:
126         return nullptr;
127     }
128 }
129 
130 }  // namespace keymaster
131