Coverage Report

Created: 2026-05-06 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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, EVP_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(X509_PUBKEY_it(), 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
0
    X509_PUBKEY_free(*x);
380
0
    if (!EVP_PKEY_up_ref(pkey)) {
381
0
        ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
382
0
        goto error;
383
0
    }
384
0
    *x = pk;
385
386
    /*
387
     * pk->pkey is NULL when using the legacy routine, but is non-NULL when
388
     * going through the encoder, and for all intents and purposes, it's
389
     * a perfect copy of the public key portions of |pkey|, just not the same
390
     * instance.  If that's all there was to pkey then we could simply return
391
     * early, right here. However, some application might very well depend on
392
     * the passed |pkey| being used and none other, so we spend a few more
393
     * cycles throwing away the newly created |pk->pkey| and replace it with
394
     * |pkey|.
395
     */
396
0
    if (pk->pkey != NULL)
397
0
        EVP_PKEY_free(pk->pkey);
398
399
0
    pk->pkey = pkey;
400
0
    return 1;
401
402
0
error:
403
0
    X509_PUBKEY_free(pk);
404
0
    return 0;
405
0
}
406
407
/*
408
 * Attempt to decode a public key.
409
 * Returns 1 on success, 0 for a decode failure and -1 for a fatal
410
 * error e.g. malloc failure.
411
 *
412
 * This function is #legacy.
413
 */
414
static int x509_pubkey_decode(EVP_PKEY **ppkey, const X509_PUBKEY *key)
415
0
{
416
0
    EVP_PKEY *pkey;
417
0
    int nid;
418
419
0
    nid = OBJ_obj2nid(key->algor->algorithm);
420
0
    if (!key->flag_force_legacy)
421
0
        return 0;
422
423
    /*
424
     * NID_rsaesOaep uses the same underlying RSAPublicKey body as
425
     * NID_rsaEncryption (TCG Credential Profiles V1.2 section 3.2.7).
426
     * Remap so EVP_PKEY_set_type() below finds the RSA ameth.  Keep
427
     * this in sync with x509_pubkey_ex_d2i_ex() and
428
     * ossl_spki2typespki_der_decode().
429
     */
430
0
    if (nid == NID_rsaesOaep)
431
0
        nid = NID_rsaEncryption;
432
433
0
    pkey = EVP_PKEY_new();
434
0
    if (pkey == NULL) {
435
0
        ERR_raise(ERR_LIB_X509, ERR_R_EVP_LIB);
436
0
        return -1;
437
0
    }
438
439
0
    if (!EVP_PKEY_set_type(pkey, nid)) {
440
0
        ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM);
441
0
        goto error;
442
0
    }
443
444
0
    if (pkey->ameth->pub_decode) {
445
        /*
446
         * Treat any failure of pub_decode as a decode error. In
447
         * future we could have different return codes for decode
448
         * errors and fatal errors such as malloc failure.
449
         */
450
0
        if (!pkey->ameth->pub_decode(pkey, key))
451
0
            goto error;
452
0
    } else {
453
0
        ERR_raise(ERR_LIB_X509, X509_R_METHOD_NOT_SUPPORTED);
454
0
        goto error;
455
0
    }
456
457
0
    *ppkey = pkey;
458
0
    return 1;
459
460
0
error:
461
0
    EVP_PKEY_free(pkey);
462
0
    return 0;
463
0
}
464
465
EVP_PKEY *X509_PUBKEY_get0(const X509_PUBKEY *key)
466
0
{
467
0
    if (key == NULL) {
468
0
        ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
469
0
        return NULL;
470
0
    }
471
472
0
    if (key->pkey == NULL) {
473
        /* We failed to decode the key when we loaded it, or it was never set */
474
0
        ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
475
0
        return NULL;
476
0
    }
477
478
0
    return key->pkey;
479
0
}
480
481
EVP_PKEY *X509_PUBKEY_get(const X509_PUBKEY *key)
482
0
{
483
0
    EVP_PKEY *ret = X509_PUBKEY_get0(key);
484
485
0
    if (ret != NULL && !EVP_PKEY_up_ref(ret)) {
486
0
        ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
487
0
        ret = NULL;
488
0
    }
489
0
    return ret;
490
0
}
491
492
/*
493
 * Now three pseudo ASN1 routines that take an EVP_PKEY structure and encode
494
 * or decode as X509_PUBKEY
495
 */
