Revert "usbd - Do not start moused by default when a usb mouse is connected"
[dragonfly.git] / contrib / bind-9.3 / lib / dns / openssldh_link.c
blob74ba39af3612dd274817c824cfbd83260ef9eed3
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.
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.
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.
20 * Principal Author: Brian Wellington
21 * $Id: openssldh_link.c,v 1.1.4.3 2006/03/02 00:37:20 marka Exp $
24 #ifdef OPENSSL
26 #include <config.h>
28 #include <ctype.h>
30 #include <isc/mem.h>
31 #include <isc/string.h>
32 #include <isc/util.h>
34 #include <dst/result.h>
36 #include "dst_internal.h"
37 #include "dst_openssl.h"
38 #include "dst_parse.h"
40 #include <openssl/dh.h>
42 #define PRIME768 "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088" \
43 "A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25" \
44 "F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A63A3620FFFFFFFFFFFFFFFF"
46 #define PRIME1024 "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08" \
47 "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF2" \
48 "5F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406" \
49 "B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF"
51 #define PRIME1536 "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \
52 "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \
53 "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \
54 "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \
55 "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \
56 "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \
57 "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \
58 "670C354E4ABC9804F1746C08CA237327FFFFFFFFFFFFFFFF"
61 static isc_result_t openssldh_todns(const dst_key_t *key, isc_buffer_t *data);
63 static BIGNUM bn2, bn768, bn1024, bn1536;
65 static isc_result_t
66 openssldh_computesecret(const dst_key_t *pub, const dst_key_t *priv,
67 isc_buffer_t *secret)
69 DH *dhpub, *dhpriv;
70 int ret;
71 isc_region_t r;
72 unsigned int len;
74 REQUIRE(pub->opaque != NULL);
75 REQUIRE(priv->opaque != NULL);
77 dhpub = (DH *) pub->opaque;
78 dhpriv = (DH *) priv->opaque;
80 len = DH_size(dhpriv);
81 isc_buffer_availableregion(secret, &r);
82 if (r.length < len)
83 return (ISC_R_NOSPACE);
84 ret = DH_compute_key(r.base, dhpub->pub_key, dhpriv);
85 if (ret == 0)
86 return (dst__openssl_toresult(DST_R_COMPUTESECRETFAILURE));
87 isc_buffer_add(secret, len);
88 return (ISC_R_SUCCESS);
91 static isc_boolean_t
92 openssldh_compare(const dst_key_t *key1, const dst_key_t *key2) {
93 int status;
94 DH *dh1, *dh2;
96 dh1 = (DH *) key1->opaque;
97 dh2 = (DH *) key2->opaque;
99 if (dh1 == NULL && dh2 == NULL)
100 return (ISC_TRUE);
101 else if (dh1 == NULL || dh2 == NULL)
102 return (ISC_FALSE);
104 status = BN_cmp(dh1->p, dh2->p) ||
105 BN_cmp(dh1->g, dh2->g) ||
106 BN_cmp(dh1->pub_key, dh2->pub_key);
108 if (status != 0)
109 return (ISC_FALSE);
111 if (dh1->priv_key != NULL || dh2->priv_key != NULL) {
112 if (dh1->priv_key == NULL || dh2->priv_key == NULL)
113 return (ISC_FALSE);
114 if (BN_cmp(dh1->priv_key, dh2->priv_key) != 0)
115 return (ISC_FALSE);
117 return (ISC_TRUE);
120 static isc_boolean_t
121 openssldh_paramcompare(const dst_key_t *key1, const dst_key_t *key2) {
122 int status;
123 DH *dh1, *dh2;
125 dh1 = (DH *) key1->opaque;
126 dh2 = (DH *) key2->opaque;
128 if (dh1 == NULL && dh2 == NULL)
129 return (ISC_TRUE);
130 else if (dh1 == NULL || dh2 == NULL)
131 return (ISC_FALSE);
133 status = BN_cmp(dh1->p, dh2->p) ||
134 BN_cmp(dh1->g, dh2->g);
136 if (status != 0)
137 return (ISC_FALSE);
138 return (ISC_TRUE);
141 #ifndef HAVE_DH_GENERATE_PARAMETERS
142 /* ====================================================================
143 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
145 * Redistribution and use in source and binary forms, with or without
146 * modification, are permitted provided that the following conditions
147 * are met:
149 * 1. Redistributions of source code must retain the above copyright
150 * notice, this list of conditions and the following disclaimer.
152 * 2. Redistributions in binary form must reproduce the above copyright
153 * notice, this list of conditions and the following disclaimer in
154 * the documentation and/or other materials provided with the
155 * distribution.
157 * 3. All advertising materials mentioning features or use of this
158 * software must display the following acknowledgment:
159 * "This product includes software developed by the OpenSSL Project
160 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
162 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
163 * endorse or promote products derived from this software without
164 * prior written permission. For written permission, please contact
165 * openssl-core@openssl.org.
167 * 5. Products derived from this software may not be called "OpenSSL"
168 * nor may "OpenSSL" appear in their names without prior written
169 * permission of the OpenSSL Project.
171 * 6. Redistributions of any form whatsoever must retain the following
172 * acknowledgment:
173 * "This product includes software developed by the OpenSSL Project
174 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
176 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
177 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
178 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
179 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
180 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
181 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
182 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
183 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
184 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
185 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
186 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
187 * OF THE POSSIBILITY OF SUCH DAMAGE.
188 * ====================================================================
190 * This product includes cryptographic software written by Eric Young
191 * (eay@cryptsoft.com). This product includes software written by Tim
192 * Hudson (tjh@cryptsoft.com).
195 static DH *
196 DH_generate_parameters(int prime_len, int generator,
197 void (*callback)(int,int,void *), void *cb_arg)
199 BN_GENCB cb;
200 DH *dh = NULL;
202 dh = DH_new();
203 if (dh != NULL) {
204 BN_GENCB_set_old(&cb, callback, cb_arg);
206 if (DH_generate_parameters_ex(dh, prime_len, generator, &cb))
207 return (dh);
208 DH_free(dh);
210 return (NULL);
212 #endif
214 static isc_result_t
215 openssldh_generate(dst_key_t *key, int generator) {
216 DH *dh = NULL;
218 if (generator == 0) {
219 if (key->key_size == 768 ||
220 key->key_size == 1024 ||
221 key->key_size == 1536)
223 dh = DH_new();
224 if (dh == NULL)
225 return (ISC_R_NOMEMORY);
226 if (key->key_size == 768)
227 dh->p = &bn768;
228 else if (key->key_size == 1024)
229 dh->p = &bn1024;
230 else
231 dh->p = &bn1536;
232 dh->g = &bn2;
234 else
235 generator = 2;
238 if (generator != 0)
239 dh = DH_generate_parameters(key->key_size, generator,
240 NULL, NULL);
242 if (dh == NULL)
243 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
245 if (DH_generate_key(dh) == 0) {
246 DH_free(dh);
247 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
249 dh->flags &= ~DH_FLAG_CACHE_MONT_P;
251 key->opaque = dh;
253 return (ISC_R_SUCCESS);
256 static isc_boolean_t
257 openssldh_isprivate(const dst_key_t *key) {
258 DH *dh = (DH *) key->opaque;
259 return (ISC_TF(dh != NULL && dh->priv_key != NULL));
262 static void
263 openssldh_destroy(dst_key_t *key) {
264 DH *dh = key->opaque;
266 if (dh == NULL)
267 return;
269 if (dh->p == &bn768 || dh->p == &bn1024 || dh->p == &bn1536)
270 dh->p = NULL;
271 if (dh->g == &bn2)
272 dh->g = NULL;
273 DH_free(dh);
274 key->opaque = NULL;
277 static void
278 uint16_toregion(isc_uint16_t val, isc_region_t *region) {
279 *region->base++ = (val & 0xff00) >> 8;
280 *region->base++ = (val & 0x00ff);
283 static isc_uint16_t
284 uint16_fromregion(isc_region_t *region) {
285 isc_uint16_t val;
286 unsigned char *cp = region->base;
288 val = ((unsigned int)(cp[0])) << 8;
289 val |= ((unsigned int)(cp[1]));
291 region->base += 2;
292 return (val);
295 static isc_result_t
296 openssldh_todns(const dst_key_t *key, isc_buffer_t *data) {
297 DH *dh;
298 isc_region_t r;
299 isc_uint16_t dnslen, plen, glen, publen;
301 REQUIRE(key->opaque != NULL);
303 dh = (DH *) key->opaque;
305 isc_buffer_availableregion(data, &r);
307 if (dh->g == &bn2 &&
308 (dh->p == &bn768 || dh->p == &bn1024 || dh->p == &bn1536)) {
309 plen = 1;
310 glen = 0;
312 else {
313 plen = BN_num_bytes(dh->p);
314 glen = BN_num_bytes(dh->g);
316 publen = BN_num_bytes(dh->pub_key);
317 dnslen = plen + glen + publen + 6;
318 if (r.length < (unsigned int) dnslen)
319 return (ISC_R_NOSPACE);
321 uint16_toregion(plen, &r);
322 if (plen == 1) {
323 if (dh->p == &bn768)
324 *r.base = 1;
325 else if (dh->p == &bn1024)
326 *r.base = 2;
327 else
328 *r.base = 3;
330 else
331 BN_bn2bin(dh->p, r.base);
332 r.base += plen;
334 uint16_toregion(glen, &r);
335 if (glen > 0)
336 BN_bn2bin(dh->g, r.base);
337 r.base += glen;
339 uint16_toregion(publen, &r);
340 BN_bn2bin(dh->pub_key, r.base);
341 r.base += publen;
343 isc_buffer_add(data, dnslen);
345 return (ISC_R_SUCCESS);
348 static isc_result_t
349 openssldh_fromdns(dst_key_t *key, isc_buffer_t *data) {
350 DH *dh;
351 isc_region_t r;
352 isc_uint16_t plen, glen, publen;
353 int special = 0;
355 isc_buffer_remainingregion(data, &r);
356 if (r.length == 0)
357 return (ISC_R_SUCCESS);
359 dh = DH_new();
360 if (dh == NULL)
361 return (ISC_R_NOMEMORY);
362 dh->flags &= ~DH_FLAG_CACHE_MONT_P;
365 * Read the prime length. 1 & 2 are table entries, > 16 means a
366 * prime follows, otherwise an error.
368 if (r.length < 2) {
369 DH_free(dh);
370 return (DST_R_INVALIDPUBLICKEY);
372 plen = uint16_fromregion(&r);
373 if (plen < 16 && plen != 1 && plen != 2) {
374 DH_free(dh);
375 return (DST_R_INVALIDPUBLICKEY);
377 if (r.length < plen) {
378 DH_free(dh);
379 return (DST_R_INVALIDPUBLICKEY);
381 if (plen == 1 || plen == 2) {
382 if (plen == 1)
383 special = *r.base++;
384 else
385 special = uint16_fromregion(&r);
386 switch (special) {
387 case 1:
388 dh->p = &bn768;
389 break;
390 case 2:
391 dh->p = &bn1024;
392 break;
393 case 3:
394 dh->p = &bn1536;
395 break;
396 default:
397 DH_free(dh);
398 return (DST_R_INVALIDPUBLICKEY);
401 else {
402 dh->p = BN_bin2bn(r.base, plen, NULL);
403 r.base += plen;
407 * Read the generator length. This should be 0 if the prime was
408 * special, but it might not be. If it's 0 and the prime is not
409 * special, we have a problem.
411 if (r.length < 2) {
412 DH_free(dh);
413 return (DST_R_INVALIDPUBLICKEY);
415 glen = uint16_fromregion(&r);
416 if (r.length < glen) {
417 DH_free(dh);
418 return (DST_R_INVALIDPUBLICKEY);
420 if (special != 0) {
421 if (glen == 0)
422 dh->g = &bn2;
423 else {
424 dh->g = BN_bin2bn(r.base, glen, NULL);
425 if (BN_cmp(dh->g, &bn2) == 0) {
426 BN_free(dh->g);
427 dh->g = &bn2;
429 else {
430 DH_free(dh);
431 return (DST_R_INVALIDPUBLICKEY);
435 else {
436 if (glen == 0) {
437 DH_free(dh);
438 return (DST_R_INVALIDPUBLICKEY);
440 dh->g = BN_bin2bn(r.base, glen, NULL);
442 r.base += glen;
444 if (r.length < 2) {
445 DH_free(dh);
446 return (DST_R_INVALIDPUBLICKEY);
448 publen = uint16_fromregion(&r);
449 if (r.length < publen) {
450 DH_free(dh);
451 return (DST_R_INVALIDPUBLICKEY);
453 dh->pub_key = BN_bin2bn(r.base, publen, NULL);
454 r.base += publen;
456 key->key_size = BN_num_bits(dh->p);
458 isc_buffer_forward(data, plen + glen + publen + 6);
460 key->opaque = (void *) dh;
462 return (ISC_R_SUCCESS);
465 static isc_result_t
466 openssldh_tofile(const dst_key_t *key, const char *directory) {
467 int i;
468 DH *dh;
469 dst_private_t priv;
470 unsigned char *bufs[4];
471 isc_result_t result;
473 if (key->opaque == NULL)
474 return (DST_R_NULLKEY);
476 dh = (DH *) key->opaque;
478 for (i = 0; i < 4; i++) {
479 bufs[i] = isc_mem_get(key->mctx, BN_num_bytes(dh->p));
480 if (bufs[i] == NULL) {
481 result = ISC_R_NOMEMORY;
482 goto fail;
486 i = 0;
488 priv.elements[i].tag = TAG_DH_PRIME;
489 priv.elements[i].length = BN_num_bytes(dh->p);
490 BN_bn2bin(dh->p, bufs[i]);
491 priv.elements[i].data = bufs[i];
492 i++;
494 priv.elements[i].tag = TAG_DH_GENERATOR;
495 priv.elements[i].length = BN_num_bytes(dh->g);
496 BN_bn2bin(dh->g, bufs[i]);
497 priv.elements[i].data = bufs[i];
498 i++;
500 priv.elements[i].tag = TAG_DH_PRIVATE;
501 priv.elements[i].length = BN_num_bytes(dh->priv_key);
502 BN_bn2bin(dh->priv_key, bufs[i]);
503 priv.elements[i].data = bufs[i];
504 i++;
506 priv.elements[i].tag = TAG_DH_PUBLIC;
507 priv.elements[i].length = BN_num_bytes(dh->pub_key);
508 BN_bn2bin(dh->pub_key, bufs[i]);
509 priv.elements[i].data = bufs[i];
510 i++;
512 priv.nelements = i;
513 result = dst__privstruct_writefile(key, &priv, directory);
514 fail:
515 for (i = 0; i < 4; i++) {
516 if (bufs[i] == NULL)
517 break;
518 isc_mem_put(key->mctx, bufs[i], BN_num_bytes(dh->p));
520 return (result);
523 static isc_result_t
524 openssldh_parse(dst_key_t *key, isc_lex_t *lexer) {
525 dst_private_t priv;
526 isc_result_t ret;
527 int i;
528 DH *dh = NULL;
529 isc_mem_t *mctx;
530 #define DST_RET(a) {ret = a; goto err;}
532 mctx = key->mctx;
534 /* read private key file */
535 ret = dst__privstruct_parse(key, DST_ALG_DH, lexer, mctx, &priv);
536 if (ret != ISC_R_SUCCESS)
537 return (ret);
539 dh = DH_new();
540 if (dh == NULL)
541 DST_RET(ISC_R_NOMEMORY);
542 dh->flags &= ~DH_FLAG_CACHE_MONT_P;
543 key->opaque = dh;
545 for (i = 0; i < priv.nelements; i++) {
546 BIGNUM *bn;
547 bn = BN_bin2bn(priv.elements[i].data,
548 priv.elements[i].length, NULL);
549 if (bn == NULL)
550 DST_RET(ISC_R_NOMEMORY);
552 switch (priv.elements[i].tag) {
553 case TAG_DH_PRIME:
554 dh->p = bn;
555 break;
556 case TAG_DH_GENERATOR:
557 dh->g = bn;
558 break;
559 case TAG_DH_PRIVATE:
560 dh->priv_key = bn;
561 break;
562 case TAG_DH_PUBLIC:
563 dh->pub_key = bn;
564 break;
567 dst__privstruct_free(&priv, mctx);
569 key->key_size = BN_num_bits(dh->p);
571 if ((key->key_size == 768 ||
572 key->key_size == 1024 ||
573 key->key_size == 1536) &&
574 BN_cmp(dh->g, &bn2) == 0)
576 if (key->key_size == 768 && BN_cmp(dh->p, &bn768) == 0) {
577 BN_free(dh->p);
578 BN_free(dh->g);
579 dh->p = &bn768;
580 dh->g = &bn2;
581 } else if (key->key_size == 1024 &&
582 BN_cmp(dh->p, &bn1024) == 0) {
583 BN_free(dh->p);
584 BN_free(dh->g);
585 dh->p = &bn1024;
586 dh->g = &bn2;
587 } else if (key->key_size == 1536 &&
588 BN_cmp(dh->p, &bn1536) == 0) {
589 BN_free(dh->p);
590 BN_free(dh->g);
591 dh->p = &bn1536;
592 dh->g = &bn2;
596 return (ISC_R_SUCCESS);
598 err:
599 openssldh_destroy(key);
600 dst__privstruct_free(&priv, mctx);
601 memset(&priv, 0, sizeof(priv));
602 return (ret);
605 static void
606 BN_fromhex(BIGNUM *b, const char *str) {
607 static const char hexdigits[] = "0123456789abcdef";
608 unsigned char data[512];
609 unsigned int i;
610 BIGNUM *out;
612 RUNTIME_CHECK(strlen(str) < 1024U && strlen(str) % 2 == 0U);
613 for (i = 0; i < strlen(str); i += 2) {
614 char *s;
615 unsigned int high, low;
617 s = strchr(hexdigits, tolower((unsigned char)str[i]));
618 RUNTIME_CHECK(s != NULL);
619 high = s - hexdigits;
621 s = strchr(hexdigits, tolower((unsigned char)str[i + 1]));
622 RUNTIME_CHECK(s != NULL);
623 low = s - hexdigits;
625 data[i/2] = (unsigned char)((high << 4) + low);
627 out = BN_bin2bn(data, strlen(str)/2, b);
628 RUNTIME_CHECK(out != NULL);
631 static void
632 openssldh_cleanup(void) {
633 BN_free(&bn2);
634 BN_free(&bn768);
635 BN_free(&bn1024);
636 BN_free(&bn1536);
639 static dst_func_t openssldh_functions = {
640 NULL, /* createctx */
641 NULL, /* destroyctx */
642 NULL, /* adddata */
643 NULL, /* openssldh_sign */
644 NULL, /* openssldh_verify */
645 openssldh_computesecret,
646 openssldh_compare,
647 openssldh_paramcompare,
648 openssldh_generate,
649 openssldh_isprivate,
650 openssldh_destroy,
651 openssldh_todns,
652 openssldh_fromdns,
653 openssldh_tofile,
654 openssldh_parse,
655 openssldh_cleanup,
658 isc_result_t
659 dst__openssldh_init(dst_func_t **funcp) {
660 REQUIRE(funcp != NULL);
661 if (*funcp == NULL) {
662 BN_init(&bn2);
663 BN_init(&bn768);
664 BN_init(&bn1024);
665 BN_init(&bn1536);
666 BN_set_word(&bn2, 2);
667 BN_fromhex(&bn768, PRIME768);
668 BN_fromhex(&bn1024, PRIME1024);
669 BN_fromhex(&bn1536, PRIME1536);
670 *funcp = &openssldh_functions;
672 return (ISC_R_SUCCESS);
675 #else /* OPENSSL */
677 #include <isc/util.h>
679 EMPTY_TRANSLATION_UNIT
681 #endif /* OPENSSL */