Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/crypto/x509/x_pubkey.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
/*
11
 * DSA low level APIs are deprecated for public use, but still ok for
12
 * internal use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <stdio.h>
17
#include "internal/cryptlib.h"
18
#include <openssl/asn1t.h>
19
#include <openssl/x509.h>
20
#include <openssl/engine.h>
21
#include "crypto/asn1.h"
22
#include "crypto/evp.h"
23
#include "crypto/x509.h"
24
#include <openssl/rsa.h>
25
#include <openssl/dsa.h>
26
#include <openssl/decoder.h>
27
#include <openssl/encoder.h>
28
#include "internal/provider.h"
29
#include "internal/sizes.h"
30
31
struct X509_pubkey_st {
32
    X509_ALGOR *algor;
33
    ASN1_BIT_STRING *public_key;
34
35
    EVP_PKEY *pkey;
36
37
    /* extra data for the callback, used by d2i_PUBKEY_ex */
38
    OSSL_LIB_CTX *libctx;
39
    char *propq;
40
41
    /* Flag to force legacy keys */
42
    unsigned int flag_force_legacy : 1;
43
};
44
45
static int x509_pubkey_decode(EVP_PKEY **pk, const X509_PUBKEY *key);
46
47
static int x509_pubkey_set0_libctx(X509_PUBKEY *x, OSSL_LIB_CTX *libctx,
48
                                   const char *propq)
49
2.06M
{
50
2.06M
    if (x != NULL) {
51
2.06M
        x->libctx = libctx;
52
2.06M
        OPENSSL_free(x->propq);
53
2.06M
        x->propq = NULL;
54
2.06M
        if (propq != NULL) {
55
0
            x->propq = OPENSSL_strdup(propq);
56
0
            if (x->propq == NULL)
57
0
                return 0;
58
0
        }
59
2.06M
    }
60
2.06M
    return 1;
61
2.06M
}
62
63
ASN1_SEQUENCE(X509_PUBKEY_INTERNAL) = {
64
        ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR),
65
        ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING)
66
} static_ASN1_SEQUENCE_END_name(X509_PUBKEY, X509_PUBKEY_INTERNAL)
67
68
X509_PUBKEY *ossl_d2i_X509_PUBKEY_INTERNAL(const unsigned char **pp,
69
                                           long len, OSSL_LIB_CTX *libctx,
70
                                           const char *propq)
71
837k
{
72
837k
    X509_PUBKEY *xpub = OPENSSL_zalloc(sizeof(*xpub));
73
74
837k
    if (xpub == NULL)
75
0
        return NULL;
76
837k
    return (X509_PUBKEY *)ASN1_item_d2i_ex((ASN1_VALUE **)&xpub, pp, len,
77
837k
                                           ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL),
78
837k
                                           libctx, propq);
79
837k
}
80
81
void ossl_X509_PUBKEY_INTERNAL_free(X509_PUBKEY *xpub)
82
1.59M
{
83
1.59M
    ASN1_item_free((ASN1_VALUE *)xpub, ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL));
84
1.59M
}
85
86
static void x509_pubkey_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
87
1.83M
{
88
1.83M
    X509_PUBKEY *pubkey;
89
90
1.83M
    if (pval != NULL && (pubkey = (X509_PUBKEY *)*pval) != NULL) {
91
1.83M
        X509_ALGOR_free(pubkey->algor);
92
1.83M
        ASN1_BIT_STRING_free(pubkey->public_key);
93
1.83M
        EVP_PKEY_free(pubkey->pkey);
94
1.83M
        OPENSSL_free(pubkey->propq);
95
1.83M
        OPENSSL_free(pubkey);
96
1.83M
        *pval = NULL;
97
1.83M
    }
98
1.83M
}
99
100
static int x509_pubkey_ex_populate(ASN1_VALUE **pval, const ASN1_ITEM *it)
101
3.01M
{
102
3.01M
    X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
103
104
3.01M
    return (pubkey->algor != NULL
105
3.01M
            || (pubkey->algor = X509_ALGOR_new()) != NULL)
106
3.01M
        && (pubkey->public_key != NULL
107
3.01M
            || (pubkey->public_key = ASN1_BIT_STRING_new()) != NULL);
108
3.01M
}
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
586k
{
114
586k
    X509_PUBKEY *ret;
115
116
586k
    if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
117
0
        return 0;
118
586k
    if (!x509_pubkey_ex_populate((ASN1_VALUE **)&ret, NULL)
119
586k
        || !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
586k
    } else {
124
586k
        *pval = (ASN1_VALUE *)ret;
125
586k
    }
126
127
586k
    return ret != NULL;
128
586k
}
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
1.81M
{
136
1.81M
    const unsigned char *in_saved = *in;
137
1.81M
    size_t publen;
138
1.81M
    X509_PUBKEY *pubkey;
139
1.81M
    int ret;
140
1.81M
    OSSL_DECODER_CTX *dctx = NULL;
141
1.81M
    unsigned char *tmpbuf = NULL;
142
143
1.81M
    if (*pval == NULL && !x509_pubkey_ex_new_ex(pval, it, libctx, propq))
144
0
        return 0;
145
1.81M
    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
1.81M
    if ((ret = ASN1_item_ex_d2i(pval, in, len,
152
1.81M
                                ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL),
153
1.81M
                                tag, aclass, opt, ctx)) <= 0)
