]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. | |
3 | * | |
4 | * Licensed under the Apache License 2.0 (the "License"). You may not use | |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
8 | */ | |
9 | ||
10 | /* | |
11 | * DSA low level APIs are deprecated for public use, but still ok for | |
12 | * internal use. | |
13 | */ | |
14 | #include "internal/deprecated.h" | |
15 | ||
16 | #include <stdio.h> | |
17 | #include "internal/cryptlib.h" | |
18 | #include <openssl/asn1t.h> | |
19 | #include <openssl/x509.h> | |
20 | #include "crypto/asn1.h" | |
21 | #include "crypto/evp.h" | |
22 | #include "crypto/x509.h" | |
23 | #include <openssl/rsa.h> | |
24 | #include <openssl/dsa.h> | |
25 | #include <openssl/decoder.h> | |
26 | #include <openssl/encoder.h> | |
27 | #include "internal/provider.h" | |
28 | #include "internal/sizes.h" | |
29 | ||
30 | struct X509_pubkey_st { | |
31 | X509_ALGOR *algor; | |
32 | ASN1_BIT_STRING *public_key; | |
33 | ||
34 | EVP_PKEY *pkey; | |
35 | ||
36 | /* extra data for the callback, used by d2i_PUBKEY_ex */ | |
37 | OSSL_LIB_CTX *libctx; | |
38 | char *propq; | |
39 | ||
40 | /* Flag to force legacy keys */ | |
41 | unsigned int flag_force_legacy : 1; | |
42 | }; | |
43 | ||
44 | static int x509_pubkey_decode(EVP_PKEY **pk, const X509_PUBKEY *key); | |
45 | ||
46 | static int x509_pubkey_set0_libctx(X509_PUBKEY *x, OSSL_LIB_CTX *libctx, | |
47 | const char *propq) | |
48 | { | |
49 | if (x != NULL) { | |
50 | x->libctx = libctx; | |
51 | OPENSSL_free(x->propq); | |
52 | x->propq = NULL; | |
53 | if (propq != NULL) { | |
54 | x->propq = OPENSSL_strdup(propq); | |
55 | if (x->propq == NULL) | |
56 | return 0; | |
57 | } | |
58 | } | |
59 | return 1; | |
60 | } | |
61 | ||
62 | ASN1_SEQUENCE(X509_PUBKEY_INTERNAL) = { | |
63 | ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR), | |
64 | ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING) | |
65 | } static_ASN1_SEQUENCE_END_name(X509_PUBKEY, X509_PUBKEY_INTERNAL) | |
66 | ||
67 | static void x509_pubkey_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) | |
68 | { | |
69 | X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval; | |
70 | ||
71 | X509_ALGOR_free(pubkey->algor); | |
72 | ASN1_BIT_STRING_free(pubkey->public_key); | |
73 | EVP_PKEY_free(pubkey->pkey); | |
74 | OPENSSL_free(pubkey); | |
75 | *pval = NULL; | |
76 | } | |
77 | ||
78 | static int x509_pubkey_ex_populate(ASN1_VALUE **pval, const ASN1_ITEM *it) | |
79 | { | |
80 | X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval; | |
81 | ||
82 | return (pubkey->algor != NULL | |
83 | || (pubkey->algor = X509_ALGOR_new()) != NULL) | |
84 | && (pubkey->public_key != NULL | |
85 | || (pubkey->public_key = ASN1_BIT_STRING_new()) != NULL); | |
86 | } | |
87 | ||
88 | static int x509_pubkey_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) | |
89 | { | |
90 | X509_PUBKEY *ret; | |
91 | ||
92 | if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL | |
93 | || !x509_pubkey_ex_populate((ASN1_VALUE **)&ret, NULL)) { | |
94 | x509_pubkey_ex_free((ASN1_VALUE **)&ret, NULL); | |
95 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); | |
96 | } else { | |
97 | *pval = (ASN1_VALUE *)ret; | |
98 | } | |
99 | ||
100 | return ret != NULL; | |
101 | } | |
102 | ||
103 | static int x509_pubkey_ex_d2i(ASN1_VALUE **pval, | |
104 | const unsigned char **in, long len, | |
105 | const ASN1_ITEM *it, int tag, int aclass, | |
106 | char opt, ASN1_TLC *ctx) | |
107 | { | |
108 | const unsigned char *in_saved = *in; | |
109 | X509_PUBKEY *pubkey; | |
110 | int ret; | |
111 | OSSL_DECODER_CTX *dctx = NULL; | |
112 | ||
113 | if (*pval == NULL && !x509_pubkey_ex_new(pval, it)) | |
114 | return 0; | |
115 | if (!x509_pubkey_ex_populate(pval, NULL)) { | |
116 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); | |
117 | return 0; | |
118 | } | |
119 | ||
120 | /* This ensures that |*in| advances properly no matter what */ | |
121 | if ((ret = ASN1_item_ex_d2i(pval, in, len, | |
122 | ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL), | |
123 | tag, aclass, opt, ctx)) <= 0) | |
124 | return ret; | |
125 | ||
126 | pubkey = (X509_PUBKEY *)*pval; | |
127 | EVP_PKEY_free(pubkey->pkey); | |
128 | pubkey->pkey = NULL; | |
129 | ||
130 | /* | |
131 | * Opportunistically decode the key but remove any non fatal errors | |
132 | * from the queue. Subsequent explicit attempts to decode/use the key | |
133 | * will return an appropriate error. | |
134 | */ | |
135 | ERR_set_mark(); | |
136 | ||
137 | /* | |
138 | * Try to decode with legacy method first. This ensures that engines | |
139 | * aren't overriden by providers. | |
140 | */ | |
141 | if ((ret = x509_pubkey_decode(&pubkey->pkey, pubkey)) == -1) { | |
142 | /* -1 indicates a fatal error, like malloc failure */ | |
143 | ERR_clear_last_mark(); | |
144 | goto end; | |
145 | } | |
146 | ||
147 | /* Try to decode it into an EVP_PKEY with OSSL_DECODER */ | |
148 | if (ret <= 0 && !pubkey->flag_force_legacy) { | |
149 | const unsigned char *p = in_saved; | |
150 | char txtoidname[OSSL_MAX_NAME_SIZE]; | |
151 | ||
152 | if (OBJ_obj2txt(txtoidname, sizeof(txtoidname), | |
153 | pubkey->algor->algorithm, 0) <= 0) { | |
154 | ERR_clear_last_mark(); | |
155 | goto end; | |
156 | } | |
157 | if ((dctx = | |
158 | OSSL_DECODER_CTX_new_for_pkey(&pubkey->pkey, | |
159 | "DER", "SubjectPublicKeyInfo", | |
160 | txtoidname, EVP_PKEY_PUBLIC_KEY, | |
161 | pubkey->libctx, | |
162 | pubkey->propq)) != NULL) | |
163 | /* | |
164 | * As said higher up, we're being opportunistic. In other words, | |
165 | * we don't care about what the return value signals. | |
166 | */ | |
167 | OSSL_DECODER_from_data(dctx, &p, NULL); | |
168 | } | |
169 | ||
170 | ERR_pop_to_mark(); | |
171 | ret = 1; | |
172 | end: | |
173 | OSSL_DECODER_CTX_free(dctx); | |
174 | return ret; | |
175 | } | |
176 | ||
177 | static int x509_pubkey_ex_i2d(const ASN1_VALUE **pval, unsigned char **out, | |
178 | const ASN1_ITEM *it, int tag, int aclass) | |
179 | { | |
180 | return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL), | |
181 | tag, aclass); | |
182 | } | |
183 | ||
184 | static int x509_pubkey_ex_print(BIO *out, const ASN1_VALUE **pval, int indent, | |
185 | const char *fname, const ASN1_PCTX *pctx) | |
186 | { | |
187 | return ASN1_item_print(out, *pval, indent, | |
188 | ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL), pctx); | |
189 | } | |
190 | ||
191 | static const ASN1_EXTERN_FUNCS x509_pubkey_ff = { | |
192 | NULL, | |
193 | x509_pubkey_ex_new, | |
194 | x509_pubkey_ex_free, | |
195 | 0, /* Default clear behaviour is OK */ | |
196 | x509_pubkey_ex_d2i, | |
197 | x509_pubkey_ex_i2d, | |
198 | x509_pubkey_ex_print | |
199 | }; | |
200 | ||
201 | IMPLEMENT_EXTERN_ASN1(X509_PUBKEY, V_ASN1_SEQUENCE, x509_pubkey_ff) | |
202 | IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY) | |
203 | ||
204 | /* | |
205 | * X509_PUBKEY_dup() must be implemented manually, because there is no | |
206 | * support for it in ASN1_EXTERN_FUNCS. | |
207 | */ | |
208 | X509_PUBKEY *X509_PUBKEY_dup(const X509_PUBKEY *a) | |
209 | { | |
210 | X509_PUBKEY *pubkey = OPENSSL_zalloc(sizeof(*pubkey)); | |
211 | ||
212 | if (pubkey == NULL | |
213 | || !x509_pubkey_set0_libctx(pubkey, a->libctx, a->propq) | |
214 | || (pubkey->algor = X509_ALGOR_dup(a->algor)) == NULL | |
215 | || (pubkey->public_key = ASN1_BIT_STRING_new()) == NULL | |
216 | || !ASN1_BIT_STRING_set(pubkey->public_key, | |
217 | a->public_key->data, a->public_key->length) | |
218 | || (a->pkey != NULL && !EVP_PKEY_up_ref(a->pkey))) { | |
219 | x509_pubkey_ex_free((ASN1_VALUE **)&pubkey, | |
220 | ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL)); | |
221 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); | |
222 | return NULL; | |
223 | } | |
224 | pubkey->pkey = a->pkey; | |
225 | return pubkey; | |
226 | } | |
227 | ||
228 | int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey) | |
229 | { | |
230 | X509_PUBKEY *pk = NULL; | |
231 | ||
232 | if (x == NULL || pkey == NULL) { | |
233 | ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER); | |
234 | return 0; | |
235 | } | |
236 | ||
237 | if (pkey->ameth != NULL) { | |
238 | if ((pk = X509_PUBKEY_new()) == NULL) { | |
239 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); | |
240 | goto error; | |
241 | } | |
242 | if (pkey->ameth->pub_encode != NULL) { | |
243 | if (!pkey->ameth->pub_encode(pk, pkey)) { | |
244 | ERR_raise(ERR_LIB_X509, X509_R_PUBLIC_KEY_ENCODE_ERROR); | |
245 | goto error; | |
246 | } | |
247 | } else { | |
248 | ERR_raise(ERR_LIB_X509, X509_R_METHOD_NOT_SUPPORTED); | |
249 | goto error; | |
250 | } | |
251 | } else if (evp_pkey_is_provided(pkey)) { | |
252 | unsigned char *der = NULL; | |
253 | size_t derlen = 0; | |
254 | OSSL_ENCODER_CTX *ectx = | |
255 | OSSL_ENCODER_CTX_new_for_pkey(pkey, EVP_PKEY_PUBLIC_KEY, | |
256 | "DER", "SubjectPublicKeyInfo", | |
257 | NULL); | |
258 | ||
259 | if (OSSL_ENCODER_to_data(ectx, &der, &derlen)) { | |
260 | const unsigned char *pder = der; | |
261 | ||
262 | pk = d2i_X509_PUBKEY(NULL, &pder, (long)derlen); | |
263 | } | |
264 | ||
265 | OSSL_ENCODER_CTX_free(ectx); | |
266 | OPENSSL_free(der); | |
267 | } | |
268 | ||
269 | if (pk == NULL) { | |
270 | ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM); | |
271 | goto error; | |
272 | } | |
273 | ||
274 | X509_PUBKEY_free(*x); | |
275 | if (!EVP_PKEY_up_ref(pkey)) { | |
276 | ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR); | |
277 | goto error; | |
278 | } | |
279 | *x = pk; | |
280 | ||
281 | /* | |
282 | * pk->pkey is NULL when using the legacy routine, but is non-NULL when | |
283 | * going through the encoder, and for all intents and purposes, it's | |
284 | * a perfect copy of the public key portions of |pkey|, just not the same | |
285 | * instance. If that's all there was to pkey then we could simply return | |
286 | * early, right here. However, some application might very well depend on | |
287 | * the passed |pkey| being used and none other, so we spend a few more | |
288 | * cycles throwing away the newly created |pk->pkey| and replace it with | |
289 | * |pkey|. | |
290 | */ | |
291 | if (pk->pkey != NULL) | |
292 | EVP_PKEY_free(pk->pkey); | |
293 | ||
294 | pk->pkey = pkey; | |
295 | return 1; | |
296 | ||
297 | error: | |
298 | X509_PUBKEY_free(pk); | |
299 | return 0; | |
300 | } | |
301 | ||
302 | /* | |
303 | * Attempt to decode a public key. | |
304 | * Returns 1 on success, 0 for a decode failure and -1 for a fatal | |
305 | * error e.g. malloc failure. | |
306 | * | |
307 | * This function is #legacy. | |
308 | */ | |
309 | static int x509_pubkey_decode(EVP_PKEY **ppkey, const X509_PUBKEY *key) | |
310 | { | |
311 | EVP_PKEY *pkey = EVP_PKEY_new(); | |
312 | ||
313 | if (pkey == NULL) { | |
314 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); | |
315 | return -1; | |
316 | } | |
317 | ||
318 | if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(key->algor->algorithm))) { | |
319 | ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM); | |
320 | goto error; | |
321 | } | |
322 | ||
323 | if (pkey->ameth->pub_decode) { | |
324 | /* | |
325 | * Treat any failure of pub_decode as a decode error. In | |
326 | * future we could have different return codes for decode | |
327 | * errors and fatal errors such as malloc failure. | |
328 | */ | |
329 | if (!pkey->ameth->pub_decode(pkey, key)) | |
330 | goto error; | |
331 | } else { | |
332 | ERR_raise(ERR_LIB_X509, X509_R_METHOD_NOT_SUPPORTED); | |
333 | goto error; | |
334 | } | |
335 | ||
336 | *ppkey = pkey; | |
337 | return 1; | |
338 | ||
339 | error: | |
340 | EVP_PKEY_free(pkey); | |
341 | return 0; | |
342 | } | |
343 | ||
344 | EVP_PKEY *X509_PUBKEY_get0(const X509_PUBKEY *key) | |
345 | { | |
346 | EVP_PKEY *ret = NULL; | |
347 | ||
348 | if (key == NULL || key->public_key == NULL) | |
349 | return NULL; | |
350 | ||
351 | if (key->pkey != NULL) | |
352 | return key->pkey; | |
353 | ||
354 | /* | |
355 | * When the key ASN.1 is initially parsed an attempt is made to | |
356 | * decode the public key and cache the EVP_PKEY structure. If this | |
357 | * operation fails the cached value will be NULL. Parsing continues | |
358 | * to allow parsing of unknown key types or unsupported forms. | |
359 | * We repeat the decode operation so the appropriate errors are left | |
360 | * in the queue. | |
361 | */ | |
362 | x509_pubkey_decode(&ret, key); | |
363 | /* If decode doesn't fail something bad happened */ | |
364 | if (ret != NULL) { | |
365 | ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR); | |
366 | EVP_PKEY_free(ret); | |
367 | } | |
368 | ||
369 | return NULL; | |
370 | } | |
371 | ||
372 | EVP_PKEY *X509_PUBKEY_get(const X509_PUBKEY *key) | |
373 | { | |
374 | EVP_PKEY *ret = X509_PUBKEY_get0(key); | |
375 | ||
376 | if (ret != NULL && !EVP_PKEY_up_ref(ret)) { | |
377 | ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR); | |
378 | ret = NULL; | |
379 | } | |
380 | return ret; | |
381 | } | |
382 | ||
383 | /* | |
384 | * Now three pseudo ASN1 routines that take an EVP_PKEY structure and encode | |
385 | * or decode as X509_PUBKEY | |
386 | */ | |
387 | static EVP_PKEY *d2i_PUBKEY_int(EVP_PKEY **a, | |
388 | const unsigned char **pp, long length, | |
389 | OSSL_LIB_CTX *libctx, const char *propq, | |
390 | unsigned int force_legacy, | |
391 | X509_PUBKEY * | |
392 | (*d2i_x509_pubkey)(X509_PUBKEY **a, | |
393 | const unsigned char **in, | |
394 | long len)) | |
395 | { | |
396 | X509_PUBKEY *xpk, *xpk2 = NULL, **pxpk = NULL; | |
397 | EVP_PKEY *pktmp = NULL; | |
398 | const unsigned char *q; | |
399 | ||
400 | q = *pp; | |
401 | ||
402 | /* | |
403 | * If libctx or propq are non-NULL, we take advantage of the reuse | |
404 | * feature. It's not generally recommended, but is safe enough for | |
405 | * newly created structures. | |
406 | */ | |
407 | if (libctx != NULL || propq != NULL || force_legacy) { | |
408 | xpk2 = OPENSSL_zalloc(sizeof(*xpk2)); | |
409 | if (xpk2 == NULL) { | |
410 | ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE); | |
411 | return NULL; | |
412 | } | |
413 | if (!x509_pubkey_set0_libctx(xpk2, libctx, propq)) | |
414 | goto end; | |
415 | xpk2->flag_force_legacy = !!force_legacy; | |
416 | pxpk = &xpk2; | |
417 | } | |
418 | xpk = d2i_x509_pubkey(pxpk, &q, length); | |
419 | if (xpk == NULL) | |
420 | goto end; | |
421 | pktmp = X509_PUBKEY_get(xpk); | |
422 | X509_PUBKEY_free(xpk); | |
423 | xpk2 = NULL; /* We know that xpk == xpk2 */ | |
424 | if (pktmp == NULL) | |
425 | goto end; | |
426 | *pp = q; | |
427 | if (a != NULL) { | |
428 | EVP_PKEY_free(*a); | |
429 | *a = pktmp; | |
430 | } | |
431 | end: | |
432 | X509_PUBKEY_free(xpk2); | |
433 | return pktmp; | |
434 | } | |
435 | ||
436 | /* For the algorithm specific d2i functions further down */ | |
437 | static EVP_PKEY *d2i_PUBKEY_legacy(EVP_PKEY **a, | |
438 | const unsigned char **pp, long length) | |
439 | { | |
440 | return d2i_PUBKEY_int(a, pp, length, NULL, NULL, 1, d2i_X509_PUBKEY); | |
441 | } | |
442 | ||
443 | EVP_PKEY *d2i_PUBKEY_ex(EVP_PKEY **a, const unsigned char **pp, long length, | |
444 | OSSL_LIB_CTX *libctx, const char *propq) | |
445 | { | |
446 | return d2i_PUBKEY_int(a, pp, length, libctx, propq, 0, d2i_X509_PUBKEY); | |
447 | } | |
448 | ||
449 | EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length) | |
450 | { | |
451 | return d2i_PUBKEY_ex(a, pp, length, NULL, NULL); | |
452 | } | |
453 | ||
454 | int i2d_PUBKEY(const EVP_PKEY *a, unsigned char **pp) | |
455 | { | |
456 | int ret = -1; | |
457 | ||
458 | if (a == NULL) | |
459 | return 0; | |
460 | if (a->ameth != NULL) { | |
461 | X509_PUBKEY *xpk = NULL; | |
462 | ||
463 | if ((xpk = X509_PUBKEY_new()) == NULL) | |
464 | return -1; | |
465 | ||
466 | /* pub_encode() only encode parameters, not the key itself */ | |
467 | if (a->ameth->pub_encode != NULL && a->ameth->pub_encode(xpk, a)) { | |
468 | xpk->pkey = (EVP_PKEY *)a; | |
469 | ret = i2d_X509_PUBKEY(xpk, pp); | |
470 | xpk->pkey = NULL; | |
471 | } | |
472 | X509_PUBKEY_free(xpk); | |
473 | } else if (a->keymgmt != NULL) { | |
474 | OSSL_ENCODER_CTX *ctx = | |
475 | OSSL_ENCODER_CTX_new_for_pkey(a, EVP_PKEY_PUBLIC_KEY, | |
476 | "DER", "SubjectPublicKeyInfo", | |
477 | NULL); | |
478 | BIO *out = BIO_new(BIO_s_mem()); | |
479 | BUF_MEM *buf = NULL; | |
480 | ||
481 | if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0 | |
482 | && out != NULL | |
483 | && OSSL_ENCODER_to_bio(ctx, out) | |
484 | && BIO_get_mem_ptr(out, &buf) > 0) { | |
485 | ret = buf->length; | |
486 | ||
487 | if (pp != NULL) { | |
488 | if (*pp == NULL) { | |
489 | *pp = (unsigned char *)buf->data; | |
490 | buf->length = 0; | |
491 | buf->data = NULL; | |
492 | } else { | |
493 | memcpy(*pp, buf->data, ret); | |
494 | *pp += ret; | |
495 | } | |
496 | } | |
497 | } | |
498 | BIO_free(out); | |
499 | OSSL_ENCODER_CTX_free(ctx); | |
500 | } | |
501 | ||
502 | return ret; | |
503 | } | |
504 | ||
505 | /* | |
506 | * The following are equivalents but which return RSA and DSA keys | |
507 | */ | |
508 | RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length) | |
509 | { | |
510 | EVP_PKEY *pkey; | |
511 | RSA *key = NULL; | |
512 | const unsigned char *q; | |
513 | ||
514 | q = *pp; | |
515 | pkey = d2i_PUBKEY_legacy(NULL, &q, length); | |
516 | if (pkey == NULL) | |
517 | return NULL; | |
518 | key = EVP_PKEY_get1_RSA(pkey); | |
519 | EVP_PKEY_free(pkey); | |
520 | if (key == NULL) | |
521 | return NULL; | |
522 | *pp = q; | |
523 | if (a != NULL) { | |
524 | RSA_free(*a); | |
525 | *a = key; | |
526 | } | |
527 | return key; | |
528 | } | |
529 | ||
530 | int i2d_RSA_PUBKEY(const RSA *a, unsigned char **pp) | |
531 | { | |
532 | EVP_PKEY *pktmp; | |
533 | int ret; | |
534 | if (!a) | |
535 | return 0; | |
536 | pktmp = EVP_PKEY_new(); | |
537 | if (pktmp == NULL) { | |
538 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); | |
539 | return -1; | |
540 | } | |
541 | (void)EVP_PKEY_assign_RSA(pktmp, (RSA *)a); | |
542 | ret = i2d_PUBKEY(pktmp, pp); | |
543 | pktmp->pkey.ptr = NULL; | |
544 | EVP_PKEY_free(pktmp); | |
545 | return ret; | |
546 | } | |
547 | ||
548 | #ifndef OPENSSL_NO_DH | |
549 | DH *ossl_d2i_DH_PUBKEY(DH **a, const unsigned char **pp, long length) | |
550 | { | |
551 | EVP_PKEY *pkey; | |
552 | DH *key = NULL; | |
553 | const unsigned char *q; | |
554 | ||
555 | q = *pp; | |
556 | pkey = d2i_PUBKEY_legacy(NULL, &q, length); | |
557 | if (pkey == NULL) | |
558 | return NULL; | |
559 | if (EVP_PKEY_get_id(pkey) == EVP_PKEY_DH) | |
560 | key = EVP_PKEY_get1_DH(pkey); | |
561 | EVP_PKEY_free(pkey); | |
562 | if (key == NULL) | |
563 | return NULL; | |
564 | *pp = q; | |
565 | if (a != NULL) { | |
566 | DH_free(*a); | |
567 | *a = key; | |
568 | } | |
569 | return key; | |
570 | } | |
571 | ||
572 | int ossl_i2d_DH_PUBKEY(const DH *a, unsigned char **pp) | |
573 | { | |
574 | EVP_PKEY *pktmp; | |
575 | int ret; | |
576 | if (!a) | |
577 | return 0; | |
578 | pktmp = EVP_PKEY_new(); | |
579 | if (pktmp == NULL) { | |
580 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); | |
581 | return -1; | |
582 | } | |
583 | (void)EVP_PKEY_assign_DH(pktmp, (DH *)a); | |
584 | ret = i2d_PUBKEY(pktmp, pp); | |
585 | pktmp->pkey.ptr = NULL; | |
586 | EVP_PKEY_free(pktmp); | |
587 | return ret; | |
588 | } | |
589 | ||
590 | DH *ossl_d2i_DHx_PUBKEY(DH **a, const unsigned char **pp, long length) | |
591 | { | |
592 | EVP_PKEY *pkey; | |
593 | DH *key = NULL; | |
594 | const unsigned char *q; | |
595 | ||
596 | q = *pp; | |
597 | pkey = d2i_PUBKEY_legacy(NULL, &q, length); | |
598 | if (pkey == NULL) | |
599 | return NULL; | |
600 | if (EVP_PKEY_get_id(pkey) == EVP_PKEY_DHX) | |
601 | key = EVP_PKEY_get1_DH(pkey); | |
602 | EVP_PKEY_free(pkey); | |
603 | if (key == NULL) | |
604 | return NULL; | |
605 | *pp = q; | |
606 | if (a != NULL) { | |
607 | DH_free(*a); | |
608 | *a = key; | |
609 | } | |
610 | return key; | |
611 | } | |
612 | ||
613 | int ossl_i2d_DHx_PUBKEY(const DH *a, unsigned char **pp) | |
614 | { | |
615 | EVP_PKEY *pktmp; | |
616 | int ret; | |
617 | if (!a) | |
618 | return 0; | |
619 | pktmp = EVP_PKEY_new(); | |
620 | if (pktmp == NULL) { | |
621 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); | |
622 | return -1; | |
623 | } | |
624 | (void)EVP_PKEY_assign(pktmp, EVP_PKEY_DHX, (DH *)a); | |
625 | ret = i2d_PUBKEY(pktmp, pp); | |
626 | pktmp->pkey.ptr = NULL; | |
627 | EVP_PKEY_free(pktmp); | |
628 | return ret; | |
629 | } | |
630 | #endif | |
631 | ||
632 | #ifndef OPENSSL_NO_DSA | |
633 | DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length) | |
634 | { | |
635 | EVP_PKEY *pkey; | |
636 | DSA *key = NULL; | |
637 | const unsigned char *q; | |
638 | ||
639 | q = *pp; | |
640 | pkey = d2i_PUBKEY_legacy(NULL, &q, length); | |
641 | if (pkey == NULL) | |
642 | return NULL; | |
643 | key = EVP_PKEY_get1_DSA(pkey); | |
644 | EVP_PKEY_free(pkey); | |
645 | if (key == NULL) | |
646 | return NULL; | |
647 | *pp = q; | |
648 | if (a != NULL) { | |
649 | DSA_free(*a); | |
650 | *a = key; | |
651 | } | |
652 | return key; | |
653 | } | |
654 | ||
655 | int i2d_DSA_PUBKEY(const DSA *a, unsigned char **pp) | |
656 | { | |
657 | EVP_PKEY *pktmp; | |
658 | int ret; | |
659 | if (!a) | |
660 | return 0; | |
661 | pktmp = EVP_PKEY_new(); | |
662 | if (pktmp == NULL) { | |
663 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); | |
664 | return -1; | |
665 | } | |
666 | (void)EVP_PKEY_assign_DSA(pktmp, (DSA *)a); | |
667 | ret = i2d_PUBKEY(pktmp, pp); | |
668 | pktmp->pkey.ptr = NULL; | |
669 | EVP_PKEY_free(pktmp); | |
670 | return ret; | |
671 | } | |
672 | #endif | |
673 | ||
674 | #ifndef OPENSSL_NO_EC | |
675 | EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length) | |
676 | { | |
677 | EVP_PKEY *pkey; | |
678 | EC_KEY *key = NULL; | |
679 | const unsigned char *q; | |
680 | int type; | |
681 | ||
682 | q = *pp; | |
683 | pkey = d2i_PUBKEY_legacy(NULL, &q, length); | |
684 | if (pkey == NULL) | |
685 | return NULL; | |
686 | type = EVP_PKEY_get_id(pkey); | |
687 | if (type == EVP_PKEY_EC || type == EVP_PKEY_SM2) | |
688 | key = EVP_PKEY_get1_EC_KEY(pkey); | |
689 | EVP_PKEY_free(pkey); | |
690 | if (key == NULL) | |
691 | return NULL; | |
692 | *pp = q; | |
693 | if (a != NULL) { | |
694 | EC_KEY_free(*a); | |
695 | *a = key; | |
696 | } | |
697 | return key; | |
698 | } | |
699 | ||
700 | int i2d_EC_PUBKEY(const EC_KEY *a, unsigned char **pp) | |
701 | { | |
702 | EVP_PKEY *pktmp; | |
703 | int ret; | |
704 | ||
705 | if (a == NULL) | |
706 | return 0; | |
707 | if ((pktmp = EVP_PKEY_new()) == NULL) { | |
708 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); | |
709 | return -1; | |
710 | } | |
711 | (void)EVP_PKEY_assign_EC_KEY(pktmp, (EC_KEY *)a); | |
712 | ret = i2d_PUBKEY(pktmp, pp); | |
713 | pktmp->pkey.ptr = NULL; | |
714 | EVP_PKEY_free(pktmp); | |
715 | return ret; | |
716 | } | |
717 | ||
718 | ECX_KEY *ossl_d2i_ED25519_PUBKEY(ECX_KEY **a, | |
719 | const unsigned char **pp, long length) | |
720 | { | |
721 | EVP_PKEY *pkey; | |
722 | ECX_KEY *key = NULL; | |
723 | const unsigned char *q; | |
724 | ||
725 | q = *pp; | |
726 | pkey = d2i_PUBKEY_legacy(NULL, &q, length); | |
727 | if (pkey == NULL) | |
728 | return NULL; | |
729 | key = ossl_evp_pkey_get1_ED25519(pkey); | |
730 | EVP_PKEY_free(pkey); | |
731 | if (key == NULL) | |
732 | return NULL; | |
733 | *pp = q; | |
734 | if (a != NULL) { | |
735 | ossl_ecx_key_free(*a); | |
736 | *a = key; | |
737 | } | |
738 | return key; | |
739 | } | |
740 | ||
741 | int ossl_i2d_ED25519_PUBKEY(const ECX_KEY *a, unsigned char **pp) | |
742 | { | |
743 | EVP_PKEY *pktmp; | |
744 | int ret; | |
745 | ||
746 | if (a == NULL) | |
747 | return 0; | |
748 | if ((pktmp = EVP_PKEY_new()) == NULL) { | |
749 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); | |
750 | return -1; | |
751 | } | |
752 | (void)EVP_PKEY_assign(pktmp, EVP_PKEY_ED25519, (ECX_KEY *)a); | |
753 | ret = i2d_PUBKEY(pktmp, pp); | |
754 | pktmp->pkey.ptr = NULL; | |
755 | EVP_PKEY_free(pktmp); | |
756 | return ret; | |
757 | } | |
758 | ||
759 | ECX_KEY *ossl_d2i_ED448_PUBKEY(ECX_KEY **a, | |
760 | const unsigned char **pp, long length) | |
761 | { | |
762 | EVP_PKEY *pkey; | |
763 | ECX_KEY *key = NULL; | |
764 | const unsigned char *q; | |
765 | ||
766 | q = *pp; | |
767 | pkey = d2i_PUBKEY_legacy(NULL, &q, length); | |
768 | if (pkey == NULL) | |
769 | return NULL; | |
770 | if (EVP_PKEY_get_id(pkey) == EVP_PKEY_ED448) | |
771 | key = ossl_evp_pkey_get1_ED448(pkey); | |
772 | EVP_PKEY_free(pkey); | |
773 | if (key == NULL) | |
774 | return NULL; | |
775 | *pp = q; | |
776 | if (a != NULL) { | |
777 | ossl_ecx_key_free(*a); | |
778 | *a = key; | |
779 | } | |
780 | return key; | |
781 | } | |
782 | ||
783 | int ossl_i2d_ED448_PUBKEY(const ECX_KEY *a, unsigned char **pp) | |
784 | { | |
785 | EVP_PKEY *pktmp; | |
786 | int ret; | |
787 | ||
788 | if (a == NULL) | |
789 | return 0; | |
790 | if ((pktmp = EVP_PKEY_new()) == NULL) { | |
791 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); | |
792 | return -1; | |
793 | } | |
794 | (void)EVP_PKEY_assign(pktmp, EVP_PKEY_ED448, (ECX_KEY *)a); | |
795 | ret = i2d_PUBKEY(pktmp, pp); | |
796 | pktmp->pkey.ptr = NULL; | |
797 | EVP_PKEY_free(pktmp); | |
798 | return ret; | |
799 | } | |
800 | ||
801 | ECX_KEY *ossl_d2i_X25519_PUBKEY(ECX_KEY **a, | |
802 | const unsigned char **pp, long length) | |
803 | { | |
804 | EVP_PKEY *pkey; | |
805 | ECX_KEY *key = NULL; | |
806 | const unsigned char *q; | |
807 | ||
808 | q = *pp; | |
809 | pkey = d2i_PUBKEY_legacy(NULL, &q, length); | |
810 | if (pkey == NULL) | |
811 | return NULL; | |
812 | if (EVP_PKEY_get_id(pkey) == EVP_PKEY_X25519) | |
813 | key = ossl_evp_pkey_get1_X25519(pkey); | |
814 | EVP_PKEY_free(pkey); | |
815 | if (key == NULL) | |
816 | return NULL; | |
817 | *pp = q; | |
818 | if (a != NULL) { | |
819 | ossl_ecx_key_free(*a); | |
820 | *a = key; | |
821 | } | |
822 | return key; | |
823 | } | |
824 | ||
825 | int ossl_i2d_X25519_PUBKEY(const ECX_KEY *a, unsigned char **pp) | |
826 | { | |
827 | EVP_PKEY *pktmp; | |
828 | int ret; | |
829 | ||
830 | if (a == NULL) | |
831 | return 0; | |
832 | if ((pktmp = EVP_PKEY_new()) == NULL) { | |
833 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); | |
834 | return -1; | |
835 | } | |
836 | (void)EVP_PKEY_assign(pktmp, EVP_PKEY_X25519, (ECX_KEY *)a); | |
837 | ret = i2d_PUBKEY(pktmp, pp); | |
838 | pktmp->pkey.ptr = NULL; | |
839 | EVP_PKEY_free(pktmp); | |
840 | return ret; | |
841 | } | |
842 | ||
843 | ECX_KEY *ossl_d2i_X448_PUBKEY(ECX_KEY **a, | |
844 | const unsigned char **pp, long length) | |
845 | { | |
846 | EVP_PKEY *pkey; | |
847 | ECX_KEY *key = NULL; | |
848 | const unsigned char *q; | |
849 | ||
850 | q = *pp; | |
851 | pkey = d2i_PUBKEY_legacy(NULL, &q, length); | |
852 | if (pkey == NULL) | |
853 | return NULL; | |
854 | if (EVP_PKEY_get_id(pkey) == EVP_PKEY_X448) | |
855 | key = ossl_evp_pkey_get1_X448(pkey); | |
856 | EVP_PKEY_free(pkey); | |
857 | if (key == NULL) | |
858 | return NULL; | |
859 | *pp = q; | |
860 | if (a != NULL) { | |
861 | ossl_ecx_key_free(*a); | |
862 | *a = key; | |
863 | } | |
864 | return key; | |
865 | } | |
866 | ||
867 | int ossl_i2d_X448_PUBKEY(const ECX_KEY *a, unsigned char **pp) | |
868 | { | |
869 | EVP_PKEY *pktmp; | |
870 | int ret; | |
871 | ||
872 | if (a == NULL) | |
873 | return 0; | |
874 | if ((pktmp = EVP_PKEY_new()) == NULL) { | |
875 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); | |
876 | return -1; | |
877 | } | |
878 | (void)EVP_PKEY_assign(pktmp, EVP_PKEY_X448, (ECX_KEY *)a); | |
879 | ret = i2d_PUBKEY(pktmp, pp); | |
880 | pktmp->pkey.ptr = NULL; | |
881 | EVP_PKEY_free(pktmp); | |
882 | return ret; | |
883 | } | |
884 | ||
885 | #endif | |
886 | ||
887 | int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj, | |
888 | int ptype, void *pval, | |
889 | unsigned char *penc, int penclen) | |
890 | { | |
891 | if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval)) | |
892 | return 0; | |
893 | if (penc) { | |
894 | OPENSSL_free(pub->public_key->data); | |
895 | pub->public_key->data = penc; | |
896 | pub->public_key->length = penclen; | |
897 | /* Set number of unused bits to zero */ | |
898 | pub->public_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); | |
899 | pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT; | |
900 | } | |
901 | return 1; | |
902 | } | |
903 | ||
904 | int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg, | |
905 | const unsigned char **pk, int *ppklen, | |
906 | X509_ALGOR **pa, const X509_PUBKEY *pub) | |
907 | { | |
908 | if (ppkalg) | |
909 | *ppkalg = pub->algor->algorithm; | |
910 | if (pk) { | |
911 | *pk = pub->public_key->data; | |
912 | *ppklen = pub->public_key->length; | |
913 | } | |
914 | if (pa) | |
915 | *pa = pub->algor; | |
916 | return 1; | |
917 | } | |
918 | ||
919 | ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x) | |
920 | { | |
921 | if (x == NULL) | |
922 | return NULL; | |
923 | return x->cert_info.key->public_key; | |
924 | } | |
925 | ||
926 | /* Returns 1 for equal, 0, for non-equal, < 0 on error */ | |
927 | int X509_PUBKEY_eq(const X509_PUBKEY *a, const X509_PUBKEY *b) | |
928 | { | |
929 | X509_ALGOR *algA, *algB; | |
930 | EVP_PKEY *pA, *pB; | |
931 | ||
932 | if (a == b) | |
933 | return 1; | |
934 | if (a == NULL || b == NULL) | |
935 | return 0; | |
936 | if (!X509_PUBKEY_get0_param(NULL, NULL, NULL, &algA, a) || algA == NULL | |
937 | || !X509_PUBKEY_get0_param(NULL, NULL, NULL, &algB, b) || algB == NULL) | |
938 | return -2; | |
939 | if (X509_ALGOR_cmp(algA, algB) != 0) | |
940 | return 0; | |
941 | if ((pA = X509_PUBKEY_get0(a)) == NULL | |
942 | || (pB = X509_PUBKEY_get0(b)) == NULL) | |
943 | return -2; | |
944 | return EVP_PKEY_eq(pA, pB); | |
945 | } | |
946 | ||
947 | int ossl_x509_PUBKEY_get0_libctx(OSSL_LIB_CTX **plibctx, const char **ppropq, | |
948 | const X509_PUBKEY *key) | |
949 | { | |
950 | if (plibctx) | |
951 | *plibctx = key->libctx; | |
952 | if (ppropq) | |
953 | *ppropq = key->propq; | |
954 | return 1; | |
955 | } |