Coverage Report

Created: 2025-06-13 06:58

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