154
226k
        return ret;
155
156
1.59M
    publen = *in - in_saved;
157
1.59M
    if (!ossl_assert(publen > 0)) {
158
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
159
0
        return 0;
160
0
    }
161
162
1.59M
    pubkey = (X509_PUBKEY *)*pval;
163
1.59M
    EVP_PKEY_free(pubkey->pkey);
164
1.59M
    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
1.59M
    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
1.59M
    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
1.59M
    if (ret <= 0 && !pubkey->flag_force_legacy) {
185
840k
        const unsigned char *p;
186
840k
        char txtoidname[OSSL_MAX_NAME_SIZE];
187
840k
        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
840k
        if (aclass != V_ASN1_UNIVERSAL) {
194
216k
            tmpbuf = OPENSSL_memdup(in_saved, publen);
195
216k
            if (tmpbuf == NULL)
196
0
                return 0;
197
216k
            in_saved = tmpbuf;
198
216k
            *tmpbuf = V_ASN1_CONSTRUCTED | V_ASN1_SEQUENCE;
199
216k
        }
200
840k
        p = in_saved;
201
202
840k
        if (OBJ_obj2txt(txtoidname, sizeof(txtoidname),
203
840k
                        pubkey->algor->algorithm, 0) <= 0) {
204
10
            ERR_clear_last_mark();
205
10
            goto end;
206
10
        }
207
840k
        if ((dctx =
208
840k
             OSSL_DECODER_CTX_new_for_pkey(&pubkey->pkey,
209
840k
                                           "DER", "SubjectPublicKeyInfo",
210
840k
                                           txtoidname, EVP_PKEY_PUBLIC_KEY,
211
840k
                                           pubkey->libctx,
212
840k
                                           pubkey->propq)) != NULL)
213
            /*
214
             * As said higher up, we're being opportunistic.  In other words,
215
             * we don't care if we fail.
216
             */
217
840k
            if (OSSL_DECODER_from_data(dctx, &p, &slen)) {
218
392k
                if (slen != 0) {
219
                    /*
220
                     * If we successfully decoded then we *must* consume all the
221
                     * bytes.
222
                     */
223
0
                    ERR_clear_last_mark();
224
0
                    ERR_raise(ERR_LIB_ASN1, EVP_R_DECODE_ERROR);
225
0
                    goto end;
226
0
                }
227
392k
            }
228
840k
    }
229
230
1.59M
    ERR_pop_to_mark();
231
1.59M
    ret = 1;
232
1.59M
 end:
233
1.59M
    OSSL_DECODER_CTX_free(dctx);
234
1.59M
    OPENSSL_free(tmpbuf);
235
1.59M
    return ret;
236
1.59M
}
237
238
static int x509_pubkey_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
239
                              const ASN1_ITEM *it, int tag, int aclass)
240
654k
{
241
654k
    return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL),
242
654k
                            tag, aclass);
243
654k
}
244
245
static int x509_pubkey_ex_print(BIO *out, const ASN1_VALUE **pval, int indent,
246
                                const char *fname, const ASN1_PCTX *pctx)
247
61.8k
{
248
61.8k
    return ASN1_item_print(out, *pval, indent,
249
61.8k
                           ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL), pctx);
250
61.8k
}
251
252
static const ASN1_EXTERN_FUNCS x509_pubkey_ff = {
253
    NULL,
254
    NULL,
255
    x509_pubkey_ex_free,
256
    0,                          /* Default clear behaviour is OK */
257
    NULL,
258
    x509_pubkey_ex_i2d,
259
    x509_pubkey_ex_print,
260
    x509_pubkey_ex_new_ex,
261
    x509_pubkey_ex_d2i_ex,
262
};
263
264
IMPLEMENT_EXTERN_ASN1(X509_PUBKEY, V_ASN1_SEQUENCE, x509_pubkey_ff)
265
IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)
266
267
X509_PUBKEY *X509_PUBKEY_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
268
0
{
269
0
    X509_PUBKEY *pubkey = NULL;
270
271
0
    pubkey = (X509_PUBKEY *)ASN1_item_new_ex(X509_PUBKEY_it(), libctx, propq);
272
0
    if (!x509_pubkey_set0_libctx(pubkey, libctx, propq)) {
273
0
        X509_PUBKEY_free(pubkey);
274
0
        pubkey = NULL;
275
0
    }
276
0
    return pubkey;
277
0
}
278
279
/*
280
 * X509_PUBKEY_dup() must be implemented manually, because there is no
281
 * support for it in ASN1_EXTERN_FUNCS.
282
 */
