Coverage Report

Created: 2025-11-16 06:40

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