Coverage Report

Created: 2026-04-28 06:29

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_set1(pubkey->public_key,
299
0
            a->public_key->data, a->public_key->length, 0)) {
300
0
        x509_pubkey_ex_free((ASN1_VALUE **)&pubkey,
301
0
            ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL));
302
0
        ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
303
0
        return NULL;
304
0
    }
305
306
0
    if (a->pkey != NULL) {
307
0
        ERR_set_mark();
308
0
        pubkey->pkey = EVP_PKEY_dup(a->pkey);
309
0
        if (pubkey->pkey == NULL) {
310
0
            pubkey->flag_force_legacy = 1;
311
0
            if (x509_pubkey_decode(&pubkey->pkey, pubkey) <= 0) {
312
0
                x509_pubkey_ex_free((ASN1_VALUE **)&pubkey,
313
0
                    ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL));
314
0
                ERR_clear_last_mark();
315
0
                return NULL;
316
0
            }
317
0
        }
318
0
        ERR_pop_to_mark();
319
0
    }
320
0
    return pubkey;
321
0
}
322
323
int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
324
0
{
325
0
    X509_PUBKEY *pk = NULL;
326
327
0
    if (x == NULL || pkey == NULL) {
328
0
        ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
329
0
        return 0;
330
0
    }
331
332
0
    if (pkey->ameth != NULL) {
333
0
        if ((pk = X509_PUBKEY_new()) == NULL) {
334
0
            ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
335
0
            goto error;
336
0
        }
337
0
        if (pkey->ameth->pub_encode != NULL) {
338
0
            if (!pkey->ameth->pub_encode(pk, pkey)) {
339
0
                ERR_raise(ERR_LIB_X509, X509_R_PUBLIC_KEY_ENCODE_ERROR);
340
0
                goto error;
341
0
            }
342
0
        } else {
343
0
            ERR_raise(ERR_LIB_X509, X509_R_METHOD_NOT_SUPPORTED);
344
0
            goto error;
345
0
        }
346
0
    } else if (evp_pkey_is_provided(pkey)) {
347
0
        unsigned char *der = NULL;
348
0
        size_t derlen = 0;
349
0
        OSSL_ENCODER_CTX *ectx = OSSL_ENCODER_CTX_new_for_pkey(pkey, EVP_PKEY_PUBLIC_KEY,
350
0
            "DER", "SubjectPublicKeyInfo",
351
0
            NULL);
352
353
0
        if (OSSL_ENCODER_to_data(ectx, &der, &derlen)) {
354
0
            const unsigned char *pder = der;
355
356
0
            pk = d2i_X509_PUBKEY(NULL, &pder, (long)derlen);
357
0
        }
358
359
0
        OSSL_ENCODER_CTX_free(ectx);
360
0
        OPENSSL_free(der);
361
0
    }
362
363
0
    if (pk == NULL) {
364
0
        ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM);
365
0
        goto error;
366
0
    }
367
368
0
    X509_PUBKEY_free(*x);
369
0
    if (!EVP_PKEY_up_ref(pkey)) {
370
0
        ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
371
0
        goto error;
372
0
    }
373
0
    *x = pk;
374
375
    /*
376
     * pk->pkey is NULL when using the legacy routine, but is non-NULL when
377
     * going through the encoder, and for all intents and purposes, it's
378
     * a perfect copy of the public key portions of |pkey|, just not the same
379
     * instance.  If that's all there was to pkey then we could simply return
380
     * early, right here. However, some application might very well depend on
381
     * the passed |pkey| being used and none other, so we spend a few more
382
     * cycles throwing away the newly created |pk->pkey| and replace it with
383
     * |pkey|.
384
     */
385
0
    if (pk->pkey != NULL)
386
0
        EVP_PKEY_free(pk->pkey);
387
388
0
    pk->pkey = pkey;
389
0
    return 1;
390
391
0
error:
392
0
    X509_PUBKEY_free(pk);
393
0
    return 0;
394
0
}
395
396
/*
397
 * Attempt to decode a public key.
398
 * Returns 1 on success, 0 for a decode failure and -1 for a fatal
399
 * error e.g. malloc failure.
400
 *
401
 * This function is #legacy.
402
 */
