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