283
X509_PUBKEY *X509_PUBKEY_dup(const X509_PUBKEY *a)
284
0
{
285
0
    X509_PUBKEY *pubkey = OPENSSL_zalloc(sizeof(*pubkey));
286
287
0
    if (pubkey == NULL)
288
0
        return NULL;
289
0
    if (!x509_pubkey_set0_libctx(pubkey, a->libctx, a->propq)) {
290
0
        ERR_raise(ERR_LIB_X509, ERR_R_X509_LIB);
291
0
        x509_pubkey_ex_free((ASN1_VALUE **)&pubkey,
292
0
                            ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL));
293
0
        return NULL;
294
0
    }
295
0
    if ((pubkey->algor = X509_ALGOR_dup(a->algor)) == NULL
296
0
        || (pubkey->public_key = ASN1_BIT_STRING_new()) == NULL
297
0
        || !ASN1_BIT_STRING_set(pubkey->public_key,
298
0
                                a->public_key->data,
299
0
                                a->public_key->length)) {
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 =
350
0
            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
1.59M
{
406
1.59M
    EVP_PKEY *pkey;
407
1.59M
    int nid;
408
409
1.59M
    nid = OBJ_obj2nid(key->algor->algorithm);
410
1.59M
    if (!key->flag_force_legacy) {
411
840k
#ifndef OPENSSL_NO_ENGINE
412
840k
        ENGINE *e = NULL;
413
414
840k
        e = ENGINE_get_pkey_meth_engine(nid);
415
840k
        if (e == NULL)
416
840k
            return 0;
417
0
        ENGINE_finish(e);
418
#else
419
        return 0;
420
#endif
421
0
    }
422
423
750k
    pkey = EVP_PKEY_new();
424
750k
    if (pkey == NULL) {
425
0
        ERR_raise(ERR_LIB_X509, ERR_R_EVP_LIB);
426
0
        return -1;
427
0
    }
428
429
750k
    if (!EVP_PKEY_set_type(pkey, nid)) {
430
0
        ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM);
431
0
        goto error;
432
0
    }
433
434
750k
    if (pkey->ameth->pub_decode) {
435
        /*
436
         * Treat any failure of pub_decode as a decode error. In
437
         * future we could have different return codes for decode
438
         * errors and fatal errors such as malloc failure.
439
         */
440
750k
        if (!pkey->ameth->pub_decode(pkey, key))
441
354k
            goto error;
442
750k
    } else {
443
0
        ERR_raise(ERR_LIB_X509, X509_R_METHOD_NOT_SUPPORTED);
444
0
        goto error;
445
0
    }
446
447
395k
    *ppkey = pkey;
448
395k
    return 1;
449
450
354k
 error:
451
354k
    EVP_PKEY_free(pkey);
452
354k
    return 0;
453
750k
}
454
455
EVP_PKEY *X509_PUBKEY_get0(const X509_PUBKEY *key)
456
1.36M
{
457
1.36M
    if (key == NULL) {
458
0
        ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
459
0
        return NULL;
460
0
    }
461
462
1.36M
    if (key->pkey == NULL) {
463
        /* We failed to decode the key when we loaded it, or it was never set */
464
403k
        ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
465
403k
        return NULL;
466
403k
    }
467
468
957k
    return key->pkey;
469
1.36M
}
470
471
EVP_PKEY *X509_PUBKEY_get(const X509_PUBKEY *key)
472
752k
{
473
752k
    EVP_PKEY *ret = X509_PUBKEY_get0(key);
474
475
752k
    if (ret != NULL && !EVP_PKEY_up_ref(ret)) {
476
0
        ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
477
0
        ret = NULL;
478
0
    }
479
752k
    return ret;
480
752k
}
481
482
/*
483
 * Now three pseudo ASN1 routines that take an EVP_PKEY structure and encode
484
 * or decode as X509_PUBKEY
485
 */
486
static EVP_PKEY *d2i_PUBKEY_int(EVP_PKEY **a,
487
                                const unsigned char **pp, long length,
488
                                OSSL_LIB_CTX *libctx, const char *propq,
489
                                unsigned int force_legacy,
490
                                X509_PUBKEY *
491
                                (*d2i_x509_pubkey)(X509_PUBKEY **a,
492
                                                   const unsigned char **in,
493
                                                   long len))
