Coverage Report

Created: 2023-06-08 06:40

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