| OLD | NEW |
| 1 /* p5_pbev2.c */ | 1 /* p5_pbev2.c */ |
| 2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL | 2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL |
| 3 * project 1999-2004. | 3 * project 1999-2004. |
| 4 */ | 4 */ |
| 5 /* ==================================================================== | 5 /* ==================================================================== |
| 6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved. | 6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved. |
| 7 * | 7 * |
| 8 * Redistribution and use in source and binary forms, with or without | 8 * Redistribution and use in source and binary forms, with or without |
| 9 * modification, are permitted provided that the following conditions | 9 * modification, are permitted provided that the following conditions |
| 10 * are met: | 10 * are met: |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 ASN1_SIMPLE(PBKDF2PARAM, salt, ASN1_ANY), | 75 ASN1_SIMPLE(PBKDF2PARAM, salt, ASN1_ANY), |
| 76 ASN1_SIMPLE(PBKDF2PARAM, iter, ASN1_INTEGER), | 76 ASN1_SIMPLE(PBKDF2PARAM, iter, ASN1_INTEGER), |
| 77 ASN1_OPT(PBKDF2PARAM, keylength, ASN1_INTEGER), | 77 ASN1_OPT(PBKDF2PARAM, keylength, ASN1_INTEGER), |
| 78 ASN1_OPT(PBKDF2PARAM, prf, X509_ALGOR) | 78 ASN1_OPT(PBKDF2PARAM, prf, X509_ALGOR) |
| 79 } ASN1_SEQUENCE_END(PBKDF2PARAM) | 79 } ASN1_SEQUENCE_END(PBKDF2PARAM) |
| 80 | 80 |
| 81 IMPLEMENT_ASN1_FUNCTIONS(PBKDF2PARAM) | 81 IMPLEMENT_ASN1_FUNCTIONS(PBKDF2PARAM) |
| 82 | 82 |
| 83 /* Return an algorithm identifier for a PKCS#5 v2.0 PBE algorithm: | 83 /* Return an algorithm identifier for a PKCS#5 v2.0 PBE algorithm: |
| 84 * yes I know this is horrible! | 84 * yes I know this is horrible! |
| 85 * |
| 86 * Extended version to allow application supplied PRF NID and IV. |
| 85 */ | 87 */ |
| 86 | 88 |
| 87 X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter, | 89 X509_ALGOR *PKCS5_pbe2_set_iv(const EVP_CIPHER *cipher, int iter, |
| 88 » » » » unsigned char *salt, int saltlen) | 90 » » » » unsigned char *salt, int saltlen, |
| 91 » » » » unsigned char *aiv, int prf_nid) |
| 89 { | 92 { |
| 90 X509_ALGOR *scheme = NULL, *kalg = NULL, *ret = NULL; | 93 X509_ALGOR *scheme = NULL, *kalg = NULL, *ret = NULL; |
| 91 int alg_nid; | 94 int alg_nid; |
| 92 EVP_CIPHER_CTX ctx; | 95 EVP_CIPHER_CTX ctx; |
| 93 unsigned char iv[EVP_MAX_IV_LENGTH]; | 96 unsigned char iv[EVP_MAX_IV_LENGTH]; |
| 94 PBKDF2PARAM *kdf = NULL; | 97 PBKDF2PARAM *kdf = NULL; |
| 95 PBE2PARAM *pbe2 = NULL; | 98 PBE2PARAM *pbe2 = NULL; |
| 96 ASN1_OCTET_STRING *osalt = NULL; | 99 ASN1_OCTET_STRING *osalt = NULL; |
| 97 ASN1_OBJECT *obj; | 100 ASN1_OBJECT *obj; |
| 98 | 101 |
| 99 alg_nid = EVP_CIPHER_type(cipher); | 102 alg_nid = EVP_CIPHER_type(cipher); |
| 100 if(alg_nid == NID_undef) { | 103 if(alg_nid == NID_undef) { |
| 101 » » ASN1err(ASN1_F_PKCS5_PBE2_SET, | 104 » » ASN1err(ASN1_F_PKCS5_PBE2_SET_IV, |
| 102 ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER); | 105 ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER); |
| 103 goto err; | 106 goto err; |
| 104 } | 107 } |
| 105 obj = OBJ_nid2obj(alg_nid); | 108 obj = OBJ_nid2obj(alg_nid); |
| 106 | 109 |
| 107 if(!(pbe2 = PBE2PARAM_new())) goto merr; | 110 if(!(pbe2 = PBE2PARAM_new())) goto merr; |
| 108 | 111 |
| 109 /* Setup the AlgorithmIdentifier for the encryption scheme */ | 112 /* Setup the AlgorithmIdentifier for the encryption scheme */ |
| 110 scheme = pbe2->encryption; | 113 scheme = pbe2->encryption; |
| 111 | 114 |
| 112 scheme->algorithm = obj; | 115 scheme->algorithm = obj; |
| 113 if(!(scheme->parameter = ASN1_TYPE_new())) goto merr; | 116 if(!(scheme->parameter = ASN1_TYPE_new())) goto merr; |
| 114 | 117 |
| 115 /* Create random IV */ | 118 /* Create random IV */ |
| 116 » if (EVP_CIPHER_iv_length(cipher) && | 119 » if (EVP_CIPHER_iv_length(cipher)) |
| 117 » » RAND_pseudo_bytes(iv, EVP_CIPHER_iv_length(cipher)) < 0) | 120 » » { |
| 118 » » goto err; | 121 » » if (aiv) |
| 122 » » » memcpy(iv, aiv, EVP_CIPHER_iv_length(cipher)); |
| 123 » » else if (RAND_pseudo_bytes(iv, EVP_CIPHER_iv_length(cipher)) < 0
) |
| 124 » » » goto err; |
| 125 » » } |
| 119 | 126 |
| 120 EVP_CIPHER_CTX_init(&ctx); | 127 EVP_CIPHER_CTX_init(&ctx); |
| 121 | 128 |
| 122 » /* Dummy cipherinit to just setup the IV */ | 129 » /* Dummy cipherinit to just setup the IV, and PRF */ |
| 123 EVP_CipherInit_ex(&ctx, cipher, NULL, NULL, iv, 0); | 130 EVP_CipherInit_ex(&ctx, cipher, NULL, NULL, iv, 0); |
| 124 if(EVP_CIPHER_param_to_asn1(&ctx, scheme->parameter) < 0) { | 131 if(EVP_CIPHER_param_to_asn1(&ctx, scheme->parameter) < 0) { |
| 125 » » ASN1err(ASN1_F_PKCS5_PBE2_SET, | 132 » » ASN1err(ASN1_F_PKCS5_PBE2_SET_IV, |
| 126 ASN1_R_ERROR_SETTING_CIPHER_PARAMS); | 133 ASN1_R_ERROR_SETTING_CIPHER_PARAMS); |
| 127 EVP_CIPHER_CTX_cleanup(&ctx); | 134 EVP_CIPHER_CTX_cleanup(&ctx); |
| 128 goto err; | 135 goto err; |
| 129 } | 136 } |
| 137 /* If prf NID unspecified see if cipher has a preference. |
| 138 * An error is OK here: just means use default PRF. |
| 139 */ |
| 140 if ((prf_nid == -1) && |
| 141 EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_PBE_PRF_NID, 0, &prf_nid) <= 0) |
| 142 { |
| 143 ERR_clear_error(); |
| 144 prf_nid = NID_hmacWithSHA1; |
| 145 } |
| 130 EVP_CIPHER_CTX_cleanup(&ctx); | 146 EVP_CIPHER_CTX_cleanup(&ctx); |
| 131 | 147 |
| 132 if(!(kdf = PBKDF2PARAM_new())) goto merr; | 148 if(!(kdf = PBKDF2PARAM_new())) goto merr; |
| 133 if(!(osalt = M_ASN1_OCTET_STRING_new())) goto merr; | 149 if(!(osalt = M_ASN1_OCTET_STRING_new())) goto merr; |
| 134 | 150 |
| 135 if (!saltlen) saltlen = PKCS5_SALT_LEN; | 151 if (!saltlen) saltlen = PKCS5_SALT_LEN; |
| 136 if (!(osalt->data = OPENSSL_malloc (saltlen))) goto merr; | 152 if (!(osalt->data = OPENSSL_malloc (saltlen))) goto merr; |
| 137 osalt->length = saltlen; | 153 osalt->length = saltlen; |
| 138 if (salt) memcpy (osalt->data, salt, saltlen); | 154 if (salt) memcpy (osalt->data, salt, saltlen); |
| 139 else if (RAND_pseudo_bytes (osalt->data, saltlen) < 0) goto merr; | 155 else if (RAND_pseudo_bytes (osalt->data, saltlen) < 0) goto merr; |
| 140 | 156 |
| 141 if(iter <= 0) iter = PKCS5_DEFAULT_ITER; | 157 if(iter <= 0) iter = PKCS5_DEFAULT_ITER; |
| 142 if(!ASN1_INTEGER_set(kdf->iter, iter)) goto merr; | 158 if(!ASN1_INTEGER_set(kdf->iter, iter)) goto merr; |
| 143 | 159 |
| 144 /* Now include salt in kdf structure */ | 160 /* Now include salt in kdf structure */ |
| 145 kdf->salt->value.octet_string = osalt; | 161 kdf->salt->value.octet_string = osalt; |
| 146 kdf->salt->type = V_ASN1_OCTET_STRING; | 162 kdf->salt->type = V_ASN1_OCTET_STRING; |
| 147 osalt = NULL; | 163 osalt = NULL; |
| 148 | 164 |
| 149 /* If its RC2 then we'd better setup the key length */ | 165 /* If its RC2 then we'd better setup the key length */ |
| 150 | 166 |
| 151 if(alg_nid == NID_rc2_cbc) { | 167 if(alg_nid == NID_rc2_cbc) { |
| 152 if(!(kdf->keylength = M_ASN1_INTEGER_new())) goto merr; | 168 if(!(kdf->keylength = M_ASN1_INTEGER_new())) goto merr; |
| 153 if(!ASN1_INTEGER_set (kdf->keylength, | 169 if(!ASN1_INTEGER_set (kdf->keylength, |
| 154 EVP_CIPHER_key_length(cipher))) goto merr; | 170 EVP_CIPHER_key_length(cipher))) goto merr; |
| 155 } | 171 } |
| 156 | 172 |
| 157 » /* prf can stay NULL because we are using hmacWithSHA1 */ | 173 » /* prf can stay NULL if we are using hmacWithSHA1 */ |
| 174 » if (prf_nid != NID_hmacWithSHA1) |
| 175 » » { |
| 176 » » kdf->prf = X509_ALGOR_new(); |
| 177 » » if (!kdf->prf) |
| 178 » » » goto merr; |
| 179 » » X509_ALGOR_set0(kdf->prf, OBJ_nid2obj(prf_nid), |
| 180 » » » » » V_ASN1_NULL, NULL); |
| 181 » » } |
| 158 | 182 |
| 159 /* Now setup the PBE2PARAM keyfunc structure */ | 183 /* Now setup the PBE2PARAM keyfunc structure */ |
| 160 | 184 |
| 161 pbe2->keyfunc->algorithm = OBJ_nid2obj(NID_id_pbkdf2); | 185 pbe2->keyfunc->algorithm = OBJ_nid2obj(NID_id_pbkdf2); |
| 162 | 186 |
| 163 /* Encode PBKDF2PARAM into parameter of pbe2 */ | 187 /* Encode PBKDF2PARAM into parameter of pbe2 */ |
| 164 | 188 |
| 165 if(!(pbe2->keyfunc->parameter = ASN1_TYPE_new())) goto merr; | 189 if(!(pbe2->keyfunc->parameter = ASN1_TYPE_new())) goto merr; |
| 166 | 190 |
| 167 » if(!ASN1_pack_string_of(PBKDF2PARAM, kdf, i2d_PBKDF2PARAM, | 191 » if(!ASN1_item_pack(kdf, ASN1_ITEM_rptr(PBKDF2PARAM), |
| 168 &pbe2->keyfunc->parameter->value.sequence)) goto merr; | 192 &pbe2->keyfunc->parameter->value.sequence)) goto merr; |
| 169 pbe2->keyfunc->parameter->type = V_ASN1_SEQUENCE; | 193 pbe2->keyfunc->parameter->type = V_ASN1_SEQUENCE; |
| 170 | 194 |
| 171 PBKDF2PARAM_free(kdf); | 195 PBKDF2PARAM_free(kdf); |
| 172 kdf = NULL; | 196 kdf = NULL; |
| 173 | 197 |
| 174 /* Now set up top level AlgorithmIdentifier */ | 198 /* Now set up top level AlgorithmIdentifier */ |
| 175 | 199 |
| 176 if(!(ret = X509_ALGOR_new())) goto merr; | 200 if(!(ret = X509_ALGOR_new())) goto merr; |
| 177 if(!(ret->parameter = ASN1_TYPE_new())) goto merr; | 201 if(!(ret->parameter = ASN1_TYPE_new())) goto merr; |
| 178 | 202 |
| 179 ret->algorithm = OBJ_nid2obj(NID_pbes2); | 203 ret->algorithm = OBJ_nid2obj(NID_pbes2); |
| 180 | 204 |
| 181 /* Encode PBE2PARAM into parameter */ | 205 /* Encode PBE2PARAM into parameter */ |
| 182 | 206 |
| 183 » if(!ASN1_pack_string_of(PBE2PARAM, pbe2, i2d_PBE2PARAM, | 207 » if(!ASN1_item_pack(pbe2, ASN1_ITEM_rptr(PBE2PARAM), |
| 184 &ret->parameter->value.sequence)) goto merr; | 208 &ret->parameter->value.sequence)) goto merr; |
| 185 ret->parameter->type = V_ASN1_SEQUENCE; | 209 ret->parameter->type = V_ASN1_SEQUENCE; |
| 186 | 210 |
| 187 PBE2PARAM_free(pbe2); | 211 PBE2PARAM_free(pbe2); |
| 188 pbe2 = NULL; | 212 pbe2 = NULL; |
| 189 | 213 |
| 190 return ret; | 214 return ret; |
| 191 | 215 |
| 192 merr: | 216 merr: |
| 193 » ASN1err(ASN1_F_PKCS5_PBE2_SET,ERR_R_MALLOC_FAILURE); | 217 » ASN1err(ASN1_F_PKCS5_PBE2_SET_IV,ERR_R_MALLOC_FAILURE); |
| 194 | 218 |
| 195 err: | 219 err: |
| 196 PBE2PARAM_free(pbe2); | 220 PBE2PARAM_free(pbe2); |
| 197 /* Note 'scheme' is freed as part of pbe2 */ | 221 /* Note 'scheme' is freed as part of pbe2 */ |
| 198 M_ASN1_OCTET_STRING_free(osalt); | 222 M_ASN1_OCTET_STRING_free(osalt); |
| 199 PBKDF2PARAM_free(kdf); | 223 PBKDF2PARAM_free(kdf); |
| 200 X509_ALGOR_free(kalg); | 224 X509_ALGOR_free(kalg); |
| 201 X509_ALGOR_free(ret); | 225 X509_ALGOR_free(ret); |
| 202 | 226 |
| 203 return NULL; | 227 return NULL; |
| 204 | 228 |
| 205 } | 229 } |
| 230 |
| 231 X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter, |
| 232 unsigned char *salt, int saltlen) |
| 233 { |
| 234 return PKCS5_pbe2_set_iv(cipher, iter, salt, saltlen, NULL, -1); |
| 235 } |
| OLD | NEW |