1 1.1 elric /* $NetBSD: pkcs12.c,v 1.2 2017/01/28 21:31:47 christos 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.2 christos #include <krb5/roken.h> 38 1.1 elric #include <assert.h> 39 1.1 elric 40 1.1 elric #include <pkcs12.h> 41 1.1 elric #include <bn.h> 42 1.1 elric 43 1.1 elric int 44 1.1 elric PKCS12_key_gen(const void *key, size_t keylen, 45 1.1 elric const void *salt, size_t saltlen, 46 1.1 elric int id, int iteration, size_t outkeysize, 47 1.1 elric void *out, const EVP_MD *md) 48 1.1 elric { 49 1.1 elric unsigned char *v, *I, hash[EVP_MAX_MD_SIZE]; 50 1.1 elric unsigned int size, size_I = 0; 51 1.1 elric unsigned char idc = id; 52 1.1 elric EVP_MD_CTX *ctx; 53 1.1 elric unsigned char *outp = out; 54 1.1 elric int i, vlen; 55 1.1 elric 56 1.2 christos /** 57 1.2 christos * The argument key is pointing to an utf16 string, and thus 58 1.2 christos * keylen that is no a multiple of 2 is invalid. 59 1.2 christos */ 60 1.2 christos if (keylen & 1) 61 1.2 christos return 0; 62 1.2 christos 63 1.1 elric ctx = EVP_MD_CTX_create(); 64 1.1 elric if (ctx == NULL) 65 1.1 elric return 0; 66 1.1 elric 67 1.1 elric vlen = EVP_MD_block_size(md); 68 1.1 elric v = malloc(vlen + 1); 69 1.1 elric if (v == NULL) { 70 1.1 elric EVP_MD_CTX_destroy(ctx); 71 1.1 elric return 0; 72 1.1 elric } 73 1.1 elric 74 1.1 elric I = calloc(1, vlen * 2); 75 1.1 elric if (I == NULL) { 76 1.1 elric EVP_MD_CTX_destroy(ctx); 77 1.1 elric free(v); 78 1.1 elric return 0; 79 1.1 elric } 80 1.1 elric 81 1.1 elric if (salt && saltlen > 0) { 82 1.1 elric for (i = 0; i < vlen; i++) 83 1.1 elric I[i] = ((unsigned char*)salt)[i % saltlen]; 84 1.1 elric size_I += vlen; 85 1.1 elric } 86 1.1 elric /* 87 1.1 elric * There is a diffrence between the no password string and the 88 1.1 elric * empty string, in the empty string the UTF16 NUL terminator is 89 1.1 elric * included into the string. 90 1.1 elric */ 91 1.2 christos if (key) { 92 1.1 elric for (i = 0; i < vlen / 2; i++) { 93 1.1 elric I[(i * 2) + size_I] = 0; 94 1.1 elric I[(i * 2) + size_I + 1] = ((unsigned char*)key)[i % (keylen + 1)]; 95 1.1 elric } 96 1.1 elric size_I += vlen; 97 1.1 elric } 98 1.1 elric 99 1.1 elric while (1) { 100 1.1 elric BIGNUM *bnB, *bnOne; 101 1.1 elric 102 1.1 elric if (!EVP_DigestInit_ex(ctx, md, NULL)) { 103 1.1 elric EVP_MD_CTX_destroy(ctx); 104 1.1 elric free(I); 105 1.1 elric free(v); 106 1.1 elric return 0; 107 1.1 elric } 108 1.1 elric for (i = 0; i < vlen; i++) 109 1.1 elric EVP_DigestUpdate(ctx, &idc, 1); 110 1.1 elric EVP_DigestUpdate(ctx, I, size_I); 111 1.1 elric EVP_DigestFinal_ex(ctx, hash, &size); 112 1.1 elric 113 1.1 elric for (i = 1; i < iteration; i++) 114 1.1 elric EVP_Digest(hash, size, hash, &size, md, NULL); 115 1.1 elric 116 1.1 elric memcpy(outp, hash, min(outkeysize, size)); 117 1.1 elric if (outkeysize < size) 118 1.1 elric break; 119 1.1 elric outkeysize -= size; 120 1.1 elric outp += size; 121 1.1 elric 122 1.1 elric for (i = 0; i < vlen; i++) 123 1.1 elric v[i] = hash[i % size]; 124 1.1 elric 125 1.1 elric bnB = BN_bin2bn(v, vlen, NULL); 126 1.1 elric bnOne = BN_new(); 127 1.1 elric BN_set_word(bnOne, 1); 128 1.1 elric 129 1.1 elric BN_uadd(bnB, bnB, bnOne); 130 1.1 elric 131 1.1 elric for (i = 0; i < vlen * 2; i += vlen) { 132 1.1 elric BIGNUM *bnI; 133 1.1 elric int j; 134 1.1 elric 135 1.1 elric bnI = BN_bin2bn(I + i, vlen, NULL); 136 1.1 elric 137 1.1 elric BN_uadd(bnI, bnI, bnB); 138 1.1 elric 139 1.1 elric j = BN_num_bytes(bnI); 140 1.1 elric if (j > vlen) { 141 1.1 elric assert(j == vlen + 1); 142 1.1 elric BN_bn2bin(bnI, v); 143 1.1 elric memcpy(I + i, v + 1, vlen); 144 1.1 elric } else { 145 1.1 elric memset(I + i, 0, vlen - j); 146 1.1 elric BN_bn2bin(bnI, I + i + vlen - j); 147 1.1 elric } 148 1.1 elric BN_free(bnI); 149 1.2 christos } 150 1.1 elric BN_free(bnB); 151 1.1 elric BN_free(bnOne); 152 1.1 elric size_I = vlen * 2; 153 1.1 elric } 154 1.1 elric 155 1.1 elric EVP_MD_CTX_destroy(ctx); 156 1.1 elric free(I); 157 1.1 elric free(v); 158 1.1 elric 159 1.1 elric return 1; 160 1.1 elric } 161