Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame] | 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 | #ifndef SYSTEM_KEYMASTER_OPENSSL_UTILS_H_ |
| 18 | #define SYSTEM_KEYMASTER_OPENSSL_UTILS_H_ |
| 19 | |
| 20 | #include <openssl/evp.h> |
| 21 | #include <openssl/bn.h> |
| 22 | |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 23 | #include <UniquePtr.h> |
| 24 | |
Shawn Willden | b9d584d | 2015-01-22 16:35:00 -0700 | [diff] [blame^] | 25 | #include <hardware/keymaster_defs.h> |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 26 | |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame] | 27 | struct EVP_PKEY_Delete { |
| 28 | void operator()(EVP_PKEY* p) const { EVP_PKEY_free(p); } |
| 29 | }; |
| 30 | |
| 31 | struct BIGNUM_Delete { |
| 32 | void operator()(BIGNUM* p) const { BN_free(p); } |
| 33 | }; |
| 34 | |
| 35 | /** |
| 36 | * Many OpenSSL APIs take ownership of an argument on success but don't free the argument on |
| 37 | * failure. This means we need to tell our scoped pointers when we've transferred ownership, without |
| 38 | * triggering a warning by not using the result of release(). |
| 39 | */ |
| 40 | template <typename T, typename Delete_T> |
| 41 | inline void release_because_ownership_transferred(UniquePtr<T, Delete_T>& p) { |
| 42 | T* val __attribute__((unused)) = p.release(); |
| 43 | } |
| 44 | |
| 45 | inline void convert_bn_to_blob(BIGNUM* bn, keymaster_blob_t* blob) { |
| 46 | blob->data_length = BN_num_bytes(bn); |
| 47 | blob->data = new uint8_t[blob->data_length]; |
| 48 | BN_bn2bin(bn, const_cast<uint8_t*>(blob->data)); |
| 49 | } |
| 50 | |
| 51 | #endif // SYSTEM_KEYMASTER_OPENSSL_UTILS_H_ |