Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/crypto/pem/pvkfmt.c
Line
Count
Source
1
/*
2
 * Copyright 2005-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
 * Support for PVK format keys and related structures (such a PUBLICKEYBLOB
12
 * and PRIVATEKEYBLOB).
13
 */
14
15
/*
16
 * RSA and DSA low level APIs are deprecated for public use, but still ok for
17
 * internal use.
18
 */
19
#include "internal/deprecated.h"
20
21
#include <openssl/pem.h>
22
#include <openssl/rand.h>
23
#include <openssl/bn.h>
24
#include <openssl/dsa.h>
25
#include <openssl/rsa.h>
26
#include <openssl/kdf.h>
27
#include <openssl/core_names.h>
28
#include "internal/cryptlib.h"
29
#include "crypto/pem.h"
30
#include "crypto/evp.h"
31
32
/*
33
 * Utility function: read a DWORD (4 byte unsigned integer) in little endian
34
 * format
35
 */
36
37
static unsigned int read_ledword(const unsigned char **in)
38
53.9k
{
39
53.9k
    const unsigned char *p = *in;
40
53.9k
    unsigned int ret;
41
42
53.9k
    ret = (unsigned int)*p++;
43
53.9k
    ret |= (unsigned int)*p++ << 8;
44
53.9k
    ret |= (unsigned int)*p++ << 16;
45
53.9k
    ret |= (unsigned int)*p++ << 24;
46
53.9k
    *in = p;
47
53.9k
    return ret;
48
53.9k
}
49
50
/*
51
 * Read a BIGNUM in little endian format. The docs say that this should take
52
 * up bitlen/8 bytes.
53
 */
54
55
static int read_lebn(const unsigned char **in, unsigned int nbyte, BIGNUM **r)
56
18.0k
{
57
18.0k
    *r = BN_lebin2bn(*in, nbyte, NULL);
58
18.0k
    if (*r == NULL)
59
0
        return 0;
60
18.0k
    *in += nbyte;
61
18.0k
    return 1;
62
18.0k
}
63
64
/*
65
 * Create an EVP_PKEY from a type specific key.
66
 * This takes ownership of |key|, as long as the |evp_type| is acceptable
67
 * (EVP_PKEY_RSA or EVP_PKEY_DSA), even if the resulting EVP_PKEY wasn't
68
 * created.
69
 */
70
#define isdss_to_evp_type(isdss)                           \
71
0
    (isdss == 0 ? EVP_PKEY_RSA : isdss == 1 ? EVP_PKEY_DSA \
72
0
                                            : EVP_PKEY_NONE)
73
static EVP_PKEY *evp_pkey_new0_key(void *key, int evp_type)
74
0
{
75
0
    EVP_PKEY *pkey = NULL;
76
77
    /*
78
     * It's assumed that if |key| is NULL, something went wrong elsewhere
79
     * and suitable errors are already reported.
80
     */
81
0
    if (key == NULL)
82
0
        return NULL;
83
84
0
    if (!ossl_assert(evp_type == EVP_PKEY_RSA || evp_type == EVP_PKEY_DSA)) {
85
0
        ERR_raise(ERR_LIB_PEM, ERR_R_INTERNAL_ERROR);
86
0
        return NULL;
87
0
    }
88
89
0
    if ((pkey = EVP_PKEY_new()) != NULL) {
90
0
        switch (evp_type) {
91
0
        case EVP_PKEY_RSA:
92
0
            if (EVP_PKEY_set1_RSA(pkey, key))
93
0
                break;
94
0
            ERR_raise(ERR_LIB_PEM, ERR_R_EVP_LIB);
95
0
            EVP_PKEY_free(pkey);
96
0
            pkey = NULL;
97
0
            break;
98
0
#ifndef OPENSSL_NO_DSA
99
0
        case EVP_PKEY_DSA:
100
0
            if (EVP_PKEY_set1_DSA(pkey, key))
101
0
                break;
102
0
            ERR_raise(ERR_LIB_PEM, ERR_R_EVP_LIB);
103
0
            EVP_PKEY_free(pkey);
104
0
            pkey = NULL;
105
0
            break;
106
0
#endif
107
0
        }
108
0
    } else {
109
0
        ERR_raise(ERR_LIB_PEM, ERR_R_EVP_LIB);
110
0
    }
111
112
0
    switch (evp_type) {
113
0
    case EVP_PKEY_RSA:
114
0
        RSA_free(key);
115
0
        break;
116
0
#ifndef OPENSSL_NO_DSA
117
0
    case EVP_PKEY_DSA:
118
0
        DSA_free(key);
119
0
        break;
120
0
#endif
121
0
    }
122
123
0
    return pkey;
124
0
}
125
126
/* Convert private key blob to EVP_PKEY: RSA and DSA keys supported */
127
128
6.18k
#define MS_PUBLICKEYBLOB 0x6
129
4.45k
#define MS_PRIVATEKEYBLOB 0x7
130
8.92k
#define MS_RSA1MAGIC 0x31415352L
131
10.0k
#define MS_RSA2MAGIC 0x32415352L
132
1.92k
#define MS_DSS1MAGIC 0x31535344L
133
3.75k
#define MS_DSS2MAGIC 0x32535344L
134
135
0
#define MS_KEYALG_RSA_KEYX 0xa400
136
0
#define MS_KEYALG_DSS_SIGN 0x2200
137
138
0
#define MS_KEYTYPE_KEYX 0x1
139
0
#define MS_KEYTYPE_SIGN 0x2
140
141
/* The PVK file magic number: seems to spell out "bobsfile", who is Bob? */
142
9.27k
#define MS_PVKMAGIC 0xb0b5f11eL
143
/* Salt length for PVK files */
144
0
#define PVK_SALTLEN 0x10
145
/* Maximum length in PVK header */
146
990
#define PVK_MAX_KEYLEN 102400
147
/* Maximum salt length */
148
421
#define PVK_MAX_SALTLEN 10240
149
150
/*
151
 * Read the MSBLOB header and get relevant data from it.
152
 *
153
 * |pisdss| and |pispub| have a double role, as they can be used for
154
 * discovery as well as to check the blob meets expectations.
155
 * |*pisdss| is the indicator for whether the key is a DSA key or not.
156
 * |*pispub| is the indicator for whether the key is public or not.
157
 * In both cases, the following input values apply:
158
 *
159
 * 0    Expected to not be what the variable indicates.
160
 * 1    Expected to be what the variable indicates.
161
 * -1   No expectations, this function will assign 0 or 1 depending on
162
 *      header data.
163
 */
