2 * Portions Copyright (C) 2004 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.1 2004/12/09 04:07:18 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))
173 openssldsa_generate(dst_key_t *key, int unused) {
175 unsigned char rand_array[ISC_SHA1_DIGESTLENGTH];
180 result = dst__entropy_getdata(rand_array, sizeof(rand_array),
182 if (result != ISC_R_SUCCESS)
185 dsa = DSA_generate_parameters(key->key_size, rand_array,
186 ISC_SHA1_DIGESTLENGTH, NULL, NULL,
190 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
192 if (DSA_generate_key(dsa) == 0) {
194 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
196 dsa->flags &= ~DSA_FLAG_CACHE_MONT_P;
200 return (ISC_R_SUCCESS);
204 openssldsa_isprivate(const dst_key_t *key) {
205 DSA *dsa = (DSA *) key->opaque;
206 return (ISC_TF(dsa != NULL && dsa->priv_key != NULL));
210 openssldsa_destroy(dst_key_t *key) {
211 DSA *dsa = key->opaque;
218 openssldsa_todns(const dst_key_t *key, isc_buffer_t *data) {
222 unsigned int t, p_bytes;
224 REQUIRE(key->opaque != NULL);
226 dsa = (DSA *) key->opaque;
228 isc_buffer_availableregion(data, &r);
230 t = (BN_num_bytes(dsa->p) - 64) / 8;
232 return (DST_R_INVALIDPUBLICKEY);
233 p_bytes = 64 + 8 * t;
235 dnslen = 1 + (key->key_size * 3)/8 + ISC_SHA1_DIGESTLENGTH;
236 if (r.length < (unsigned int) dnslen)
237 return (ISC_R_NOSPACE);
240 BN_bn2bin_fixed(dsa->q, r.base, ISC_SHA1_DIGESTLENGTH);
241 r.base += ISC_SHA1_DIGESTLENGTH;
242 BN_bn2bin_fixed(dsa->p, r.base, key->key_size/8);
244 BN_bn2bin_fixed(dsa->g, r.base, key->key_size/8);
246 BN_bn2bin_fixed(dsa->pub_key, r.base, key->key_size/8);
249 isc_buffer_add(data, dnslen);
251 return (ISC_R_SUCCESS);
255 openssldsa_fromdns(dst_key_t *key, isc_buffer_t *data) {
258 unsigned int t, p_bytes;
259 isc_mem_t *mctx = key->mctx;
263 isc_buffer_remainingregion(data, &r);
265 return (ISC_R_SUCCESS);
269 return (ISC_R_NOMEMORY);
270 dsa->flags &= ~DSA_FLAG_CACHE_MONT_P;
272 t = (unsigned int) *r.base++;
275 return (DST_R_INVALIDPUBLICKEY);
277 p_bytes = 64 + 8 * t;
279 if (r.length < 1 + ISC_SHA1_DIGESTLENGTH + 3 * p_bytes) {
281 return (DST_R_INVALIDPUBLICKEY);
284 dsa->q = BN_bin2bn(r.base, ISC_SHA1_DIGESTLENGTH, NULL);
285 r.base += ISC_SHA1_DIGESTLENGTH;
287 dsa->p = BN_bin2bn(r.base, p_bytes, NULL);
290 dsa->g = BN_bin2bn(r.base, p_bytes, NULL);
293 dsa->pub_key = BN_bin2bn(r.base, p_bytes, NULL);
296 key->key_size = p_bytes * 8;
298 isc_buffer_forward(data, 1 + ISC_SHA1_DIGESTLENGTH + 3 * p_bytes);
300 key->opaque = (void *) dsa;
302 return (ISC_R_SUCCESS);
307 openssldsa_tofile(const dst_key_t *key, const char *directory) {
311 unsigned char bufs[5][128];
313 if (key->opaque == NULL)
314 return (DST_R_NULLKEY);
316 dsa = (DSA *) key->opaque;
318 priv.elements[cnt].tag = TAG_DSA_PRIME;
319 priv.elements[cnt].length = BN_num_bytes(dsa->p);
320 BN_bn2bin(dsa->p, bufs[cnt]);
321 priv.elements[cnt].data = bufs[cnt];
324 priv.elements[cnt].tag = TAG_DSA_SUBPRIME;
325 priv.elements[cnt].length = BN_num_bytes(dsa->q);
326 BN_bn2bin(dsa->q, bufs[cnt]);
327 priv.elements[cnt].data = bufs[cnt];
330 priv.elements[cnt].tag = TAG_DSA_BASE;
331 priv.elements[cnt].length = BN_num_bytes(dsa->g);
332 BN_bn2bin(dsa->g, bufs[cnt]);
333 priv.elements[cnt].data = bufs[cnt];
336 priv.elements[cnt].tag = TAG_DSA_PRIVATE;
337 priv.elements[cnt].length = BN_num_bytes(dsa->priv_key);
338 BN_bn2bin(dsa->priv_key, bufs[cnt]);
339 priv.elements[cnt].data = bufs[cnt];
342 priv.elements[cnt].tag = TAG_DSA_PUBLIC;
343 priv.elements[cnt].length = BN_num_bytes(dsa->pub_key);
344 BN_bn2bin(dsa->pub_key, bufs[cnt]);
345 priv.elements[cnt].data = bufs[cnt];
348 priv.nelements = cnt;
349 return (dst__privstruct_writefile(key, &priv, directory));
353 openssldsa_parse(dst_key_t *key, isc_lex_t *lexer) {
358 isc_mem_t *mctx = key->mctx;
359 #define DST_RET(a) {ret = a; goto err;}
361 /* read private key file */
362 ret = dst__privstruct_parse(key, DST_ALG_DSA, lexer, mctx, &priv);
363 if (ret != ISC_R_SUCCESS)
368 DST_RET(ISC_R_NOMEMORY);
369 dsa->flags &= ~DSA_FLAG_CACHE_MONT_P;
372 for (i=0; i < priv.nelements; i++) {
374 bn = BN_bin2bn(priv.elements[i].data,
375 priv.elements[i].length, NULL);
377 DST_RET(ISC_R_NOMEMORY);
379 switch (priv.elements[i].tag) {
383 case TAG_DSA_SUBPRIME:
389 case TAG_DSA_PRIVATE:
397 dst__privstruct_free(&priv, mctx);
399 key->key_size = BN_num_bits(dsa->p);
401 return (ISC_R_SUCCESS);
404 openssldsa_destroy(key);
405 dst__privstruct_free(&priv, mctx);
406 memset(&priv, 0, sizeof(priv));
410 static dst_func_t openssldsa_functions = {
411 openssldsa_createctx,
412 openssldsa_destroyctx,
416 NULL, /* computesecret */
418 NULL, /* paramcompare */
420 openssldsa_isprivate,
430 dst__openssldsa_init(dst_func_t **funcp) {
431 REQUIRE(funcp != NULL);
433 *funcp = &openssldsa_functions;
434 return (ISC_R_SUCCESS);
439 #include <isc/util.h>
441 EMPTY_TRANSLATION_UNIT