1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/pkcs12/p12_crpt.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,130 @@
1.4 +/* p12_crpt.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 +
1.66 +/* PKCS#12 specific PBE functions */
1.67 +
1.68 +EXPORT_C void PKCS12_PBE_add(void)
1.69 +{
1.70 +#ifndef OPENSSL_NO_RC4
1.71 +EVP_PBE_alg_add(NID_pbe_WithSHA1And128BitRC4, EVP_rc4(), EVP_sha1(),
1.72 + PKCS12_PBE_keyivgen);
1.73 +EVP_PBE_alg_add(NID_pbe_WithSHA1And40BitRC4, EVP_rc4_40(), EVP_sha1(),
1.74 + PKCS12_PBE_keyivgen);
1.75 +#endif
1.76 +#ifndef OPENSSL_NO_DES
1.77 +EVP_PBE_alg_add(NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
1.78 + EVP_des_ede3_cbc(), EVP_sha1(), PKCS12_PBE_keyivgen);
1.79 +EVP_PBE_alg_add(NID_pbe_WithSHA1And2_Key_TripleDES_CBC,
1.80 + EVP_des_ede_cbc(), EVP_sha1(), PKCS12_PBE_keyivgen);
1.81 +#endif
1.82 +#ifndef OPENSSL_NO_RC2
1.83 +EVP_PBE_alg_add(NID_pbe_WithSHA1And128BitRC2_CBC, EVP_rc2_cbc(),
1.84 + EVP_sha1(), PKCS12_PBE_keyivgen);
1.85 +EVP_PBE_alg_add(NID_pbe_WithSHA1And40BitRC2_CBC, EVP_rc2_40_cbc(),
1.86 + EVP_sha1(), PKCS12_PBE_keyivgen);
1.87 +#endif
1.88 +}
1.89 +
1.90 +EXPORT_C int PKCS12_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
1.91 + ASN1_TYPE *param, const EVP_CIPHER *cipher, const EVP_MD *md, int en_de)
1.92 +{
1.93 + PBEPARAM *pbe;
1.94 + int saltlen, iter, ret;
1.95 + unsigned char *salt;
1.96 + const unsigned char *pbuf;
1.97 + unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH];
1.98 +
1.99 + /* Extract useful info from parameter */
1.100 + if (param == NULL || param->type != V_ASN1_SEQUENCE ||
1.101 + param->value.sequence == NULL) {
1.102 + PKCS12err(PKCS12_F_PKCS12_PBE_KEYIVGEN,PKCS12_R_DECODE_ERROR);
1.103 + return 0;
1.104 + }
1.105 +
1.106 + pbuf = param->value.sequence->data;
1.107 + if (!(pbe = d2i_PBEPARAM(NULL, &pbuf, param->value.sequence->length))) {
1.108 + PKCS12err(PKCS12_F_PKCS12_PBE_KEYIVGEN,PKCS12_R_DECODE_ERROR);
1.109 + return 0;
1.110 + }
1.111 +
1.112 + if (!pbe->iter) iter = 1;
1.113 + else iter = ASN1_INTEGER_get (pbe->iter);
1.114 + salt = pbe->salt->data;
1.115 + saltlen = pbe->salt->length;
1.116 + if (!PKCS12_key_gen (pass, passlen, salt, saltlen, PKCS12_KEY_ID,
1.117 + iter, EVP_CIPHER_key_length(cipher), key, md)) {
1.118 + PKCS12err(PKCS12_F_PKCS12_PBE_KEYIVGEN,PKCS12_R_KEY_GEN_ERROR);
1.119 + PBEPARAM_free(pbe);
1.120 + return 0;
1.121 + }
1.122 + if (!PKCS12_key_gen (pass, passlen, salt, saltlen, PKCS12_IV_ID,
1.123 + iter, EVP_CIPHER_iv_length(cipher), iv, md)) {
1.124 + PKCS12err(PKCS12_F_PKCS12_PBE_KEYIVGEN,PKCS12_R_IV_GEN_ERROR);
1.125 + PBEPARAM_free(pbe);
1.126 + return 0;
1.127 + }
1.128 + PBEPARAM_free(pbe);
1.129 + ret = EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, en_de);
1.130 + OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH);
1.131 + OPENSSL_cleanse(iv, EVP_MAX_IV_LENGTH);
1.132 + return ret;
1.133 +}