403
static int x509_pubkey_decode(EVP_PKEY **ppkey, const X509_PUBKEY *key)
404
0
{
405
0
    EVP_PKEY *pkey;
406
0
    int nid;
407
408
0
    nid = OBJ_obj2nid(key->algor->algorithm);
409
0
    if (!key->flag_force_legacy)
410
0
        return 0;
411
412
0
    pkey = EVP_PKEY_new();
413
0
    if (pkey == NULL) {
414
0
        ERR_raise(ERR_LIB_X509, ERR_R_EVP_LIB);
415
0
        return -1;
416
0
    }
417
418
0
    if (!EVP_PKEY_set_type(pkey, nid)) {
419
0
        ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM);
420
0
        goto error;
421
0
    }
422
423
0
    if (pkey->ameth->pub_decode) {
424
        /*
425
         * Treat any failure of pub_decode as a decode error. In
426
         * future we could have different return codes for decode
427
         * errors and fatal errors such as malloc failure.
428
         */
429
0
        if (!pkey->ameth->pub_decode(pkey, key))
430
0
            goto error;
431
0
    } else {
432
0
        ERR_raise(ERR_LIB_X509, X509_R_METHOD_NOT_SUPPORTED);
433
0
        goto error;
434
0
    }
435
436
0
    *ppkey = pkey;
437
0
    return 1;
438
439
0
error:
440
0
    EVP_PKEY_free(pkey);
441
0
    return 0;
442
0
}
443
444
EVP_PKEY *X509_PUBKEY_get0(const X509_PUBKEY *key)
445
0
{
446
0
    if (key == NULL) {
447
0
        ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
448
0
        return NULL;
449
0
    }
450
451
0
    if (key->pkey == NULL) {
452
        /* We failed to decode the key when we loaded it, or it was never set */
453
0
        ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
454
0
        return NULL;
455
0
    }
456
457
0
    return key->pkey;
458
0
}
459
460
EVP_PKEY *X509_PUBKEY_get(const X509_PUBKEY *key)
461
0
{
462
0
    EVP_PKEY *ret = X509_PUBKEY_get0(key);
463
464
0
    if (ret != NULL && !EVP_PKEY_up_ref(ret)) {
465
0
        ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
466
0
        ret = NULL;
467
0
    }
468
0
    return ret;
469
0
}
470
471
/*
472
 * Now three pseudo ASN1 routines that take an EVP_PKEY structure and encode
473
 * or decode as X509_PUBKEY
474
 */
475
static EVP_PKEY *d2i_PUBKEY_int(EVP_PKEY **a,
476
    const unsigned char **pp, long length,
477
    OSSL_LIB_CTX *libctx, const char *propq,
478
    unsigned int force_legacy,
479
    X509_PUBKEY *(*d2i_x509_pubkey)(X509_PUBKEY **a,
480
        const unsigned char **in,
481
        long len))
482
0
{
483
0
    X509_PUBKEY *xpk, *xpk2 = NULL, **pxpk = NULL;
484
0
    EVP_PKEY *pktmp = NULL;
485
0
    const unsigned char *q;
486
487
0
    q = *pp;
488
489
    /*
490
     * If libctx or propq are non-NULL, we take advantage of the reuse
491
     * feature.  It's not generally recommended, but is safe enough for
492
     * newly created structures.
493
     */
494
0
    if (libctx != NULL || propq != NULL || force_legacy) {
495
0
        xpk2 = OPENSSL_zalloc(sizeof(*xpk2));
496
0
        if (xpk2 == NULL)
497
0
            return NULL;
498
0
        if (!x509_pubkey_set0_libctx(xpk2, libctx, propq))
499
0
            goto end;
500
0
        xpk2->flag_force_legacy = !!force_legacy;
501
0
        pxpk = &xpk2;
502
0
    }
503
0
    xpk = d2i_x509_pubkey(pxpk, &q, length);
504
0
    if (xpk == NULL)
505
0
        goto end;
506
0
    pktmp = X509_PUBKEY_get(xpk);
507
0
    X509_PUBKEY_free(xpk);
508
0
    xpk2 = NULL; /* We know that xpk == xpk2 */
509
0
    if (pktmp == NULL)
510
0
        goto end;
511
0
    *pp = q;
512
0
    if (a != NULL) {
513
0
        EVP_PKEY_free(*a);
514
0
        *a = pktmp;
515
0
    }
516
0
end:
517
0
    X509_PUBKEY_free(xpk2);
518
0
    return pktmp;
519
0
}
520
521
/* For the algorithm specific d2i functions further down */
522
EVP_PKEY *ossl_d2i_PUBKEY_legacy(EVP_PKEY **a, const unsigned char **pp,
523
    long length)
