1 /* Written by Dr Stephen N Henson (steve (at) openssl.org) for the OpenSSL 2 * project 1999-2004. 3 */ 4 /* ==================================================================== 5 * Copyright (c) 1999 The OpenSSL Project. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 19 * 3. All advertising materials mentioning features or use of this 20 * software must display the following acknowledgment: 21 * "This product includes software developed by the OpenSSL Project 22 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 23 * 24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 25 * endorse or promote products derived from this software without 26 * prior written permission. For written permission, please contact 27 * licensing (at) OpenSSL.org. 28 * 29 * 5. Products derived from this software may not be called "OpenSSL" 30 * nor may "OpenSSL" appear in their names without prior written 31 * permission of the OpenSSL Project. 32 * 33 * 6. Redistributions of any form whatsoever must retain the following 34 * acknowledgment: 35 * "This product includes software developed by the OpenSSL Project 36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 37 * 38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 49 * OF THE POSSIBILITY OF SUCH DAMAGE. 50 * ==================================================================== 51 * 52 * This product includes cryptographic software written by Eric Young 53 * (eay (at) cryptsoft.com). This product includes software written by Tim 54 * Hudson (tjh (at) cryptsoft.com). */ 55 56 57 #include <openssl/asn1t.h> 58 #include <openssl/cipher.h> 59 #include <openssl/err.h> 60 #include <openssl/mem.h> 61 #include <openssl/pkcs8.h> 62 #include <openssl/rand.h> 63 #include <openssl/x509.h> 64 65 #include "internal.h" 66 67 68 /* PKCS#5 v2.0 password based encryption structures */ 69 70 ASN1_SEQUENCE(PBE2PARAM) = { 71 ASN1_SIMPLE(PBE2PARAM, keyfunc, X509_ALGOR), 72 ASN1_SIMPLE(PBE2PARAM, encryption, X509_ALGOR) 73 } ASN1_SEQUENCE_END(PBE2PARAM) 74 75 IMPLEMENT_ASN1_FUNCTIONS(PBE2PARAM) 76 77 ASN1_SEQUENCE(PBKDF2PARAM) = { 78 ASN1_SIMPLE(PBKDF2PARAM, salt, ASN1_ANY), 79 ASN1_SIMPLE(PBKDF2PARAM, iter, ASN1_INTEGER), 80 ASN1_OPT(PBKDF2PARAM, keylength, ASN1_INTEGER), 81 ASN1_OPT(PBKDF2PARAM, prf, X509_ALGOR) 82 } ASN1_SEQUENCE_END(PBKDF2PARAM) 83 84 IMPLEMENT_ASN1_FUNCTIONS(PBKDF2PARAM); 85 86 static int ASN1_TYPE_set_octetstring(ASN1_TYPE *a, unsigned char *data, int len) 87 { 88 ASN1_STRING *os; 89 90 if ((os=M_ASN1_OCTET_STRING_new()) == NULL) return(0); 91 if (!M_ASN1_OCTET_STRING_set(os,data,len)) 92 { 93 M_ASN1_OCTET_STRING_free(os); 94 return 0; 95 } 96 ASN1_TYPE_set(a,V_ASN1_OCTET_STRING,os); 97 return(1); 98 } 99 100 static int param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type) 101 { 102 unsigned iv_len; 103 104 iv_len = EVP_CIPHER_CTX_iv_length(c); 105 return ASN1_TYPE_set_octetstring(type, c->oiv, iv_len); 106 } 107 108 /* Return an algorithm identifier for a PKCS#5 v2.0 PBE algorithm: 109 * yes I know this is horrible! 110 * 111 * Extended version to allow application supplied PRF NID and IV. */ 112 113 X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter, 114 unsigned char *salt, int saltlen, 115 unsigned char *aiv, int prf_nid) 116 { 117 X509_ALGOR *scheme = NULL, *kalg = NULL, *ret = NULL; 118 int alg_nid, keylen; 119 EVP_CIPHER_CTX ctx; 120 unsigned char iv[EVP_MAX_IV_LENGTH]; 121 PBE2PARAM *pbe2 = NULL; 122 const ASN1_OBJECT *obj; 123 124 alg_nid = EVP_CIPHER_nid(cipher); 125 if(alg_nid == NID_undef) { 126 OPENSSL_PUT_ERROR(PKCS8, PKCS5_pbe2_set_iv, PKCS8_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER); 127 goto err; 128 } 129 obj = OBJ_nid2obj(alg_nid); 130 131 if(!(pbe2 = PBE2PARAM_new())) goto merr; 132 133 /* Setup the AlgorithmIdentifier for the encryption scheme */ 134 scheme = pbe2->encryption; 135 136 scheme->algorithm = (ASN1_OBJECT*) obj; 137 if(!(scheme->parameter = ASN1_TYPE_new())) goto merr; 138 139 /* Create random IV */ 140 if (EVP_CIPHER_iv_length(cipher)) 141 { 142 if (aiv) 143 memcpy(iv, aiv, EVP_CIPHER_iv_length(cipher)); 144 else if (RAND_pseudo_bytes(iv, EVP_CIPHER_iv_length(cipher)) < 0) 145 goto err; 146 } 147 148 EVP_CIPHER_CTX_init(&ctx); 149 150 /* Dummy cipherinit to just setup the IV, and PRF */ 151 if (!EVP_CipherInit_ex(&ctx, cipher, NULL, NULL, iv, 0)) 152 goto err; 153 if(param_to_asn1(&ctx, scheme->parameter) < 0) { 154 OPENSSL_PUT_ERROR(PKCS8, PKCS5_pbe2_set_iv, PKCS8_R_ERROR_SETTING_CIPHER_PARAMS); 155 EVP_CIPHER_CTX_cleanup(&ctx); 156 goto err; 157 } 158 /* If prf NID unspecified see if cipher has a preference. 159 * An error is OK here: just means use default PRF. 160 */ 161 if ((prf_nid == -1) && 162 EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_PBE_PRF_NID, 0, &prf_nid) <= 0) 163 { 164 ERR_clear_error(); 165 prf_nid = NID_hmacWithSHA1; 166 } 167 EVP_CIPHER_CTX_cleanup(&ctx); 168 169 /* If its RC2 then we'd better setup the key length */ 170 171 if(alg_nid == NID_rc2_cbc) 172 keylen = EVP_CIPHER_key_length(cipher); 173 else 174 keylen = -1; 175 176 /* Setup keyfunc */ 177 178 X509_ALGOR_free(pbe2->keyfunc); 179 180 pbe2->keyfunc = PKCS5_pbkdf2_set(iter, salt, saltlen, prf_nid, keylen); 181 182 if (!pbe2->keyfunc) 183 goto merr; 184 185 /* Now set up top level AlgorithmIdentifier */ 186 187 if(!(ret = X509_ALGOR_new())) goto merr; 188 if(!(ret->parameter = ASN1_TYPE_new())) goto merr; 189 190 ret->algorithm = (ASN1_OBJECT*) OBJ_nid2obj(NID_pbes2); 191 192 /* Encode PBE2PARAM into parameter */ 193 194 if(!ASN1_item_pack(pbe2, ASN1_ITEM_rptr(PBE2PARAM), 195 &ret->parameter->value.sequence)) goto merr; 196 ret->parameter->type = V_ASN1_SEQUENCE; 197 198 PBE2PARAM_free(pbe2); 199 pbe2 = NULL; 200 201 return ret; 202 203 merr: 204 OPENSSL_PUT_ERROR(PKCS8, PKCS5_pbe2_set_iv, ERR_R_MALLOC_FAILURE); 205 206 err: 207 PBE2PARAM_free(pbe2); 208 /* Note 'scheme' is freed as part of pbe2 */ 209 X509_ALGOR_free(kalg); 210 X509_ALGOR_free(ret); 211 212 return NULL; 213 214 } 215 216 X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter, 217 unsigned char *salt, int saltlen) 218 { 219 return PKCS5_pbe2_set_iv(cipher, iter, salt, saltlen, NULL, -1); 220 } 221 222 X509_ALGOR *PKCS5_pbkdf2_set(int iter, unsigned char *salt, int saltlen, 223 int prf_nid, int keylen) 224 { 225 X509_ALGOR *keyfunc = NULL; 226 PBKDF2PARAM *kdf = NULL; 227 ASN1_OCTET_STRING *osalt = NULL; 228 229 if(!(kdf = PBKDF2PARAM_new())) 230 goto merr; 231 if(!(osalt = M_ASN1_OCTET_STRING_new())) 232 goto merr; 233 234 kdf->salt->value.octet_string = osalt; 235 kdf->salt->type = V_ASN1_OCTET_STRING; 236 237 if (!saltlen) 238 saltlen = PKCS5_SALT_LEN; 239 if (!(osalt->data = OPENSSL_malloc (saltlen))) 240 goto merr; 241 242 osalt->length = saltlen; 243 244 if (salt) 245 memcpy (osalt->data, salt, saltlen); 246 else if (RAND_pseudo_bytes (osalt->data, saltlen) < 0) 247 goto merr; 248 249 if(iter <= 0) 250 iter = PKCS5_DEFAULT_ITERATIONS; 251 252 if(!ASN1_INTEGER_set(kdf->iter, iter)) 253 goto merr; 254 255 /* If have a key len set it up */ 256 257 if(keylen > 0) 258 { 259 if(!(kdf->keylength = M_ASN1_INTEGER_new())) 260 goto merr; 261 if(!ASN1_INTEGER_set (kdf->keylength, keylen)) 262 goto merr; 263 } 264 265 /* prf can stay NULL if we are using hmacWithSHA1 */ 266 if (prf_nid > 0 && prf_nid != NID_hmacWithSHA1) 267 { 268 kdf->prf = X509_ALGOR_new(); 269 if (!kdf->prf) 270 goto merr; 271 X509_ALGOR_set0(kdf->prf, OBJ_nid2obj(prf_nid), 272 V_ASN1_NULL, NULL); 273 } 274 275 /* Finally setup the keyfunc structure */ 276 277 keyfunc = X509_ALGOR_new(); 278 if (!keyfunc) 279 goto merr; 280 281 keyfunc->algorithm = (ASN1_OBJECT*) OBJ_nid2obj(NID_id_pbkdf2); 282 283 /* Encode PBKDF2PARAM into parameter of pbe2 */ 284 285 if(!(keyfunc->parameter = ASN1_TYPE_new())) 286 goto merr; 287 288 if(!ASN1_item_pack(kdf, ASN1_ITEM_rptr(PBKDF2PARAM), 289 &keyfunc->parameter->value.sequence)) 290 goto merr; 291 keyfunc->parameter->type = V_ASN1_SEQUENCE; 292 293 PBKDF2PARAM_free(kdf); 294 return keyfunc; 295 296 merr: 297 OPENSSL_PUT_ERROR(PKCS8, PKCS5_pbkdf2_set, ERR_R_MALLOC_FAILURE); 298 PBKDF2PARAM_free(kdf); 299 X509_ALGOR_free(keyfunc); 300 return NULL; 301 } 302 303