494
868k
{
495
868k
    X509_PUBKEY *xpk, *xpk2 = NULL, **pxpk = NULL;
496
868k
    EVP_PKEY *pktmp = NULL;
497
868k
    const unsigned char *q;
498
499
868k
    q = *pp;
500
501
    /*
502
     * If libctx or propq are non-NULL, we take advantage of the reuse
503
     * feature.  It's not generally recommended, but is safe enough for
504
     * newly created structures.
505
     */
506
868k
    if (libctx != NULL || propq != NULL || force_legacy) {
507
868k
        xpk2 = OPENSSL_zalloc(sizeof(*xpk2));
508
868k
        if (xpk2 == NULL)
509
0
            return NULL;
510
868k
        if (!x509_pubkey_set0_libctx(xpk2, libctx, propq))
511
0
            goto end;
512
868k
        xpk2->flag_force_legacy = !!force_legacy;
513
868k
        pxpk = &xpk2;
514
868k
    }
515
868k
    xpk = d2i_x509_pubkey(pxpk, &q, length);
516
868k
    if (xpk == NULL)
517
118k
        goto end;
518
750k
    pktmp = X509_PUBKEY_get(xpk);
519
750k
    X509_PUBKEY_free(xpk);
520
750k
    xpk2 = NULL;                 /* We know that xpk == xpk2 */
521
750k
    if (pktmp == NULL)
522
354k
        goto end;
523
395k
    *pp = q;
524
395k
    if (a != NULL) {
525
0
        EVP_PKEY_free(*a);
526
0
        *a = pktmp;
527
0
    }
528
868k
 end:
529
868k
    X509_PUBKEY_free(xpk2);
530
868k
    return pktmp;
531
395k
}
532
533
/* For the algorithm specific d2i functions further down */
534
EVP_PKEY *ossl_d2i_PUBKEY_legacy(EVP_PKEY **a, const unsigned char **pp,
535
                                 long length)
536
868k
{
537
868k
    return d2i_PUBKEY_int(a, pp, length, NULL, NULL, 1, d2i_X509_PUBKEY);
538
868k
}
539
540
EVP_PKEY *d2i_PUBKEY_ex(EVP_PKEY **a, const unsigned char **pp, long length,
541
                        OSSL_LIB_CTX *libctx, const char *propq)
542
4
{
543
4
    return d2i_PUBKEY_int(a, pp, length, libctx, propq, 0, d2i_X509_PUBKEY);
544
4
}
545
546
EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length)
547
0
{
548
0
    return d2i_PUBKEY_ex(a, pp, length, NULL, NULL);
549
0
}
550
551
int i2d_PUBKEY(const EVP_PKEY *a, unsigned char **pp)
552
0
{
553
0
    int ret = -1;
554
555
0
    if (a == NULL)
556
0
        return 0;
557
0
    if (a->ameth != NULL) {
558
0
        X509_PUBKEY *xpk = NULL;
559
560
0
        if ((xpk = X509_PUBKEY_new()) == NULL)
561
0
            return -1;
562
563
        /* pub_encode() only encode parameters, not the key itself */
564
0
        if (a->ameth->pub_encode != NULL && a->ameth->pub_encode(xpk, a)) {
565
0
            xpk->pkey = (EVP_PKEY *)a;
566
0
            ret = i2d_X509_PUBKEY(xpk, pp);
567
0
            xpk->pkey = NULL;
568
0
        }
569
0
        X509_PUBKEY_free(xpk);
570
0
    } else if (a->keymgmt != NULL) {
571
0
        OSSL_ENCODER_CTX *ctx =
572
0
            OSSL_ENCODER_CTX_new_for_pkey(a, EVP_PKEY_PUBLIC_KEY,
573
0
                                          "DER", "SubjectPublicKeyInfo",
574
0
                                          NULL);
575
0
        BIO *out = BIO_new(BIO_s_mem());
576
0
        BUF_MEM *buf = NULL;
577
578
0
        if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0
579
0
            && out != NULL
580
0
            && OSSL_ENCODER_to_bio(ctx, out)
581
0
            && BIO_get_mem_ptr(out, &buf) > 0) {
582
0
            ret = buf->length;
583
584
0
            if (pp != NULL) {
585
0
                if (*pp == NULL) {
586
0
                    *pp = (unsigned char *)buf->data;
587
0
                    buf->length = 0;
588
0
                    buf->data = NULL;
589
0
                } else {
590
0
                    memcpy(*pp, buf->data, ret);
591
0
                    *pp += ret;
592
0
                }
593
0
            }
594
0
        }
595
0
        BIO_free(out);
596
0
        OSSL_ENCODER_CTX_free(ctx);
597
0
    }
598
599
0
    return ret;
600
0
}
601
602
/*
603
 * The following are equivalents but which return RSA and DSA keys
604
 */
