095227bc2f85769209be3723e5b072f37392e357
[openssl.git] / crypto / dh / dh_pmeth.c
1 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
2  * project 2006.
3  */
4 /* ====================================================================
5  * Copyright (c) 2006 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@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@cryptsoft.com).  This product includes software written by Tim
54  * Hudson (tjh@cryptsoft.com).
55  *
56  */
57
58 #include <stdio.h>
59 #include "cryptlib.h"
60 #include <openssl/asn1t.h>
61 #include <openssl/x509.h>
62 #include <openssl/evp.h>
63 #include <openssl/dh.h>
64 #include <openssl/bn.h>
65 #ifndef OPENSSL_NO_DSA
66 #include <openssl/dsa.h>
67 #endif
68 #include "evp_locl.h"
69
70 /* DH pkey context structure */
71
72 typedef struct
73         {
74         /* Parameter gen parameters */
75         int prime_len;
76         int generator;
77         int use_dsa;
78         int subprime_len;
79         const EVP_MD *md;
80         int rfc5114_param;
81         /* Keygen callback info */
82         int gentmp[2];
83         /* message digest */
84         } DH_PKEY_CTX;
85
86 static int pkey_dh_init(EVP_PKEY_CTX *ctx)
87         {
88         DH_PKEY_CTX *dctx;
89         dctx = OPENSSL_malloc(sizeof(DH_PKEY_CTX));
90         if (!dctx)
91                 return 0;
92         dctx->prime_len = 1024;
93         dctx->subprime_len = -1;
94         dctx->generator = 2;
95         dctx->use_dsa = 0;
96         dctx->md = NULL;
97         dctx->rfc5114_param = 0;
98
99         ctx->data = dctx;
100         ctx->keygen_info = dctx->gentmp;
101         ctx->keygen_info_count = 2;
102         
103         return 1;
104         }
105
106 static int pkey_dh_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
107         {
108         DH_PKEY_CTX *dctx, *sctx;
109         if (!pkey_dh_init(dst))
110                 return 0;
111         sctx = src->data;
112         dctx = dst->data;
113         dctx->prime_len = sctx->prime_len;
114         dctx->subprime_len = sctx->subprime_len;
115         dctx->generator = sctx->generator;
116         dctx->use_dsa = sctx->use_dsa;
117         dctx->md = sctx->md;
118         dctx->rfc5114_param = sctx->rfc5114_param;
119         return 1;
120         }
121
122 static void pkey_dh_cleanup(EVP_PKEY_CTX *ctx)
123         {
124         DH_PKEY_CTX *dctx = ctx->data;
125         if (dctx)
126                 OPENSSL_free(dctx);
127         }
128
129 static int pkey_dh_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
130         {
131         DH_PKEY_CTX *dctx = ctx->data;
132         switch (type)
133                 {
134                 case EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN:
135                 if (p1 < 256)
136                         return -2;
137                 dctx->prime_len = p1;
138                 return 1;
139
140                 case EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN:
141                 if (dctx->use_dsa == 0)
142                         return -2;
143                 dctx->subprime_len = p1;
144                 return 1;
145
146                 case EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR:
147                 if (dctx->use_dsa)
148                         return -2;
149                 dctx->generator = p1;
150                 return 1;
151
152                 case EVP_PKEY_CTRL_DH_PARAMGEN_TYPE:
153 #ifdef OPENSSL_NO_DSA
154                 if (p1 != 0)
155                         return -2;
156 #else
157                 if (p1 < 0 || p1 > 2)
158                         return -2;
159 #endif
160                 dctx->use_dsa = p1;
161                 return 1;
162
163                 case EVP_PKEY_CTRL_DH_RFC5114:
164                 if (p1 < 1 || p1 > 3)
165                         return -2;
166                 dctx->rfc5114_param = p1;
167                 return 1;
168
169                 case EVP_PKEY_CTRL_PEER_KEY:
170                 /* Default behaviour is OK */
171                 return 1;
172
173                 default:
174                 return -2;
175
176                 }
177         }
178
179
180 static int pkey_dh_ctrl_str(EVP_PKEY_CTX *ctx,
181                         const char *type, const char *value)
182         {
183         if (!strcmp(type, "dh_paramgen_prime_len"))
184                 {
185                 int len;
186                 len = atoi(value);
187                 return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, len);
188                 }
189         if (!strcmp(type, "dh_rfc5114"))
190                 {
191                 DH_PKEY_CTX *dctx = ctx->data;
192                 int len;
193                 len = atoi(value);
194                 if (len < 0 || len > 3)
195                         return -2;
196                 dctx->rfc5114_param = len;
197                 return 1;
198                 }
199         if (!strcmp(type, "dh_paramgen_generator"))
200                 {
201                 int len;
202                 len = atoi(value);
203                 return EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, len);
204                 }
205         if (!strcmp(type, "dh_paramgen_subprime_len"))
206                 {
207                 int len;
208                 len = atoi(value);
209                 return EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx, len);
210                 }
211         if (!strcmp(type, "dh_paramgen_type"))
212                 {
213                 int typ;
214                 typ = atoi(value);
215                 return EVP_PKEY_CTX_set_dh_paramgen_type(ctx, typ);
216                 }
217         return -2;
218         }
219
220 #ifndef OPENSSL_NO_DSA
221
222 extern int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits,
223         const EVP_MD *evpmd, const unsigned char *seed_in, size_t seed_len,
224         unsigned char *seed_out,
225         int *counter_ret, unsigned long *h_ret, BN_GENCB *cb);
226
227 extern int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,
228         const EVP_MD *evpmd, const unsigned char *seed_in, size_t seed_len,
229         int idx, unsigned char *seed_out,
230         int *counter_ret, unsigned long *h_ret, BN_GENCB *cb);
231
232 static DSA *dsa_dh_generate(DH_PKEY_CTX *dctx, BN_GENCB *pcb)
233         {
234         DSA *ret;
235         int rv = 0;
236         int prime_len = dctx->prime_len;
237         int subprime_len = dctx->subprime_len;
238         const EVP_MD *md = dctx->md;
239         if (dctx->use_dsa > 2)
240                 return NULL;
241         ret = DSA_new();
242         if (!ret)
243                 return NULL;
244         if (subprime_len == -1)
245                 {
246                 if (prime_len >= 2048)
247                         subprime_len = 256;
248                 else
249                         subprime_len = 160;
250                 }
251         if (md == NULL)
252                 {
253                 if (prime_len >= 2048)
254                         md = EVP_sha256();
255                 else
256                         md = EVP_sha1();
257                 }
258         if (dctx->use_dsa == 1)
259                 rv = dsa_builtin_paramgen(ret, prime_len, subprime_len, md,
260                                                 NULL, 0, NULL, NULL, NULL, pcb);
261         else if(dctx->use_dsa == 2)
262                 rv = dsa_builtin_paramgen2(ret, prime_len, subprime_len, md,
263                                            NULL, 0, -1, NULL, NULL, NULL, pcb);
264         if (rv <= 0)
265                 {
266                 DSA_free(ret);
267                 return NULL;
268                 }
269         return ret;
270         }
271
272 #endif
273
274 static int pkey_dh_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
275         {
276         DH *dh = NULL;
277         DH_PKEY_CTX *dctx = ctx->data;
278         BN_GENCB *pcb, cb;
279         int ret;
280         if (dctx->rfc5114_param)
281                 {
282                 switch (dctx->rfc5114_param)
283                         {
284                         case 1:
285                         dh = DH_get_1024_160();
286                         break;
287
288                         case 2:
289                         dh = DH_get_2048_224();
290                         break;
291
292                         case 3:
293                         dh = DH_get_2048_256();
294                         break;
295         
296                         default:
297                         return -2;
298                         }
299                 EVP_PKEY_assign(pkey, EVP_PKEY_DHX, dh);
300                 return 1;
301                 }
302
303         if (ctx->pkey_gencb)
304                 {
305                 pcb = &cb;
306                 evp_pkey_set_cb_translate(pcb, ctx);
307                 }
308         else
309                 pcb = NULL;
310 #ifndef OPENSSL_NO_DSA
311         if (dctx->use_dsa)
312                 {
313                 DSA *dsa_dh;
314                 dsa_dh = dsa_dh_generate(dctx, pcb);
315                 if (!dsa_dh)
316                         return 0;
317                 dh = DSA_dup_DH(dsa_dh);
318                 DSA_free(dsa_dh);
319                 if (!dh)
320                         return 0;
321                 EVP_PKEY_assign(pkey, EVP_PKEY_DHX, dh);
322                 return 1;
323                 }
324 #endif
325         dh = DH_new();
326         if (!dh)
327                 return 0;
328         ret = DH_generate_parameters_ex(dh,
329                                         dctx->prime_len, dctx->generator, pcb);
330                 
331         if (ret)
332                 EVP_PKEY_assign_DH(pkey, dh);
333         else
334                 DH_free(dh);
335         return ret;
336         }
337
338 static int pkey_dh_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
339         {
340         DH *dh = NULL;
341         if (ctx->pkey == NULL)
342                 {
343                 DHerr(DH_F_PKEY_DH_KEYGEN, DH_R_NO_PARAMETERS_SET);
344                 return 0;
345                 }
346         dh = DH_new();
347         if (!dh)
348                 return 0;
349         EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, dh);
350         /* Note: if error return, pkey is freed by parent routine */
351         if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
352                 return 0;
353         return DH_generate_key(pkey->pkey.dh);
354         }
355
356 static int pkey_dh_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
357         {
358         int ret;
359         if (!ctx->pkey || !ctx->peerkey)
360                 {
361                 DHerr(DH_F_PKEY_DH_DERIVE, DH_R_KEYS_NOT_SET);
362                 return 0;
363                 }
364         ret = DH_compute_key(key, ctx->peerkey->pkey.dh->pub_key,
365                                                         ctx->pkey->pkey.dh);
366         if (ret < 0)
367                 return ret;
368         *keylen = ret;
369         return 1;
370         }
371
372 const EVP_PKEY_METHOD dh_pkey_meth = 
373         {
374         EVP_PKEY_DH,
375         EVP_PKEY_FLAG_AUTOARGLEN,
376         pkey_dh_init,
377         pkey_dh_copy,
378         pkey_dh_cleanup,
379
380         0,
381         pkey_dh_paramgen,
382
383         0,
384         pkey_dh_keygen,
385
386         0,
387         0,
388
389         0,
390         0,
391
392         0,0,
393
394         0,0,0,0,
395
396         0,0,
397
398         0,0,
399
400         0,
401         pkey_dh_derive,
402
403         pkey_dh_ctrl,
404         pkey_dh_ctrl_str
405
406         };
407
408 const EVP_PKEY_METHOD dhx_pkey_meth = 
409         {
410         EVP_PKEY_DHX,
411         EVP_PKEY_FLAG_AUTOARGLEN,
412         pkey_dh_init,
413         pkey_dh_copy,
414         pkey_dh_cleanup,
415
416         0,
417         pkey_dh_paramgen,
418
419         0,
420         pkey_dh_keygen,
421
422         0,
423         0,
424
425         0,
426         0,
427
428         0,0,
429
430         0,0,0,0,
431
432         0,0,
433
434         0,0,
435
436         0,
437         pkey_dh_derive,
438
439         pkey_dh_ctrl,
440         pkey_dh_ctrl_str
441
442         };