xref: /openbsd-src/lib/libcrypto/pkcs12/p12_key.c (revision a49530d730a50ca4dac4bc7514f79894aa6176d0)
1 /* $OpenBSD: p12_key.c,v 1.35 2024/03/02 10:15:16 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_local.h"
67 #include "pkcs12_local.h"
68 
69 /* PKCS12 compatible key/IV generation */
70 #ifndef min
71 #define min(a,b) ((a) < (b) ? (a) : (b))
72 #endif
73 
74 int
PKCS12_key_gen_asc(const char * pass,int passlen,unsigned char * salt,int saltlen,int id,int iter,int n,unsigned char * out,const EVP_MD * md_type)75 PKCS12_key_gen_asc(const char *pass, int passlen, unsigned char *salt,
76     int saltlen, int id, int iter, int n, unsigned char *out,
77     const EVP_MD *md_type)
78 {
79 	int ret;
80 	unsigned char *unipass;
81 	int uniplen;
82 
83 	if (!pass) {
84 		unipass = NULL;
85 		uniplen = 0;
86 	} else if (!OPENSSL_asc2uni(pass, passlen, &unipass, &uniplen)) {
87 		PKCS12error(ERR_R_MALLOC_FAILURE);
88 		return 0;
89 	}
90 	ret = PKCS12_key_gen_uni(unipass, uniplen, salt, saltlen,
91 	    id, iter, n, out, md_type);
92 	if (ret <= 0)
93 		return 0;
94 	freezero(unipass, uniplen);
95 	return ret;
96 }
97 
98 int
PKCS12_key_gen_uni(unsigned char * pass,int passlen,unsigned char * salt,int saltlen,int id,int iter,int n,unsigned char * out,const EVP_MD * md_type)99 PKCS12_key_gen_uni(unsigned char *pass, int passlen, unsigned char *salt,
100     int saltlen, int id, int iter, int n, unsigned char *out,
101     const EVP_MD *md_type)
102 {
103 	EVP_MD_CTX *ctx = NULL;
104 	unsigned char *B = NULL, *D = NULL, *I = NULL, *Ai = NULL;
105 	unsigned char *p;
106 	int Slen, Plen, Ilen;
107 	int i, j, u, v;
108 	int ret = 0;
109 
110 	if ((ctx = EVP_MD_CTX_new()) == NULL)
111 		goto err;
112 
113 	if ((v = EVP_MD_block_size(md_type)) <= 0)
114 		goto err;
115 	if ((u = EVP_MD_size(md_type)) <= 0)
116 		goto err;
117 
118 	if ((D = malloc(v)) == NULL)
119 		goto err;
120 	if ((Ai = malloc(u)) == NULL)
121 		goto err;
122 	if ((B = malloc(v + 1)) == NULL)
123 		goto err;
124 
125 	Slen = v * ((saltlen + v - 1) / v);
126 
127 	Plen = 0;
128 	if (passlen)
129 		Plen = v * ((passlen + v - 1) / v);
130 
131 	Ilen = Slen + Plen;
132 
133 	if ((I = malloc(Ilen)) == NULL)
134 		goto err;
135 
136 	for (i = 0; i < v; i++)
137 		D[i] = id;
138 
139 	p = I;
140 	for (i = 0; i < Slen; i++)
141 		*p++ = salt[i % saltlen];
142 	for (i = 0; i < Plen; i++)
143 		*p++ = pass[i % passlen];
144 
145 	for (;;) {
146 		if (!EVP_DigestInit_ex(ctx, md_type, NULL))
147 			goto err;
148 		if (!EVP_DigestUpdate(ctx, D, v))
149 			goto err;
150 		if (!EVP_DigestUpdate(ctx, I, Ilen))
151 			goto err;
152 		if (!EVP_DigestFinal_ex(ctx, Ai, NULL))
153 			goto err;
154 		for (j = 1; j < iter; j++) {
155 			if (!EVP_DigestInit_ex(ctx, md_type, NULL))
156 				goto err;
157 			if (!EVP_DigestUpdate(ctx, Ai, u))
158 				goto err;
159 			if (!EVP_DigestFinal_ex(ctx, Ai, NULL))
160 				goto err;
161 		}
162 		memcpy(out, Ai, min(n, u));
163 		if (u >= n) {
164 			ret = 1;
165 			goto end;
166 		}
167 		n -= u;
168 		out += u;
169 		for (j = 0; j < v; j++)
170 			B[j] = Ai[j % u];
171 
172 		for (j = 0; j < Ilen; j += v) {
173 			uint16_t c = 1;
174 			int k;
175 
176 			/* Work out I[j] = I[j] + B + 1. */
177 			for (k = v - 1; k >= 0; k--) {
178 				c += I[j + k] + B[k];
179 				I[j + k] = (unsigned char)c;
180 				c >>= 8;
181 			}
182 		}
183 	}
184 
185  err:
186 	PKCS12error(ERR_R_MALLOC_FAILURE);
187 
188  end:
189 	free(Ai);
190 	free(B);
191 	free(D);
192 	free(I);
193 	EVP_MD_CTX_free(ctx);
194 
195 	return ret;
196 }
197