Merge from vendor branch LESS:
[dragonfly.git] / contrib / bind-9.3 / lib / dns / openssldsa_link.c
1 /*
2  * Portions Copyright (C) 2004, 2006  Internet Systems Consortium, Inc. ("ISC")
3  * Portions Copyright (C) 1999-2002  Internet Software Consortium.
4  * Portions Copyright (C) 1995-2000 by Network Associates, Inc.
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
11  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12  * WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE
13  * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
16  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18
19 /* $Id: openssldsa_link.c,v 1.1.4.3 2006/03/02 00:37:20 marka Exp $ */
20
21 #ifdef OPENSSL
22
23 #include <config.h>
24
25 #include <string.h>
26
27 #include <isc/entropy.h>
28 #include <isc/mem.h>
29 #include <isc/sha1.h>
30 #include <isc/util.h>
31
32 #include <dst/result.h>
33
34 #include "dst_internal.h"
35 #include "dst_openssl.h"
36 #include "dst_parse.h"
37
38 #include <openssl/dsa.h>
39
40 static isc_result_t openssldsa_todns(const dst_key_t *key, isc_buffer_t *data);
41
42 static isc_result_t
43 openssldsa_createctx(dst_key_t *key, dst_context_t *dctx) {
44         isc_sha1_t *sha1ctx;
45
46         UNUSED(key);
47
48         sha1ctx = isc_mem_get(dctx->mctx, sizeof(isc_sha1_t));
49         isc_sha1_init(sha1ctx);
50         dctx->opaque = sha1ctx;
51         return (ISC_R_SUCCESS);
52 }
53
54 static void
55 openssldsa_destroyctx(dst_context_t *dctx) {
56         isc_sha1_t *sha1ctx = dctx->opaque;
57
58         if (sha1ctx != NULL) {
59                 isc_sha1_invalidate(sha1ctx);
60                 isc_mem_put(dctx->mctx, sha1ctx, sizeof(isc_sha1_t));
61                 dctx->opaque = NULL;
62         }
63 }
64
65 static isc_result_t
66 openssldsa_adddata(dst_context_t *dctx, const isc_region_t *data) {
67         isc_sha1_t *sha1ctx = dctx->opaque;
68
69         isc_sha1_update(sha1ctx, data->base, data->length);
70         return (ISC_R_SUCCESS);
71 }
72
73 static int
74 BN_bn2bin_fixed(BIGNUM *bn, unsigned char *buf, int size) {
75         int bytes = size - BN_num_bytes(bn);
76         while (bytes-- > 0)
77                 *buf++ = 0;
78         BN_bn2bin(bn, buf);
79         return (size);
80 }
81
82 static isc_result_t
83 openssldsa_sign(dst_context_t *dctx, isc_buffer_t *sig) {
84         isc_sha1_t *sha1ctx = dctx->opaque;
85         dst_key_t *key = dctx->key;
86         DSA *dsa = key->opaque;
87         DSA_SIG *dsasig;
88         isc_region_t r;
89         unsigned char digest[ISC_SHA1_DIGESTLENGTH];
90
91         isc_buffer_availableregion(sig, &r);
92         if (r.length < ISC_SHA1_DIGESTLENGTH * 2 + 1)
93                 return (ISC_R_NOSPACE);
94
95         isc_sha1_final(sha1ctx, digest);
96
97         dsasig = DSA_do_sign(digest, ISC_SHA1_DIGESTLENGTH, dsa);
98         if (dsasig == NULL)
99                 return (dst__openssl_toresult(DST_R_SIGNFAILURE));
100
101         *r.base++ = (key->key_size - 512)/64;
102         BN_bn2bin_fixed(dsasig->r, r.base, ISC_SHA1_DIGESTLENGTH);
103         r.base += ISC_SHA1_DIGESTLENGTH;
104         BN_bn2bin_fixed(dsasig->s, r.base, ISC_SHA1_DIGESTLENGTH);
105         r.base += ISC_SHA1_DIGESTLENGTH;
106         DSA_SIG_free(dsasig);
107         isc_buffer_add(sig, ISC_SHA1_DIGESTLENGTH * 2 + 1);
108
109         return (ISC_R_SUCCESS);
110 }
111
112 static isc_result_t
113 openssldsa_verify(dst_context_t *dctx, const isc_region_t *sig) {
114         isc_sha1_t *sha1ctx = dctx->opaque;
115         dst_key_t *key = dctx->key;
116         DSA *dsa = key->opaque;
117         DSA_SIG *dsasig;
118         int status = 0;
119         unsigned char digest[ISC_SHA1_DIGESTLENGTH];
120         unsigned char *cp = sig->base;
121
122         isc_sha1_final(sha1ctx, digest);
123
124         if (sig->length < 2 * ISC_SHA1_DIGESTLENGTH + 1)
125                 return (DST_R_VERIFYFAILURE);
126
127         cp++;   /* Skip T */
128         dsasig = DSA_SIG_new();
129         dsasig->r = BN_bin2bn(cp, ISC_SHA1_DIGESTLENGTH, NULL);
130         cp += ISC_SHA1_DIGESTLENGTH;
131         dsasig->s = BN_bin2bn(cp, ISC_SHA1_DIGESTLENGTH, NULL);
132         cp += ISC_SHA1_DIGESTLENGTH;
133
134         status = DSA_do_verify(digest, ISC_SHA1_DIGESTLENGTH, dsasig, dsa);
135         DSA_SIG_free(dsasig);
136         if (status == 0)
137                 return (dst__openssl_toresult(DST_R_VERIFYFAILURE));
138
139         return (ISC_R_SUCCESS);
140 }
141
142 static isc_boolean_t
143 openssldsa_compare(const dst_key_t *key1, const dst_key_t *key2) {
144         int status;
145         DSA *dsa1, *dsa2;
146
147         dsa1 = (DSA *) key1->opaque;
148         dsa2 = (DSA *) key2->opaque;
149
150         if (dsa1 == NULL && dsa2 == NULL)
151                 return (ISC_TRUE);
152         else if (dsa1 == NULL || dsa2 == NULL)
153                 return (ISC_FALSE);
154
155         status = BN_cmp(dsa1->p, dsa2->p) ||
156                  BN_cmp(dsa1->q, dsa2->q) ||
157                  BN_cmp(dsa1->g, dsa2->g) ||
158                  BN_cmp(dsa1->pub_key, dsa2->pub_key);
159
160         if (status != 0)
161                 return (ISC_FALSE);
162
163         if (dsa1->priv_key != NULL || dsa2->priv_key != NULL) {
164                 if (dsa1->priv_key == NULL || dsa2->priv_key == NULL)
165                         return (ISC_FALSE);
166                 if (BN_cmp(dsa1->priv_key, dsa2->priv_key))
167                         return (ISC_FALSE);
168         }
169         return (ISC_TRUE);
170 }
171
172 #ifndef HAVE_DSA_GENERATE_PARAMETERS
173 /* ====================================================================
174  * Copyright (c) 1998-2002 The OpenSSL Project.  All rights reserved.
175  *
176  * Redistribution and use in source and binary forms, with or without
177  * modification, are permitted provided that the following conditions
178  * are met:
179  *
180  * 1. Redistributions of source code must retain the above copyright
181  *    notice, this list of conditions and the following disclaimer.
182  *
183  * 2. Redistributions in binary form must reproduce the above copyright
184  *    notice, this list of conditions and the following disclaimer in
185  *    the documentation and/or other materials provided with the
186  *    distribution.
187  *
188  * 3. All advertising materials mentioning features or use of this
189  *    software must display the following acknowledgment:
190  *    "This product includes software developed by the OpenSSL Project
191  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
192  *
193  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
194  *    endorse or promote products derived from this software without
195  *    prior written permission. For written permission, please contact
196  *    openssl-core@openssl.org.
197  *
198  * 5. Products derived from this software may not be called "OpenSSL"
199  *    nor may "OpenSSL" appear in their names without prior written
200  *    permission of the OpenSSL Project.
201  *
202  * 6. Redistributions of any form whatsoever must retain the following
203  *    acknowledgment:
204  *    "This product includes software developed by the OpenSSL Project
205  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
206  *
207  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
208  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
209  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
210  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
211  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
213  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
214  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
215  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
216  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
217  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
218  * OF THE POSSIBILITY OF SUCH DAMAGE.
219  * ====================================================================
220  *
221  * This product includes cryptographic software written by Eric Young
222  * (eay@cryptsoft.com).  This product includes software written by Tim
223  * Hudson (tjh@cryptsoft.com).
224  *
225  */
226 static DSA *
227 DSA_generate_parameters(int bits, unsigned char *seed_in, int seed_len,
228                         int *counter_ret, unsigned long *h_ret,
229                         void (*callback)(int, int, void *),
230                         void *cb_arg)
231 {
232         BN_GENCB cb;
233         DSA *dsa;
234  
235         dsa = DSA_new();
236         if (dsa != NULL) {
237  
238                 BN_GENCB_set_old(&cb, callback, cb_arg);
239   
240                 if (DSA_generate_parameters_ex(dsa, bits, seed_in, seed_len,  
241                                                counter_ret, h_ret, &cb))
242                         return (dsa);
243                 DSA_free(dsa);
244         }
245         return (NULL);
246 }
247 #endif
248
249 static isc_result_t
250 openssldsa_generate(dst_key_t *key, int unused) {
251         DSA *dsa;
252         unsigned char rand_array[ISC_SHA1_DIGESTLENGTH];
253         isc_result_t result;
254
255         UNUSED(unused);
256
257         result = dst__entropy_getdata(rand_array, sizeof(rand_array),
258                                       ISC_FALSE);
259         if (result != ISC_R_SUCCESS)
260                 return (result);
261
262         dsa = DSA_generate_parameters(key->key_size, rand_array,
263                                       ISC_SHA1_DIGESTLENGTH, NULL, NULL,
264                                       NULL, NULL);
265
266         if (dsa == NULL)
267                 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
268
269         if (DSA_generate_key(dsa) == 0) {
270                 DSA_free(dsa);
271                 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
272         }
273         dsa->flags &= ~DSA_FLAG_CACHE_MONT_P;
274
275         key->opaque = dsa;
276
277         return (ISC_R_SUCCESS);
278 }
279
280 static isc_boolean_t
281 openssldsa_isprivate(const dst_key_t *key) {
282         DSA *dsa = (DSA *) key->opaque;
283         return (ISC_TF(dsa != NULL && dsa->priv_key != NULL));
284 }
285
286 static void
287 openssldsa_destroy(dst_key_t *key) {
288         DSA *dsa = key->opaque;
289         DSA_free(dsa);
290         key->opaque = NULL;
291 }
292
293
294 static isc_result_t
295 openssldsa_todns(const dst_key_t *key, isc_buffer_t *data) {
296         DSA *dsa;
297         isc_region_t r;
298         int dnslen;
299         unsigned int t, p_bytes;
300
301         REQUIRE(key->opaque != NULL);
302
303         dsa = (DSA *) key->opaque;
304
305         isc_buffer_availableregion(data, &r);
306
307         t = (BN_num_bytes(dsa->p) - 64) / 8;
308         if (t > 8)
309                 return (DST_R_INVALIDPUBLICKEY);
310         p_bytes = 64 + 8 * t;
311
312         dnslen = 1 + (key->key_size * 3)/8 + ISC_SHA1_DIGESTLENGTH;
313         if (r.length < (unsigned int) dnslen)
314                 return (ISC_R_NOSPACE);
315
316         *r.base++ = t;
317         BN_bn2bin_fixed(dsa->q, r.base, ISC_SHA1_DIGESTLENGTH);
318         r.base += ISC_SHA1_DIGESTLENGTH;
319         BN_bn2bin_fixed(dsa->p, r.base, key->key_size/8);
320         r.base += p_bytes;
321         BN_bn2bin_fixed(dsa->g, r.base, key->key_size/8);
322         r.base += p_bytes;
323         BN_bn2bin_fixed(dsa->pub_key, r.base, key->key_size/8);
324         r.base += p_bytes;
325
326         isc_buffer_add(data, dnslen);
327
328         return (ISC_R_SUCCESS);
329 }
330
331 static isc_result_t
332 openssldsa_fromdns(dst_key_t *key, isc_buffer_t *data) {
333         DSA *dsa;
334         isc_region_t r;
335         unsigned int t, p_bytes;
336         isc_mem_t *mctx = key->mctx;
337
338         UNUSED(mctx);
339
340         isc_buffer_remainingregion(data, &r);
341         if (r.length == 0)
342                 return (ISC_R_SUCCESS);
343
344         dsa = DSA_new();
345         if (dsa == NULL)
346                 return (ISC_R_NOMEMORY);
347         dsa->flags &= ~DSA_FLAG_CACHE_MONT_P;
348
349         t = (unsigned int) *r.base++;
350         if (t > 8) {
351                 DSA_free(dsa);
352                 return (DST_R_INVALIDPUBLICKEY);
353         }
354         p_bytes = 64 + 8 * t;
355
356         if (r.length < 1 + ISC_SHA1_DIGESTLENGTH + 3 * p_bytes) {
357                 DSA_free(dsa);
358                 return (DST_R_INVALIDPUBLICKEY);
359         }
360
361         dsa->q = BN_bin2bn(r.base, ISC_SHA1_DIGESTLENGTH, NULL);
362         r.base += ISC_SHA1_DIGESTLENGTH;
363
364         dsa->p = BN_bin2bn(r.base, p_bytes, NULL);
365         r.base += p_bytes;
366
367         dsa->g = BN_bin2bn(r.base, p_bytes, NULL);
368         r.base += p_bytes;
369
370         dsa->pub_key = BN_bin2bn(r.base, p_bytes, NULL);
371         r.base += p_bytes;
372
373         key->key_size = p_bytes * 8;
374
375         isc_buffer_forward(data, 1 + ISC_SHA1_DIGESTLENGTH + 3 * p_bytes);
376
377         key->opaque = (void *) dsa;
378
379         return (ISC_R_SUCCESS);
380 }
381
382
383 static isc_result_t
384 openssldsa_tofile(const dst_key_t *key, const char *directory) {
385         int cnt = 0;
386         DSA *dsa;
387         dst_private_t priv;
388         unsigned char bufs[5][128];
389
390         if (key->opaque == NULL)
391                 return (DST_R_NULLKEY);
392
393         dsa = (DSA *) key->opaque;
394
395         priv.elements[cnt].tag = TAG_DSA_PRIME;
396         priv.elements[cnt].length = BN_num_bytes(dsa->p);
397         BN_bn2bin(dsa->p, bufs[cnt]);
398         priv.elements[cnt].data = bufs[cnt];
399         cnt++;
400
401         priv.elements[cnt].tag = TAG_DSA_SUBPRIME;
402         priv.elements[cnt].length = BN_num_bytes(dsa->q);
403         BN_bn2bin(dsa->q, bufs[cnt]);
404         priv.elements[cnt].data = bufs[cnt];
405         cnt++;
406
407         priv.elements[cnt].tag = TAG_DSA_BASE;
408         priv.elements[cnt].length = BN_num_bytes(dsa->g);
409         BN_bn2bin(dsa->g, bufs[cnt]);
410         priv.elements[cnt].data = bufs[cnt];
411         cnt++;
412
413         priv.elements[cnt].tag = TAG_DSA_PRIVATE;
414         priv.elements[cnt].length = BN_num_bytes(dsa->priv_key);
415         BN_bn2bin(dsa->priv_key, bufs[cnt]);
416         priv.elements[cnt].data = bufs[cnt];
417         cnt++;
418
419         priv.elements[cnt].tag = TAG_DSA_PUBLIC;
420         priv.elements[cnt].length = BN_num_bytes(dsa->pub_key);
421         BN_bn2bin(dsa->pub_key, bufs[cnt]);
422         priv.elements[cnt].data = bufs[cnt];
423         cnt++;
424
425         priv.nelements = cnt;
426         return (dst__privstruct_writefile(key, &priv, directory));
427 }
428
429 static isc_result_t
430 openssldsa_parse(dst_key_t *key, isc_lex_t *lexer) {
431         dst_private_t priv;
432         isc_result_t ret;
433         int i;
434         DSA *dsa = NULL;
435         isc_mem_t *mctx = key->mctx;
436 #define DST_RET(a) {ret = a; goto err;}
437
438         /* read private key file */
439         ret = dst__privstruct_parse(key, DST_ALG_DSA, lexer, mctx, &priv);
440         if (ret != ISC_R_SUCCESS)
441                 return (ret);
442
443         dsa = DSA_new();
444         if (dsa == NULL)
445                 DST_RET(ISC_R_NOMEMORY);
446         dsa->flags &= ~DSA_FLAG_CACHE_MONT_P;
447         key->opaque = dsa;
448
449         for (i=0; i < priv.nelements; i++) {
450                 BIGNUM *bn;
451                 bn = BN_bin2bn(priv.elements[i].data,
452                                priv.elements[i].length, NULL);
453                 if (bn == NULL)
454                         DST_RET(ISC_R_NOMEMORY);
455
456                 switch (priv.elements[i].tag) {
457                         case TAG_DSA_PRIME:
458                                 dsa->p = bn;
459                                 break;
460                         case TAG_DSA_SUBPRIME:
461                                 dsa->q = bn;
462                                 break;
463                         case TAG_DSA_BASE:
464                                 dsa->g = bn;
465                                 break;
466                         case TAG_DSA_PRIVATE:
467                                 dsa->priv_key = bn;
468                                 break;
469                         case TAG_DSA_PUBLIC:
470                                 dsa->pub_key = bn;
471                                 break;
472                 }
473         }
474         dst__privstruct_free(&priv, mctx);
475
476         key->key_size = BN_num_bits(dsa->p);
477
478         return (ISC_R_SUCCESS);
479
480  err:
481         openssldsa_destroy(key);
482         dst__privstruct_free(&priv, mctx);
483         memset(&priv, 0, sizeof(priv));
484         return (ret);
485 }
486
487 static dst_func_t openssldsa_functions = {
488         openssldsa_createctx,
489         openssldsa_destroyctx,
490         openssldsa_adddata,
491         openssldsa_sign,
492         openssldsa_verify,
493         NULL, /* computesecret */
494         openssldsa_compare,
495         NULL, /* paramcompare */
496         openssldsa_generate,
497         openssldsa_isprivate,
498         openssldsa_destroy,
499         openssldsa_todns,
500         openssldsa_fromdns,
501         openssldsa_tofile,
502         openssldsa_parse,
503         NULL, /* cleanup */
504 };
505
506 isc_result_t
507 dst__openssldsa_init(dst_func_t **funcp) {
508         REQUIRE(funcp != NULL);
509         if (*funcp == NULL)
510                 *funcp = &openssldsa_functions;
511         return (ISC_R_SUCCESS);
512 }
513
514 #else /* OPENSSL */
515
516 #include <isc/util.h>
517
518 EMPTY_TRANSLATION_UNIT
519
520 #endif /* OPENSSL */