Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/pkcs12/p12_decr.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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/pkcs12.h>
13
14
/* Define this to dump decrypted output to files called DERnnn */
15
/*
16
 * #define OPENSSL_DEBUG_DECRYPT
17
 */
18
19
/*
20
 * Encrypt/Decrypt a buffer based on password and algor, result in a
21
 * OPENSSL_malloc'ed buffer
22
 */
23
unsigned char *PKCS12_pbe_crypt(const X509_ALGOR *algor,
24
                                const char *pass, int passlen,
25
                                const unsigned char *in, int inlen,
26
                                unsigned char **data, int *datalen, int en_de)
27
0
{
28
0
    unsigned char *out = NULL;
29
0
    int outlen, i;
30
0
    EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
31
0
32
0
    if (ctx == NULL) {
33
0
        PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT, ERR_R_MALLOC_FAILURE);
34
0
        goto err;
35
0
    }
36
0
37
0
    /* Decrypt data */
38
0
    if (!EVP_PBE_CipherInit(algor->algorithm, pass, passlen,
39
0
                            algor->parameter, ctx, en_de)) {
40
0
        PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT,
41
0
                  PKCS12_R_PKCS12_ALGOR_CIPHERINIT_ERROR);
42
0
        goto err;
43
0
    }
44
0
45
0
    if ((out = OPENSSL_malloc(inlen + EVP_CIPHER_CTX_block_size(ctx)))
46
0
            == NULL) {
47
0
        PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT, ERR_R_MALLOC_FAILURE);
48
0
        goto err;
49
0
    }
50
0
51
0
    if (!EVP_CipherUpdate(ctx, out, &i, in, inlen)) {
52
0
        OPENSSL_free(out);
53
0
        out = NULL;
54
0
        PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT, ERR_R_EVP_LIB);
55
0
        goto err;
56
0
    }
57
0
58
0
    outlen = i;
59
0
    if (!EVP_CipherFinal_ex(ctx, out + i, &i)) {
60
0
        OPENSSL_free(out);
61
0
        out = NULL;
62
0
        PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT,
63
0
                  PKCS12_R_PKCS12_CIPHERFINAL_ERROR);
64
0
        goto err;
65
0
    }
66
0
    outlen += i;
67
0
    if (datalen)
68
0
        *datalen = outlen;
69
0
    if (data)
70
0
        *data = out;
71
0
 err:
72
0
    EVP_CIPHER_CTX_free(ctx);
73
0
    return out;
74
0
75
0
}
76
77
/*
78
 * Decrypt an OCTET STRING and decode ASN1 structure if zbuf set zero buffer
79
 * after use.
80
 */
81
82
void *PKCS12_item_decrypt_d2i(const X509_ALGOR *algor, const ASN1_ITEM *it,
83
                              const char *pass, int passlen,
84
                              const ASN1_OCTET_STRING *oct, int zbuf)
85
0
{
86
0
    unsigned char *out;
87
0
    const unsigned char *p;
88
0
    void *ret;
89
0
    int outlen;
90
0
91
0
    if (!PKCS12_pbe_crypt(algor, pass, passlen, oct->data, oct->length,
92
0
                          &out, &outlen, 0)) {
93
0
        PKCS12err(PKCS12_F_PKCS12_ITEM_DECRYPT_D2I,
94
0
                  PKCS12_R_PKCS12_PBE_CRYPT_ERROR);
95
0
        return NULL;
96
0
    }
97
0
    p = out;
98
#ifdef OPENSSL_DEBUG_DECRYPT
99
    {
100
        FILE *op;
101
102
        char fname[30];
103
        static int fnm = 1;
104
        sprintf(fname, "DER%d", fnm++);
105
        op = fopen(fname, "wb");
106
        fwrite(p, 1, outlen, op);
107
        fclose(op);
108
    }
109
#endif
110
    ret = ASN1_item_d2i(NULL, &p, outlen, it);
111
0
    if (zbuf)
112
0
        OPENSSL_cleanse(out, outlen);
113
0
    if (!ret)
114
0
        PKCS12err(PKCS12_F_PKCS12_ITEM_DECRYPT_D2I, PKCS12_R_DECODE_ERROR);
115
0
    OPENSSL_free(out);
116
0
    return ret;
117
0
}
118
119
/*
120
 * Encode ASN1 structure and encrypt, return OCTET STRING if zbuf set zero
121
 * encoding.
122
 */
123
124
ASN1_OCTET_STRING *PKCS12_item_i2d_encrypt(X509_ALGOR *algor,
125
                                           const ASN1_ITEM *it,
126
                                           const char *pass, int passlen,
127
                                           void *obj, int zbuf)
128
0
{
129
0
    ASN1_OCTET_STRING *oct = NULL;
130
0
    unsigned char *in = NULL;
131
0
    int inlen;
132
0
133
0
    if ((oct = ASN1_OCTET_STRING_new()) == NULL) {
134
0
        PKCS12err(PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT, ERR_R_MALLOC_FAILURE);
135
0
        goto err;
136
0
    }
137
0
    inlen = ASN1_item_i2d(obj, &in, it);
138
0
    if (!in) {
139
0
        PKCS12err(PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT, PKCS12_R_ENCODE_ERROR);
140
0
        goto err;
141
0
    }
142
0
    if (!PKCS12_pbe_crypt(algor, pass, passlen, in, inlen, &oct->data,
143
0
                          &oct->length, 1)) {
144
0
        PKCS12err(PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT, PKCS12_R_ENCRYPT_ERROR);
145
0
        OPENSSL_free(in);
146
0
        goto err;
147
0
    }
148
0
    if (zbuf)
149
0
        OPENSSL_cleanse(in, inlen);
150
0
    OPENSSL_free(in);
151
0
    return oct;
152
0
 err:
153
0
    ASN1_OCTET_STRING_free(oct);
154
0
    return NULL;
155
0
}