496
static EVP_PKEY *d2i_PUBKEY_int(EVP_PKEY **a,
497
    const unsigned char **pp, long length,
498
    OSSL_LIB_CTX *libctx, const char *propq,
499
    unsigned int force_legacy,
500
    X509_PUBKEY *(*d2i_x509_pubkey)(X509_PUBKEY **a,
501
        const unsigned char **in,
502
        long len))
503
0
{
504
0
    X509_PUBKEY *xpk, *xpk2 = NULL, **pxpk = NULL;
505
0
    EVP_PKEY *pktmp = NULL;
506
0
    const unsigned char *q;
507
508
0
    q = *pp;
509
510
    /*
511
     * If libctx or propq are non-NULL, we take advantage of the reuse
512
     * feature.  It's not generally recommended, but is safe enough for
513
     * newly created structures.
514
     */
515
0
    if (libctx != NULL || propq != NULL || force_legacy) {
516
0
        xpk2 = OPENSSL_zalloc(sizeof(*xpk2));
517
0
        if (xpk2 == NULL)
518
0
            return NULL;
519
0
        if (!x509_pubkey_set0_libctx(xpk2, libctx, propq))
520
0
            goto end;
521
0
        xpk2->flag_force_legacy = !!force_legacy;
522
0
        pxpk = &xpk2;
523
0
    }
524
0
    xpk = d2i_x509_pubkey(pxpk, &q, length);
525
0
    if (xpk == NULL)
526
0
        goto end;
527
0
    pktmp = X509_PUBKEY_get(xpk);
528
0
    X509_PUBKEY_free(xpk);
529
0
    xpk2 = NULL; /* We know that xpk == xpk2 */
530
0
    if (pktmp == NULL)
531
0
        goto end;
532
0
    *pp = q;
533
0
    if (a != NULL) {
534
0
        EVP_PKEY_free(*a);
535
0
        *a = pktmp;
536
0
    }
537
0
end:
538
0
    X509_PUBKEY_free(xpk2);
539
0
    return pktmp;
540
0
}
541
542
/* For the algorithm specific d2i functions further down */
543
EVP_PKEY *ossl_d2i_PUBKEY_legacy(EVP_PKEY **a, const unsigned char **pp,
544
    long length)
545
0
{
546
0
    return d2i_PUBKEY_int(a, pp, length, NULL, NULL, 1, d2i_X509_PUBKEY);
547
0
}
548
549
EVP_PKEY *d2i_PUBKEY_ex(EVP_PKEY **a, const unsigned char **pp, long length,
550
    OSSL_LIB_CTX *libctx, const char *propq)