524
0
{
525
0
    return d2i_PUBKEY_int(a, pp, length, NULL, NULL, 1, d2i_X509_PUBKEY);
526
0
}
527
528
EVP_PKEY *d2i_PUBKEY_ex(EVP_PKEY **a, const unsigned char **pp, long length,
529
    OSSL_LIB_CTX *libctx, const char *propq)
530
0
{
531
0
    return d2i_PUBKEY_int(a, pp, length, libctx, propq, 0, d2i_X509_PUBKEY);
532
0
}
533
534
EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length)
535
0
{
536
0
    return d2i_PUBKEY_ex(a, pp, length, NULL, NULL);
537
0
}
538
539
int i2d_PUBKEY(const EVP_PKEY *a, unsigned char **pp)
540
0
{
541
0
    int ret = -1;
542
543
0
    if (a == NULL)
544
0
        return 0;
545
0
    if (a->ameth != NULL) {
546
0
        X509_PUBKEY *xpk = NULL;
547
548
0
        if ((xpk = X509_PUBKEY_new()) == NULL)
549
0
            return -1;
550
551
        /* pub_encode() only encode parameters, not the key itself */
552
0
        if (a->ameth->pub_encode != NULL && a->ameth->pub_encode(xpk, a)) {
553
0
            xpk->pkey = (EVP_PKEY *)a;
554
0
            ret = i2d_X509_PUBKEY(xpk, pp);
555
0
            xpk->pkey = NULL;
556
0
        }
557
0
        X509_PUBKEY_free(xpk);
558
0
    } else if (a->keymgmt != NULL) {
559
0
        OSSL_ENCODER_CTX *ctx = OSSL_ENCODER_CTX_new_for_pkey(a, EVP_PKEY_PUBLIC_KEY,
560
0
            "DER", "SubjectPublicKeyInfo",
561
0
            NULL);
562
0
        BIO *out = BIO_new(BIO_s_mem());
563
0
        BUF_MEM *buf = NULL;
564
565
0
        if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0
566
0
            && out != NULL
567
0
            && OSSL_ENCODER_to_bio(ctx, out)
568
0
            && BIO_get_mem_ptr(out, &buf) > 0) {
569
0
            ret = (int)buf->length;
570
571
0
            if (pp != NULL) {
572
0
                if (*pp == NULL) {
573
0
                    *pp = (unsigned char *)buf->data;
574
0
                    buf->length = 0;
575
0
                    buf->data = NULL;
576
0
                } else {
577
0
                    memcpy(*pp, buf->data, ret);
578
0
                    *pp += ret;
579
0
                }
580
0
            }
581
0
        }
582
0
        BIO_free(out);
583
0
        OSSL_ENCODER_CTX_free(ctx);
584
0
    }
585
586
0
    return ret;
587
0
}
588
589
/*
590
 * The following are equivalents but which return RSA and DSA keys
591
 */