164
int ossl_do_blob_header(const unsigned char **in, unsigned int length,
165
    unsigned int *pmagic, unsigned int *pbitlen,
166
    int *pisdss, int *pispub)
167
32.7k
{
168
32.7k
    const unsigned char *p = *in;
169
170
32.7k
    if (length < 16)
171
13
        return 0;
172
    /* bType */
173
32.6k
    switch (*p) {
174
6.18k
    case MS_PUBLICKEYBLOB:
175
6.18k
        if (*pispub == 0) {
176
7
            ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
177
7
            return 0;
178
7
        }
179
6.17k
        *pispub = 1;
180
6.17k
        break;
181
182
4.45k
    case MS_PRIVATEKEYBLOB:
183
4.45k
        if (*pispub == 1) {
184
0
            ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
185
0
            return 0;
186
0
        }
187
4.45k
        *pispub = 0;
188
4.45k
        break;
189
190
22.0k
    default:
191
22.0k
        return 0;
192
32.6k
    }
193
10.6k
    p++;
194
    /* Version */
195
10.6k
    if (*p++ != 0x2) {
196
243
        ERR_raise(ERR_LIB_PEM, PEM_R_BAD_VERSION_NUMBER);
197
243
        return 0;
198
243
    }
199
    /* Ignore reserved, aiKeyAlg */
200
10.3k
    p += 6;
201
10.3k
    *pmagic = read_ledword(&p);
202
10.3k
    *pbitlen = read_ledword(&p);
203
204
    /* Consistency check for private vs public */
205
10.3k
    switch (*pmagic) {
206
966
    case MS_DSS1MAGIC:
207
4.94k
    case MS_RSA1MAGIC:
208
4.94k
        if (*pispub == 0) {
209
14
            ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PRIVATE_KEY_BLOB);
210
14
            return 0;
211
14
        }
212
4.93k
        break;
213
214
4.93k
    case MS_DSS2MAGIC:
215
3.73k
    case MS_RSA2MAGIC:
216
3.73k
        if (*pispub == 1) {
217
10
            ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_PUBLIC_KEY_BLOB);
218
10
            return 0;
219
10
        }
220
3.72k
        break;
221
222
3.72k
    default:
223
1.70k
        ERR_raise(ERR_LIB_PEM, PEM_R_BAD_MAGIC_NUMBER);
224
1.70k
        return -1;
225
10.3k
    }
226
227
    /* Check that we got the expected type */
228
8.65k
    switch (*pmagic) {
229
962
    case MS_DSS1MAGIC:
230
2.35k
    case MS_DSS2MAGIC:
231
2.35k
        if (*pisdss == 0) {
232
14
            ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_DSS_KEY_BLOB);
233
14
            return 0;
234
14
        }
235
2.34k
        *pisdss = 1;
236
2.34k
        break;
237
3.97k
    case MS_RSA1MAGIC:
238
6.30k
    case MS_RSA2MAGIC:
239
6.30k
        if (*pisdss == 1) {
240
30
            ERR_raise(ERR_LIB_PEM, PEM_R_EXPECTING_RSA_KEY_BLOB);
241
30
            return 0;
242
30
        }
243
6.27k
        *pisdss = 0;
244
6.27k
        break;
245
246
0
    default:
247
0
        ERR_raise(ERR_LIB_PEM, PEM_R_BAD_MAGIC_NUMBER);
248
0
        return -1;
249
8.65k
    }
250
8.61k
    *in = p;
251
8.61k
    return 1;
252
8.65k
}
253
254
unsigned int ossl_blob_length(unsigned bitlen, int isdss, int ispub)
255
5.40k
{
256
5.40k
    unsigned int nbyte = (bitlen + 7) >> 3;
257
5.40k
    unsigned int hnbyte = (bitlen + 15) >> 4;
258
259
5.40k
    if (isdss) {
260
261
        /*
262
         * Expected length: 20 for q + 3 components bitlen each + 24 for seed
263
         * structure.
264
         */
265
2.24k
        if (ispub)
266
911
            return 44 + 3 * nbyte;
267
        /*
268
         * Expected length: 20 for q, priv, 2 bitlen components + 24 for seed
269
         * structure.
270
         */
271
1.33k
        else
272
1.33k
            return 64 + 2 * nbyte;
273
3.16k
    } else {
274
        /* Expected length: 4 for 'e' + 'n' */
275
3.16k
        if (ispub)
276
1.98k
            return 4 + nbyte;
277
1.17k
        else
278
            /*
279
             * Expected length: 4 for 'e' and 7 other components. 2
280
             * components are bitlen size, 5 are bitlen/2
281
             */
282
1.17k
            return 4 + 2 * nbyte + 5 * hnbyte;
283
3.16k
    }
284
5.40k
}
285
286
static void *do_b2i_key(const unsigned char **in, unsigned int length,
287
    int *isdss, int *ispub)
288
172
{
289
172
    const unsigned char *p = *in;
290
172
    unsigned int bitlen, magic;
291
172
    void *key = NULL;
292
293
172
    if (ossl_do_blob_header(&p, length, &magic, &bitlen, isdss, ispub) <= 0) {
294
85
        ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_HEADER_PARSE_ERROR);
295
85
        return NULL;
296
85
    }
297
87
    length -= 16;
298
87
    if (length < ossl_blob_length(bitlen, *isdss, *ispub)) {
299
67
        ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
300
67
        return NULL;
301
67
    }
302
20
    if (!*isdss)
303
12
        key = ossl_b2i_RSA_after_header(&p, bitlen, *ispub);
304
8
#ifndef OPENSSL_NO_DSA
305
8
    else
