diff options
Diffstat (limited to 'src/lib/libcrypto/dh/dh_ameth.c')
-rw-r--r-- | src/lib/libcrypto/dh/dh_ameth.c | 295 |
1 files changed, 146 insertions, 149 deletions
diff --git a/src/lib/libcrypto/dh/dh_ameth.c b/src/lib/libcrypto/dh/dh_ameth.c index 88ef78d98d..9e911c8779 100644 --- a/src/lib/libcrypto/dh/dh_ameth.c +++ b/src/lib/libcrypto/dh/dh_ameth.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: dh_ameth.c,v 1.8 2014/06/30 14:15:34 tedu Exp $ */ | 1 | /* $OpenBSD: dh_ameth.c,v 1.9 2014/07/09 13:26:47 miod Exp $ */ |
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 2006. | 3 | * project 2006. |
4 | */ | 4 | */ |
@@ -64,13 +64,15 @@ | |||
64 | #include <openssl/bn.h> | 64 | #include <openssl/bn.h> |
65 | #include "asn1_locl.h" | 65 | #include "asn1_locl.h" |
66 | 66 | ||
67 | static void int_dh_free(EVP_PKEY *pkey) | 67 | static void |
68 | { | 68 | int_dh_free(EVP_PKEY *pkey) |
69 | { | ||
69 | DH_free(pkey->pkey.dh); | 70 | DH_free(pkey->pkey.dh); |
70 | } | 71 | } |
71 | 72 | ||
72 | static int dh_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey) | 73 | static int |
73 | { | 74 | dh_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey) |
75 | { | ||
74 | const unsigned char *p, *pm; | 76 | const unsigned char *p, *pm; |
75 | int pklen, pmlen; | 77 | int pklen, pmlen; |
76 | int ptype; | 78 | int ptype; |
@@ -78,57 +80,52 @@ static int dh_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey) | |||
78 | ASN1_STRING *pstr; | 80 | ASN1_STRING *pstr; |
79 | X509_ALGOR *palg; | 81 | X509_ALGOR *palg; |
80 | ASN1_INTEGER *public_key = NULL; | 82 | ASN1_INTEGER *public_key = NULL; |
81 | |||
82 | DH *dh = NULL; | 83 | DH *dh = NULL; |
83 | 84 | ||
84 | if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey)) | 85 | if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey)) |
85 | return 0; | 86 | return 0; |
86 | X509_ALGOR_get0(NULL, &ptype, &pval, palg); | 87 | X509_ALGOR_get0(NULL, &ptype, &pval, palg); |
87 | 88 | ||
88 | if (ptype != V_ASN1_SEQUENCE) | 89 | if (ptype != V_ASN1_SEQUENCE) { |
89 | { | ||
90 | DHerr(DH_F_DH_PUB_DECODE, DH_R_PARAMETER_ENCODING_ERROR); | 90 | DHerr(DH_F_DH_PUB_DECODE, DH_R_PARAMETER_ENCODING_ERROR); |
91 | goto err; | 91 | goto err; |
92 | } | 92 | } |
93 | 93 | ||
94 | pstr = pval; | 94 | pstr = pval; |
95 | pm = pstr->data; | 95 | pm = pstr->data; |
96 | pmlen = pstr->length; | 96 | pmlen = pstr->length; |
97 | 97 | ||
98 | if (!(dh = d2i_DHparams(NULL, &pm, pmlen))) | 98 | if (!(dh = d2i_DHparams(NULL, &pm, pmlen))) { |
99 | { | ||
100 | DHerr(DH_F_DH_PUB_DECODE, DH_R_DECODE_ERROR); | 99 | DHerr(DH_F_DH_PUB_DECODE, DH_R_DECODE_ERROR); |
101 | goto err; | 100 | goto err; |
102 | } | 101 | } |
103 | 102 | ||
104 | if (!(public_key=d2i_ASN1_INTEGER(NULL, &p, pklen))) | 103 | if (!(public_key=d2i_ASN1_INTEGER(NULL, &p, pklen))) { |
105 | { | ||
106 | DHerr(DH_F_DH_PUB_DECODE, DH_R_DECODE_ERROR); | 104 | DHerr(DH_F_DH_PUB_DECODE, DH_R_DECODE_ERROR); |
107 | goto err; | 105 | goto err; |
108 | } | 106 | } |
109 | 107 | ||
110 | /* We have parameters now set public key */ | 108 | /* We have parameters now set public key */ |
111 | if (!(dh->pub_key = ASN1_INTEGER_to_BN(public_key, NULL))) | 109 | if (!(dh->pub_key = ASN1_INTEGER_to_BN(public_key, NULL))) { |
112 | { | ||
113 | DHerr(DH_F_DH_PUB_DECODE, DH_R_BN_DECODE_ERROR); | 110 | DHerr(DH_F_DH_PUB_DECODE, DH_R_BN_DECODE_ERROR); |
114 | goto err; | 111 | goto err; |
115 | } | 112 | } |
116 | 113 | ||
117 | ASN1_INTEGER_free(public_key); | 114 | ASN1_INTEGER_free(public_key); |
118 | EVP_PKEY_assign_DH(pkey, dh); | 115 | EVP_PKEY_assign_DH(pkey, dh); |
119 | return 1; | 116 | return 1; |
120 | 117 | ||
121 | err: | 118 | err: |
122 | if (public_key) | 119 | if (public_key) |
123 | ASN1_INTEGER_free(public_key); | 120 | ASN1_INTEGER_free(public_key); |
124 | if (dh) | 121 | if (dh) |
125 | DH_free(dh); | 122 | DH_free(dh); |
126 | return 0; | 123 | return 0; |
124 | } | ||
127 | 125 | ||
128 | } | 126 | static int |
129 | 127 | dh_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) | |
130 | static int dh_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) | 128 | { |
131 | { | ||
132 | DH *dh; | 129 | DH *dh; |
133 | int ptype; | 130 | int ptype; |
134 | unsigned char *penc = NULL; | 131 | unsigned char *penc = NULL; |
@@ -145,11 +142,10 @@ static int dh_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) | |||
145 | } | 142 | } |
146 | 143 | ||
147 | str->length = i2d_DHparams(dh, &str->data); | 144 | str->length = i2d_DHparams(dh, &str->data); |
148 | if (str->length <= 0) | 145 | if (str->length <= 0) { |
149 | { | ||
150 | DHerr(DH_F_DH_PUB_ENCODE, ERR_R_MALLOC_FAILURE); | 146 | DHerr(DH_F_DH_PUB_ENCODE, ERR_R_MALLOC_FAILURE); |
151 | goto err; | 147 | goto err; |
152 | } | 148 | } |
153 | ptype = V_ASN1_SEQUENCE; | 149 | ptype = V_ASN1_SEQUENCE; |
154 | 150 | ||
155 | pub_key = BN_to_ASN1_INTEGER(dh->pub_key, NULL); | 151 | pub_key = BN_to_ASN1_INTEGER(dh->pub_key, NULL); |
@@ -160,32 +156,32 @@ static int dh_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) | |||
160 | 156 | ||
161 | ASN1_INTEGER_free(pub_key); | 157 | ASN1_INTEGER_free(pub_key); |
162 | 158 | ||
163 | if (penclen <= 0) | 159 | if (penclen <= 0) { |
164 | { | ||
165 | DHerr(DH_F_DH_PUB_ENCODE, ERR_R_MALLOC_FAILURE); | 160 | DHerr(DH_F_DH_PUB_ENCODE, ERR_R_MALLOC_FAILURE); |
166 | goto err; | 161 | goto err; |
167 | } | 162 | } |
168 | 163 | ||
169 | if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_DH), | 164 | if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_DH), ptype, |
170 | ptype, (void *)str, penc, penclen)) | 165 | (void *)str, penc, penclen)) |
171 | return 1; | 166 | return 1; |
172 | 167 | ||
173 | err: | 168 | err: |
174 | free(penc); | 169 | free(penc); |
175 | if (str) | 170 | if (str) |
176 | ASN1_STRING_free(str); | 171 | ASN1_STRING_free(str); |
177 | 172 | ||
178 | return 0; | 173 | return 0; |
179 | } | 174 | } |
180 | |||
181 | 175 | ||
182 | /* PKCS#8 DH is defined in PKCS#11 of all places. It is similar to DH in | 176 | /* |
177 | * PKCS#8 DH is defined in PKCS#11 of all places. It is similar to DH in | ||
183 | * that the AlgorithmIdentifier contains the paramaters, the private key | 178 | * that the AlgorithmIdentifier contains the paramaters, the private key |
184 | * is explcitly included and the pubkey must be recalculated. | 179 | * is explcitly included and the pubkey must be recalculated. |
185 | */ | 180 | */ |
186 | 181 | ||
187 | static int dh_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8) | 182 | static int |
188 | { | 183 | dh_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8) |
184 | { | ||
189 | const unsigned char *p, *pm; | 185 | const unsigned char *p, *pm; |
190 | int pklen, pmlen; | 186 | int pklen, pmlen; |
191 | int ptype; | 187 | int ptype; |
@@ -193,7 +189,6 @@ static int dh_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8) | |||
193 | ASN1_STRING *pstr; | 189 | ASN1_STRING *pstr; |
194 | X509_ALGOR *palg; | 190 | X509_ALGOR *palg; |
195 | ASN1_INTEGER *privkey = NULL; | 191 | ASN1_INTEGER *privkey = NULL; |
196 | |||
197 | DH *dh = NULL; | 192 | DH *dh = NULL; |
198 | 193 | ||
199 | if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8)) | 194 | if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8)) |
@@ -202,23 +197,21 @@ static int dh_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8) | |||
202 | X509_ALGOR_get0(NULL, &ptype, &pval, palg); | 197 | X509_ALGOR_get0(NULL, &ptype, &pval, palg); |
203 | 198 | ||
204 | if (ptype != V_ASN1_SEQUENCE) | 199 | if (ptype != V_ASN1_SEQUENCE) |
205 | goto decerr; | 200 | goto decerr; |
206 | 201 | ||
207 | if (!(privkey=d2i_ASN1_INTEGER(NULL, &p, pklen))) | 202 | if (!(privkey=d2i_ASN1_INTEGER(NULL, &p, pklen))) |
208 | goto decerr; | 203 | goto decerr; |
209 | 204 | ||
210 | |||
211 | pstr = pval; | 205 | pstr = pval; |
212 | pm = pstr->data; | 206 | pm = pstr->data; |
213 | pmlen = pstr->length; | 207 | pmlen = pstr->length; |
214 | if (!(dh = d2i_DHparams(NULL, &pm, pmlen))) | 208 | if (!(dh = d2i_DHparams(NULL, &pm, pmlen))) |
215 | goto decerr; | 209 | goto decerr; |
216 | /* We have parameters now set private key */ | 210 | /* We have parameters now set private key */ |
217 | if (!(dh->priv_key = ASN1_INTEGER_to_BN(privkey, NULL))) | 211 | if (!(dh->priv_key = ASN1_INTEGER_to_BN(privkey, NULL))) { |
218 | { | 212 | DHerr(DH_F_DH_PRIV_DECODE, DH_R_BN_ERROR); |
219 | DHerr(DH_F_DH_PRIV_DECODE,DH_R_BN_ERROR); | ||
220 | goto dherr; | 213 | goto dherr; |
221 | } | 214 | } |
222 | /* Calculate public key */ | 215 | /* Calculate public key */ |
223 | if (!DH_generate_key(dh)) | 216 | if (!DH_generate_key(dh)) |
224 | goto dherr; | 217 | goto dherr; |
@@ -229,14 +222,15 @@ static int dh_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8) | |||
229 | 222 | ||
230 | return 1; | 223 | return 1; |
231 | 224 | ||
232 | decerr: | 225 | decerr: |
233 | DHerr(DH_F_DH_PRIV_DECODE, EVP_R_DECODE_ERROR); | 226 | DHerr(DH_F_DH_PRIV_DECODE, EVP_R_DECODE_ERROR); |
234 | dherr: | 227 | dherr: |
235 | DH_free(dh); | 228 | DH_free(dh); |
236 | return 0; | 229 | return 0; |
237 | } | 230 | } |
238 | 231 | ||
239 | static int dh_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey) | 232 | static int |
233 | dh_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey) | ||
240 | { | 234 | { |
241 | ASN1_STRING *params = NULL; | 235 | ASN1_STRING *params = NULL; |
242 | ASN1_INTEGER *prkey = NULL; | 236 | ASN1_INTEGER *prkey = NULL; |
@@ -245,35 +239,32 @@ static int dh_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey) | |||
245 | 239 | ||
246 | params = ASN1_STRING_new(); | 240 | params = ASN1_STRING_new(); |
247 | 241 | ||
248 | if (!params) | 242 | if (!params) { |
249 | { | 243 | DHerr(DH_F_DH_PRIV_ENCODE, ERR_R_MALLOC_FAILURE); |
250 | DHerr(DH_F_DH_PRIV_ENCODE,ERR_R_MALLOC_FAILURE); | ||
251 | goto err; | 244 | goto err; |
252 | } | 245 | } |
253 | 246 | ||
254 | params->length = i2d_DHparams(pkey->pkey.dh, ¶ms->data); | 247 | params->length = i2d_DHparams(pkey->pkey.dh, ¶ms->data); |
255 | if (params->length <= 0) | 248 | if (params->length <= 0) { |
256 | { | ||
257 | DHerr(DH_F_DH_PRIV_ENCODE,ERR_R_MALLOC_FAILURE); | 249 | DHerr(DH_F_DH_PRIV_ENCODE,ERR_R_MALLOC_FAILURE); |
258 | goto err; | 250 | goto err; |
259 | } | 251 | } |
260 | params->type = V_ASN1_SEQUENCE; | 252 | params->type = V_ASN1_SEQUENCE; |
261 | 253 | ||
262 | /* Get private key into integer */ | 254 | /* Get private key into integer */ |
263 | prkey = BN_to_ASN1_INTEGER(pkey->pkey.dh->priv_key, NULL); | 255 | prkey = BN_to_ASN1_INTEGER(pkey->pkey.dh->priv_key, NULL); |
264 | 256 | ||
265 | if (!prkey) | 257 | if (!prkey) { |
266 | { | 258 | DHerr(DH_F_DH_PRIV_ENCODE, DH_R_BN_ERROR); |
267 | DHerr(DH_F_DH_PRIV_ENCODE,DH_R_BN_ERROR); | ||
268 | goto err; | 259 | goto err; |
269 | } | 260 | } |
270 | 261 | ||
271 | dplen = i2d_ASN1_INTEGER(prkey, &dp); | 262 | dplen = i2d_ASN1_INTEGER(prkey, &dp); |
272 | 263 | ||
273 | ASN1_INTEGER_free(prkey); | 264 | ASN1_INTEGER_free(prkey); |
274 | 265 | ||
275 | if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_dhKeyAgreement), 0, | 266 | if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_dhKeyAgreement), 0, |
276 | V_ASN1_SEQUENCE, params, dp, dplen)) | 267 | V_ASN1_SEQUENCE, params, dp, dplen)) |
277 | goto err; | 268 | goto err; |
278 | 269 | ||
279 | return 1; | 270 | return 1; |
@@ -287,43 +278,43 @@ err: | |||
287 | return 0; | 278 | return 0; |
288 | } | 279 | } |
289 | 280 | ||
290 | 281 | static void | |
291 | static void update_buflen(const BIGNUM *b, size_t *pbuflen) | 282 | update_buflen(const BIGNUM *b, size_t *pbuflen) |
292 | { | 283 | { |
293 | size_t i; | 284 | size_t i; |
285 | |||
294 | if (!b) | 286 | if (!b) |
295 | return; | 287 | return; |
296 | if (*pbuflen < (i = (size_t)BN_num_bytes(b))) | 288 | if (*pbuflen < (i = (size_t)BN_num_bytes(b))) |
297 | *pbuflen = i; | 289 | *pbuflen = i; |
298 | } | 290 | } |
299 | 291 | ||
300 | static int dh_param_decode(EVP_PKEY *pkey, | 292 | static int |
301 | const unsigned char **pder, int derlen) | 293 | dh_param_decode(EVP_PKEY *pkey, const unsigned char **pder, int derlen) |
302 | { | 294 | { |
303 | DH *dh; | 295 | DH *dh; |
304 | if (!(dh = d2i_DHparams(NULL, pder, derlen))) | 296 | |
305 | { | 297 | if (!(dh = d2i_DHparams(NULL, pder, derlen))) { |
306 | DHerr(DH_F_DH_PARAM_DECODE, ERR_R_DH_LIB); | 298 | DHerr(DH_F_DH_PARAM_DECODE, ERR_R_DH_LIB); |
307 | return 0; | 299 | return 0; |
308 | } | 300 | } |
309 | EVP_PKEY_assign_DH(pkey, dh); | 301 | EVP_PKEY_assign_DH(pkey, dh); |
310 | return 1; | 302 | return 1; |
311 | } | 303 | } |
312 | 304 | ||
313 | static int dh_param_encode(const EVP_PKEY *pkey, unsigned char **pder) | 305 | static int |
314 | { | 306 | dh_param_encode(const EVP_PKEY *pkey, unsigned char **pder) |
307 | { | ||
315 | return i2d_DHparams(pkey->pkey.dh, pder); | 308 | return i2d_DHparams(pkey->pkey.dh, pder); |
316 | } | 309 | } |
317 | |||
318 | static int do_dh_print(BIO *bp, const DH *x, int indent, | ||
319 | ASN1_PCTX *ctx, int ptype) | ||
320 | { | ||
321 | unsigned char *m=NULL; | ||
322 | int reason=ERR_R_BUF_LIB,ret=0; | ||
323 | size_t buf_len=0; | ||
324 | 310 | ||
311 | static int | ||
312 | do_dh_print(BIO *bp, const DH *x, int indent, ASN1_PCTX *ctx, int ptype) | ||
313 | { | ||
314 | unsigned char *m = NULL; | ||
315 | int reason = ERR_R_BUF_LIB, ret = 0; | ||
316 | size_t buf_len = 0; | ||
325 | const char *ktype = NULL; | 317 | const char *ktype = NULL; |
326 | |||
327 | BIGNUM *priv_key, *pub_key; | 318 | BIGNUM *priv_key, *pub_key; |
328 | 319 | ||
329 | if (ptype == 2) | 320 | if (ptype == 2) |
@@ -338,11 +329,10 @@ static int do_dh_print(BIO *bp, const DH *x, int indent, | |||
338 | 329 | ||
339 | update_buflen(x->p, &buf_len); | 330 | update_buflen(x->p, &buf_len); |
340 | 331 | ||
341 | if (buf_len == 0) | 332 | if (buf_len == 0) { |
342 | { | ||
343 | reason = ERR_R_PASSED_NULL_PARAMETER; | 333 | reason = ERR_R_PASSED_NULL_PARAMETER; |
344 | goto err; | 334 | goto err; |
345 | } | 335 | } |
346 | 336 | ||
347 | update_buflen(x->g, &buf_len); | 337 | update_buflen(x->g, &buf_len); |
348 | update_buflen(pub_key, &buf_len); | 338 | update_buflen(pub_key, &buf_len); |
@@ -355,118 +345,126 @@ static int do_dh_print(BIO *bp, const DH *x, int indent, | |||
355 | else | 345 | else |
356 | ktype = "PKCS#3 DH Parameters"; | 346 | ktype = "PKCS#3 DH Parameters"; |
357 | 347 | ||
358 | m= malloc(buf_len+10); | 348 | m= malloc(buf_len + 10); |
359 | if (m == NULL) | 349 | if (m == NULL) { |
360 | { | 350 | reason = ERR_R_MALLOC_FAILURE; |
361 | reason=ERR_R_MALLOC_FAILURE; | ||
362 | goto err; | 351 | goto err; |
363 | } | 352 | } |
364 | 353 | ||
365 | BIO_indent(bp, indent, 128); | 354 | BIO_indent(bp, indent, 128); |
366 | if (BIO_printf(bp,"%s: (%d bit)\n", ktype, BN_num_bits(x->p)) <= 0) | 355 | if (BIO_printf(bp, "%s: (%d bit)\n", ktype, BN_num_bits(x->p)) <= 0) |
367 | goto err; | 356 | goto err; |
368 | indent += 4; | 357 | indent += 4; |
369 | 358 | ||
370 | if (!ASN1_bn_print(bp,"private-key:",priv_key,m,indent)) goto err; | 359 | if (!ASN1_bn_print(bp, "private-key:", priv_key, m, indent)) |
371 | if (!ASN1_bn_print(bp,"public-key:",pub_key,m,indent)) goto err; | 360 | goto err; |
361 | if (!ASN1_bn_print(bp, "public-key:", pub_key, m, indent)) | ||
362 | goto err; | ||
372 | 363 | ||
373 | if (!ASN1_bn_print(bp,"prime:",x->p,m,indent)) goto err; | 364 | if (!ASN1_bn_print(bp, "prime:", x->p, m, indent)) |
374 | if (!ASN1_bn_print(bp,"generator:",x->g,m,indent)) goto err; | 365 | goto err; |
375 | if (x->length != 0) | 366 | if (!ASN1_bn_print(bp, "generator:", x->g, m, indent)) |
376 | { | 367 | goto err; |
368 | if (x->length != 0) { | ||
377 | BIO_indent(bp, indent, 128); | 369 | BIO_indent(bp, indent, 128); |
378 | if (BIO_printf(bp,"recommended-private-length: %d bits\n", | 370 | if (BIO_printf(bp, "recommended-private-length: %d bits\n", |
379 | (int)x->length) <= 0) goto err; | 371 | (int)x->length) <= 0) |
380 | } | 372 | goto err; |
381 | 373 | } | |
382 | 374 | ||
383 | ret=1; | 375 | ret = 1; |
384 | if (0) | 376 | if (0) { |
385 | { | ||
386 | err: | 377 | err: |
387 | DHerr(DH_F_DO_DH_PRINT,reason); | 378 | DHerr(DH_F_DO_DH_PRINT,reason); |
388 | } | 379 | } |
389 | free(m); | 380 | free(m); |
390 | return(ret); | 381 | return(ret); |
391 | } | 382 | } |
392 | 383 | ||
393 | static int int_dh_size(const EVP_PKEY *pkey) | 384 | static int |
394 | { | 385 | int_dh_size(const EVP_PKEY *pkey) |
395 | return(DH_size(pkey->pkey.dh)); | 386 | { |
396 | } | 387 | return DH_size(pkey->pkey.dh); |
388 | } | ||
397 | 389 | ||
398 | static int dh_bits(const EVP_PKEY *pkey) | 390 | static int |
399 | { | 391 | dh_bits(const EVP_PKEY *pkey) |
392 | { | ||
400 | return BN_num_bits(pkey->pkey.dh->p); | 393 | return BN_num_bits(pkey->pkey.dh->p); |
401 | } | 394 | } |
402 | 395 | ||
403 | static int dh_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) | 396 | static int |
404 | { | 397 | dh_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) |
405 | if ( BN_cmp(a->pkey.dh->p,b->pkey.dh->p) || | 398 | { |
406 | BN_cmp(a->pkey.dh->g,b->pkey.dh->g)) | 399 | if (BN_cmp(a->pkey.dh->p, b->pkey.dh->p) || |
400 | BN_cmp(a->pkey.dh->g, b->pkey.dh->g)) | ||
407 | return 0; | 401 | return 0; |
408 | else | 402 | else |
409 | return 1; | 403 | return 1; |
410 | } | 404 | } |
411 | 405 | ||
412 | static int dh_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) | 406 | static int |
413 | { | 407 | dh_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) |
408 | { | ||
414 | BIGNUM *a; | 409 | BIGNUM *a; |
415 | 410 | ||
416 | if ((a=BN_dup(from->pkey.dh->p)) == NULL) | 411 | if ((a = BN_dup(from->pkey.dh->p)) == NULL) |
417 | return 0; | 412 | return 0; |
418 | if (to->pkey.dh->p != NULL) | 413 | if (to->pkey.dh->p != NULL) |
419 | BN_free(to->pkey.dh->p); | 414 | BN_free(to->pkey.dh->p); |
420 | to->pkey.dh->p=a; | 415 | to->pkey.dh->p = a; |
421 | 416 | ||
422 | if ((a=BN_dup(from->pkey.dh->g)) == NULL) | 417 | if ((a = BN_dup(from->pkey.dh->g)) == NULL) |
423 | return 0; | 418 | return 0; |
424 | if (to->pkey.dh->g != NULL) | 419 | if (to->pkey.dh->g != NULL) |
425 | BN_free(to->pkey.dh->g); | 420 | BN_free(to->pkey.dh->g); |
426 | to->pkey.dh->g=a; | 421 | to->pkey.dh->g = a; |
427 | 422 | ||
428 | return 1; | 423 | return 1; |
429 | } | 424 | } |
430 | 425 | ||
431 | static int dh_missing_parameters(const EVP_PKEY *a) | 426 | static int |
432 | { | 427 | dh_missing_parameters(const EVP_PKEY *a) |
428 | { | ||
433 | if (!a->pkey.dh->p || !a->pkey.dh->g) | 429 | if (!a->pkey.dh->p || !a->pkey.dh->g) |
434 | return 1; | 430 | return 1; |
435 | return 0; | 431 | return 0; |
436 | } | 432 | } |
437 | 433 | ||
438 | static int dh_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) | 434 | static int |
439 | { | 435 | dh_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) |
436 | { | ||
440 | if (dh_cmp_parameters(a, b) == 0) | 437 | if (dh_cmp_parameters(a, b) == 0) |
441 | return 0; | 438 | return 0; |
442 | if (BN_cmp(b->pkey.dh->pub_key,a->pkey.dh->pub_key) != 0) | 439 | if (BN_cmp(b->pkey.dh->pub_key, a->pkey.dh->pub_key) != 0) |
443 | return 0; | 440 | return 0; |
444 | else | 441 | else |
445 | return 1; | 442 | return 1; |
446 | } | 443 | } |
447 | 444 | ||
448 | static int dh_param_print(BIO *bp, const EVP_PKEY *pkey, int indent, | 445 | static int |
449 | ASN1_PCTX *ctx) | 446 | dh_param_print(BIO *bp, const EVP_PKEY *pkey, int indent, ASN1_PCTX *ctx) |
450 | { | 447 | { |
451 | return do_dh_print(bp, pkey->pkey.dh, indent, ctx, 0); | 448 | return do_dh_print(bp, pkey->pkey.dh, indent, ctx, 0); |
452 | } | 449 | } |
453 | 450 | ||
454 | static int dh_public_print(BIO *bp, const EVP_PKEY *pkey, int indent, | 451 | static int |
455 | ASN1_PCTX *ctx) | 452 | dh_public_print(BIO *bp, const EVP_PKEY *pkey, int indent, ASN1_PCTX *ctx) |
456 | { | 453 | { |
457 | return do_dh_print(bp, pkey->pkey.dh, indent, ctx, 1); | 454 | return do_dh_print(bp, pkey->pkey.dh, indent, ctx, 1); |
458 | } | 455 | } |
459 | 456 | ||
460 | static int dh_private_print(BIO *bp, const EVP_PKEY *pkey, int indent, | 457 | static int |
461 | ASN1_PCTX *ctx) | 458 | dh_private_print(BIO *bp, const EVP_PKEY *pkey, int indent, ASN1_PCTX *ctx) |
462 | { | 459 | { |
463 | return do_dh_print(bp, pkey->pkey.dh, indent, ctx, 2); | 460 | return do_dh_print(bp, pkey->pkey.dh, indent, ctx, 2); |
464 | } | 461 | } |
465 | 462 | ||
466 | int DHparams_print(BIO *bp, const DH *x) | 463 | int |
467 | { | 464 | DHparams_print(BIO *bp, const DH *x) |
465 | { | ||
468 | return do_dh_print(bp, x, 4, NULL, 0); | 466 | return do_dh_print(bp, x, 4, NULL, 0); |
469 | } | 467 | } |
470 | 468 | ||
471 | const EVP_PKEY_ASN1_METHOD dh_asn1_meth = { | 469 | const EVP_PKEY_ASN1_METHOD dh_asn1_meth = { |
472 | .pkey_id = EVP_PKEY_DH, | 470 | .pkey_id = EVP_PKEY_DH, |
@@ -496,4 +494,3 @@ const EVP_PKEY_ASN1_METHOD dh_asn1_meth = { | |||
496 | 494 | ||
497 | .pkey_free = int_dh_free, | 495 | .pkey_free = int_dh_free, |
498 | }; | 496 | }; |
499 | |||