592
RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length)
593
0
{
594
0
    EVP_PKEY *pkey;
595
0
    RSA *key = NULL;
596
0
    const unsigned char *q;
597
598
0
    q = *pp;
599
0
    pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
600
0
    if (pkey == NULL)
601
0
        return NULL;
602
0
    key = EVP_PKEY_get1_RSA(pkey);
603
0
    EVP_PKEY_free(pkey);
604
0
    if (key == NULL)
605
0
        return NULL;
606
0
    *pp = q;
607
0
    if (a != NULL) {
608
0
        RSA_free(*a);
609
0
        *a = key;
610
0
    }
611
0
    return key;
612
0
}
613
614
int i2d_RSA_PUBKEY(const RSA *a, unsigned char **pp)
615
0
{
616
0
    EVP_PKEY *pktmp;
617
0
    int ret;
618
0
    if (!a)
619
0
        return 0;
620
0
    pktmp = EVP_PKEY_new();
621
0
    if (pktmp == NULL) {
622
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
623
0
        return -1;
624
0
    }
625
0
    (void)EVP_PKEY_assign_RSA(pktmp, (RSA *)a);
626
0
    ret = i2d_PUBKEY(pktmp, pp);
627
0
    pktmp->pkey.ptr = NULL;
628
0
    EVP_PKEY_free(pktmp);
629
0
    return ret;
630
0
}
631
632
#ifndef OPENSSL_NO_DH
633
DH *ossl_d2i_DH_PUBKEY(DH **a, const unsigned char **pp, long length)
634
0
{
635
0
    EVP_PKEY *pkey;
636
0
    DH *key = NULL;
637
0
    const unsigned char *q;
638
639
0
    q = *pp;
640
0
    pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
641
0
    if (pkey == NULL)
642
0
        return NULL;
643
0
    if (EVP_PKEY_get_id(pkey) == EVP_PKEY_DH)
644
0
        key = EVP_PKEY_get1_DH(pkey);
645
0
    EVP_PKEY_free(pkey);
646
0
    if (key == NULL)
647
0
        return NULL;
648
0
    *pp = q;
649
0
    if (a != NULL) {
650
0
        DH_free(*a);
651
0
        *a = key;
652
0
    }
653
0
    return key;
654
0
}
655
656
int ossl_i2d_DH_PUBKEY(const DH *a, unsigned char **pp)
657
0
{
658
0
    EVP_PKEY *pktmp;
659
0
    int ret;
660
0
    if (!a)
661
0
        return 0;
662
0
    pktmp = EVP_PKEY_new();
663
0
    if (pktmp == NULL) {
664
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
665
0
        return -1;
666
0
    }
667
0
    (void)EVP_PKEY_assign_DH(pktmp, (DH *)a);
668
0
    ret = i2d_PUBKEY(pktmp, pp);
669
0
    pktmp->pkey.ptr = NULL;
670
0
    EVP_PKEY_free(pktmp);
671
0
    return ret;
672
0
}
673
674
DH *ossl_d2i_DHx_PUBKEY(DH **a, const unsigned char **pp, long length)
675
0
{
676
0
    EVP_PKEY *pkey;
677
0
    DH *key = NULL;
678
0
    const unsigned char *q;
679
680
0
    q = *pp;
681
0
    pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
682
0
    if (pkey == NULL)
683
0
        return NULL;
684
0
    if (EVP_PKEY_get_id(pkey) == EVP_PKEY_DHX)
685
0
        key = EVP_PKEY_get1_DH(pkey);
686
0
    EVP_PKEY_free(pkey);
687
0
    if (key == NULL)
688
0
        return NULL;
689
0
    *pp = q;
690
0
    if (a != NULL) {
691
0
        DH_free(*a);
692
0
        *a = key;
693
0
    }
694
0
    return key;
695
0
}
696
697
int ossl_i2d_DHx_PUBKEY(const DH *a, unsigned char **pp)
698
0
{
699
0
    EVP_PKEY *pktmp;
700
0
    int ret;
701
0
    if (!a)
702
0
        return 0;
703
0
    pktmp = EVP_PKEY_new();
704
0
    if (pktmp == NULL) {
705
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
706
0
        return -1;
707
0
    }
708
0
    (void)EVP_PKEY_assign(pktmp, EVP_PKEY_DHX, (DH *)a);
709
0
    ret = i2d_PUBKEY(pktmp, pp);
710
0
    pktmp->pkey.ptr = NULL;
711
0
    EVP_PKEY_free(pktmp);
712
0
    return ret;
713
0
}
714
#endif
715
716
#ifndef OPENSSL_NO_DSA
717
DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
718
0
{
719
0
    EVP_PKEY *pkey;
720
0
    DSA *key = NULL;
721
0
    const unsigned char *q;
722
723
0
    q = *pp;
724
0
    pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
725
0
    if (pkey == NULL)
726
0
        return NULL;
727
0
    key = EVP_PKEY_get1_DSA(pkey);
728
0
    EVP_PKEY_free(pkey);
729
0
    if (key == NULL)
730
0
        return NULL;
731
0
    *pp = q;
732
0
    if (a != NULL) {
733
0
        DSA_free(*a);
734
0
        *a = key;
735
0
    }
736
0
    return key;
737
0
}
738
739
/* Called from decoders; disallows provided DSA keys without parameters. */
740
DSA *ossl_d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
741
0
{
742
0
    DSA *key = NULL;
743
0
    const unsigned char *data;
744
0
    const BIGNUM *p, *q, *g;
745
746
0
    data = *pp;
747
0
    key = d2i_DSA_PUBKEY(NULL, &data, length);
748
0
    if (key == NULL)
749
0
        return NULL;
750
0
    DSA_get0_pqg(key, &p, &q, &g);
751
0
    if (p == NULL || q == NULL || g == NULL) {
752
0
        DSA_free(key);
753
0
        return NULL;
754
0
    }
755
0
    *pp = data;
756
0
    if (a != NULL) {
757
0
        DSA_free(*a);
758
0
        *a = key;
759
0
    }
760
0
    return key;
761
0
}
762
763
int i2d_DSA_PUBKEY(const DSA *a, unsigned char **pp)
764
0
{
765
0
    EVP_PKEY *pktmp;
766
0
    int ret;
767
0
    if (!a)
768
0
        return 0;
769
0
    pktmp = EVP_PKEY_new();
770
0
    if (pktmp == NULL) {
771
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
772
0
        return -1;
773
0
    }
774
0
    (void)EVP_PKEY_assign_DSA(pktmp, (DSA *)a);
775
0
    ret = i2d_PUBKEY(pktmp, pp);
776
0
    pktmp->pkey.ptr = NULL;
777
0
    EVP_PKEY_free(pktmp);
778
0
    return ret;
779
0
}
780
#endif
781
782
#ifndef OPENSSL_NO_EC
783
EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
784
0
{
785
0
    EVP_PKEY *pkey;
786
0
    EC_KEY *key = NULL;
787
0
    const unsigned char *q;
788
0
    int type;
789
790
0
    q = *pp;
791
0
    pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
792
0
    if (pkey == NULL)
793
0
        return NULL;
794
0
    type = EVP_PKEY_get_id(pkey);
795
0
    if (type == EVP_PKEY_EC || type == EVP_PKEY_SM2)
796
0
        key = EVP_PKEY_get1_EC_KEY(pkey);
797
0
    EVP_PKEY_free(pkey);
798
0
    if (key == NULL)
799
0
        return NULL;
800
0
    *pp = q;
801
0
    if (a != NULL) {
802
0
        EC_KEY_free(*a);
803
0
        *a = key;
804
0
    }
805
0
    return key;
806
0
}
807
808
int i2d_EC_PUBKEY(const EC_KEY *a, unsigned char **pp)
809
0
{
810
0
    EVP_PKEY *pktmp;
811
0
    int ret;
812
813
0
    if (a == NULL)
814
0
        return 0;
815
0
    if ((pktmp = EVP_PKEY_new()) == NULL) {
816
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
817
0
        return -1;
818
0
    }
819
0
    (void)EVP_PKEY_assign_EC_KEY(pktmp, (EC_KEY *)a);
820
0
    ret = i2d_PUBKEY(pktmp, pp);
821
0
    pktmp->pkey.ptr = NULL;
822
0
    EVP_PKEY_free(pktmp);
823
0
    return ret;
824
0
}
825
826
#ifndef OPENSSL_NO_ECX
827
ECX_KEY *ossl_d2i_ED25519_PUBKEY(ECX_KEY **a,
828
    const unsigned char **pp, long length)
