Coverage Report

Created: 2025-12-10 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/pkcs12/p12_p8e.c
Line
Count
Source
1
/*
2
 * Copyright 2001-2021 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
#include <stdio.h>
11
#include "internal/cryptlib.h"
12
#include <openssl/core.h>
13
#include <openssl/pkcs12.h>
14
#include "crypto/x509.h"
15
16
X509_SIG *PKCS8_encrypt_ex(int pbe_nid, const EVP_CIPHER *cipher,
17
    const char *pass, int passlen,
18
    unsigned char *salt, int saltlen, int iter,
19
    PKCS8_PRIV_KEY_INFO *p8inf,
20
    OSSL_LIB_CTX *libctx, const char *propq)
21
0
{
22
0
    X509_SIG *p8 = NULL;
23
0
    X509_ALGOR *pbe;
24
25
0
    if (pbe_nid == -1) {
26
0
        if (cipher == NULL) {
27
0
            ERR_raise(ERR_LIB_PKCS12, ERR_R_PASSED_NULL_PARAMETER);
28
0
            return NULL;
29
0
        }
30
0
        pbe = PKCS5_pbe2_set_iv_ex(cipher, iter, salt, saltlen, NULL, -1,
31
0
            libctx);
32
0
    } else {
33
0
        ERR_set_mark();
34
0
        if (EVP_PBE_find(EVP_PBE_TYPE_PRF, pbe_nid, NULL, NULL, 0)) {
35
0
            ERR_clear_last_mark();
36
0
            if (cipher == NULL) {
37
0
                ERR_raise(ERR_LIB_PKCS12, ERR_R_PASSED_NULL_PARAMETER);
38
0
                return NULL;
39
0
            }
40
0
            pbe = PKCS5_pbe2_set_iv_ex(cipher, iter, salt, saltlen, NULL,
41
0
                pbe_nid, libctx);
42
0
        } else {
43
0
            ERR_pop_to_mark();
44
0
            pbe = PKCS5_pbe_set_ex(pbe_nid, iter, salt, saltlen, libctx);
45
0
        }
46
0
    }
47
0
    if (pbe == NULL) {
48
0
        ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
49
0
        return NULL;
50
0
    }
51
0
    p8 = PKCS8_set0_pbe_ex(pass, passlen, p8inf, pbe, libctx, propq);
52
0
    if (p8 == NULL) {
53
0
        X509_ALGOR_free(pbe);
54
0
        return NULL;
55
0
    }
56
57
0
    return p8;
58
0
}
59
60
X509_SIG *PKCS8_encrypt(int pbe_nid, const EVP_CIPHER *cipher,
61
    const char *pass, int passlen,
62
    unsigned char *salt, int saltlen, int iter,
63
    PKCS8_PRIV_KEY_INFO *p8inf)
64
0
{
65
0
    return PKCS8_encrypt_ex(pbe_nid, cipher, pass, passlen, salt, saltlen, iter,
66
0
        p8inf, NULL, NULL);
67
0
}
68
69
X509_SIG *PKCS8_set0_pbe_ex(const char *pass, int passlen,
70
    PKCS8_PRIV_KEY_INFO *p8inf, X509_ALGOR *pbe,
71
    OSSL_LIB_CTX *ctx, const char *propq)
72
0
{
73
0
    X509_SIG *p8;
74
0
    ASN1_OCTET_STRING *enckey;
75
76
0
    enckey = PKCS12_item_i2d_encrypt_ex(pbe, ASN1_ITEM_rptr(PKCS8_PRIV_KEY_INFO),
77
0
        pass, passlen, p8inf, 1, ctx, propq);
78
0
    if (!enckey) {
79
0
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCRYPT_ERROR);
80
0
        return NULL;
81
0
    }
82
83
0
    p8 = OPENSSL_zalloc(sizeof(*p8));
84
85
0
    if (p8 == NULL) {
86
0
        ASN1_OCTET_STRING_free(enckey);
87
0
        return NULL;
88
0
    }
89
0
    p8->algor = pbe;
90
0
    p8->digest = enckey;
91
92
0
    return p8;
93
0
}
94
95
X509_SIG *PKCS8_set0_pbe(const char *pass, int passlen,
96
    PKCS8_PRIV_KEY_INFO *p8inf, X509_ALGOR *pbe)
97
0
{
98
0
    return PKCS8_set0_pbe_ex(pass, passlen, p8inf, pbe, NULL, NULL);
99
0
}