551
0
{
552
0
    return d2i_PUBKEY_int(a, pp, length, libctx, propq, 0, d2i_X509_PUBKEY);
553
0
}
554
555
EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length)
556
0
{
557
0
    return d2i_PUBKEY_ex(a, pp, length, NULL, NULL);
558
0
}
559
560
int i2d_PUBKEY(const EVP_PKEY *a, unsigned char **pp)
561
0
{
562
0
    int ret = -1;
563
564
0
    if (a == NULL)
565
0
        return 0;
566
0
    if (a->ameth != NULL) {
567
0
        X509_PUBKEY *xpk = NULL;
568
569
0
        if ((xpk = X509_PUBKEY_new()) == NULL)
570
0
            return -1;
571
572
        /* pub_encode() only encode parameters, not the key itself */
573
0
        if (a->ameth->pub_encode != NULL && a->ameth->pub_encode(xpk, a)) {
574
0
            xpk->pkey = (EVP_PKEY *)a;
575
0
            ret = i2d_X509_PUBKEY(xpk, pp);
576
0
            xpk->pkey = NULL;
577
0
        }
578
0
        X509_PUBKEY_free(xpk);
579
0
    } else if (a->keymgmt != NULL) {
580
0
        OSSL_ENCODER_CTX *ctx = OSSL_ENCODER_CTX_new_for_pkey(a, EVP_PKEY_PUBLIC_KEY,
581
0
            "DER", "SubjectPublicKeyInfo",
582
0
            NULL);
583
0
        BIO *out = BIO_new(BIO_s_mem());
584
0
        BUF_MEM *buf = NULL;
585
586
0
        if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0
587
0
            && out != NULL
588
0
            && OSSL_ENCODER_to_bio(ctx, out)
589
0
            && BIO_get_mem_ptr(out, &buf) > 0) {
590
0
            ret = (int)buf->length;
591
592
0
            if (pp != NULL) {
593
0
                if (*pp == NULL) {
594
0
                    *pp = (unsigned char *)buf->data;
595
0
                    buf->length = 0;
596
0
                    buf->data = NULL;
597
0
                } else {
598
0
                    memcpy(*pp, buf->data, ret);
599
0
                    *pp += ret;
600
0
                }
601
0
            }
602
0
        }
603
0
        BIO_free(out);
604
0
        OSSL_ENCODER_CTX_free(ctx);
605
0
    }
606
607
0
    return ret;
608
0
}
609
610
/*
611
 * The following are equivalents but which return RSA and DSA keys
612
 */
