Coverage Report

Created: 2022-08-24 06:30

/src/libressl/crypto/pkcs12/p12_key.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD: p12_key.c,v 1.31 2022/07/30 11:27:06 tb Exp $ */
2
/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3
 * project 1999.
4
 */
5
/* ====================================================================
6
 * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 *
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 *
15
 * 2. Redistributions in binary form must reproduce the above copyright
16
 *    notice, this list of conditions and the following disclaimer in
17
 *    the documentation and/or other materials provided with the
18
 *    distribution.
19
 *
20
 * 3. All advertising materials mentioning features or use of this
21
 *    software must display the following acknowledgment:
22
 *    "This product includes software developed by the OpenSSL Project
23
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24
 *
25
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26
 *    endorse or promote products derived from this software without
27
 *    prior written permission. For written permission, please contact
28
 *    licensing@OpenSSL.org.
29
 *
30
 * 5. Products derived from this software may not be called "OpenSSL"
31
 *    nor may "OpenSSL" appear in their names without prior written
32
 *    permission of the OpenSSL Project.
33
 *
34
 * 6. Redistributions of any form whatsoever must retain the following
35
 *    acknowledgment:
36
 *    "This product includes software developed by the OpenSSL Project
37
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38
 *
39
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50
 * OF THE POSSIBILITY OF SUCH DAMAGE.
51
 * ====================================================================
52
 *
53
 * This product includes cryptographic software written by Eric Young
54
 * (eay@cryptsoft.com).  This product includes software written by Tim
55
 * Hudson (tjh@cryptsoft.com).
56
 *
57
 */
58
59
#include <stdio.h>
60
#include <string.h>
61
62
#include <openssl/bn.h>
63
#include <openssl/err.h>
64
#include <openssl/pkcs12.h>
65
66
#include "evp_locl.h"
67
68
/* PKCS12 compatible key/IV generation */
69
#ifndef min
70
0
#define min(a,b) ((a) < (b) ? (a) : (b))
71
#endif
72
73
int
74
PKCS12_key_gen_asc(const char *pass, int passlen, unsigned char *salt,
75
    int saltlen, int id, int iter, int n, unsigned char *out,
76
    const EVP_MD *md_type)
77
0
{
78
0
  int ret;
79
0
  unsigned char *unipass;
80
0
  int uniplen;
81
82
0
  if (!pass) {
83
0
    unipass = NULL;
84
0
    uniplen = 0;
85
0
  } else if (!OPENSSL_asc2uni(pass, passlen, &unipass, &uniplen)) {
86
0
    PKCS12error(ERR_R_MALLOC_FAILURE);
87
0
    return 0;
88
0
  }
89
0
  ret = PKCS12_key_gen_uni(unipass, uniplen, salt, saltlen,
90
0
      id, iter, n, out, md_type);
91
0
  if (ret <= 0)
92
0
    return 0;
93
0
  freezero(unipass, uniplen);
94
0
  return ret;
95
0
}
96
97
int
98
PKCS12_key_gen_uni(unsigned char *pass, int passlen, unsigned char *salt,
99
    int saltlen, int id, int iter, int n, unsigned char *out,
100
    const EVP_MD *md_type)
101
0
{
102
0
  EVP_MD_CTX *ctx = NULL;
103
0
  unsigned char *B = NULL, *D = NULL, *I = NULL, *Ai = NULL;
104
0
  unsigned char *p;
105
0
  int Slen, Plen, Ilen;
106
0
  int i, j, u, v;
107
0
  int ret = 0;
108
109
0
  if ((ctx = EVP_MD_CTX_new()) == NULL)
110
0
    goto err;
111
112
0
  if ((v = EVP_MD_block_size(md_type)) <= 0)
113
0
    goto err;
114
0
  if ((u = EVP_MD_size(md_type)) <= 0)
115
0
    goto err;
116
117
0
  if ((D = malloc(v)) == NULL)
118
0
    goto err;
119
0
  if ((Ai = malloc(u)) == NULL)
120
0
    goto err;
121
0
  if ((B = malloc(v + 1)) == NULL)
122
0
    goto err;
123
124
0
  Slen = v * ((saltlen + v - 1) / v);
125
126
0
  Plen = 0;
127
0
  if (passlen)
128
0
    Plen = v * ((passlen + v - 1) / v);
129
130
0
  Ilen = Slen + Plen;
131
132
0
  if ((I = malloc(Ilen)) == NULL)
133
0
    goto err;
134
135
0
  for (i = 0; i < v; i++)
136
0
    D[i] = id;
137
138
0
  p = I;
139
0
  for (i = 0; i < Slen; i++)
140
0
    *p++ = salt[i % saltlen];
141
0
  for (i = 0; i < Plen; i++)
142
0
    *p++ = pass[i % passlen];
143
144
0
  for (;;) {
145
0
    if (!EVP_DigestInit_ex(ctx, md_type, NULL))
146
0
      goto err;
147
0
    if (!EVP_DigestUpdate(ctx, D, v))
148
0
      goto err;
149
0
    if (!EVP_DigestUpdate(ctx, I, Ilen))
150
0
      goto err;
151
0
    if (!EVP_DigestFinal_ex(ctx, Ai, NULL))
152
0
      goto err;
153
0
    for (j = 1; j < iter; j++) {
154
0
      if (!EVP_DigestInit_ex(ctx, md_type, NULL))
155
0
        goto err;
156
0
      if (!EVP_DigestUpdate(ctx, Ai, u))
157
0
        goto err;
158
0
      if (!EVP_DigestFinal_ex(ctx, Ai, NULL))
159
0
        goto err;
160
0
    }
161
0
    memcpy(out, Ai, min(n, u));
162
0
    if (u >= n) {
163
0
      ret = 1;
164
0
      goto end;
165
0
    }
166
0
    n -= u;
167
0
    out += u;
168
0
    for (j = 0; j < v; j++)
169
0
      B[j] = Ai[j % u];
170
171
0
    for (j = 0; j < Ilen; j += v) {
172
0
      uint16_t c = 1;
173
0
      int k;
174
175
      /* Work out I[j] = I[j] + B + 1. */
176
0
      for (k = v - 1; k >= 0; k--) {
177
0
        c += I[j + k] + B[k];
178
0
        I[j + k] = (unsigned char)c;
179
0
        c >>= 8;
180
0
      }
181
0
    }
182
0
  }
183
184
0
 err:
185
0
  PKCS12error(ERR_R_MALLOC_FAILURE);
186
187
0
 end:
188
0
  free(Ai);
189
0
  free(B);
190
0
  free(D);
191
0
  free(I);
192
0
  EVP_MD_CTX_free(ctx);
193
194
0
  return ret;
195
0
}