306
8
        key = ossl_b2i_DSA_after_header(&p, bitlen, *ispub);
307
20
#endif
308
309
20
    if (key == NULL) {
310
4
        ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
311
4
        return NULL;
312
4
    }
313
314
16
    return key;
315
20
}
316
317
EVP_PKEY *ossl_b2i(const unsigned char **in, unsigned int length, int *ispub)
318
0
{
319
0
    int isdss = -1;
320
0
    void *key = do_b2i_key(in, length, &isdss, ispub);
321
322
0
    return evp_pkey_new0_key(key, isdss_to_evp_type(isdss));
323
0
}
324
325
EVP_PKEY *ossl_b2i_bio(BIO *in, int *ispub)
326
0
{
327
0
    const unsigned char *p;
328
0
    unsigned char hdr_buf[16], *buf = NULL;
329
0
    unsigned int bitlen, magic, length;
330
0
    int isdss = -1;
331
0
    void *key = NULL;
332
0
    EVP_PKEY *pkey = NULL;
333
334
0
    if (BIO_read(in, hdr_buf, 16) != 16) {
335
0
        ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
336
0
        return NULL;
337
0
    }
338
0
    p = hdr_buf;
339
0
    if (ossl_do_blob_header(&p, 16, &magic, &bitlen, &isdss, ispub) <= 0)
340
0
        return NULL;
341
342
0
    length = ossl_blob_length(bitlen, isdss, *ispub);
343
0
    if (length > BLOB_MAX_LENGTH) {
344
0
        ERR_raise(ERR_LIB_PEM, PEM_R_HEADER_TOO_LONG);
345
0
        return NULL;
346
0
    }
347
0
    buf = OPENSSL_malloc(length);
348
0
    if (buf == NULL)
349
0
        goto err;
350
0
    p = buf;
351
0
    if (BIO_read(in, buf, length) != (int)length) {
352
0
        ERR_raise(ERR_LIB_PEM, PEM_R_KEYBLOB_TOO_SHORT);
353
0
        goto err;
354
0
    }
355
356
0
    if (!isdss)
357
0
        key = ossl_b2i_RSA_after_header(&p, bitlen, *ispub);
358
0
#ifndef OPENSSL_NO_DSA
359
0
    else
360
0
        key = ossl_b2i_DSA_after_header(&p, bitlen, *ispub);
361
0
#endif
362
363
0
    if (key == NULL) {
364
0
        ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
365
0
        goto err;
366
0
    }
367
368
0
    pkey = evp_pkey_new0_key(key, isdss_to_evp_type(isdss));
369
0
err:
370
0
    OPENSSL_free(buf);
371
0
    return pkey;
372
0
}
373
374
#ifndef OPENSSL_NO_DSA
375
DSA *ossl_b2i_DSA_after_header(const unsigned char **in, unsigned int bitlen,
376
    int ispub)
377
2.13k
{
378
2.13k
    const unsigned char *p = *in;
379
2.13k
    DSA *dsa = NULL;
380
2.13k
    BN_CTX *ctx = NULL;
381
2.13k
    BIGNUM *pbn = NULL, *qbn = NULL, *gbn = NULL, *priv_key = NULL;
382
2.13k
    BIGNUM *pub_key = NULL;
383
2.13k
    unsigned int nbyte = (bitlen + 7) >> 3;
384
385
2.13k
    dsa = DSA_new();
386
2.13k
    if (dsa == NULL)
387
0
        goto dsaerr;
388
2.13k
    if (!read_lebn(&p, nbyte, &pbn))
389
0
        goto bnerr;
390
391
2.13k
    if (!read_lebn(&p, 20, &qbn))
392
0
        goto bnerr;
393
394
2.13k
    if (!read_lebn(&p, nbyte, &gbn))
395
0
        goto bnerr;
396
397
2.13k
    if (ispub) {
398
860
        if (!read_lebn(&p, nbyte, &pub_key))
399
0
            goto bnerr;
400
1.27k
    } else {
401
1.27k
        if (!read_lebn(&p, 20, &priv_key))
402
0
            goto bnerr;
403
404
        /* Set constant time flag before public key calculation */
405
1.27k
        BN_set_flags(priv_key, BN_FLG_CONSTTIME);
406
407
        /* Calculate public key */
408
1.27k
        pub_key = BN_new();
409
1.27k
        if (pub_key == NULL)
410
0
            goto bnerr;
411
1.27k
        if ((ctx = BN_CTX_new()) == NULL)
412
0
            goto bnerr;
413
414
1.27k
        if (!BN_mod_exp(pub_key, gbn, priv_key, pbn, ctx))
415
9
            goto bnerr;
416
417
1.26k
        BN_CTX_free(ctx);
418
1.26k
        ctx = NULL;
419
1.26k
    }
420
2.12k
    if (!DSA_set0_pqg(dsa, pbn, qbn, gbn))
421
0
        goto dsaerr;
422
2.12k
    pbn = qbn = gbn = NULL;
423
2.12k
    if (!DSA_set0_key(dsa, pub_key, priv_key))
424
0
        goto dsaerr;
425
2.12k
    pub_key = priv_key = NULL;
426
427
2.12k
    *in = p;
428
2.12k
    return dsa;
429
430
0
dsaerr:
431
0
    ERR_raise(ERR_LIB_PEM, ERR_R_DSA_LIB);
432
0
    goto err;
433
9
bnerr:
434
9
    ERR_raise(ERR_LIB_PEM, ERR_R_BN_LIB);
435
436
9
err:
437
9
    DSA_free(dsa);
438
9
    BN_free(pbn);
439
9
    BN_free(qbn);
440
9
    BN_free(gbn);
441
9
    BN_free(pub_key);
442
9
    BN_free(priv_key);
443
9
    BN_CTX_free(ctx);
444
9
    return NULL;
445
9
}
446
#endif
447
448
RSA *ossl_b2i_RSA_after_header(const unsigned char **in, unsigned int bitlen,
449
    int ispub)
