pkcs12.c revision 1.1.1.1.10.1 1 1.1 elric /* $NetBSD: pkcs12.c,v 1.1.1.1.10.1 2014/08/19 23:45:18 tls Exp $ */
2 1.1 elric
3 1.1 elric /*
4 1.1 elric * Copyright (c) 2006 Kungliga Tekniska Hgskolan
5 1.1 elric * (Royal Institute of Technology, Stockholm, Sweden).
6 1.1 elric * All rights reserved.
7 1.1 elric *
8 1.1 elric * Redistribution and use in source and binary forms, with or without
9 1.1 elric * modification, are permitted provided that the following conditions
10 1.1 elric * are met:
11 1.1 elric *
12 1.1 elric * 1. Redistributions of source code must retain the above copyright
13 1.1 elric * notice, this list of conditions and the following disclaimer.
14 1.1 elric *
15 1.1 elric * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 elric * notice, this list of conditions and the following disclaimer in the
17 1.1 elric * documentation and/or other materials provided with the distribution.
18 1.1 elric *
19 1.1 elric * 3. Neither the name of the Institute nor the names of its contributors
20 1.1 elric * may be used to endorse or promote products derived from this software
21 1.1 elric * without specific prior written permission.
22 1.1 elric *
23 1.1 elric * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 1.1 elric * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 elric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 elric * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 1.1 elric * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 elric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 elric * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 elric * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 elric * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 elric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 elric * SUCH DAMAGE.
34 1.1 elric */
35 1.1 elric
36 1.1 elric #include <config.h>
37 1.1 elric
38 1.1 elric #include <stdio.h>
39 1.1 elric #include <stdlib.h>
40 1.1 elric #include <assert.h>
41 1.1 elric
42 1.1 elric #include <pkcs12.h>
43 1.1 elric #include <bn.h>
44 1.1 elric
45 1.1 elric #include <krb5/roken.h>
46 1.1 elric
47 1.1 elric int
48 1.1 elric PKCS12_key_gen(const void *key, size_t keylen,
49 1.1 elric const void *salt, size_t saltlen,
50 1.1 elric int id, int iteration, size_t outkeysize,
51 1.1 elric void *out, const EVP_MD *md)
52 1.1 elric {
53 1.1 elric unsigned char *v, *I, hash[EVP_MAX_MD_SIZE];
54 1.1 elric unsigned int size, size_I = 0;
55 1.1 elric unsigned char idc = id;
56 1.1 elric EVP_MD_CTX *ctx;
57 1.1 elric unsigned char *outp = out;
58 1.1 elric int i, vlen;
59 1.1 elric
60 1.1.1.1.10.1 tls /**
61 1.1.1.1.10.1 tls * The argument key is pointing to an utf16 string, and thus
62 1.1.1.1.10.1 tls * keylen that is no a multiple of 2 is invalid.
63 1.1.1.1.10.1 tls */
64 1.1.1.1.10.1 tls if (keylen & 1)
65 1.1.1.1.10.1 tls return 0;
66 1.1.1.1.10.1 tls
67 1.1 elric ctx = EVP_MD_CTX_create();
68 1.1 elric if (ctx == NULL)
69 1.1 elric return 0;
70 1.1 elric
71 1.1 elric vlen = EVP_MD_block_size(md);
72 1.1 elric v = malloc(vlen + 1);
73 1.1 elric if (v == NULL) {
74 1.1 elric EVP_MD_CTX_destroy(ctx);
75 1.1 elric return 0;
76 1.1 elric }
77 1.1 elric
78 1.1 elric I = calloc(1, vlen * 2);
79 1.1 elric if (I == NULL) {
80 1.1 elric EVP_MD_CTX_destroy(ctx);
81 1.1 elric free(v);
82 1.1 elric return 0;
83 1.1 elric }
84 1.1 elric
85 1.1 elric if (salt && saltlen > 0) {
86 1.1 elric for (i = 0; i < vlen; i++)
87 1.1 elric I[i] = ((unsigned char*)salt)[i % saltlen];
88 1.1 elric size_I += vlen;
89 1.1 elric }
90 1.1 elric /*
91 1.1 elric * There is a diffrence between the no password string and the
92 1.1 elric * empty string, in the empty string the UTF16 NUL terminator is
93 1.1 elric * included into the string.
94 1.1 elric */
95 1.1.1.1.10.1 tls if (key) {
96 1.1 elric for (i = 0; i < vlen / 2; i++) {
97 1.1 elric I[(i * 2) + size_I] = 0;
98 1.1 elric I[(i * 2) + size_I + 1] = ((unsigned char*)key)[i % (keylen + 1)];
99 1.1 elric }
100 1.1 elric size_I += vlen;
101 1.1 elric }
102 1.1 elric
103 1.1 elric while (1) {
104 1.1 elric BIGNUM *bnB, *bnOne;
105 1.1 elric
106 1.1 elric if (!EVP_DigestInit_ex(ctx, md, NULL)) {
107 1.1 elric EVP_MD_CTX_destroy(ctx);
108 1.1 elric free(I);
109 1.1 elric free(v);
110 1.1 elric return 0;
111 1.1 elric }
112 1.1 elric for (i = 0; i < vlen; i++)
113 1.1 elric EVP_DigestUpdate(ctx, &idc, 1);
114 1.1 elric EVP_DigestUpdate(ctx, I, size_I);
115 1.1 elric EVP_DigestFinal_ex(ctx, hash, &size);
116 1.1 elric
117 1.1 elric for (i = 1; i < iteration; i++)
118 1.1 elric EVP_Digest(hash, size, hash, &size, md, NULL);
119 1.1 elric
120 1.1 elric memcpy(outp, hash, min(outkeysize, size));
121 1.1 elric if (outkeysize < size)
122 1.1 elric break;
123 1.1 elric outkeysize -= size;
124 1.1 elric outp += size;
125 1.1 elric
126 1.1 elric for (i = 0; i < vlen; i++)
127 1.1 elric v[i] = hash[i % size];
128 1.1 elric
129 1.1 elric bnB = BN_bin2bn(v, vlen, NULL);
130 1.1 elric bnOne = BN_new();
131 1.1 elric BN_set_word(bnOne, 1);
132 1.1 elric
133 1.1 elric BN_uadd(bnB, bnB, bnOne);
134 1.1 elric
135 1.1 elric for (i = 0; i < vlen * 2; i += vlen) {
136 1.1 elric BIGNUM *bnI;
137 1.1 elric int j;
138 1.1 elric
139 1.1 elric bnI = BN_bin2bn(I + i, vlen, NULL);
140 1.1 elric
141 1.1 elric BN_uadd(bnI, bnI, bnB);
142 1.1 elric
143 1.1 elric j = BN_num_bytes(bnI);
144 1.1 elric if (j > vlen) {
145 1.1 elric assert(j == vlen + 1);
146 1.1 elric BN_bn2bin(bnI, v);
147 1.1 elric memcpy(I + i, v + 1, vlen);
148 1.1 elric } else {
149 1.1 elric memset(I + i, 0, vlen - j);
150 1.1 elric BN_bn2bin(bnI, I + i + vlen - j);
151 1.1 elric }
152 1.1 elric BN_free(bnI);
153 1.1.1.1.10.1 tls }
154 1.1 elric BN_free(bnB);
155 1.1 elric BN_free(bnOne);
156 1.1 elric size_I = vlen * 2;
157 1.1 elric }
158 1.1 elric
159 1.1 elric EVP_MD_CTX_destroy(ctx);
160 1.1 elric free(I);
161 1.1 elric free(v);
162 1.1 elric
163 1.1 elric return 1;
164 1.1 elric }
165