diff options
Diffstat (limited to 'src/lib/libcrypto/asn1/p5_pbev2.c')
-rw-r--r-- | src/lib/libcrypto/asn1/p5_pbev2.c | 137 |
1 files changed, 33 insertions, 104 deletions
diff --git a/src/lib/libcrypto/asn1/p5_pbev2.c b/src/lib/libcrypto/asn1/p5_pbev2.c index 09f4bf6112..91e1c8987d 100644 --- a/src/lib/libcrypto/asn1/p5_pbev2.c +++ b/src/lib/libcrypto/asn1/p5_pbev2.c | |||
@@ -58,108 +58,27 @@ | |||
58 | 58 | ||
59 | #include <stdio.h> | 59 | #include <stdio.h> |
60 | #include "cryptlib.h" | 60 | #include "cryptlib.h" |
61 | #include <openssl/asn1_mac.h> | 61 | #include <openssl/asn1t.h> |
62 | #include <openssl/x509.h> | 62 | #include <openssl/x509.h> |
63 | #include <openssl/rand.h> | 63 | #include <openssl/rand.h> |
64 | 64 | ||
65 | /* PKCS#5 v2.0 password based encryption structures */ | 65 | /* PKCS#5 v2.0 password based encryption structures */ |
66 | 66 | ||
67 | int i2d_PBE2PARAM(PBE2PARAM *a, unsigned char **pp) | 67 | ASN1_SEQUENCE(PBE2PARAM) = { |
68 | { | 68 | ASN1_SIMPLE(PBE2PARAM, keyfunc, X509_ALGOR), |
69 | M_ASN1_I2D_vars(a); | 69 | ASN1_SIMPLE(PBE2PARAM, encryption, X509_ALGOR) |
70 | M_ASN1_I2D_len (a->keyfunc, i2d_X509_ALGOR); | 70 | } ASN1_SEQUENCE_END(PBE2PARAM) |
71 | M_ASN1_I2D_len (a->encryption, i2d_X509_ALGOR); | ||
72 | |||
73 | M_ASN1_I2D_seq_total (); | ||
74 | |||
75 | M_ASN1_I2D_put (a->keyfunc, i2d_X509_ALGOR); | ||
76 | M_ASN1_I2D_put (a->encryption, i2d_X509_ALGOR); | ||
77 | |||
78 | M_ASN1_I2D_finish(); | ||
79 | } | ||
80 | |||
81 | PBE2PARAM *PBE2PARAM_new(void) | ||
82 | { | ||
83 | PBE2PARAM *ret=NULL; | ||
84 | ASN1_CTX c; | ||
85 | M_ASN1_New_Malloc(ret, PBE2PARAM); | ||
86 | M_ASN1_New(ret->keyfunc,X509_ALGOR_new); | ||
87 | M_ASN1_New(ret->encryption,X509_ALGOR_new); | ||
88 | return (ret); | ||
89 | M_ASN1_New_Error(ASN1_F_PBE2PARAM_NEW); | ||
90 | } | ||
91 | |||
92 | PBE2PARAM *d2i_PBE2PARAM(PBE2PARAM **a, unsigned char **pp, long length) | ||
93 | { | ||
94 | M_ASN1_D2I_vars(a,PBE2PARAM *,PBE2PARAM_new); | ||
95 | M_ASN1_D2I_Init(); | ||
96 | M_ASN1_D2I_start_sequence(); | ||
97 | M_ASN1_D2I_get (ret->keyfunc, d2i_X509_ALGOR); | ||
98 | M_ASN1_D2I_get (ret->encryption, d2i_X509_ALGOR); | ||
99 | M_ASN1_D2I_Finish(a, PBE2PARAM_free, ASN1_F_D2I_PBE2PARAM); | ||
100 | } | ||
101 | 71 | ||
102 | void PBE2PARAM_free (PBE2PARAM *a) | 72 | IMPLEMENT_ASN1_FUNCTIONS(PBE2PARAM) |
103 | { | ||
104 | if(a==NULL) return; | ||
105 | X509_ALGOR_free(a->keyfunc); | ||
106 | X509_ALGOR_free(a->encryption); | ||
107 | Free ((char *)a); | ||
108 | } | ||
109 | 73 | ||
110 | int i2d_PBKDF2PARAM(PBKDF2PARAM *a, unsigned char **pp) | 74 | ASN1_SEQUENCE(PBKDF2PARAM) = { |
111 | { | 75 | ASN1_SIMPLE(PBKDF2PARAM, salt, ASN1_ANY), |
112 | M_ASN1_I2D_vars(a); | 76 | ASN1_SIMPLE(PBKDF2PARAM, iter, ASN1_INTEGER), |
113 | M_ASN1_I2D_len (a->salt, i2d_ASN1_TYPE); | 77 | ASN1_OPT(PBKDF2PARAM, keylength, ASN1_INTEGER), |
114 | M_ASN1_I2D_len (a->iter, i2d_ASN1_INTEGER); | 78 | ASN1_OPT(PBKDF2PARAM, prf, X509_ALGOR) |
115 | M_ASN1_I2D_len (a->keylength, i2d_ASN1_INTEGER); | 79 | } ASN1_SEQUENCE_END(PBKDF2PARAM) |
116 | M_ASN1_I2D_len (a->prf, i2d_X509_ALGOR); | ||
117 | 80 | ||
118 | M_ASN1_I2D_seq_total (); | 81 | IMPLEMENT_ASN1_FUNCTIONS(PBKDF2PARAM) |
119 | |||
120 | M_ASN1_I2D_put (a->salt, i2d_ASN1_TYPE); | ||
121 | M_ASN1_I2D_put (a->iter, i2d_ASN1_INTEGER); | ||
122 | M_ASN1_I2D_put (a->keylength, i2d_ASN1_INTEGER); | ||
123 | M_ASN1_I2D_put (a->prf, i2d_X509_ALGOR); | ||
124 | |||
125 | M_ASN1_I2D_finish(); | ||
126 | } | ||
127 | |||
128 | PBKDF2PARAM *PBKDF2PARAM_new(void) | ||
129 | { | ||
130 | PBKDF2PARAM *ret=NULL; | ||
131 | ASN1_CTX c; | ||
132 | M_ASN1_New_Malloc(ret, PBKDF2PARAM); | ||
133 | M_ASN1_New(ret->salt, ASN1_TYPE_new); | ||
134 | M_ASN1_New(ret->iter, ASN1_INTEGER_new); | ||
135 | ret->keylength = NULL; | ||
136 | ret->prf = NULL; | ||
137 | return (ret); | ||
138 | M_ASN1_New_Error(ASN1_F_PBKDF2PARAM_NEW); | ||
139 | } | ||
140 | |||
141 | PBKDF2PARAM *d2i_PBKDF2PARAM(PBKDF2PARAM **a, unsigned char **pp, | ||
142 | long length) | ||
143 | { | ||
144 | M_ASN1_D2I_vars(a,PBKDF2PARAM *,PBKDF2PARAM_new); | ||
145 | M_ASN1_D2I_Init(); | ||
146 | M_ASN1_D2I_start_sequence(); | ||
147 | M_ASN1_D2I_get (ret->salt, d2i_ASN1_TYPE); | ||
148 | M_ASN1_D2I_get (ret->iter, d2i_ASN1_INTEGER); | ||
149 | M_ASN1_D2I_get_opt (ret->keylength, d2i_ASN1_INTEGER, V_ASN1_INTEGER); | ||
150 | M_ASN1_D2I_get_opt (ret->prf, d2i_X509_ALGOR, V_ASN1_SEQUENCE); | ||
151 | M_ASN1_D2I_Finish(a, PBKDF2PARAM_free, ASN1_F_D2I_PBKDF2PARAM); | ||
152 | } | ||
153 | |||
154 | void PBKDF2PARAM_free (PBKDF2PARAM *a) | ||
155 | { | ||
156 | if(a==NULL) return; | ||
157 | ASN1_TYPE_free(a->salt); | ||
158 | ASN1_INTEGER_free(a->iter); | ||
159 | ASN1_INTEGER_free(a->keylength); | ||
160 | X509_ALGOR_free(a->prf); | ||
161 | Free ((char *)a); | ||
162 | } | ||
163 | 82 | ||
164 | /* 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: |
165 | * yes I know this is horrible! | 84 | * yes I know this is horrible! |
@@ -175,22 +94,32 @@ X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter, | |||
175 | PBKDF2PARAM *kdf = NULL; | 94 | PBKDF2PARAM *kdf = NULL; |
176 | PBE2PARAM *pbe2 = NULL; | 95 | PBE2PARAM *pbe2 = NULL; |
177 | ASN1_OCTET_STRING *osalt = NULL; | 96 | ASN1_OCTET_STRING *osalt = NULL; |
97 | ASN1_OBJECT *obj; | ||
98 | |||
99 | alg_nid = EVP_CIPHER_type(cipher); | ||
100 | if(alg_nid == NID_undef) { | ||
101 | ASN1err(ASN1_F_PKCS5_PBE2_SET, | ||
102 | ASN1_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER); | ||
103 | goto err; | ||
104 | } | ||
105 | obj = OBJ_nid2obj(alg_nid); | ||
178 | 106 | ||
179 | if(!(pbe2 = PBE2PARAM_new())) goto merr; | 107 | if(!(pbe2 = PBE2PARAM_new())) goto merr; |
180 | 108 | ||
181 | /* Setup the AlgorithmIdentifier for the encryption scheme */ | 109 | /* Setup the AlgorithmIdentifier for the encryption scheme */ |
182 | scheme = pbe2->encryption; | 110 | scheme = pbe2->encryption; |
183 | 111 | ||
184 | alg_nid = EVP_CIPHER_type(cipher); | 112 | scheme->algorithm = obj; |
185 | |||
186 | scheme->algorithm = OBJ_nid2obj(alg_nid); | ||
187 | if(!(scheme->parameter = ASN1_TYPE_new())) goto merr; | 113 | if(!(scheme->parameter = ASN1_TYPE_new())) goto merr; |
188 | 114 | ||
189 | /* Create random IV */ | 115 | /* Create random IV */ |
190 | RAND_bytes(iv, EVP_CIPHER_iv_length(cipher)); | 116 | if (RAND_pseudo_bytes(iv, EVP_CIPHER_iv_length(cipher)) < 0) |
117 | goto err; | ||
118 | |||
119 | EVP_CIPHER_CTX_init(&ctx); | ||
191 | 120 | ||
192 | /* Dummy cipherinit to just setup the IV */ | 121 | /* Dummy cipherinit to just setup the IV */ |
193 | EVP_CipherInit(&ctx, cipher, NULL, iv, 0); | 122 | EVP_CipherInit_ex(&ctx, cipher, NULL, NULL, iv, 0); |
194 | if(EVP_CIPHER_param_to_asn1(&ctx, scheme->parameter) < 0) { | 123 | if(EVP_CIPHER_param_to_asn1(&ctx, scheme->parameter) < 0) { |
195 | ASN1err(ASN1_F_PKCS5_PBE2_SET, | 124 | ASN1err(ASN1_F_PKCS5_PBE2_SET, |
196 | ASN1_R_ERROR_SETTING_CIPHER_PARAMS); | 125 | ASN1_R_ERROR_SETTING_CIPHER_PARAMS); |
@@ -199,13 +128,13 @@ X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter, | |||
199 | EVP_CIPHER_CTX_cleanup(&ctx); | 128 | EVP_CIPHER_CTX_cleanup(&ctx); |
200 | 129 | ||
201 | if(!(kdf = PBKDF2PARAM_new())) goto merr; | 130 | if(!(kdf = PBKDF2PARAM_new())) goto merr; |
202 | if(!(osalt = ASN1_OCTET_STRING_new())) goto merr; | 131 | if(!(osalt = M_ASN1_OCTET_STRING_new())) goto merr; |
203 | 132 | ||
204 | if (!saltlen) saltlen = PKCS5_SALT_LEN; | 133 | if (!saltlen) saltlen = PKCS5_SALT_LEN; |
205 | if (!(osalt->data = Malloc (saltlen))) goto merr; | 134 | if (!(osalt->data = OPENSSL_malloc (saltlen))) goto merr; |
206 | osalt->length = saltlen; | 135 | osalt->length = saltlen; |
207 | if (salt) memcpy (osalt->data, salt, saltlen); | 136 | if (salt) memcpy (osalt->data, salt, saltlen); |
208 | else RAND_bytes (osalt->data, saltlen); | 137 | else if (RAND_pseudo_bytes (osalt->data, saltlen) < 0) goto merr; |
209 | 138 | ||
210 | if(iter <= 0) iter = PKCS5_DEFAULT_ITER; | 139 | if(iter <= 0) iter = PKCS5_DEFAULT_ITER; |
211 | if(!ASN1_INTEGER_set(kdf->iter, iter)) goto merr; | 140 | if(!ASN1_INTEGER_set(kdf->iter, iter)) goto merr; |
@@ -218,7 +147,7 @@ X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter, | |||
218 | /* If its RC2 then we'd better setup the key length */ | 147 | /* If its RC2 then we'd better setup the key length */ |
219 | 148 | ||
220 | if(alg_nid == NID_rc2_cbc) { | 149 | if(alg_nid == NID_rc2_cbc) { |
221 | if(!(kdf->keylength = ASN1_INTEGER_new())) goto merr; | 150 | if(!(kdf->keylength = M_ASN1_INTEGER_new())) goto merr; |
222 | if(!ASN1_INTEGER_set (kdf->keylength, | 151 | if(!ASN1_INTEGER_set (kdf->keylength, |
223 | EVP_CIPHER_key_length(cipher))) goto merr; | 152 | EVP_CIPHER_key_length(cipher))) goto merr; |
224 | } | 153 | } |
@@ -264,7 +193,7 @@ X509_ALGOR *PKCS5_pbe2_set(const EVP_CIPHER *cipher, int iter, | |||
264 | err: | 193 | err: |
265 | PBE2PARAM_free(pbe2); | 194 | PBE2PARAM_free(pbe2); |
266 | /* Note 'scheme' is freed as part of pbe2 */ | 195 | /* Note 'scheme' is freed as part of pbe2 */ |
267 | ASN1_OCTET_STRING_free(osalt); | 196 | M_ASN1_OCTET_STRING_free(osalt); |
268 | PBKDF2PARAM_free(kdf); | 197 | PBKDF2PARAM_free(kdf); |
269 | X509_ALGOR_free(kalg); | 198 | X509_ALGOR_free(kalg); |
270 | X509_ALGOR_free(ret); | 199 | X509_ALGOR_free(ret); |