450
3.02k
{
451
3.02k
    const unsigned char *pin = *in;
452
3.02k
    BIGNUM *e = NULL, *n = NULL, *d = NULL;
453
3.02k
    BIGNUM *p = NULL, *q = NULL, *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL;
454
3.02k
    RSA *rsa = NULL;
455
3.02k
    unsigned int nbyte = (bitlen + 7) >> 3;
456
3.02k
    unsigned int hnbyte = (bitlen + 15) >> 4;
457
458
3.02k
    rsa = RSA_new();
459
3.02k
    if (rsa == NULL)
460
0
        goto rsaerr;
461
3.02k
    e = BN_new();
462
3.02k
    if (e == NULL)
463
0
        goto bnerr;
464
3.02k
    if (!BN_set_word(e, read_ledword(&pin)))
465
0
        goto bnerr;
466
3.02k
    if (!read_lebn(&pin, nbyte, &n))
467
0
        goto bnerr;
468
3.02k
    if (!ispub) {
469
1.08k
        if (!read_lebn(&pin, hnbyte, &p))
470
0
            goto bnerr;
471
1.08k
        if (!read_lebn(&pin, hnbyte, &q))
472
0
            goto bnerr;
473
1.08k
        if (!read_lebn(&pin, hnbyte, &dmp1))
474
0
            goto bnerr;
475
1.08k
        if (!read_lebn(&pin, hnbyte, &dmq1))
476
0
            goto bnerr;
477
1.08k
        if (!read_lebn(&pin, hnbyte, &iqmp))
478
0
            goto bnerr;
479
1.08k
        if (!read_lebn(&pin, nbyte, &d))
480
0
            goto bnerr;
481
1.08k
        if (!RSA_set0_factors(rsa, p, q))
482
0
            goto rsaerr;
483
1.08k
        p = q = NULL;
484
1.08k
        if (!RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp))
485
0
            goto rsaerr;
486
1.08k
        dmp1 = dmq1 = iqmp = NULL;
487
1.08k
    }
488
3.02k
    if (!RSA_set0_key(rsa, n, e, d))
489
0
        goto rsaerr;
490
3.02k
    n = e = d = NULL;
491
492
3.02k
    *in = pin;
493
3.02k
    return rsa;
494
495
0
rsaerr:
496
0
    ERR_raise(ERR_LIB_PEM, ERR_R_RSA_LIB);
497
0
    goto err;
498
0
bnerr:
499
0
    ERR_raise(ERR_LIB_PEM, ERR_R_BN_LIB);
500
501
0
err:
502
0
    BN_free(e);
503
0
    BN_free(n);
504
0
    BN_free(p);
505
0
    BN_free(q);
506
0
    BN_free(dmp1);
507
0
    BN_free(dmq1);
508
0
    BN_free(iqmp);
509
0
    BN_free(d);
510
0
    RSA_free(rsa);
511
0
    return NULL;
