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.
19 /* $Id: openssldsa_link.c,v 1.1.4.3 2006/03/02 00:37:20 marka Exp $ */
27 #include <isc/entropy.h>
32 #include <dst/result.h>
34 #include "dst_internal.h"
35 #include "dst_openssl.h"
36 #include "dst_parse.h"
38 #include <openssl/dsa.h>
40 static isc_result_t openssldsa_todns(const dst_key_t *key, isc_buffer_t *data);
43 openssldsa_createctx(dst_key_t *key, dst_context_t *dctx) {
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);
55 openssldsa_destroyctx(dst_context_t *dctx) {
56 isc_sha1_t *sha1ctx = dctx->opaque;
58 if (sha1ctx != NULL) {
59 isc_sha1_invalidate(sha1ctx);
60 isc_mem_put(dctx->mctx, sha1ctx, sizeof(isc_sha1_t));
66 openssldsa_adddata(dst_context_t *dctx, const isc_region_t *data) {
67 isc_sha1_t *sha1ctx = dctx->opaque;
69 isc_sha1_update(sha1ctx, data->base, data->length);
70 return (ISC_R_SUCCESS);
74 BN_bn2bin_fixed(BIGNUM *bn, unsigned char *buf, int size) {
75 int bytes = size - BN_num_bytes(bn);
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;
89 unsigned char digest[ISC_SHA1_DIGESTLENGTH];
91 isc_buffer_availableregion(sig, &r);
92 if (r.length < ISC_SHA1_DIGESTLENGTH * 2 + 1)
93 return (ISC_R_NOSPACE);
95 isc_sha1_final(sha1ctx, digest);
97 dsasig = DSA_do_sign(digest, ISC_SHA1_DIGESTLENGTH, dsa);
99 return (dst__openssl_toresult(DST_R_SIGNFAILURE));
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);
109 return (ISC_R_SUCCESS);
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;
119 unsigned char digest[ISC_SHA1_DIGESTLENGTH];
120 unsigned char *cp = sig->base;
122 isc_sha1_final(sha1ctx, digest);
124 if (sig->length < 2 * ISC_SHA1_DIGESTLENGTH + 1)
125 return (DST_R_VERIFYFAILURE);
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;
134 status = DSA_do_verify(digest, ISC_SHA1_DIGESTLENGTH, dsasig, dsa);
135 DSA_SIG_free(dsasig);
137 return (dst__openssl_toresult(DST_R_VERIFYFAILURE));
139 return (ISC_R_SUCCESS);
143 openssldsa_compare(const dst_key_t *key1, const dst_key_t *key2) {
147 dsa1 = (DSA *) key1->opaque;
148 dsa2 = (DSA *) key2->opaque;
150 if (dsa1 == NULL && dsa2 == NULL)
152 else if (dsa1 == NULL || dsa2 == NULL)
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);
163 if (dsa1->priv_key != NULL || dsa2->priv_key != NULL) {
164 if (dsa1->priv_key == NULL || dsa2->priv_key == NULL)
166 if (BN_cmp(dsa1->priv_key, dsa2->priv_key))
172 #ifndef HAVE_DSA_GENERATE_PARAMETERS
173 /* ====================================================================
174 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
176 * Redistribution and use in source and binary forms, with or without
177 * modification, are permitted provided that the following conditions
180 * 1. Redistributions of source code must retain the above copyright
181 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
202 * 6. Redistributions of any form whatsoever must retain the following
204 * "This product includes software developed by the OpenSSL Project
205 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
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 *),
238 BN_GENCB_set_old(&cb, callback, cb_arg);
240 if (DSA_generate_parameters_ex(dsa, bits, seed_in, seed_len,
241 counter_ret, h_ret, &cb))
250 openssldsa_generate(dst_key_t *key, int unused) {
252 unsigned char rand_array[ISC_SHA1_DIGESTLENGTH];
257 result = dst__entropy_getdata(rand_array, sizeof(rand_array),
259 if (result != ISC_R_SUCCESS)
262 dsa = DSA_generate_parameters(key->key_size, rand_array,
263 ISC_SHA1_DIGESTLENGTH, NULL, NULL,
267 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
269 if (DSA_generate_key(dsa) == 0) {
271 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
273 dsa->flags &= ~DSA_FLAG_CACHE_MONT_P;
277 return (ISC_R_SUCCESS);
281 openssldsa_isprivate(const dst_key_t *key) {
282 DSA *dsa = (DSA *) key->opaque;
283 return (ISC_TF(dsa != NULL && dsa->priv_key != NULL));
287 openssldsa_destroy(dst_key_t *key) {
288 DSA *dsa = key->opaque;
295 openssldsa_todns(const dst_key_t *key, isc_buffer_t *data) {
299 unsigned int t, p_bytes;
301 REQUIRE(key->opaque != NULL);
303 dsa = (DSA *) key->opaque;
305 isc_buffer_availableregion(data, &r);
307 t = (BN_num_bytes(dsa->p) - 64) / 8;
309 return (DST_R_INVALIDPUBLICKEY);
310 p_bytes = 64 + 8 * t;
312 dnslen = 1 + (key->key_size * 3)/8 + ISC_SHA1_DIGESTLENGTH;
313 if (r.length < (unsigned int) dnslen)
314 return (ISC_R_NOSPACE);
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);
321 BN_bn2bin_fixed(dsa->g, r.base, key->key_size/8);
323 BN_bn2bin_fixed(dsa->pub_key, r.base, key->key_size/8);
326 isc_buffer_add(data, dnslen);
328 return (ISC_R_SUCCESS);
332 openssldsa_fromdns(dst_key_t *key, isc_buffer_t *data) {
335 unsigned int t, p_bytes;
336 isc_mem_t *mctx = key->mctx;
340 isc_buffer_remainingregion(data, &r);
342 return (ISC_R_SUCCESS);
346 return (ISC_R_NOMEMORY);
347 dsa->flags &= ~DSA_FLAG_CACHE_MONT_P;
349 t = (unsigned int) *r.base++;
352 return (DST_R_INVALIDPUBLICKEY);
354 p_bytes = 64 + 8 * t;
356 if (r.length < 1 + ISC_SHA1_DIGESTLENGTH + 3 * p_bytes) {
358 return (DST_R_INVALIDPUBLICKEY);
361 dsa->q = BN_bin2bn(r.base, ISC_SHA1_DIGESTLENGTH, NULL);
362 r.base += ISC_SHA1_DIGESTLENGTH;
364 dsa->p = BN_bin2bn(r.base, p_bytes, NULL);
367 dsa->g = BN_bin2bn(r.base, p_bytes, NULL);
370 dsa->pub_key = BN_bin2bn(r.base, p_bytes, NULL);
373 key->key_size = p_bytes * 8;
375 isc_buffer_forward(data, 1 + ISC_SHA1_DIGESTLENGTH + 3 * p_bytes);
377 key->opaque = (void *) dsa;
379 return (ISC_R_SUCCESS);
384 openssldsa_tofile(const dst_key_t *key, const char *directory) {
388 unsigned char bufs[5][128];
390 if (key->opaque == NULL)
391 return (DST_R_NULLKEY);
393 dsa = (DSA *) key->opaque;
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];
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];
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];
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];
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];
425 priv.nelements = cnt;
426 return (dst__privstruct_writefile(key, &priv, directory));
430 openssldsa_parse(dst_key_t *key, isc_lex_t *lexer) {
435 isc_mem_t *mctx = key->mctx;
436 #define DST_RET(a) {ret = a; goto err;}
438 /* read private key file */
439 ret = dst__privstruct_parse(key, DST_ALG_DSA, lexer, mctx, &priv);
440 if (ret != ISC_R_SUCCESS)
445 DST_RET(ISC_R_NOMEMORY);
446 dsa->flags &= ~DSA_FLAG_CACHE_MONT_P;
449 for (i=0; i < priv.nelements; i++) {
451 bn = BN_bin2bn(priv.elements[i].data,
452 priv.elements[i].length, NULL);
454 DST_RET(ISC_R_NOMEMORY);
456 switch (priv.elements[i].tag) {
460 case TAG_DSA_SUBPRIME:
466 case TAG_DSA_PRIVATE:
474 dst__privstruct_free(&priv, mctx);
476 key->key_size = BN_num_bits(dsa->p);
478 return (ISC_R_SUCCESS);
481 openssldsa_destroy(key);
482 dst__privstruct_free(&priv, mctx);
483 memset(&priv, 0, sizeof(priv));
487 static dst_func_t openssldsa_functions = {
488 openssldsa_createctx,
489 openssldsa_destroyctx,
493 NULL, /* computesecret */
495 NULL, /* paramcompare */
497 openssldsa_isprivate,
507 dst__openssldsa_init(dst_func_t **funcp) {
508 REQUIRE(funcp != NULL);
510 *funcp = &openssldsa_functions;
511 return (ISC_R_SUCCESS);
516 #include <isc/util.h>
518 EMPTY_TRANSLATION_UNIT