Coverage Report

Created: 2025-10-28 06:56

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