512
0
}
513
514
EVP_PKEY *b2i_PrivateKey(const unsigned char **in, long length)
515
0
{
516
0
    int ispub = 0;
517
518
0
    return ossl_b2i(in, length, &ispub);
519
0
}
520
521
EVP_PKEY *b2i_PublicKey(const unsigned char **in, long length)
522
0
{
523
0
    int ispub = 1;
524
525
0
    return ossl_b2i(in, length, &ispub);
526
0
}
527
528
EVP_PKEY *b2i_PrivateKey_bio(BIO *in)
529
0
{
530
0
    int ispub = 0;
531
532
0
    return ossl_b2i_bio(in, &ispub);
533
0
}
534
535
EVP_PKEY *b2i_PublicKey_bio(BIO *in)
536
0
{
537
0
    int ispub = 1;
538
539
0
    return ossl_b2i_bio(in, &ispub);
540
0
}
541
542
static void write_ledword(unsigned char **out, unsigned int dw)
543
0
{
544
0
    unsigned char *p = *out;
545
546
0
    *p++ = dw & 0xff;
547
0
    *p++ = (dw >> 8) & 0xff;
548
0
    *p++ = (dw >> 16) & 0xff;
549
0
    *p++ = (dw >> 24) & 0xff;
550
0
    *out = p;
551
0
}
552
553
static void write_lebn(unsigned char **out, const BIGNUM *bn, int len)
554
0
{
555
0
    BN_bn2lebinpad(bn, *out, len);
556
0
    *out += len;
557
0
}
558
559
static int check_bitlen_rsa(const RSA *rsa, int ispub, unsigned int *magic);
560
static void write_rsa(unsigned char **out, const RSA *rsa, int ispub);
561
562
#ifndef OPENSSL_NO_DSA
563
static int check_bitlen_dsa(const DSA *dsa, int ispub, unsigned int *magic);
564
static void write_dsa(unsigned char **out, const DSA *dsa, int ispub);
565
#endif
566
567
static int do_i2b(unsigned char **out, const EVP_PKEY *pk, int ispub)
568
0
{
569
0
    unsigned char *p;
570
0
    unsigned int bitlen = 0, magic = 0, keyalg = 0;
571
0
    int outlen = -1, noinc = 0;
572
573
0
    if (EVP_PKEY_is_a(pk, "RSA")) {
574
0
        bitlen = check_bitlen_rsa(EVP_PKEY_get0_RSA(pk), ispub, &magic);
575
0
        keyalg = MS_KEYALG_RSA_KEYX;
576
0
#ifndef OPENSSL_NO_DSA
577
0
    } else if (EVP_PKEY_is_a(pk, "DSA")) {
578
0
        bitlen = check_bitlen_dsa(EVP_PKEY_get0_DSA(pk), ispub, &magic);
579
0
        keyalg = MS_KEYALG_DSS_SIGN;
580
0
#endif
581
0
    }
582
0
    if (bitlen == 0) {
583
0
        goto end;
584
0
    }
585
0
    outlen = 16
586
0
        + ossl_blob_length(bitlen, keyalg == MS_KEYALG_DSS_SIGN ? 1 : 0, ispub);
587
0
    if (out == NULL)
588
0
        goto end;
589
0
    if (*out)
590
0
        p = *out;
591
0
    else {
592
0
        if ((p = OPENSSL_malloc(outlen)) == NULL) {
593
0
            outlen = -1;
594
0
            goto end;
595
0
        }
596
0
        *out = p;
597
0
        noinc = 1;
598
0
    }
599
0
    if (ispub)
600
0
        *p++ = MS_PUBLICKEYBLOB;
601
0
    else
602
0
        *p++ = MS_PRIVATEKEYBLOB;
603
0
    *p++ = 0x2;
604
0
    *p++ = 0;
605
0
    *p++ = 0;
606
0
    write_ledword(&p, keyalg);
607
0
    write_ledword(&p, magic);
608
0
    write_ledword(&p, bitlen);
609
0
    if (keyalg == MS_KEYALG_RSA_KEYX)
610
0
        write_rsa(&p, EVP_PKEY_get0_RSA(pk), ispub);
611
0
#ifndef OPENSSL_NO_DSA
612
0
    else
613
0
        write_dsa(&p, EVP_PKEY_get0_DSA(pk), ispub);
614
0
#endif
615
0
    if (!noinc)
616
0
        *out += outlen;
617
0
end:
618
0
    return outlen;
619
0
}
620
621
static int do_i2b_bio(BIO *out, const EVP_PKEY *pk, int ispub)
622
0
{
623
0
    unsigned char *tmp = NULL;
624
0
    int outlen, wrlen;
625
626
0
    outlen = do_i2b(&tmp, pk, ispub);
627
0
    if (outlen < 0)
628
0
        return -1;
629
0
    wrlen = BIO_write(out, tmp, outlen);
630
0
    OPENSSL_free(tmp);
631
0
    if (wrlen == outlen)
632
0
        return outlen;
633
0
    return -1;
634
0
}
635
636
static int check_bitlen_rsa(const RSA *rsa, int ispub, unsigned int *pmagic)
637
0
{
638
0
    int nbyte, hnbyte, bitlen;
639
0
    const BIGNUM *e;
640
641
0
    RSA_get0_key(rsa, NULL, &e, NULL);
642
0
    if (BN_num_bits(e) > 32)
643
0
        goto badkey;
644
0
    bitlen = RSA_bits(rsa);
645
0
    nbyte = RSA_size(rsa);
646
0
    hnbyte = (bitlen + 15) >> 4;
647
0
    if (ispub) {
648
0
        *pmagic = MS_RSA1MAGIC;
649
0
        return bitlen;
650
0
    } else {
651
0
        const BIGNUM *d, *p, *q, *iqmp, *dmp1, *dmq1;
652
653
0
        *pmagic = MS_RSA2MAGIC;
654
655
        /*
656
         * For private key each component must fit within nbyte or hnbyte.
657
         */
658
0
        RSA_get0_key(rsa, NULL, NULL, &d);
659
0
        if (BN_num_bytes(d) > nbyte)
660
0
            goto badkey;
661
0
        RSA_get0_factors(rsa, &p, &q);
662
0
        RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
663
0
        if ((BN_num_bytes(iqmp) > hnbyte)
664
0
            || (BN_num_bytes(p) > hnbyte)
665
0
            || (BN_num_bytes(q) > hnbyte)
666
0
            || (BN_num_bytes(dmp1) > hnbyte)
667
0
            || (BN_num_bytes(dmq1) > hnbyte))
668
0
            goto badkey;
669
0
    }
670
0
    return bitlen;
671
0
badkey:
672
0
    ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
673
0
    return 0;
674
0
}
675
676
static void write_rsa(unsigned char **out, const RSA *rsa, int ispub)
677
0
{
678
0
    int nbyte, hnbyte;
679
0
    const BIGNUM *n, *d, *e, *p, *q, *iqmp, *dmp1, *dmq1;
680
681
0
    nbyte = RSA_size(rsa);
682
0
    hnbyte = (RSA_bits(rsa) + 15) >> 4;
683
0
    RSA_get0_key(rsa, &n, &e, &d);
684
0
    write_lebn(out, e, 4);
685
0
    write_lebn(out, n, nbyte);
686
0
    if (ispub)
687
0
        return;
688
0
    RSA_get0_factors(rsa, &p, &q);
689
0
    RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
690
0
    write_lebn(out, p, hnbyte);
691
0
    write_lebn(out, q, hnbyte);
692
0
    write_lebn(out, dmp1, hnbyte);
693
0
    write_lebn(out, dmq1, hnbyte);
694
0
    write_lebn(out, iqmp, hnbyte);
695
0
    write_lebn(out, d, nbyte);
696
0
}
697
698
#ifndef OPENSSL_NO_DSA
699
static int check_bitlen_dsa(const DSA *dsa, int ispub, unsigned int *pmagic)
700
0
{
701
0
    int bitlen;
702
0
    const BIGNUM *p = NULL, *q = NULL, *g = NULL;
703
0
    const BIGNUM *pub_key = NULL, *priv_key = NULL;
704
705
0
    DSA_get0_pqg(dsa, &p, &q, &g);
706
0
    DSA_get0_key(dsa, &pub_key, &priv_key);
707
0
    bitlen = BN_num_bits(p);
708
0
    if ((bitlen & 7) || (BN_num_bits(q) != 160)
709
0
        || (BN_num_bits(g) > bitlen))
710
0
        goto badkey;
711
0
    if (ispub) {
712
0
        if (BN_num_bits(pub_key) > bitlen)
713
0
            goto badkey;
714
0
        *pmagic = MS_DSS1MAGIC;
715
0
    } else {
716
0
        if (BN_num_bits(priv_key) > 160)
717
0
            goto badkey;
718
0
        *pmagic = MS_DSS2MAGIC;
719
0
    }
720
721
0
    return bitlen;
722
0
badkey:
723
0
    ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_KEY_COMPONENTS);
724
0
    return 0;