613
RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length)
614
0
{
615
0
    EVP_PKEY *pkey;
616
0
    RSA *key = NULL;
617
0
    const unsigned char *q;
618
619
0
    q = *pp;
620
0
    pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
621
0
    if (pkey == NULL)
622
0
        return NULL;
623
0
    key = EVP_PKEY_get1_RSA(pkey);
624
0
    EVP_PKEY_free(pkey);
625
0
    if (key == NULL)
626
0
        return NULL;
627
0
    *pp = q;
628
0
    if (a != NULL) {
629
0
        RSA_free(*a);
630
0
        *a = key;
631
0
    }
632
0
    return key;
633
0
}
634
635
int i2d_RSA_PUBKEY(const RSA *a, unsigned char **pp)
636
0
{
637
0
    EVP_PKEY *pktmp;
638
0
    int ret;
639
0
    if (!a)
640
0
        return 0;
641
0
    pktmp = EVP_PKEY_new();
642
0
    if (pktmp == NULL) {
643
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
644
0
        return -1;
645
0
    }
646
0
    (void)EVP_PKEY_assign_RSA(pktmp, (RSA *)a);
647
0
    ret = i2d_PUBKEY(pktmp, pp);
648
0
    pktmp->pkey.ptr = NULL;
649
0
    EVP_PKEY_free(pktmp);
650
0
    return ret;
651
0
}
652
653
#ifndef OPENSSL_NO_DH
654
DH *ossl_d2i_DH_PUBKEY(DH **a, const unsigned char **pp, long length)
655
0
{
656
0
    EVP_PKEY *pkey;
657
0
    DH *key = NULL;
658
0
    const unsigned char *q;
659
660
0
    q = *pp;
661
0
    pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
662
0
    if (pkey == NULL)
663
0
        return NULL;
664
0
    if (EVP_PKEY_get_id(pkey) == EVP_PKEY_DH)
665
0
        key = EVP_PKEY_get1_DH(pkey);
666
0
    EVP_PKEY_free(pkey);
667
0
    if (key == NULL)
668
0
        return NULL;
669
0
    *pp = q;
670
0
    if (a != NULL) {
671
0
        DH_free(*a);
672
0
        *a = key;
673
0
    }
674
0
    return key;
675
0
}
676
677
int ossl_i2d_DH_PUBKEY(const DH *a, unsigned char **pp)
678
0
{
679
0
    EVP_PKEY *pktmp;
680
0
    int ret;
681
0
    if (!a)
682
0
        return 0;
683
0
    pktmp = EVP_PKEY_new();
684
0
    if (pktmp == NULL) {
685
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
686
0
        return -1;
687
0
    }
688
0
    (void)EVP_PKEY_assign_DH(pktmp, (DH *)a);
689
0
    ret = i2d_PUBKEY(pktmp, pp);
690
0
    pktmp->pkey.ptr = NULL;
691
0
    EVP_PKEY_free(pktmp);
692
0
    return ret;
693
0
}
694
695
DH *ossl_d2i_DHx_PUBKEY(DH **a, const unsigned char **pp, long length)
696
0
{
697
0
    EVP_PKEY *pkey;
698
0
    DH *key = NULL;
699
0
    const unsigned char *q;
700
701
0
    q = *pp;
702
0
    pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
703
0
    if (pkey == NULL)
704
0
        return NULL;
705
0
    if (EVP_PKEY_get_id(pkey) == EVP_PKEY_DHX)
706
0
        key = EVP_PKEY_get1_DH(pkey);
707
0
    EVP_PKEY_free(pkey);
708
0
    if (key == NULL)
709
0
        return NULL;
710
0
    *pp = q;
711
0
    if (a != NULL) {
712
0
        DH_free(*a);
713
0
        *a = key;
714
0
    }
715
0
    return key;
716
0
}
717
718
int ossl_i2d_DHx_PUBKEY(const DH *a, unsigned char **pp)
719
0
{
720
0
    EVP_PKEY *pktmp;
721
0
    int ret;
722
0
    if (!a)
723
0
        return 0;
724
0
    pktmp = EVP_PKEY_new();
725
0
    if (pktmp == NULL) {
726
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
727
0
        return -1;
728
0
    }
729
0
    (void)EVP_PKEY_assign(pktmp, EVP_PKEY_DHX, (DH *)a);
730
0
    ret = i2d_PUBKEY(pktmp, pp);
731
0
    pktmp->pkey.ptr = NULL;
732
0
    EVP_PKEY_free(pktmp);
733
0
    return ret;
734
0
}
735
#endif
736
737
#ifndef OPENSSL_NO_DSA
738
DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
739
0
{
740
0
    EVP_PKEY *pkey;
741
0
    DSA *key = NULL;
742
0
    const unsigned char *q;
743
744
0
    q = *pp;
745
0
    pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
746
0
    if (pkey == NULL)
747
0
        return NULL;
748
0
    key = EVP_PKEY_get1_DSA(pkey);
749
0
    EVP_PKEY_free(pkey);
750
0
    if (key == NULL)
751
0
        return NULL;
752
0
    *pp = q;
753
0
    if (a != NULL) {
754
0
        DSA_free(*a);
755
0
        *a = key;
756
0
    }
757
0
    return key;
758
0
}
759
760
/* Called from decoders; disallows provided DSA keys without parameters. */
761
DSA *ossl_d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
762
0
{
763
0
    DSA *key = NULL;
764
0
    const unsigned char *data;
765
0
    const BIGNUM *p, *q, *g;
766
767
0
    data = *pp;
768
0
    key = d2i_DSA_PUBKEY(NULL, &data, length);
769
0
    if (key == NULL)
770
0
        return NULL;
771
0
    DSA_get0_pqg(key, &p, &q, &g);
772
0
    if (p == NULL || q == NULL || g == NULL) {
773
0
        DSA_free(key);
774
0
        return NULL;
775
0
    }
776
0
    *pp = data;
777
0
    if (a != NULL) {
778
0
        DSA_free(*a);
779
0
        *a = key;
780
0
    }
781
0
    return key;
782
0
}
783
784
int i2d_DSA_PUBKEY(const DSA *a, unsigned char **pp)
785
0
{
786
0
    EVP_PKEY *pktmp;
787
0
    int ret;
788
0
    if (!a)
789
0
        return 0;
790
0
    pktmp = EVP_PKEY_new();
791
0
    if (pktmp == NULL) {
792
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
793
0
        return -1;
794
0
    }
795
0
    (void)EVP_PKEY_assign_DSA(pktmp, (DSA *)a);
796
0
    ret = i2d_PUBKEY(pktmp, pp);
797
0
    pktmp->pkey.ptr = NULL;
798
0
    EVP_PKEY_free(pktmp);
799
0
    return ret;
800
0
}
801
#endif
802
803
#ifndef OPENSSL_NO_EC
804
EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
805
0
{
806
0
    EVP_PKEY *pkey;
807
0
    EC_KEY *key = NULL;
808
0
    const unsigned char *q;
809
0
    int type;
810
811
0
    q = *pp;
812
0
    pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
813
0
    if (pkey == NULL)
814
0
        return NULL;
815
0
    type = EVP_PKEY_get_id(pkey);
816
0
    if (type == EVP_PKEY_EC || type == EVP_PKEY_SM2)
817
0
        key = EVP_PKEY_get1_EC_KEY(pkey);
818
0
    EVP_PKEY_free(pkey);
819
0
    if (key == NULL)
820
0
        return NULL;
821
0
    *pp = q;
822
0
    if (a != NULL) {
823
0
        EC_KEY_free(*a);
824
0
        *a = key;
825
0
    }
826
0
    return key;
827
0
}
828
829
int i2d_EC_PUBKEY(const EC_KEY *a, unsigned char **pp)
830
0
{
831
0
    EVP_PKEY *pktmp;
832
0
    int ret;
833
834
0
    if (a == NULL)
835
0
        return 0;
836
0
    if ((pktmp = EVP_PKEY_new()) == NULL) {
837
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
838
0
        return -1;
839
0
    }
840
0
    (void)EVP_PKEY_assign_EC_KEY(pktmp, (EC_KEY *)a);
841
0
    ret = i2d_PUBKEY(pktmp, pp);
842
0
    pktmp->pkey.ptr = NULL;
843
0
    EVP_PKEY_free(pktmp);
844
0
    return ret;
845
0
}
846
847
#ifndef OPENSSL_NO_ECX
848
ECX_KEY *ossl_d2i_ED25519_PUBKEY(ECX_KEY **a,
849
    const unsigned char **pp, long length)
