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