Coverage Report

Created: 2026-02-14 07:20

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