botan: Add support for Ed25519 keys
[strongswan.git] / src / libstrongswan / plugins / botan / botan_util_keys.c
1 /*
2 * Copyright (C) 2018 Tobias Brunner
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24 #include "botan_util_keys.h"
25 #include "botan_ec_public_key.h"
26 #include "botan_ec_private_key.h"
27 #include "botan_ed_public_key.h"
28 #include "botan_ed_private_key.h"
29 #include "botan_rsa_public_key.h"
30 #include "botan_rsa_private_key.h"
31
32 #include <asn1/asn1.h>
33 #include <asn1/oid.h>
34
35 /**
36 * Get the algorithm name of a public key
37 */
38 static char *get_algo_name(botan_pubkey_t pubkey)
39 {
40 char *name;
41 size_t len = 0;
42
43 if (botan_pubkey_algo_name(pubkey, NULL, &len)
44 != BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE)
45 {
46 return NULL;
47 }
48
49 name = malloc(len);
50 if (botan_pubkey_algo_name(pubkey, name, &len))
51 {
52 free(name);
53 return NULL;
54 }
55 return name;
56 }
57
58 /*
59 * Described in header
60 */
61 public_key_t *botan_public_key_load(key_type_t type, va_list args)
62 {
63 public_key_t *this = NULL;
64 botan_pubkey_t pubkey;
65 chunk_t blob = chunk_empty;
66 botan_rng_t rng;
67 char *name;
68
69 while (TRUE)
70 {
71 switch (va_arg(args, builder_part_t))
72 {
73 case BUILD_BLOB_ASN1_DER:
74 blob = va_arg(args, chunk_t);
75 continue;
76 case BUILD_END:
77 break;
78 default:
79 return NULL;
80 }
81 break;
82 }
83
84 if (botan_rng_init(&rng, "user"))
85 {
86 return NULL;
87 }
88 if (botan_pubkey_load(&pubkey, blob.ptr, blob.len))
89 {
90 botan_rng_destroy(rng);
91 return NULL;
92 }
93 if (botan_pubkey_check_key(pubkey, rng, BOTAN_CHECK_KEY_EXPENSIVE_TESTS))
94 {
95 DBG1(DBG_LIB, "public key failed key checks");
96 botan_pubkey_destroy(pubkey);
97 botan_rng_destroy(rng);
98 return NULL;
99 }
100 botan_rng_destroy(rng);
101
102 name = get_algo_name(pubkey);
103 if (!name)
104 {
105 botan_pubkey_destroy(pubkey);
106 return NULL;
107 }
108
109 if (streq(name, "RSA") && (type == KEY_ANY || type == KEY_RSA))
110 {
111 this = (public_key_t*)botan_rsa_public_key_adopt(pubkey);
112 }
113 else if (streq(name, "ECDSA") && (type == KEY_ANY || type == KEY_ECDSA))
114 {
115 this = (public_key_t*)botan_ec_public_key_adopt(pubkey);
116 }
117 else if (streq(name, "Ed25519") && (type == KEY_ANY || type == KEY_ED25519))
118 {
119 this = botan_ed_public_key_adopt(pubkey);
120 }
121 else
122 {
123 botan_pubkey_destroy(pubkey);
124 }
125 free(name);
126 return this;
127 }
128
129 /**
130 * Determine the curve OID from a PKCS#8 structure
131 */
132 static int determine_ec_oid(chunk_t pkcs8)
133 {
134 int oid = OID_UNKNOWN;
135 chunk_t inner, params = chunk_empty;
136
137 if (asn1_unwrap(&pkcs8, &pkcs8) == ASN1_SEQUENCE &&
138 asn1_unwrap(&pkcs8, &inner) == ASN1_INTEGER &&
139 asn1_parse_integer_uint64(inner) == 0 &&
140 asn1_parse_algorithmIdentifier(pkcs8, 0, &params) == OID_EC_PUBLICKEY &&
141 params.len &&
142 asn1_unwrap(&params, &params) == ASN1_OID)
143 {
144 oid = asn1_known_oid(params);
145 }
146 return oid;
147 }
148
149 /*
150 * Described in header
151 */
152 private_key_t *botan_private_key_load(key_type_t type, va_list args)
153 {
154 private_key_t *this = NULL;
155 botan_privkey_t key;
156 botan_pubkey_t pubkey;
157 chunk_t blob = chunk_empty;
158 botan_rng_t rng;
159 char *name;
160 int oid;
161
162 while (TRUE)
163 {
164 switch (va_arg(args, builder_part_t))
165 {
166 case BUILD_BLOB_ASN1_DER:
167 blob = va_arg(args, chunk_t);
168 continue;
169 case BUILD_END:
170 break;
171 default:
172 return NULL;
173 }
174 break;
175 }
176
177 if (botan_rng_init(&rng, "user"))
178 {
179 return NULL;
180 }
181 if (botan_privkey_load(&key, rng, blob.ptr, blob.len, NULL))
182 {
183 botan_rng_destroy(rng);
184 return NULL;
185 }
186 botan_rng_destroy(rng);
187
188 if (botan_privkey_export_pubkey(&pubkey, key))
189 {
190 botan_privkey_destroy(key);
191 return NULL;
192 }
193 name = get_algo_name(pubkey);
194 botan_pubkey_destroy(pubkey);
195 if (!name)
196 {
197 botan_privkey_destroy(key);
198 return NULL;
199 }
200 if (streq(name, "RSA") && (type == KEY_ANY || type == KEY_RSA))
201 {
202 this = (private_key_t*)botan_rsa_private_key_adopt(key);
203 }
204 else if (streq(name, "ECDSA") && (type == KEY_ANY || type == KEY_ECDSA))
205 {
206 oid = determine_ec_oid(blob);
207 if (oid != OID_UNKNOWN)
208 {
209 this = (private_key_t*)botan_ec_private_key_adopt(key, oid);
210 }
211 }
212 else if (streq(name, "Ed25519") && (type == KEY_ANY || type == KEY_ED25519))
213 {
214 this = botan_ed_private_key_adopt(key);
215 }
216 if (!this)
217 {
218 botan_privkey_destroy(key);
219 }
220 free(name);
221 return this;
222 }