diff options
Diffstat (limited to 'src/lib/libcrypto/asn1/p5_pbe.c')
-rw-r--r-- | src/lib/libcrypto/asn1/p5_pbe.c | 105 |
1 files changed, 61 insertions, 44 deletions
diff --git a/src/lib/libcrypto/asn1/p5_pbe.c b/src/lib/libcrypto/asn1/p5_pbe.c index c4582f8041..94bc38b99f 100644 --- a/src/lib/libcrypto/asn1/p5_pbe.c +++ b/src/lib/libcrypto/asn1/p5_pbe.c | |||
@@ -71,61 +71,78 @@ ASN1_SEQUENCE(PBEPARAM) = { | |||
71 | 71 | ||
72 | IMPLEMENT_ASN1_FUNCTIONS(PBEPARAM) | 72 | IMPLEMENT_ASN1_FUNCTIONS(PBEPARAM) |
73 | 73 | ||
74 | /* Return an algorithm identifier for a PKCS#5 PBE algorithm */ | ||
75 | 74 | ||
76 | X509_ALGOR *PKCS5_pbe_set(int alg, int iter, unsigned char *salt, | 75 | /* Set an algorithm identifier for a PKCS#5 PBE algorithm */ |
77 | int saltlen) | 76 | |
78 | { | 77 | int PKCS5_pbe_set0_algor(X509_ALGOR *algor, int alg, int iter, |
78 | const unsigned char *salt, int saltlen) | ||
79 | { | ||
79 | PBEPARAM *pbe=NULL; | 80 | PBEPARAM *pbe=NULL; |
80 | ASN1_OBJECT *al; | 81 | ASN1_STRING *pbe_str=NULL; |
81 | X509_ALGOR *algor; | 82 | unsigned char *sstr; |
82 | ASN1_TYPE *astype=NULL; | ||
83 | 83 | ||
84 | if (!(pbe = PBEPARAM_new ())) { | 84 | pbe = PBEPARAM_new(); |
85 | ASN1err(ASN1_F_PKCS5_PBE_SET,ERR_R_MALLOC_FAILURE); | 85 | if (!pbe) |
86 | { | ||
87 | ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR,ERR_R_MALLOC_FAILURE); | ||
86 | goto err; | 88 | goto err; |
87 | } | 89 | } |
88 | if(iter <= 0) iter = PKCS5_DEFAULT_ITER; | 90 | if(iter <= 0) |
89 | if (!ASN1_INTEGER_set(pbe->iter, iter)) { | 91 | iter = PKCS5_DEFAULT_ITER; |
90 | ASN1err(ASN1_F_PKCS5_PBE_SET,ERR_R_MALLOC_FAILURE); | 92 | if (!ASN1_INTEGER_set(pbe->iter, iter)) |
93 | { | ||
94 | ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR,ERR_R_MALLOC_FAILURE); | ||
91 | goto err; | 95 | goto err; |
92 | } | 96 | } |
93 | if (!saltlen) saltlen = PKCS5_SALT_LEN; | 97 | if (!saltlen) |
94 | if (!(pbe->salt->data = OPENSSL_malloc (saltlen))) { | 98 | saltlen = PKCS5_SALT_LEN; |
95 | ASN1err(ASN1_F_PKCS5_PBE_SET,ERR_R_MALLOC_FAILURE); | 99 | if (!ASN1_STRING_set(pbe->salt, NULL, saltlen)) |
100 | { | ||
101 | ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR,ERR_R_MALLOC_FAILURE); | ||
96 | goto err; | 102 | goto err; |
97 | } | 103 | } |
98 | pbe->salt->length = saltlen; | 104 | sstr = ASN1_STRING_data(pbe->salt); |
99 | if (salt) memcpy (pbe->salt->data, salt, saltlen); | 105 | if (salt) |
100 | else if (RAND_pseudo_bytes (pbe->salt->data, saltlen) < 0) | 106 | memcpy(sstr, salt, saltlen); |
107 | else if (RAND_pseudo_bytes(sstr, saltlen) < 0) | ||
101 | goto err; | 108 | goto err; |
102 | 109 | ||
103 | if (!(astype = ASN1_TYPE_new())) { | 110 | if(!ASN1_item_pack(pbe, ASN1_ITEM_rptr(PBEPARAM), &pbe_str)) |
104 | ASN1err(ASN1_F_PKCS5_PBE_SET,ERR_R_MALLOC_FAILURE); | 111 | { |
112 | ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR,ERR_R_MALLOC_FAILURE); | ||
105 | goto err; | 113 | goto err; |
106 | } | 114 | } |
107 | 115 | ||
108 | astype->type = V_ASN1_SEQUENCE; | 116 | PBEPARAM_free(pbe); |
109 | if(!ASN1_pack_string_of(PBEPARAM, pbe, i2d_PBEPARAM, | ||
110 | &astype->value.sequence)) { | ||
111 | ASN1err(ASN1_F_PKCS5_PBE_SET,ERR_R_MALLOC_FAILURE); | ||
112 | goto err; | ||
113 | } | ||
114 | PBEPARAM_free (pbe); | ||
115 | pbe = NULL; | 117 | pbe = NULL; |
116 | |||
117 | al = OBJ_nid2obj(alg); /* never need to free al */ | ||
118 | if (!(algor = X509_ALGOR_new())) { | ||
119 | ASN1err(ASN1_F_PKCS5_PBE_SET,ERR_R_MALLOC_FAILURE); | ||
120 | goto err; | ||
121 | } | ||
122 | ASN1_OBJECT_free(algor->algorithm); | ||
123 | algor->algorithm = al; | ||
124 | algor->parameter = astype; | ||
125 | 118 | ||
126 | return (algor); | 119 | if (X509_ALGOR_set0(algor, OBJ_nid2obj(alg), V_ASN1_SEQUENCE, pbe_str)) |
120 | return 1; | ||
121 | |||
127 | err: | 122 | err: |
128 | if (pbe != NULL) PBEPARAM_free(pbe); | 123 | if (pbe != NULL) |
129 | if (astype != NULL) ASN1_TYPE_free(astype); | 124 | PBEPARAM_free(pbe); |
125 | if (pbe_str != NULL) | ||
126 | ASN1_STRING_free(pbe_str); | ||
127 | return 0; | ||
128 | } | ||
129 | |||
130 | /* Return an algorithm identifier for a PKCS#5 PBE algorithm */ | ||
131 | |||
132 | X509_ALGOR *PKCS5_pbe_set(int alg, int iter, | ||
133 | const unsigned char *salt, int saltlen) | ||
134 | { | ||
135 | X509_ALGOR *ret; | ||
136 | ret = X509_ALGOR_new(); | ||
137 | if (!ret) | ||
138 | { | ||
139 | ASN1err(ASN1_F_PKCS5_PBE_SET,ERR_R_MALLOC_FAILURE); | ||
140 | return NULL; | ||
141 | } | ||
142 | |||
143 | if (PKCS5_pbe_set0_algor(ret, alg, iter, salt, saltlen)) | ||
144 | return ret; | ||
145 | |||
146 | X509_ALGOR_free(ret); | ||
130 | return NULL; | 147 | return NULL; |
131 | } | 148 | } |