605
RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length)
606
129k
{
607
129k
    EVP_PKEY *pkey;
608
129k
    RSA *key = NULL;
609
129k
    const unsigned char *q;
610
611
129k
    q = *pp;
612
129k
    pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
613
129k
    if (pkey == NULL)
614
32.4k
        return NULL;
615
97.0k
    key = EVP_PKEY_get1_RSA(pkey);
616
97.0k
    EVP_PKEY_free(pkey);
617
97.0k
    if (key == NULL)
618
0
        return NULL;
619
97.0k
    *pp = q;
620
97.0k
    if (a != NULL) {
621
0
        RSA_free(*a);
622
0
        *a = key;
623
0
    }
624
97.0k
    return key;
625
97.0k
}
626
627
int i2d_RSA_PUBKEY(const RSA *a, unsigned char **pp)
628
0
{
629
0
    EVP_PKEY *pktmp;
630
0
    int ret;
631
0
    if (!a)
632
0
        return 0;
633
0
    pktmp = EVP_PKEY_new();
634
0
    if (pktmp == NULL) {
635
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
636
0
        return -1;
637
0
    }
638
0
    (void)EVP_PKEY_assign_RSA(pktmp, (RSA *)a);
639
0
    ret = i2d_PUBKEY(pktmp, pp);
640
0
    pktmp->pkey.ptr = NULL;
641
0
    EVP_PKEY_free(pktmp);
642
0
    return ret;
643
0
}
644
645
#ifndef OPENSSL_NO_DH
646
DH *ossl_d2i_DH_PUBKEY(DH **a, const unsigned char **pp, long length)
647
28.9k
{
648
28.9k
    EVP_PKEY *pkey;
649
28.9k
    DH *key = NULL;
650
28.9k
    const unsigned char *q;
651
652
28.9k
    q = *pp;
653
28.9k
    pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
654
28.9k
    if (pkey == NULL)
655
15.6k
        return NULL;
656
13.3k
    if (EVP_PKEY_get_id(pkey) == EVP_PKEY_DH)
657
13.3k
        key = EVP_PKEY_get1_DH(pkey);
658
13.3k
    EVP_PKEY_free(pkey);
659
13.3k
    if (key == NULL)
660
0
        return NULL;
661
13.3k
    *pp = q;
662
13.3k
    if (a != NULL) {
663
0
        DH_free(*a);
664
0
        *a = key;
665
0
    }
666
13.3k
    return key;
667
13.3k
}
668
669
int ossl_i2d_DH_PUBKEY(const DH *a, unsigned char **pp)
670
0
{
671
0
    EVP_PKEY *pktmp;
672
0
    int ret;
673
0
    if (!a)
674
0
        return 0;
675
0
    pktmp = EVP_PKEY_new();
676
0
    if (pktmp == NULL) {
677
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
678
0
        return -1;
679
0
    }
680
0
    (void)EVP_PKEY_assign_DH(pktmp, (DH *)a);
681
0
    ret = i2d_PUBKEY(pktmp, pp);
682
0
    pktmp->pkey.ptr = NULL;
683
0
    EVP_PKEY_free(pktmp);
684
0
    return ret;
685
0
}
686
687
DH *ossl_d2i_DHx_PUBKEY(DH **a, const unsigned char **pp, long length)
688
55.5k
{
689
55.5k
    EVP_PKEY *pkey;
690
55.5k
    DH *key = NULL;
691
55.5k
    const unsigned char *q;
692
693
55.5k
    q = *pp;
694
55.5k
    pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
695
55.5k
    if (pkey == NULL)
696
37.7k
        return NULL;
697
17.8k
    if (EVP_PKEY_get_id(pkey) == EVP_PKEY_DHX)
698
17.8k
        key = EVP_PKEY_get1_DH(pkey);
699
17.8k
    EVP_PKEY_free(pkey);
700
17.8k
    if (key == NULL)
701
0
        return NULL;
702
17.8k
    *pp = q;
703
17.8k
    if (a != NULL) {
704
0
        DH_free(*a);
705
0
        *a = key;
706
0
    }
707
17.8k
    return key;
708
17.8k
}
709
710
int ossl_i2d_DHx_PUBKEY(const DH *a, unsigned char **pp)
711
0
{
712
0
    EVP_PKEY *pktmp;
713
0
    int ret;
714
0
    if (!a)
715
0
        return 0;
716
0
    pktmp = EVP_PKEY_new();
717
0
    if (pktmp == NULL) {
718
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
719
0
        return -1;
720
0
    }
721
0
    (void)EVP_PKEY_assign(pktmp, EVP_PKEY_DHX, (DH *)a);
722
0
    ret = i2d_PUBKEY(pktmp, pp);
723
0
    pktmp->pkey.ptr = NULL;
724
0
    EVP_PKEY_free(pktmp);
725
0
    return ret;
726
0
}
727
#endif
728
729
#ifndef OPENSSL_NO_DSA
730
DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
731
117k
{
732
117k
    EVP_PKEY *pkey;
733
117k
    DSA *key = NULL;
734
117k
    const unsigned char *q;
735
736
117k
    q = *pp;
737
117k
    pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
738
117k
    if (pkey == NULL)
739
79.0k
        return NULL;
740
38.2k
    key = EVP_PKEY_get1_DSA(pkey);
741
38.2k
    EVP_PKEY_free(pkey);
742
38.2k
    if (key == NULL)
743
0
        return NULL;
744
38.2k
    *pp = q;
745
38.2k
    if (a != NULL) {
746
0
        DSA_free(*a);
747
0
        *a = key;
748
0
    }
749
38.2k
    return key;
750
38.2k
}
751
752
/* Called from decoders; disallows provided DSA keys without parameters. */
753
DSA *ossl_d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
754
117k
{
755
117k
    DSA *key = NULL;
756
117k
    const unsigned char *data;
757
117k
    const BIGNUM *p, *q, *g;
758
759
117k
    data = *pp;
760
117k
    key = d2i_DSA_PUBKEY(NULL, &data, length);
761
117k
    if (key == NULL)
762
79.0k
        return NULL;
763
38.2k
    DSA_get0_pqg(key, &p, &q, &g);
764
38.2k
    if (p == NULL || q == NULL || g == NULL) {
765
2.06k
        DSA_free(key);
766
2.06k
        return NULL;
767
2.06k
    }
768
36.1k
    *pp = data;
769
36.1k
    if (a != NULL) {
770
0
        DSA_free(*a);
771
0
        *a = key;
772
0
    }
773
36.1k
    return key;
774
38.2k
}
775
776
int i2d_DSA_PUBKEY(const DSA *a, unsigned char **pp)
777
0
{
778
0
    EVP_PKEY *pktmp;
779
0
    int ret;
780
0
    if (!a)
781
0
        return 0;
782
0
    pktmp = EVP_PKEY_new();
783
0
    if (pktmp == NULL) {
784
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
785
0
        return -1;
786
0
    }
787
0
    (void)EVP_PKEY_assign_DSA(pktmp, (DSA *)a);
788
0
    ret = i2d_PUBKEY(pktmp, pp);
789
0
    pktmp->pkey.ptr = NULL;
790
0
    EVP_PKEY_free(pktmp);
791
0
    return ret;
792
0
}
793
#endif
794
795
#ifndef OPENSSL_NO_EC
796
EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
797
463k
{
798
463k
    EVP_PKEY *pkey;
799
463k
    EC_KEY *key = NULL;
800
463k
    const unsigned char *q;
801
463k
    int type;
802
803
463k
    q = *pp;
804
463k
    pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
805
463k
    if (pkey == NULL)
806
238k
        return NULL;
807
225k
    type = EVP_PKEY_get_id(pkey);
808
225k
    if (type == EVP_PKEY_EC || type == EVP_PKEY_SM2)
809
225k
        key = EVP_PKEY_get1_EC_KEY(pkey);
810
225k
    EVP_PKEY_free(pkey);
811
225k
    if (key == NULL)
812
0
        return NULL;
813
225k
    *pp = q;
814
225k
    if (a != NULL) {
815
0
        EC_KEY_free(*a);
816
0
        *a = key;
817
0
    }
818
225k
    return key;
819
225k
}
820
821
int i2d_EC_PUBKEY(const EC_KEY *a, unsigned char **pp)
822
0
{
823
0
    EVP_PKEY *pktmp;
824
0
    int ret;
825
826
0
    if (a == NULL)
827
0
        return 0;
828
0
    if ((pktmp = EVP_PKEY_new()) == NULL) {
829
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
830
0
        return -1;
831
0
    }
832
0
    (void)EVP_PKEY_assign_EC_KEY(pktmp, (EC_KEY *)a);
833
0
    ret = i2d_PUBKEY(pktmp, pp);
834
0
    pktmp->pkey.ptr = NULL;
835
0
    EVP_PKEY_free(pktmp);
836
0
    return ret;
837
0
}
838
839
# ifndef OPENSSL_NO_ECX
840
ECX_KEY *ossl_d2i_ED25519_PUBKEY(ECX_KEY **a,
841
                                 const unsigned char **pp, long length)
