blob: 9df320ca39eb7db16221c9f63c034f2ef0a3e8dd [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 Willdenf268d742014-08-19 15:36:26 -060019#include <openssl/x509.h>
20
Shawn Willden567a4a02014-12-31 12:14:46 -070021#include "openssl_err.h"
Shawn Willdend67afae2014-08-19 12:36:27 -060022#include "openssl_utils.h"
Shawn Willdend67afae2014-08-19 12:36:27 -060023
24namespace keymaster {
25
Shawn Willdend67afae2014-08-19 12:36:27 -060026keymaster_error_t AsymmetricKey::key_material(UniquePtr<uint8_t[]>* material, size_t* size) const {
27 if (material == NULL || size == NULL)
28 return KM_ERROR_OUTPUT_PARAMETER_NULL;
29
30 UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey(EVP_PKEY_new());
31 if (pkey.get() == NULL)
32 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
33
34 if (!InternalToEvp(pkey.get()))
Shawn Willden567a4a02014-12-31 12:14:46 -070035 return TranslateLastOpenSslError();
Shawn Willdend67afae2014-08-19 12:36:27 -060036
37 *size = i2d_PrivateKey(pkey.get(), NULL /* key_data*/);
38 if (*size <= 0)
Shawn Willden567a4a02014-12-31 12:14:46 -070039 return TranslateLastOpenSslError();
Shawn Willdend67afae2014-08-19 12:36:27 -060040
41 material->reset(new uint8_t[*size]);
42 uint8_t* tmp = material->get();
43 i2d_PrivateKey(pkey.get(), &tmp);
44
45 return KM_ERROR_OK;
46}
47
Shawn Willdenf268d742014-08-19 15:36:26 -060048keymaster_error_t AsymmetricKey::formatted_key_material(keymaster_key_format_t format,
49 UniquePtr<uint8_t[]>* material,
Shawn Willdend67afae2014-08-19 12:36:27 -060050 size_t* size) const {
Shawn Willdenf268d742014-08-19 15:36:26 -060051 if (format != KM_KEY_FORMAT_X509)
52 return KM_ERROR_UNSUPPORTED_KEY_FORMAT;
53
Shawn Willdend67afae2014-08-19 12:36:27 -060054 if (material == NULL || size == NULL)
55 return KM_ERROR_OUTPUT_PARAMETER_NULL;
56
Shawn Willdenf268d742014-08-19 15:36:26 -060057 UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey(EVP_PKEY_new());
58 if (!InternalToEvp(pkey.get()))
Shawn Willden567a4a02014-12-31 12:14:46 -070059 return TranslateLastOpenSslError();
Shawn Willdenf268d742014-08-19 15:36:26 -060060
61 int key_data_length = i2d_PUBKEY(pkey.get(), NULL);
62 if (key_data_length <= 0)
Shawn Willden567a4a02014-12-31 12:14:46 -070063 return TranslateLastOpenSslError();
Shawn Willdenf268d742014-08-19 15:36:26 -060064
65 material->reset(new uint8_t[key_data_length]);
66 if (material->get() == NULL)
67 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
68
69 uint8_t* tmp = material->get();
70 if (i2d_PUBKEY(pkey.get(), &tmp) != key_data_length) {
71 material->reset();
Shawn Willden567a4a02014-12-31 12:14:46 -070072 return TranslateLastOpenSslError();
Shawn Willdenf268d742014-08-19 15:36:26 -060073 }
74
75 *size = key_data_length;
76 return KM_ERROR_OK;
Shawn Willdend67afae2014-08-19 12:36:27 -060077}
78
Shawn Willdend67afae2014-08-19 12:36:27 -060079} // namespace keymaster