725
0
}
726
727
static void write_dsa(unsigned char **out, const DSA *dsa, int ispub)
728
0
{
729
0
    int nbyte;
730
0
    const BIGNUM *p = NULL, *q = NULL, *g = NULL;
731
0
    const BIGNUM *pub_key = NULL, *priv_key = NULL;
732
733
0
    DSA_get0_pqg(dsa, &p, &q, &g);
734
0
    DSA_get0_key(dsa, &pub_key, &priv_key);
735
0
    nbyte = BN_num_bytes(p);
736
0
    write_lebn(out, p, nbyte);
737
0
    write_lebn(out, q, 20);
738
0
    write_lebn(out, g, nbyte);
739
0
    if (ispub)
740
0
        write_lebn(out, pub_key, nbyte);
741
0
    else
742
0
        write_lebn(out, priv_key, 20);
743
    /* Set "invalid" for seed structure values */
744
0
    memset(*out, 0xff, 24);
745
0
    *out += 24;
746
0
    return;
747
0
}
748
#endif
749
750
int i2b_PrivateKey_bio(BIO *out, const EVP_PKEY *pk)
751
0
{
752
0
    return do_i2b_bio(out, pk, 0);
753
0
}
754
755
int i2b_PublicKey_bio(BIO *out, const EVP_PKEY *pk)
756
0
{
757
0
    return do_i2b_bio(out, pk, 1);
758
0
}
759
760
int ossl_do_PVK_header(const unsigned char **in, unsigned int length,
761
    int skip_magic,
762
    unsigned int *psaltlen, unsigned int *pkeylen)
763
9.27k
{
764
9.27k
    const unsigned char *p = *in;
765
9.27k
    unsigned int pvk_magic, is_encrypted;
766
767
9.27k
    if (skip_magic) {
768
0
        if (length < 20) {
769
0
            ERR_raise(ERR_LIB_PEM, PEM_R_PVK_TOO_SHORT);
770
0
            return 0;
771
0
        }
772
9.27k
    } else {
773
9.27k
        if (length < 24) {
774
0
            ERR_raise(ERR_LIB_PEM, PEM_R_PVK_TOO_SHORT);
775
0
            return 0;
776
0
        }
777
9.27k
        pvk_magic = read_ledword(&p);
778
9.27k
        if (pvk_magic != MS_PVKMAGIC) {
779
8.78k
            ERR_raise(ERR_LIB_PEM, PEM_R_BAD_MAGIC_NUMBER);
780
8.78k
            return 0;
781
8.78k
        }
782
9.27k
    }
783
    /* Skip reserved */
784
495
    p += 4;
785
    /*
786
     * keytype =
787
     */
788
495
    read_ledword(&p);
789
495
    is_encrypted = read_ledword(&p);
790
495
    *psaltlen = read_ledword(&p);
791
495
    *pkeylen = read_ledword(&p);
792
793
495
    if (*pkeylen > PVK_MAX_KEYLEN || *psaltlen > PVK_MAX_SALTLEN)
794
184
        return 0;
795
796
311
    if (is_encrypted && *psaltlen == 0) {
797
46
        ERR_raise(ERR_LIB_PEM, PEM_R_INCONSISTENT_HEADER);
798
46
        return 0;
799
46
    }
800
801
265
    *in = p;
802
265
    return 1;
803
311
}
804
805
#ifndef OPENSSL_NO_RC4
806
static int derive_pvk_key(unsigned char *key, size_t keylen,
807
    const unsigned char *salt, unsigned int saltlen,
808
    const unsigned char *pass, int passlen,
809
    OSSL_LIB_CTX *libctx, const char *propq)
810
0
{
811
0
    EVP_KDF *kdf;
812
0
    EVP_KDF_CTX *ctx;
813
0
    OSSL_PARAM params[5], *p = params;
814
0
    int rv;
815
816
0
    if ((kdf = EVP_KDF_fetch(libctx, "PVKKDF", propq)) == NULL)
817
0
        return 0;
818
0
    ctx = EVP_KDF_CTX_new(kdf);
819
0
    EVP_KDF_free(kdf);
820
0
    if (ctx == NULL)
821
0
        return 0;
822
823
0
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
824
0
        (void *)salt, saltlen);
825
0
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
826
0
        (void *)pass, passlen);
827
0
    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, SN_sha1, 0);
828
0
    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_PROPERTIES,
829
0
        (char *)propq, 0);
830
0
    *p = OSSL_PARAM_construct_end();
831
832
0
    rv = EVP_KDF_derive(ctx, key, keylen, params);
833
0
    EVP_KDF_CTX_free(ctx);
834
0
    return rv;
835
0
}
836
#endif
837
838
static void *do_PVK_body_key(const unsigned char **in,
839
    unsigned int saltlen, unsigned int keylen,
840
    pem_password_cb *cb, void *u,
841
    int *isdss, int *ispub,
842
    OSSL_LIB_CTX *libctx, const char *propq)
843
193
{
844
193
    const unsigned char *p = *in;
845
193
    unsigned char *enctmp = NULL;
846
193
    unsigned char keybuf[20];
847
193
    void *key = NULL;
848
193
#ifndef OPENSSL_NO_RC4
849
193
    EVP_CIPHER *rc4 = NULL;
850
193
#endif
851
193
    EVP_CIPHER_CTX *cctx = EVP_CIPHER_CTX_new();
852
853
193
    if (cctx == NULL) {
854
0
        ERR_raise(ERR_LIB_PEM, ERR_R_EVP_LIB);
855
0
        goto err;
856
0
    }
857
858
193
    if (saltlen) {
859
21
#ifndef OPENSSL_NO_RC4
860
21
        unsigned int magic;
861
21
        char psbuf[PEM_BUFSIZE];
862
21
        int enctmplen, inlen;
863
21
        unsigned char *q;
864
865
21
        if (cb)
866
21
            inlen = cb(psbuf, PEM_BUFSIZE, 0, u);
867
0
        else
868
0
            inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
869
21
        if (inlen < 0) {
870
21
            ERR_raise(ERR_LIB_PEM, PEM_R_BAD_PASSWORD_READ);
871
21
            goto err;
872
21
        }
873
0
        enctmp = OPENSSL_malloc(keylen + 8);
874
0
        if (enctmp == NULL)
875
0
            goto err;
876
0
        if (!derive_pvk_key(keybuf, sizeof(keybuf), p, saltlen,
877
0
                (unsigned char *)psbuf, inlen, libctx, propq))
878
0
            goto err;
879
0
        p += saltlen;
880
        /* Copy BLOBHEADER across, decrypt rest */
881
0
        memcpy(enctmp, p, 8);
882
0
        p += 8;
883
0
        if (keylen < 8) {
884
0
            ERR_raise(ERR_LIB_PEM, PEM_R_PVK_TOO_SHORT);
885
0
            goto err;
886
0
        }
887
0
        inlen = keylen - 8;
888
0
        q = enctmp + 8;
889
0
        if ((rc4 = EVP_CIPHER_fetch(libctx, "RC4", propq)) == NULL)
890
0
            goto err;
891
0
        if (!EVP_DecryptInit_ex(cctx, rc4, NULL, keybuf, NULL))
892
0
            goto err;
893
0
        if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen))