842
25.9k
{
843
25.9k
    EVP_PKEY *pkey;
844
25.9k
    ECX_KEY *key = NULL;
845
25.9k
    const unsigned char *q;
846
847
25.9k
    q = *pp;
848
25.9k
    pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
849
25.9k
    if (pkey == NULL)
850
25.6k
        return NULL;
851
291
    key = ossl_evp_pkey_get1_ED25519(pkey);
852
291
    EVP_PKEY_free(pkey);
853
291
    if (key == NULL)
854
0
        return NULL;
855
291
    *pp = q;
856
291
    if (a != NULL) {
857
0
        ossl_ecx_key_free(*a);
858
0
        *a = key;
859
0
    }
860
291
    return key;
861
291
}
862
863
int ossl_i2d_ED25519_PUBKEY(const ECX_KEY *a, unsigned char **pp)
864
0
{
865
0
    EVP_PKEY *pktmp;
866
0
    int ret;
867
868
0
    if (a == NULL)
869
0
        return 0;
870
0
    if ((pktmp = EVP_PKEY_new()) == NULL) {
871
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
872
0
        return -1;
873
0
    }
874
0
    (void)EVP_PKEY_assign(pktmp, EVP_PKEY_ED25519, (ECX_KEY *)a);
875
0
    ret = i2d_PUBKEY(pktmp, pp);
876
0
    pktmp->pkey.ptr = NULL;
877
0
    EVP_PKEY_free(pktmp);
878
0
    return ret;
879
0
}
880
881
ECX_KEY *ossl_d2i_ED448_PUBKEY(ECX_KEY **a,
882
                               const unsigned char **pp, long length)