850
0
{
851
0
    EVP_PKEY *pkey;
852
0
    ECX_KEY *key = NULL;
853
0
    const unsigned char *q;
854
855
0
    q = *pp;
856
0
    pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
857
0
    if (pkey == NULL)
858
0
        return NULL;
859
0
    key = ossl_evp_pkey_get1_ED25519(pkey);
860
0
    EVP_PKEY_free(pkey);
861
0
    if (key == NULL)
862
0
        return NULL;
863
0
    *pp = q;
864
0
    if (a != NULL) {
865
0
        ossl_ecx_key_free(*a);
866
0
        *a = key;
867
0
    }
868
0
    return key;
869
0
}
870
871
int ossl_i2d_ED25519_PUBKEY(const ECX_KEY *a, unsigned char **pp)
872
0
{
873
0
    EVP_PKEY *pktmp;
874
0
    int ret;
875
876
0
    if (a == NULL)
877
0
        return 0;
878
0
    if ((pktmp = EVP_PKEY_new()) == NULL) {
879
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
880
0
        return -1;
881
0
    }
882
0
    (void)EVP_PKEY_assign(pktmp, EVP_PKEY_ED25519, (ECX_KEY *)a);
883
0
    ret = i2d_PUBKEY(pktmp, pp);
884
0
    pktmp->pkey.ptr = NULL;
885
0
    EVP_PKEY_free(pktmp);
886
0
    return ret;
887
0
}
888
889
ECX_KEY *ossl_d2i_ED448_PUBKEY(ECX_KEY **a,
890
    const unsigned char **pp, long length)
