blob: 6c863dfdba3cdb9d1cdfb0632d2b93a5f9dc8f61 [file] [log] [blame]
Shawn Willden28e41472014-08-18 13:35:22 -06001/*
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#ifndef SYSTEM_KEYMASTER_OPENSSL_UTILS_H_
18#define SYSTEM_KEYMASTER_OPENSSL_UTILS_H_
19
20#include <openssl/evp.h>
21#include <openssl/bn.h>
Shawn Willden4d306ec2015-03-04 07:29:49 -070022#include <openssl/x509.h>
Shawn Willden28e41472014-08-18 13:35:22 -060023
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060024#include <UniquePtr.h>
25
Shawn Willdenb9d584d2015-01-22 16:35:00 -070026#include <hardware/keymaster_defs.h>
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060027
Shawn Willden4d306ec2015-03-04 07:29:49 -070028namespace keymaster {
29
Shawn Willden28e41472014-08-18 13:35:22 -060030struct EVP_PKEY_Delete {
31 void operator()(EVP_PKEY* p) const { EVP_PKEY_free(p); }
32};
33
34struct BIGNUM_Delete {
35 void operator()(BIGNUM* p) const { BN_free(p); }
36};
37
Shawn Willden4d306ec2015-03-04 07:29:49 -070038struct PKCS8_PRIV_KEY_INFO_Delete {
39 void operator()(PKCS8_PRIV_KEY_INFO* p) const { PKCS8_PRIV_KEY_INFO_free(p); }
40};
41
Shawn Willden28e41472014-08-18 13:35:22 -060042/**
43 * Many OpenSSL APIs take ownership of an argument on success but don't free the argument on
44 * failure. This means we need to tell our scoped pointers when we've transferred ownership, without
45 * triggering a warning by not using the result of release().
46 */
47template <typename T, typename Delete_T>
48inline void release_because_ownership_transferred(UniquePtr<T, Delete_T>& p) {
49 T* val __attribute__((unused)) = p.release();
50}
51
Shawn Willden4d306ec2015-03-04 07:29:49 -070052void convert_bn_to_blob(BIGNUM* bn, keymaster_blob_t* blob);
53
54keymaster_error_t convert_pkcs8_blob_to_evp(const uint8_t* key_data, size_t key_length,
55 keymaster_algorithm_t expected_algorithm,
56 UniquePtr<EVP_PKEY, EVP_PKEY_Delete>* pkey);
57
58} // namespace keymaster
Shawn Willden28e41472014-08-18 13:35:22 -060059
60#endif // SYSTEM_KEYMASTER_OPENSSL_UTILS_H_