894
0
            goto err;
895
0
        if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen))
896
0
            goto err;
897
0
        magic = read_ledword((const unsigned char **)&q);
898
0
        if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
899
0
            q = enctmp + 8;
900
0
            memset(keybuf + 5, 0, 11);
901
0
            if (!EVP_DecryptInit_ex(cctx, rc4, NULL, keybuf, NULL))
902
0
                goto err;
903
0
            if (!EVP_DecryptUpdate(cctx, q, &enctmplen, p, inlen))
904
0
                goto err;
905
0
            if (!EVP_DecryptFinal_ex(cctx, q + enctmplen, &enctmplen))
906
0
                goto err;
907
0
            magic = read_ledword((const unsigned char **)&q);
908
0
            if (magic != MS_RSA2MAGIC && magic != MS_DSS2MAGIC) {
909
0
                ERR_raise(ERR_LIB_PEM, PEM_R_BAD_DECRYPT);
910
0
                goto err;
911
0
            }
912
0
        }
913
0
        p = enctmp;
914
#else
915
        ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_CIPHER);
916
        goto err;
917
#endif
918
0
    }
919
920
172
    key = do_b2i_key(&p, keylen, isdss, ispub);
921
193
err:
922
193
    EVP_CIPHER_CTX_free(cctx);
923
193
#ifndef OPENSSL_NO_RC4
924
193
    EVP_CIPHER_free(rc4);
925
193
#endif
926
193
    if (enctmp != NULL) {
927
0
        OPENSSL_cleanse(keybuf, sizeof(keybuf));
928
0
        OPENSSL_free(enctmp);
929
0
    }
930
193
    return key;
931
172
}
932
933
static void *do_PVK_key_bio(BIO *in, pem_password_cb *cb, void *u,
934
    int *isdss, int *ispub,
935
    OSSL_LIB_CTX *libctx, const char *propq)
936
43.6k
{
937
43.6k
    unsigned char pvk_hdr[24], *buf = NULL;
938
43.6k
    const unsigned char *p;
939
43.6k
    int buflen;
940
43.6k
    void *key = NULL;
941
43.6k
    unsigned int saltlen, keylen;
942
943
43.6k
    if (BIO_read(in, pvk_hdr, 24) != 24) {
944
17.1k
        ERR_raise(ERR_LIB_PEM, PEM_R_PVK_DATA_TOO_SHORT);
945
17.1k
        return NULL;
946
17.1k
    }
947
26.4k
    p = pvk_hdr;
948
949
26.4k
    if (!ossl_do_PVK_header(&p, 24, 0, &saltlen, &keylen))
950
26.0k
        return 0;
951
452
    buflen = (int)keylen + saltlen;
952
452
    buf = OPENSSL_malloc(buflen);
953
452
    if (buf == NULL)
954
7
        return 0;
955
445
    p = buf;
956
445
    if (BIO_read(in, buf, buflen) != buflen) {
957
252
        ERR_raise(ERR_LIB_PEM, PEM_R_PVK_DATA_TOO_SHORT);
958
252
        goto err;
959
252
    }
960
193
    key = do_PVK_body_key(&p, saltlen, keylen, cb, u, isdss, ispub, libctx, propq);
961
962
445
err:
963
445
    OPENSSL_clear_free(buf, buflen);
964
445
    return key;
965
193
}
966
967
#ifndef OPENSSL_NO_DSA
968
DSA *b2i_DSA_PVK_bio_ex(BIO *in, pem_password_cb *cb, void *u,
969
    OSSL_LIB_CTX *libctx, const char *propq)
970
24.9k
{
971
24.9k
    int isdss = 1;
972
24.9k
    int ispub = 0; /* PVK keys are always private */
973
974
24.9k
    return do_PVK_key_bio(in, cb, u, &isdss, &ispub, libctx, propq);
975
24.9k
}
976
977
DSA *b2i_DSA_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
978
0
{
979
0
    return b2i_DSA_PVK_bio_ex(in, cb, u, NULL, NULL);
980
0
}
981
#endif
982
983
RSA *b2i_RSA_PVK_bio_ex(BIO *in, pem_password_cb *cb, void *u,
984
    OSSL_LIB_CTX *libctx, const char *propq)
985
18.7k
{
986
18.7k
    int isdss = 0;
987
18.7k
    int ispub = 0; /* PVK keys are always private */
988
989
18.7k
    return do_PVK_key_bio(in, cb, u, &isdss, &ispub, libctx, propq);
990
18.7k
}
991
992
RSA *b2i_RSA_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
993
0
{
994
0
    return b2i_RSA_PVK_bio_ex(in, cb, u, NULL, NULL);
995
0
}
996
997
EVP_PKEY *b2i_PVK_bio_ex(BIO *in, pem_password_cb *cb, void *u,
998
    OSSL_LIB_CTX *libctx, const char *propq)
999
0
{
1000
0
    int isdss = -1;
1001
0
    int ispub = -1;
1002
0
    void *key = do_PVK_key_bio(in, cb, u, &isdss, &ispub, NULL, NULL);
1003
1004
0
    return evp_pkey_new0_key(key, isdss_to_evp_type(isdss));
1005
0
}
1006
1007
EVP_PKEY *b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
1008
0
{
1009
0
    return b2i_PVK_bio_ex(in, cb, u, NULL, NULL);
1010
0
}
1011
1012
static int i2b_PVK(unsigned char **out, const EVP_PKEY *pk, int enclevel,
1013
    pem_password_cb *cb, void *u, OSSL_LIB_CTX *libctx,
1014
    const char *propq)