891
0
{
892
0
    EVP_PKEY *pkey;
893
0
    ECX_KEY *key = NULL;
894
0
    const unsigned char *q;
895
896
0
    q = *pp;
897
0
    pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
898
0
    if (pkey == NULL)
899
0
        return NULL;
900
0
    if (EVP_PKEY_get_id(pkey) == EVP_PKEY_ED448)
901
0
        key = ossl_evp_pkey_get1_ED448(pkey);
902
0
    EVP_PKEY_free(pkey);
903
0
    if (key == NULL)
904
0
        return NULL;
905
0
    *pp = q;
906
0
    if (a != NULL) {
907
0
        ossl_ecx_key_free(*a);
908
0
        *a = key;
909
0
    }
910
0
    return key;
911
0
}
912
913
int ossl_i2d_ED448_PUBKEY(const ECX_KEY *a, unsigned char **pp)
914
0
{
915
0
    EVP_PKEY *pktmp;
916
0
    int ret;
917
918
0
    if (a == NULL)
919
0
        return 0;
920
0
    if ((pktmp = EVP_PKEY_new()) == NULL) {
921
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
922
0
        return -1;
923
0
    }
924
0
    (void)EVP_PKEY_assign(pktmp, EVP_PKEY_ED448, (ECX_KEY *)a);
925
0
    ret = i2d_PUBKEY(pktmp, pp);
926
0
    pktmp->pkey.ptr = NULL;
927
0
    EVP_PKEY_free(pktmp);
928
0
    return ret;
929
0
}
930
931
ECX_KEY *ossl_d2i_X25519_PUBKEY(ECX_KEY **a,
932
    const unsigned char **pp, long length)
933
0
{
934
0
    EVP_PKEY *pkey;
935
0
    ECX_KEY *key = NULL;
936
0
    const unsigned char *q;
937
938
0
    q = *pp;
939
0
    pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
940
0
    if (pkey == NULL)
941
0
        return NULL;
942
0
    if (EVP_PKEY_get_id(pkey) == EVP_PKEY_X25519)
943
0
        key = ossl_evp_pkey_get1_X25519(pkey);
944
0
    EVP_PKEY_free(pkey);
945
0
    if (key == NULL)
946
0
        return NULL;
947
0
    *pp = q;
948
0
    if (a != NULL) {
949
0
        ossl_ecx_key_free(*a);
950
0
        *a = key;
951
0
    }
952
0
    return key;
953
0
}
954
955
int ossl_i2d_X25519_PUBKEY(const ECX_KEY *a, unsigned char **pp)
956
0
{
957
0
    EVP_PKEY *pktmp;
958
0
    int ret;
959
960
0
    if (a == NULL)
961
0
        return 0;
962
0
    if ((pktmp = EVP_PKEY_new()) == NULL) {
963
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
964
0
        return -1;
965
0
    }
966
0
    (void)EVP_PKEY_assign(pktmp, EVP_PKEY_X25519, (ECX_KEY *)a);
967
0
    ret = i2d_PUBKEY(pktmp, pp);
968
0
    pktmp->pkey.ptr = NULL;
969
0
    EVP_PKEY_free(pktmp);
970
0
    return ret;
971
0
}
972
973
ECX_KEY *ossl_d2i_X448_PUBKEY(ECX_KEY **a,
974
    const unsigned char **pp, long length)
