os/ossrv/ssl/libcrypto/src/crypto/pkcs12/p12_key.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/pkcs12/p12_key.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,206 @@
     1.4 +/* p12_key.c */
     1.5 +/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
     1.6 + * project 1999.
     1.7 + */
     1.8 +/* ====================================================================
     1.9 + * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
    1.10 + *
    1.11 + * Redistribution and use in source and binary forms, with or without
    1.12 + * modification, are permitted provided that the following conditions
    1.13 + * are met:
    1.14 + *
    1.15 + * 1. Redistributions of source code must retain the above copyright
    1.16 + *    notice, this list of conditions and the following disclaimer. 
    1.17 + *
    1.18 + * 2. Redistributions in binary form must reproduce the above copyright
    1.19 + *    notice, this list of conditions and the following disclaimer in
    1.20 + *    the documentation and/or other materials provided with the
    1.21 + *    distribution.
    1.22 + *
    1.23 + * 3. All advertising materials mentioning features or use of this
    1.24 + *    software must display the following acknowledgment:
    1.25 + *    "This product includes software developed by the OpenSSL Project
    1.26 + *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
    1.27 + *
    1.28 + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
    1.29 + *    endorse or promote products derived from this software without
    1.30 + *    prior written permission. For written permission, please contact
    1.31 + *    licensing@OpenSSL.org.
    1.32 + *
    1.33 + * 5. Products derived from this software may not be called "OpenSSL"
    1.34 + *    nor may "OpenSSL" appear in their names without prior written
    1.35 + *    permission of the OpenSSL Project.
    1.36 + *
    1.37 + * 6. Redistributions of any form whatsoever must retain the following
    1.38 + *    acknowledgment:
    1.39 + *    "This product includes software developed by the OpenSSL Project
    1.40 + *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
    1.41 + *
    1.42 + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
    1.43 + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.44 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    1.45 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
    1.46 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.47 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    1.48 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    1.49 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.50 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    1.51 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.52 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    1.53 + * OF THE POSSIBILITY OF SUCH DAMAGE.
    1.54 + * ====================================================================
    1.55 + *
    1.56 + * This product includes cryptographic software written by Eric Young
    1.57 + * (eay@cryptsoft.com).  This product includes software written by Tim
    1.58 + * Hudson (tjh@cryptsoft.com).
    1.59 + *
    1.60 + */
    1.61 +
    1.62 +#include <stdio.h>
    1.63 +#include "cryptlib.h"
    1.64 +#include <openssl/pkcs12.h>
    1.65 +#include <openssl/bn.h>
    1.66 +
    1.67 +/* Uncomment out this line to get debugging info about key generation */
    1.68 +/*#define DEBUG_KEYGEN*/
    1.69 +#ifdef DEBUG_KEYGEN
    1.70 +#include <openssl/bio.h>
    1.71 +extern BIO *bio_err;
    1.72 +void h__dump (unsigned char *p, int len);
    1.73 +#endif
    1.74 +
    1.75 +/* PKCS12 compatible key/IV generation */
    1.76 +#ifndef min
    1.77 +#define min(a,b) ((a) < (b) ? (a) : (b))
    1.78 +#endif
    1.79 +
    1.80 +EXPORT_C int PKCS12_key_gen_asc(const char *pass, int passlen, unsigned char *salt,
    1.81 +	     int saltlen, int id, int iter, int n, unsigned char *out,
    1.82 +	     const EVP_MD *md_type)
    1.83 +{
    1.84 +	int ret;
    1.85 +	unsigned char *unipass;
    1.86 +	int uniplen;
    1.87 +	if(!pass) {
    1.88 +		unipass = NULL;
    1.89 +		uniplen = 0;
    1.90 +	} else if (!asc2uni(pass, passlen, &unipass, &uniplen)) {
    1.91 +		PKCS12err(PKCS12_F_PKCS12_KEY_GEN_ASC,ERR_R_MALLOC_FAILURE);
    1.92 +		return 0;
    1.93 +	}
    1.94 +	ret = PKCS12_key_gen_uni(unipass, uniplen, salt, saltlen,
    1.95 +						 id, iter, n, out, md_type);
    1.96 +	if(unipass) {
    1.97 +		OPENSSL_cleanse(unipass, uniplen);	/* Clear password from memory */
    1.98 +		OPENSSL_free(unipass);
    1.99 +	}
   1.100 +	return ret;
   1.101 +}
   1.102 +
   1.103 +EXPORT_C int PKCS12_key_gen_uni(unsigned char *pass, int passlen, unsigned char *salt,
   1.104 +	     int saltlen, int id, int iter, int n, unsigned char *out,
   1.105 +	     const EVP_MD *md_type)
   1.106 +{
   1.107 +	unsigned char *B, *D, *I, *p, *Ai;
   1.108 +	int Slen, Plen, Ilen, Ijlen;
   1.109 +	int i, j, u, v;
   1.110 +	BIGNUM *Ij, *Bpl1;	/* These hold Ij and B + 1 */
   1.111 +	EVP_MD_CTX ctx;
   1.112 +#ifdef  DEBUG_KEYGEN
   1.113 +	unsigned char *tmpout = out;
   1.114 +	int tmpn = n;
   1.115 +#endif
   1.116 +
   1.117 +#if 0
   1.118 +	if (!pass) {
   1.119 +		PKCS12err(PKCS12_F_PKCS12_KEY_GEN_UNI,ERR_R_PASSED_NULL_PARAMETER);
   1.120 +		return 0;
   1.121 +	}
   1.122 +#endif
   1.123 +
   1.124 +	EVP_MD_CTX_init(&ctx);
   1.125 +#ifdef  DEBUG_KEYGEN
   1.126 +	fprintf(stderr, "KEYGEN DEBUG\n");
   1.127 +	fprintf(stderr, "ID %d, ITER %d\n", id, iter);
   1.128 +	fprintf(stderr, "Password (length %d):\n", passlen);
   1.129 +	h__dump(pass, passlen);
   1.130 +	fprintf(stderr, "Salt (length %d):\n", saltlen);
   1.131 +	h__dump(salt, saltlen);
   1.132 +#endif
   1.133 +	v = EVP_MD_block_size (md_type);
   1.134 +	u = EVP_MD_size (md_type);
   1.135 +	D = OPENSSL_malloc (v);
   1.136 +	Ai = OPENSSL_malloc (u);
   1.137 +	B = OPENSSL_malloc (v + 1);
   1.138 +	Slen = v * ((saltlen+v-1)/v);
   1.139 +	if(passlen) Plen = v * ((passlen+v-1)/v);
   1.140 +	else Plen = 0;
   1.141 +	Ilen = Slen + Plen;
   1.142 +	I = OPENSSL_malloc (Ilen);
   1.143 +	Ij = BN_new();
   1.144 +	Bpl1 = BN_new();
   1.145 +	if (!D || !Ai || !B || !I || !Ij || !Bpl1) {
   1.146 +		PKCS12err(PKCS12_F_PKCS12_KEY_GEN_UNI,ERR_R_MALLOC_FAILURE);
   1.147 +		return 0;
   1.148 +	}
   1.149 +	for (i = 0; i < v; i++) D[i] = id;
   1.150 +	p = I;
   1.151 +	for (i = 0; i < Slen; i++) *p++ = salt[i % saltlen];
   1.152 +	for (i = 0; i < Plen; i++) *p++ = pass[i % passlen];
   1.153 +	for (;;) {
   1.154 +		EVP_DigestInit_ex(&ctx, md_type, NULL);
   1.155 +		EVP_DigestUpdate(&ctx, D, v);
   1.156 +		EVP_DigestUpdate(&ctx, I, Ilen);
   1.157 +		EVP_DigestFinal_ex(&ctx, Ai, NULL);
   1.158 +		for (j = 1; j < iter; j++) {
   1.159 +			EVP_DigestInit_ex(&ctx, md_type, NULL);
   1.160 +			EVP_DigestUpdate(&ctx, Ai, u);
   1.161 +			EVP_DigestFinal_ex(&ctx, Ai, NULL);
   1.162 +		}
   1.163 +		memcpy (out, Ai, min (n, u));
   1.164 +		if (u >= n) {
   1.165 +			OPENSSL_free (Ai);
   1.166 +			OPENSSL_free (B);
   1.167 +			OPENSSL_free (D);
   1.168 +			OPENSSL_free (I);
   1.169 +			BN_free (Ij);
   1.170 +			BN_free (Bpl1);
   1.171 +			EVP_MD_CTX_cleanup(&ctx);
   1.172 +#ifdef DEBUG_KEYGEN
   1.173 +			fprintf(stderr, "Output KEY (length %d)\n", tmpn);
   1.174 +			h__dump(tmpout, tmpn);
   1.175 +#endif
   1.176 +			return 1;	
   1.177 +		}
   1.178 +		n -= u;
   1.179 +		out += u;
   1.180 +		for (j = 0; j < v; j++) B[j] = Ai[j % u];
   1.181 +		/* Work out B + 1 first then can use B as tmp space */
   1.182 +		BN_bin2bn (B, v, Bpl1);
   1.183 +		BN_add_word (Bpl1, 1);
   1.184 +		for (j = 0; j < Ilen ; j+=v) {
   1.185 +			BN_bin2bn (I + j, v, Ij);
   1.186 +			BN_add (Ij, Ij, Bpl1);
   1.187 +			BN_bn2bin (Ij, B);
   1.188 +			Ijlen = BN_num_bytes (Ij);
   1.189 +			/* If more than 2^(v*8) - 1 cut off MSB */
   1.190 +			if (Ijlen > v) {
   1.191 +				BN_bn2bin (Ij, B);
   1.192 +				memcpy (I + j, B + 1, v);
   1.193 +#ifndef PKCS12_BROKEN_KEYGEN
   1.194 +			/* If less than v bytes pad with zeroes */
   1.195 +			} else if (Ijlen < v) {
   1.196 +				memset(I + j, 0, v - Ijlen);
   1.197 +				BN_bn2bin(Ij, I + j + v - Ijlen); 
   1.198 +#endif
   1.199 +			} else BN_bn2bin (Ij, I + j);
   1.200 +		}
   1.201 +	}
   1.202 +}
   1.203 +#ifdef DEBUG_KEYGEN
   1.204 +void h__dump (unsigned char *p, int len)
   1.205 +{
   1.206 +	for (; len --; p++) fprintf(stderr, "%02X", *p);
   1.207 +	fprintf(stderr, "\n");	
   1.208 +}
   1.209 +#endif