blob: 74751f793c0f2878cce2e1bad7e9fad88206b7e7 [file] [log] [blame]
Shawn Willdend67afae2014-08-19 12:36:27 -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
Shawn Willdena278f612014-12-23 11:22:21 -070017#include "asymmetric_key.h"
18
Shawn Willden0f906ec2015-06-20 09:16:30 -060019#include <new>
20
Shawn Willdenf268d742014-08-19 15:36:26 -060021#include <openssl/x509.h>
22
Shawn Willden567a4a02014-12-31 12:14:46 -070023#include "openssl_err.h"
Shawn Willdend67afae2014-08-19 12:36:27 -060024#include "openssl_utils.h"
Shawn Willdend67afae2014-08-19 12:36:27 -060025
26namespace keymaster {
27
Shawn Willdend67afae2014-08-19 12:36:27 -060028keymaster_error_t AsymmetricKey::key_material(UniquePtr<uint8_t[]>* material, size_t* size) const {
29 if (material == NULL || size == NULL)
30 return KM_ERROR_OUTPUT_PARAMETER_NULL;
31
32 UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey(EVP_PKEY_new());
33 if (pkey.get() == NULL)
34 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
35
36 if (!InternalToEvp(pkey.get()))
Shawn Willden567a4a02014-12-31 12:14:46 -070037 return TranslateLastOpenSslError();
Shawn Willdend67afae2014-08-19 12:36:27 -060038
39 *size = i2d_PrivateKey(pkey.get(), NULL /* key_data*/);
40 if (*size <= 0)
Shawn Willden567a4a02014-12-31 12:14:46 -070041 return TranslateLastOpenSslError();
Shawn Willdend67afae2014-08-19 12:36:27 -060042
Shawn Willden0f906ec2015-06-20 09:16:30 -060043 material->reset(new (std::nothrow) uint8_t[*size]);
Shawn Willdend67afae2014-08-19 12:36:27 -060044 uint8_t* tmp = material->get();
45 i2d_PrivateKey(pkey.get(), &tmp);
46
47 return KM_ERROR_OK;
48}
49
Shawn Willdenf268d742014-08-19 15:36:26 -060050keymaster_error_t AsymmetricKey::formatted_key_material(keymaster_key_format_t format,
51 UniquePtr<uint8_t[]>* material,
Shawn Willdend67afae2014-08-19 12:36:27 -060052 size_t* size) const {
Shawn Willdenf268d742014-08-19 15:36:26 -060053 if (format != KM_KEY_FORMAT_X509)
54 return KM_ERROR_UNSUPPORTED_KEY_FORMAT;
55
Shawn Willdend67afae2014-08-19 12:36:27 -060056 if (material == NULL || size == NULL)
57 return KM_ERROR_OUTPUT_PARAMETER_NULL;
58
Shawn Willdenf268d742014-08-19 15:36:26 -060059 UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey(EVP_PKEY_new());
60 if (!InternalToEvp(pkey.get()))
Shawn Willden567a4a02014-12-31 12:14:46 -070061 return TranslateLastOpenSslError();
Shawn Willdenf268d742014-08-19 15:36:26 -060062
63 int key_data_length = i2d_PUBKEY(pkey.get(), NULL);
64 if (key_data_length <= 0)
Shawn Willden567a4a02014-12-31 12:14:46 -070065 return TranslateLastOpenSslError();
Shawn Willdenf268d742014-08-19 15:36:26 -060066
Shawn Willden0f906ec2015-06-20 09:16:30 -060067 material->reset(new (std::nothrow) uint8_t[key_data_length]);
Shawn Willdenf268d742014-08-19 15:36:26 -060068 if (material->get() == NULL)
69 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
70
71 uint8_t* tmp = material->get();
72 if (i2d_PUBKEY(pkey.get(), &tmp) != key_data_length) {
73 material->reset();
Shawn Willden567a4a02014-12-31 12:14:46 -070074 return TranslateLastOpenSslError();
Shawn Willdenf268d742014-08-19 15:36:26 -060075 }
76
77 *size = key_data_length;
78 return KM_ERROR_OK;
Shawn Willdend67afae2014-08-19 12:36:27 -060079}
80
Shawn Willdend67afae2014-08-19 12:36:27 -060081} // namespace keymaster