Coverage Report

Created: 2026-01-09 07:00

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