pkcs12.c revision 1.1.1.1 1 1.1 elric /* $NetBSD: pkcs12.c,v 1.1.1.1 2011/04/13 18:14:50 elric 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 elric ctx = EVP_MD_CTX_create();
61 1.1 elric if (ctx == NULL)
62 1.1 elric return 0;
63 1.1 elric
64 1.1 elric vlen = EVP_MD_block_size(md);
65 1.1 elric v = malloc(vlen + 1);
66 1.1 elric if (v == NULL) {
67 1.1 elric EVP_MD_CTX_destroy(ctx);
68 1.1 elric return 0;
69 1.1 elric }
70 1.1 elric
71 1.1 elric I = calloc(1, vlen * 2);
72 1.1 elric if (I == NULL) {
73 1.1 elric EVP_MD_CTX_destroy(ctx);
74 1.1 elric free(v);
75 1.1 elric return 0;
76 1.1 elric }
77 1.1 elric
78 1.1 elric if (salt && saltlen > 0) {
79 1.1 elric for (i = 0; i < vlen; i++)
80 1.1 elric I[i] = ((unsigned char*)salt)[i % saltlen];
81 1.1 elric size_I += vlen;
82 1.1 elric }
83 1.1 elric /*
84 1.1 elric * There is a diffrence between the no password string and the
85 1.1 elric * empty string, in the empty string the UTF16 NUL terminator is
86 1.1 elric * included into the string.
87 1.1 elric */
88 1.1 elric if (key && keylen >= 0) {
89 1.1 elric for (i = 0; i < vlen / 2; i++) {
90 1.1 elric I[(i * 2) + size_I] = 0;
91 1.1 elric I[(i * 2) + size_I + 1] = ((unsigned char*)key)[i % (keylen + 1)];
92 1.1 elric }
93 1.1 elric size_I += vlen;
94 1.1 elric }
95 1.1 elric
96 1.1 elric while (1) {
97 1.1 elric BIGNUM *bnB, *bnOne;
98 1.1 elric
99 1.1 elric if (!EVP_DigestInit_ex(ctx, md, NULL)) {
100 1.1 elric EVP_MD_CTX_destroy(ctx);
101 1.1 elric free(I);
102 1.1 elric free(v);
103 1.1 elric return 0;
104 1.1 elric }
105 1.1 elric for (i = 0; i < vlen; i++)
106 1.1 elric EVP_DigestUpdate(ctx, &idc, 1);
107 1.1 elric EVP_DigestUpdate(ctx, I, size_I);
108 1.1 elric EVP_DigestFinal_ex(ctx, hash, &size);
109 1.1 elric
110 1.1 elric for (i = 1; i < iteration; i++)
111 1.1 elric EVP_Digest(hash, size, hash, &size, md, NULL);
112 1.1 elric
113 1.1 elric memcpy(outp, hash, min(outkeysize, size));
114 1.1 elric if (outkeysize < size)
115 1.1 elric break;
116 1.1 elric outkeysize -= size;
117 1.1 elric outp += size;
118 1.1 elric
119 1.1 elric for (i = 0; i < vlen; i++)
120 1.1 elric v[i] = hash[i % size];
121 1.1 elric
122 1.1 elric bnB = BN_bin2bn(v, vlen, NULL);
123 1.1 elric bnOne = BN_new();
124 1.1 elric BN_set_word(bnOne, 1);
125 1.1 elric
126 1.1 elric BN_uadd(bnB, bnB, bnOne);
127 1.1 elric
128 1.1 elric for (i = 0; i < vlen * 2; i += vlen) {
129 1.1 elric BIGNUM *bnI;
130 1.1 elric int j;
131 1.1 elric
132 1.1 elric bnI = BN_bin2bn(I + i, vlen, NULL);
133 1.1 elric
134 1.1 elric BN_uadd(bnI, bnI, bnB);
135 1.1 elric
136 1.1 elric j = BN_num_bytes(bnI);
137 1.1 elric if (j > vlen) {
138 1.1 elric assert(j == vlen + 1);
139 1.1 elric BN_bn2bin(bnI, v);
140 1.1 elric memcpy(I + i, v + 1, vlen);
141 1.1 elric } else {
142 1.1 elric memset(I + i, 0, vlen - j);
143 1.1 elric BN_bn2bin(bnI, I + i + vlen - j);
144 1.1 elric }
145 1.1 elric BN_free(bnI);
146 1.1 elric }
147 1.1 elric BN_free(bnB);
148 1.1 elric BN_free(bnOne);
149 1.1 elric size_I = vlen * 2;
150 1.1 elric }
151 1.1 elric
152 1.1 elric EVP_MD_CTX_destroy(ctx);
153 1.1 elric free(I);
154 1.1 elric free(v);
155 1.1 elric
156 1.1 elric return 1;
157 1.1 elric }
158