883
13.8k
{
884
13.8k
    EVP_PKEY *pkey;
885
13.8k
    ECX_KEY *key = NULL;
886
13.8k
    const unsigned char *q;
887
888
13.8k
    q = *pp;
889
13.8k
    pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
890
13.8k
    if (pkey == NULL)
891
13.3k
        return NULL;
892
505
    if (EVP_PKEY_get_id(pkey) == EVP_PKEY_ED448)
893
505
        key = ossl_evp_pkey_get1_ED448(pkey);
894
505
    EVP_PKEY_free(pkey);
895
505
    if (key == NULL)
896
0
        return NULL;
897
505
    *pp = q;
898
505
    if (a != NULL) {
899
0
        ossl_ecx_key_free(*a);
900
0
        *a = key;
901
0
    }
902
505
    return key;
903
505
}
904
905
int ossl_i2d_ED448_PUBKEY(const ECX_KEY *a, unsigned char **pp)
906
0
{
907
0
    EVP_PKEY *pktmp;
908
0
    int ret;
909
910
0
    if (a == NULL)
911
0
        return 0;
912
0
    if ((pktmp = EVP_PKEY_new()) == NULL) {
913
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
914
0
        return -1;
915
0
    }
916
0
    (void)EVP_PKEY_assign(pktmp, EVP_PKEY_ED448, (ECX_KEY *)a);
917
0
    ret = i2d_PUBKEY(pktmp, pp);
918
0
    pktmp->pkey.ptr = NULL;
919
0
    EVP_PKEY_free(pktmp);
920
0
    return ret;
921
0
}
922
923
ECX_KEY *ossl_d2i_X25519_PUBKEY(ECX_KEY **a,
924
                                const unsigned char **pp, long length)
925
13.1k
{
926
13.1k
    EVP_PKEY *pkey;
927
13.1k
    ECX_KEY *key = NULL;
928
13.1k
    const unsigned char *q;
929
930
13.1k
    q = *pp;
931
13.1k
    pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
932
13.1k
    if (pkey == NULL)
933
12.9k
        return NULL;
934
247
    if (EVP_PKEY_get_id(pkey) == EVP_PKEY_X25519)
935
247
        key = ossl_evp_pkey_get1_X25519(pkey);
936
247
    EVP_PKEY_free(pkey);
937
247
    if (key == NULL)
938
0
        return NULL;
939
247
    *pp = q;
940
247
    if (a != NULL) {
941
0
        ossl_ecx_key_free(*a);
942
0
        *a = key;
943
0
    }
944
247
    return key;
945
247
}
946
947
int ossl_i2d_X25519_PUBKEY(const ECX_KEY *a, unsigned char **pp)
948
0
{
949
0
    EVP_PKEY *pktmp;
950
0
    int ret;
951
952
0
    if (a == NULL)
953
0
        return 0;
954
0
    if ((pktmp = EVP_PKEY_new()) == NULL) {
955
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
956
0
        return -1;
957
0
    }
958
0
    (void)EVP_PKEY_assign(pktmp, EVP_PKEY_X25519, (ECX_KEY *)a);
959
0
    ret = i2d_PUBKEY(pktmp, pp);
960
0
    pktmp->pkey.ptr = NULL;
961
0
    EVP_PKEY_free(pktmp);
962
0
    return ret;
963
0
}
964
965
ECX_KEY *ossl_d2i_X448_PUBKEY(ECX_KEY **a,
966
                              const unsigned char **pp, long length)
