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