975
0
{
976
0
    EVP_PKEY *pkey;
977
0
    ECX_KEY *key = NULL;
978
0
    const unsigned char *q;
979
980
0
    q = *pp;
981
0
    pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
982
0
    if (pkey == NULL)
983
0
        return NULL;
984
0
    if (EVP_PKEY_get_id(pkey) == EVP_PKEY_X448)
985
0
        key = ossl_evp_pkey_get1_X448(pkey);
986
0
    EVP_PKEY_free(pkey);
987
0
    if (key == NULL)
988
0
        return NULL;
989
0
    *pp = q;
990
0
    if (a != NULL) {
991
0
        ossl_ecx_key_free(*a);
992
0
        *a = key;
993
0
    }
994
0
    return key;
995
0
}
996
997
int ossl_i2d_X448_PUBKEY(const ECX_KEY *a, unsigned char **pp)
998
0
{
999
0
    EVP_PKEY *pktmp;
1000
0
    int ret;
1001
1002
0
    if (a == NULL)
1003
0
        return 0;
1004
0
    if ((pktmp = EVP_PKEY_new()) == NULL) {
1005
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
1006
0
        return -1;
1007
0
    }
1008
0
    (void)EVP_PKEY_assign(pktmp, EVP_PKEY_X448, (ECX_KEY *)a);
1009
0
    ret = i2d_PUBKEY(pktmp, pp);
1010
0
    pktmp->pkey.ptr = NULL;
1011
0
    EVP_PKEY_free(pktmp);
1012
0
    return ret;
1013
0
}
1014
1015
#endif /* OPENSSL_NO_ECX */
1016
#endif
1017
1018
void X509_PUBKEY_set0_public_key(X509_PUBKEY *pub,
1019
    unsigned char *penc, int penclen)
1020
0
{
1021
0
    ASN1_STRING_set0(pub->public_key, penc, penclen);
1022
0
    ossl_asn1_bit_string_set_unused_bits(pub->public_key, 0);
1023
0
}
1024
1025
int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
1026
    int ptype, void *pval,
1027
    unsigned char *penc, int penclen)
1028
0
{
1029
0
    if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
1030
0
        return 0;
1031
0
    if (penc != NULL)
1032
0
        X509_PUBKEY_set0_public_key(pub, penc, penclen);
1033
0
    return 1;
1034
0
}
1035
1036
int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
1037
    const unsigned char **pk, int *ppklen,
1038
    X509_ALGOR **pa, const X509_PUBKEY *pub)
1039
0
{
1040
0
    if (ppkalg)
1041
0
        *ppkalg = pub->algor->algorithm;
1042
0
    if (pk) {
1043
0
        *pk = pub->public_key->data;
1044
0
        *ppklen = pub->public_key->length;
1045
0
    }
1046
0
    if (pa)
1047
0
        *pa = pub->algor;
1048
0
    return 1;
1049
0
}
1050
1051
const ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x)
1052
0
{
1053
0
    if (x == NULL)
1054
0
        return NULL;
1055
0
    return x->cert_info.key->public_key;
1056
0
}
1057
1058
/* Returns 1 for equal, 0, for non-equal, < 0 on error */
1059
int X509_PUBKEY_eq(const X509_PUBKEY *a, const X509_PUBKEY *b)
1060
0
{
1061
0
    X509_ALGOR *algA, *algB;
1062
0
    EVP_PKEY *pA, *pB;
1063
1064
0
    if (a == b)
1065
0
        return 1;
1066
0
    if (a == NULL || b == NULL)
1067
0
        return 0;
1068
0
    if (!X509_PUBKEY_get0_param(NULL, NULL, NULL, &algA, a) || algA == NULL
1069
0
        || !X509_PUBKEY_get0_param(NULL, NULL, NULL, &algB, b) || algB == NULL)
1070
0
        return -2;
1071
0
    if (X509_ALGOR_cmp(algA, algB) != 0)
1072
0
        return 0;
1073
0
    if ((pA = X509_PUBKEY_get0(a)) == NULL
1074
0
        || (pB = X509_PUBKEY_get0(b)) == NULL)
1075
0
        return -2;
1076
0
    return EVP_PKEY_eq(pA, pB);
1077
0
}
1078
1079
int ossl_x509_PUBKEY_get0_libctx(OSSL_LIB_CTX **plibctx, const char **ppropq,
1080
    const X509_PUBKEY *key)
1081
0
{
1082
0
    if (plibctx)
1083
0
        *plibctx = key->libctx;
1084
0
    if (ppropq)
1085
0
        *ppropq = key->propq;
1086
0
    return 1;
1087
0
}