829
0
{
830
0
    EVP_PKEY *pkey;
831
0
    ECX_KEY *key = NULL;
832
0
    const unsigned char *q;
833
834
0
    q = *pp;
835
0
    pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
836
0
    if (pkey == NULL)
837
0
        return NULL;
838
0
    key = ossl_evp_pkey_get1_ED25519(pkey);
839
0
    EVP_PKEY_free(pkey);
840
0
    if (key == NULL)
841
0
        return NULL;
842
0
    *pp = q;
843
0
    if (a != NULL) {
844
0
        ossl_ecx_key_free(*a);
845
0
        *a = key;
846
0
    }
847
0
    return key;
848
0
}
849
850
int ossl_i2d_ED25519_PUBKEY(const ECX_KEY *a, unsigned char **pp)
851
0
{
852
0
    EVP_PKEY *pktmp;
853
0
    int ret;
854
855
0
    if (a == NULL)
856
0
        return 0;
857
0
    if ((pktmp = EVP_PKEY_new()) == NULL) {
858
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
859
0
        return -1;
860
0
    }
861
0
    (void)EVP_PKEY_assign(pktmp, EVP_PKEY_ED25519, (ECX_KEY *)a);
862
0
    ret = i2d_PUBKEY(pktmp, pp);
863
0
    pktmp->pkey.ptr = NULL;
864
0
    EVP_PKEY_free(pktmp);
865
0
    return ret;
866
0
}
867
868
ECX_KEY *ossl_d2i_ED448_PUBKEY(ECX_KEY **a,
869
    const unsigned char **pp, long length)