967
21.1k
{
968
21.1k
    EVP_PKEY *pkey;
969
21.1k
    ECX_KEY *key = NULL;
970
21.1k
    const unsigned char *q;
971
972
21.1k
    q = *pp;
973
21.1k
    pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
974
21.1k
    if (pkey == NULL)
975
18.4k
        return NULL;
976
2.66k
    if (EVP_PKEY_get_id(pkey) == EVP_PKEY_X448)
977
2.66k
        key = ossl_evp_pkey_get1_X448(pkey);
978
2.66k
    EVP_PKEY_free(pkey);
979
2.66k
    if (key == NULL)
980
0
        return NULL;
981
2.66k
    *pp = q;
982
2.66k
    if (a != NULL) {
983
0
        ossl_ecx_key_free(*a);
984
0
        *a = key;
985
0
    }
986
2.66k
    return key;
987
2.66k
}
988
989
int ossl_i2d_X448_PUBKEY(const ECX_KEY *a, unsigned char **pp)
990
0
{
991
0
    EVP_PKEY *pktmp;
992
0
    int ret;
993
994
0
    if (a == NULL)
995
0
        return 0;
996
0
    if ((pktmp = EVP_PKEY_new()) == NULL) {
997
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
998
0
        return -1;
999
0
    }
1000
0
    (void)EVP_PKEY_assign(pktmp, EVP_PKEY_X448, (ECX_KEY *)a);
1001
0
    ret = i2d_PUBKEY(pktmp, pp);
1002
0
    pktmp->pkey.ptr = NULL;
1003
0
    EVP_PKEY_free(pktmp);
1004
0
    return ret;
1005
0
}
1006
1007
# endif /* OPENSSL_NO_ECX */ 
1008
#endif
1009
1010
void X509_PUBKEY_set0_public_key(X509_PUBKEY *pub,
1011
                                 unsigned char *penc, int penclen)
1012
0
{
1013
0
    ASN1_STRING_set0(pub->public_key, penc, penclen);
1014
0
    ossl_asn1_string_set_bits_left(pub->public_key, 0);
1015
0
}
1016
1017
int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
1018
                           int ptype, void *pval,
1019
                           unsigned char *penc, int penclen)
1020
0
{
1021
0
    if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
1022
0
        return 0;
1023
0
    if (penc != NULL)
1024
0
        X509_PUBKEY_set0_public_key(pub, penc, penclen);
1025
0
    return 1;
1026
0
}
1027
1028
int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
1029
                           const unsigned char **pk, int *ppklen,
1030
                           X509_ALGOR **pa, const X509_PUBKEY *pub)
1031
1.54M
{
1032
1.54M
    if (ppkalg)
1033
29.2k
        *ppkalg = pub->algor->algorithm;
1034
1.54M
    if (pk) {
1035
751k
        *pk = pub->public_key->data;
1036
751k
        *ppklen = pub->public_key->length;
1037
751k
    }
1038
1.54M
    if (pa)
1039
1.51M
        *pa = pub->algor;
1040
1.54M
    return 1;
1041
1.54M
}
1042
1043
ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x)
1044
373
{
1045
373
    if (x == NULL)
1046
0
        return NULL;
1047
373
    return x->cert_info.key->public_key;
1048
373
}
1049
1050
/* Returns 1 for equal, 0, for non-equal, < 0 on error */
1051
int X509_PUBKEY_eq(const X509_PUBKEY *a, const X509_PUBKEY *b)
1052
0
{
1053
0
    X509_ALGOR *algA, *algB;
1054
0
    EVP_PKEY *pA, *pB;
1055
1056
0
    if (a == b)
1057
0
        return 1;
1058
0
    if (a == NULL || b == NULL)
1059
0
        return 0;
1060
0
    if (!X509_PUBKEY_get0_param(NULL, NULL, NULL, &algA, a) || algA == NULL
1061
0
        || !X509_PUBKEY_get0_param(NULL, NULL, NULL, &algB, b) || algB == NULL)
1062
0
        return -2;
1063
0
    if (X509_ALGOR_cmp(algA, algB) != 0)
1064
0
        return 0;
1065
0
    if ((pA = X509_PUBKEY_get0(a)) == NULL
1066
0
        || (pB = X509_PUBKEY_get0(b)) == NULL)
1067
0
        return -2;
1068
0
    return EVP_PKEY_eq(pA, pB);
1069
0
}
1070
1071
int ossl_x509_PUBKEY_get0_libctx(OSSL_LIB_CTX **plibctx, const char **ppropq,
1072
                                 const X509_PUBKEY *key)
1073
441k
{
1074
441k
    if (plibctx)
1075
441k
        *plibctx = key->libctx;
1076
441k
    if (ppropq)
1077
441k
        *ppropq = key->propq;
1078
441k
    return 1;
1079
441k
}