1015
0
{
1016
0
    int ret = -1;
1017
0
    int outlen = 24, pklen;
1018
0
    unsigned char *p = NULL, *start = NULL;
1019
0
    EVP_CIPHER_CTX *cctx = NULL;
1020
0
#ifndef OPENSSL_NO_RC4
1021
0
    unsigned char *salt = NULL;
1022
0
    EVP_CIPHER *rc4 = NULL;
1023
0
#endif
1024
1025
0
    if (enclevel)
1026
0
        outlen += PVK_SALTLEN;
1027
0
    pklen = do_i2b(NULL, pk, 0);
1028
0
    if (pklen < 0)
1029
0
        return -1;
1030
0
    outlen += pklen;
1031
0
    if (out == NULL)
1032
0
        return outlen;
1033
0
    if (*out != NULL) {
1034
0
        p = *out;
1035
0
    } else {
1036
0
        start = p = OPENSSL_malloc(outlen);
1037
0
        if (p == NULL)
1038
0
            return -1;
1039
0
    }
1040
1041
0
    cctx = EVP_CIPHER_CTX_new();
1042
0
    if (cctx == NULL)
1043
0
        goto error;
1044
1045
0
    write_ledword(&p, MS_PVKMAGIC);
1046
0
    write_ledword(&p, 0);
1047
0
    if (EVP_PKEY_get_id(pk) == EVP_PKEY_RSA)
1048
0
        write_ledword(&p, MS_KEYTYPE_KEYX);
1049
0
#ifndef OPENSSL_NO_DSA
1050
0
    else
1051
0
        write_ledword(&p, MS_KEYTYPE_SIGN);
1052
0
#endif
1053
0
    write_ledword(&p, enclevel ? 1 : 0);
1054
0
    write_ledword(&p, enclevel ? PVK_SALTLEN : 0);
1055
0
    write_ledword(&p, pklen);
1056
0
    if (enclevel) {
1057
0
#ifndef OPENSSL_NO_RC4
1058
0
        if (RAND_bytes_ex(libctx, p, PVK_SALTLEN, 0) <= 0)
1059
0
            goto error;
1060
0
        salt = p;
1061
0
        p += PVK_SALTLEN;
1062
0
#endif
1063
0
    }
1064
0
    do_i2b(&p, pk, 0);
1065
0
    if (enclevel != 0) {
1066
0
#ifndef OPENSSL_NO_RC4
1067
0
        char psbuf[PEM_BUFSIZE];
1068
0
        unsigned char keybuf[20];
1069
0
        int enctmplen, inlen;
1070
0
        if (cb)
1071
0
            inlen = cb(psbuf, PEM_BUFSIZE, 1, u);
1072
0
        else
1073
0
            inlen = PEM_def_callback(psbuf, PEM_BUFSIZE, 1, u);
1074
0
        if (inlen <= 0) {
1075
0
            ERR_raise(ERR_LIB_PEM, PEM_R_BAD_PASSWORD_READ);
1076
0
            goto error;
1077
0
        }
1078
0
        if (!derive_pvk_key(keybuf, sizeof(keybuf), salt, PVK_SALTLEN,
1079
0
                (unsigned char *)psbuf, inlen, libctx, propq))
1080
0
            goto error;
1081
0
        if ((rc4 = EVP_CIPHER_fetch(libctx, "RC4", propq)) == NULL)
1082
0
            goto error;
1083
0
        if (enclevel == 1)
1084
0
            memset(keybuf + 5, 0, 11);
1085
0
        p = salt + PVK_SALTLEN + 8;
1086
0
        if (!EVP_EncryptInit_ex(cctx, rc4, NULL, keybuf, NULL))
1087
0
            goto error;
1088
0
        OPENSSL_cleanse(keybuf, 20);
1089
0
        if (!EVP_EncryptUpdate(cctx, p, &enctmplen, p, pklen - 8))
1090
0
            goto error;
1091
0
        if (!EVP_EncryptFinal_ex(cctx, p + enctmplen, &enctmplen))
1092
0
            goto error;
1093
#else
1094
        ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_CIPHER);
1095
        goto error;
1096
#endif
1097
0
    }
1098
1099
0
    if (*out == NULL)
1100
0
        *out = start;
1101
0
    ret = outlen;
1102
0
error:
1103
0
    EVP_CIPHER_CTX_free(cctx);
1104
0
#ifndef OPENSSL_NO_RC4
1105
0
    EVP_CIPHER_free(rc4);
1106
0
#endif
1107
0
    if (*out == NULL)
1108
0
        OPENSSL_free(start);
1109
1110
0
    return ret;
1111
0
}
1112
1113
int i2b_PVK_bio_ex(BIO *out, const EVP_PKEY *pk, int enclevel,
1114
    pem_password_cb *cb, void *u, OSSL_LIB_CTX *libctx,
1115
    const char *propq)
1116
0
{
1117
0
    unsigned char *tmp = NULL;
1118
0
    int outlen, wrlen;
1119
1120
0
    outlen = i2b_PVK(&tmp, pk, enclevel, cb, u, libctx, propq);
1121
0
    if (outlen < 0)
1122
0
        return -1;
1123
0
    wrlen = BIO_write(out, tmp, outlen);
1124
0
    OPENSSL_free(tmp);
1125
0
    if (wrlen == outlen) {
1126
0
        return outlen;
1127
0
    }
1128
0
    ERR_raise(ERR_LIB_PEM, PEM_R_BIO_WRITE_FAILURE);
1129
0
    return -1;
1130
0
}
1131
1132
int i2b_PVK_bio(BIO *out, const EVP_PKEY *pk, int enclevel,
1133
    pem_password_cb *cb, void *u)
1134
0
{
1135
0
    return i2b_PVK_bio_ex(out, pk, enclevel, cb, u, NULL, NULL);
1136
0
}