blob: 0706ce77ba70a67e34c61a44fb888921768a64dc [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 Willden437fbd12014-08-20 11:59:49 -060017#include <openssl/x509.h>
18
Shawn Willdend67afae2014-08-19 12:36:27 -060019#include "asymmetric_key.h"
20#include "key_blob.h"
Shawn Willden437fbd12014-08-20 11:59:49 -060021#include "openssl_utils.h"
Shawn Willdend67afae2014-08-19 12:36:27 -060022
23#include "key.h"
24
25namespace keymaster {
26
Shawn Willden437fbd12014-08-20 11:59:49 -060027struct PKCS8_PRIV_KEY_INFO_Delete {
28 void operator()(PKCS8_PRIV_KEY_INFO* p) const { PKCS8_PRIV_KEY_INFO_free(p); }
29};
30
Shawn Willdend67afae2014-08-19 12:36:27 -060031Key::Key(const KeyBlob& blob) {
32 authorizations_.push_back(blob.unenforced());
33 authorizations_.push_back(blob.enforced());
34}
35
36/* static */
37Key* Key::CreateKey(const KeyBlob& blob, keymaster_error_t* error) {
38 switch (blob.algorithm()) {
39 case KM_ALGORITHM_RSA:
40 return new RsaKey(blob, error);
41 case KM_ALGORITHM_DSA:
42 return new DsaKey(blob, error);
43 case KM_ALGORITHM_ECDSA:
44 return new EcdsaKey(blob, error);
45 default:
46 *error = KM_ERROR_UNSUPPORTED_ALGORITHM;
47 return NULL;
48 }
49}
50
51/* static */
52Key* Key::GenerateKey(const AuthorizationSet& key_description, keymaster_error_t* error) {
53 keymaster_algorithm_t algorithm;
54 if (!key_description.GetTagValue(TAG_ALGORITHM, &algorithm)) {
55 *error = KM_ERROR_UNSUPPORTED_ALGORITHM;
56 return NULL;
57 }
58
59 switch (algorithm) {
60 case KM_ALGORITHM_RSA:
61 return RsaKey::GenerateKey(key_description, error);
62 case KM_ALGORITHM_DSA:
63 return DsaKey::GenerateKey(key_description, error);
64 case KM_ALGORITHM_ECDSA:
65 return EcdsaKey::GenerateKey(key_description, error);
66 default:
67 *error = KM_ERROR_UNSUPPORTED_ALGORITHM;
68 return NULL;
69 }
70}
71
Shawn Willden437fbd12014-08-20 11:59:49 -060072/* static */
73Key* Key::ImportKey(const AuthorizationSet& key_description, keymaster_key_format_t key_format,
74 const uint8_t* key_data, size_t key_data_length, keymaster_error_t* error) {
75 *error = KM_ERROR_OK;
76
77 if (key_data == NULL || key_data_length <= 0) {
78 *error = KM_ERROR_INVALID_KEY_BLOB;
79 return NULL;
80 }
81
82 if (key_format != KM_KEY_FORMAT_PKCS8) {
83 *error = KM_ERROR_UNSUPPORTED_KEY_FORMAT;
84 return NULL;
85 }
86
87 UniquePtr<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_Delete> pkcs8(
88 d2i_PKCS8_PRIV_KEY_INFO(NULL, &key_data, key_data_length));
89 if (pkcs8.get() == NULL) {
90 *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
91 return NULL;
92 }
93
94 UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey(EVP_PKCS82PKEY(pkcs8.get()));
95 if (pkey.get() == NULL) {
96 *error = KM_ERROR_INVALID_KEY_BLOB;
97 return NULL;
98 }
99
100 UniquePtr<Key> key;
101 switch (EVP_PKEY_type(pkey->type)) {
102 case EVP_PKEY_RSA:
103 return RsaKey::ImportKey(key_description, pkey.get(), error);
104 case EVP_PKEY_DSA:
105 case EVP_PKEY_EC:
106 default:
107 *error = KM_ERROR_UNSUPPORTED_ALGORITHM;
108 return NULL;
109 }
110
111 *error = KM_ERROR_UNIMPLEMENTED;
112 return NULL;
113}
114
Shawn Willdend67afae2014-08-19 12:36:27 -0600115} // namespace keymaster