870
0
{
871
0
    EVP_PKEY *pkey;
872
0
    ECX_KEY *key = NULL;
873
0
    const unsigned char *q;
874
875
0
    q = *pp;
876
0
    pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
877
0
    if (pkey == NULL)
878
0
        return NULL;
879
0
    if (EVP_PKEY_get_id(pkey) == EVP_PKEY_ED448)
880
0
        key = ossl_evp_pkey_get1_ED448(pkey);
881
0
    EVP_PKEY_free(pkey);
882
0
    if (key == NULL)
883
0
        return NULL;
884
0
    *pp = q;
885
0
    if (a != NULL) {
886
0
        ossl_ecx_key_free(*a);
887
0
        *a = key;
888
0
    }
889
0
    return key;
890
0
}
891
892
int ossl_i2d_ED448_PUBKEY(const ECX_KEY *a, unsigned char **pp)
893
0
{
894
0
    EVP_PKEY *pktmp;
895
0
    int ret;
896
897
0
    if (a == NULL)
898
0
        return 0;
899
0
    if ((pktmp = EVP_PKEY_new()) == NULL) {
900
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
901
0
        return -1;
902
0
    }
903
0
    (void)EVP_PKEY_assign(pktmp, EVP_PKEY_ED448, (ECX_KEY *)a);
904
0
    ret = i2d_PUBKEY(pktmp, pp);
905
0
    pktmp->pkey.ptr = NULL;
906
0
    EVP_PKEY_free(pktmp);
907
0
    return ret;
908
0
}
909
910
ECX_KEY *ossl_d2i_X25519_PUBKEY(ECX_KEY **a,
911
    const unsigned char **pp, long length)
912
0
{
913
0
    EVP_PKEY *pkey;
914
0
    ECX_KEY *key = NULL;
915
0
    const unsigned char *q;
916
917
0
    q = *pp;
918
0
    pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
919
0
    if (pkey == NULL)
920
0
        return NULL;
921
0
    if (EVP_PKEY_get_id(pkey) == EVP_PKEY_X25519)
922
0
        key = ossl_evp_pkey_get1_X25519(pkey);
923
0
    EVP_PKEY_free(pkey);
924
0
    if (key == NULL)
925
0
        return NULL;
926
0
    *pp = q;
927
0
    if (a != NULL) {
928
0
        ossl_ecx_key_free(*a);
929
0
        *a = key;
930
0
    }
931
0
    return key;
932
0
}
933
934
int ossl_i2d_X25519_PUBKEY(const ECX_KEY *a, unsigned char **pp)
935
0
{
936
0
    EVP_PKEY *pktmp;
937
0
    int ret;
938
939
0
    if (a == NULL)
940
0
        return 0;
941
0
    if ((pktmp = EVP_PKEY_new()) == NULL) {
942
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
943
0
        return -1;
944
0
    }
945
0
    (void)EVP_PKEY_assign(pktmp, EVP_PKEY_X25519, (ECX_KEY *)a);
946
0
    ret = i2d_PUBKEY(pktmp, pp);
947
0
    pktmp->pkey.ptr = NULL;
948
0
    EVP_PKEY_free(pktmp);
949
0
    return ret;
950
0
}
951
952
ECX_KEY *ossl_d2i_X448_PUBKEY(ECX_KEY **a,
953
    const unsigned char **pp, long length)
954
0
{
955
0
    EVP_PKEY *pkey;
956
0
    ECX_KEY *key = NULL;
957
0
    const unsigned char *q;
958
959
0
    q = *pp;
960
0
    pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
961
0
    if (pkey == NULL)
962
0
        return NULL;
963
0
    if (EVP_PKEY_get_id(pkey) == EVP_PKEY_X448)
964
0
        key = ossl_evp_pkey_get1_X448(pkey);
965
0
    EVP_PKEY_free(pkey);
966
0
    if (key == NULL)
967
0
        return NULL;
968
0
    *pp = q;
969
0
    if (a != NULL) {
970
0
        ossl_ecx_key_free(*a);
971
0
        *a = key;
972
0
    }
973
0
    return key;
974
0
}
975
976
int ossl_i2d_X448_PUBKEY(const ECX_KEY *a, unsigned char **pp)
977
0
{
978
0
    EVP_PKEY *pktmp;
979
0
    int ret;
980
981
0
    if (a == NULL)
982
0
        return 0;
983
0
    if ((pktmp = EVP_PKEY_new()) == NULL) {
984
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
985
0
        return -1;
986
0
    }
987
0
    (void)EVP_PKEY_assign(pktmp, EVP_PKEY_X448, (ECX_KEY *)a);
988
0
    ret = i2d_PUBKEY(pktmp, pp);
989
0
    pktmp->pkey.ptr = NULL;
990
0
    EVP_PKEY_free(pktmp);
991
0
    return ret;
992
0
}
993
994
#endif /* OPENSSL_NO_ECX */
995
#endif
996
997
void X509_PUBKEY_set0_public_key(X509_PUBKEY *pub,
998
    unsigned char *penc, int penclen)
999
0
{
1000
0
    ASN1_STRING_set0(pub->public_key, penc, penclen);
1001
0
    ossl_asn1_bit_string_set_unused_bits(pub->public_key, 0);
1002
0
}
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 != NULL)
1011
0
        X509_PUBKEY_set0_public_key(pub, penc, penclen);
1012
0
    return 1;
1013
0
}
1014
1015
int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
1016
    const unsigned char **pk, int *ppklen,
1017
    X509_ALGOR **pa, const X509_PUBKEY *pub)
1018
0
{
1019
0
    if (ppkalg)
1020
0
        *ppkalg = pub->algor->algorithm;
1021
0
    if (pk) {
1022
0
        *pk = pub->public_key->data;
1023
0
        *ppklen = pub->public_key->length;
1024
0
    }
1025
0
    if (pa)
1026
0
        *pa = pub->algor;
1027
0
    return 1;
1028
0
}
1029
1030
const ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x)
1031
0
{
1032
0
    if (x == NULL)
1033
0
        return NULL;
1034
0
    return x->cert_info.key->public_key;
1035
0
}
1036
1037
/* Returns 1 for equal, 0, for non-equal, < 0 on error */
1038
int X509_PUBKEY_eq(const X509_PUBKEY *a, const X509_PUBKEY *b)
1039
0
{
1040
0
    X509_ALGOR *algA, *algB;
1041
0
    EVP_PKEY *pA, *pB;
1042
1043
0
    if (a == b)
1044
0
        return 1;
1045
0
    if (a == NULL || b == NULL)
1046
0
        return 0;
1047
0
    if (!X509_PUBKEY_get0_param(NULL, NULL, NULL, &algA, a) || algA == NULL
1048
0
        || !X509_PUBKEY_get0_param(NULL, NULL, NULL, &algB, b) || algB == NULL)
1049
0
        return -2;
1050
0
    if (X509_ALGOR_cmp(algA, algB) != 0)
1051
0
        return 0;
1052
0
    if ((pA = X509_PUBKEY_get0(a)) == NULL
1053
0
        || (pB = X509_PUBKEY_get0(b)) == NULL)
1054
0
        return -2;
1055
0
    return EVP_PKEY_eq(pA, pB);
1056
0
}
1057
1058
int ossl_x509_PUBKEY_get0_libctx(OSSL_LIB_CTX **plibctx, const char **ppropq,
1059
    const X509_PUBKEY *key)
1060
0
{
1061
0
    if (plibctx)
1062
0
        *plibctx = key->libctx;
1063
0
    if (ppropq)
1064
0
        *ppropq = key->propq;
1065
0
    return 1;
1066
0
}