openssldsa_link.c 16.6 KB
Newer Older
1
/*
2
 * Portions Copyright (C) 2004-2009, 2011-2015  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 * Portions Copyright (C) 1999-2002  Internet Software Consortium.
Automatic Updater's avatar
Automatic Updater committed
4
5
6
7
8
9
10
11
12
13
14
15
16
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE
 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
17
18
 * Portions Copyright (C) 1995-2000 by Network Associates, Inc.
 *
Automatic Updater's avatar
Automatic Updater committed
19
 * Permission to use, copy, modify, and/or distribute this software for any
20
21
22
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
Mark Andrews's avatar
Mark Andrews committed
23
24
25
26
27
28
29
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE
 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30
31
 */

32
#ifdef OPENSSL
Francis Dupont's avatar
Francis Dupont committed
33
34
35
#ifndef USE_EVP
#define USE_EVP 1
#endif
36
37
38
39
40
41
42
43
44
45
46
47
48

#include <config.h>

#include <string.h>

#include <isc/entropy.h>
#include <isc/mem.h>
#include <isc/sha1.h>
#include <isc/util.h>

#include <dst/result.h>

#include "dst_internal.h"
49
#include "dst_openssl.h"
50
51
52
53
54
55
56
57
#include "dst_parse.h"

#include <openssl/dsa.h>

static isc_result_t openssldsa_todns(const dst_key_t *key, isc_buffer_t *data);

static isc_result_t
openssldsa_createctx(dst_key_t *key, dst_context_t *dctx) {
Francis Dupont's avatar
Francis Dupont committed
58
59
#if USE_EVP
	EVP_MD_CTX *evp_md_ctx;
Automatic Updater's avatar
Automatic Updater committed
60

Francis Dupont's avatar
Francis Dupont committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
	UNUSED(key);

	evp_md_ctx = EVP_MD_CTX_create();
	if (evp_md_ctx == NULL)
		return (ISC_R_NOMEMORY);

	if (!EVP_DigestInit_ex(evp_md_ctx, EVP_dss1(), NULL)) {
		EVP_MD_CTX_destroy(evp_md_ctx);
			return (ISC_R_FAILURE);
	}

	dctx->ctxdata.evp_md_ctx = evp_md_ctx;

	return (ISC_R_SUCCESS);
#else
76
77
78
79
80
	isc_sha1_t *sha1ctx;

	UNUSED(key);

	sha1ctx = isc_mem_get(dctx->mctx, sizeof(isc_sha1_t));
81
82
	if (sha1ctx == NULL)
		return (ISC_R_NOMEMORY);
83
	isc_sha1_init(sha1ctx);
84
	dctx->ctxdata.sha1ctx = sha1ctx;
85
	return (ISC_R_SUCCESS);
Francis Dupont's avatar
Francis Dupont committed
86
#endif
87
88
89
90
}

static void
openssldsa_destroyctx(dst_context_t *dctx) {
Francis Dupont's avatar
Francis Dupont committed
91
92
93
94
95
96
97
98
#if USE_EVP
	EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;

	if (evp_md_ctx != NULL) {
		EVP_MD_CTX_destroy(evp_md_ctx);
		dctx->ctxdata.evp_md_ctx = NULL;
	}
#else
99
	isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
100
101
102
103

	if (sha1ctx != NULL) {
		isc_sha1_invalidate(sha1ctx);
		isc_mem_put(dctx->mctx, sha1ctx, sizeof(isc_sha1_t));
104
		dctx->ctxdata.sha1ctx = NULL;
105
	}
Francis Dupont's avatar
Francis Dupont committed
106
#endif
107
108
109
110
}

static isc_result_t
openssldsa_adddata(dst_context_t *dctx, const isc_region_t *data) {
Francis Dupont's avatar
Francis Dupont committed
111
112
113
114
115
116
117
#if USE_EVP
	EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;

	if (!EVP_DigestUpdate(evp_md_ctx, data->base, data->length)) {
		return (ISC_R_FAILURE);
	}
#else
118
	isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
119
120

	isc_sha1_update(sha1ctx, data->base, data->length);
Francis Dupont's avatar
Francis Dupont committed
121
#endif
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
	return (ISC_R_SUCCESS);
}

static int
BN_bn2bin_fixed(BIGNUM *bn, unsigned char *buf, int size) {
	int bytes = size - BN_num_bytes(bn);
	while (bytes-- > 0)
		*buf++ = 0;
	BN_bn2bin(bn, buf);
	return (size);
}

static isc_result_t
openssldsa_sign(dst_context_t *dctx, isc_buffer_t *sig) {
	dst_key_t *key = dctx->key;
137
	DSA *dsa = key->keydata.dsa;
138
	isc_region_t r;
Francis Dupont's avatar
Francis Dupont committed
139
	DSA_SIG *dsasig;
140
	unsigned int klen;
Francis Dupont's avatar
Francis Dupont committed
141
#if USE_EVP
Automatic Updater's avatar
Automatic Updater committed
142
143
	EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
	EVP_PKEY *pkey;
Francis Dupont's avatar
Francis Dupont committed
144
145
146
147
148
	unsigned char *sigbuf;
	const unsigned char *sb;
	unsigned int siglen;
#else
	isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
149
	unsigned char digest[ISC_SHA1_DIGESTLENGTH];
Francis Dupont's avatar
Francis Dupont committed
150
#endif
151
152
153
154
155

	isc_buffer_availableregion(sig, &r);
	if (r.length < ISC_SHA1_DIGESTLENGTH * 2 + 1)
		return (ISC_R_NOSPACE);

Francis Dupont's avatar
Francis Dupont committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#if USE_EVP
	pkey = EVP_PKEY_new();
	if (pkey == NULL)
		return (ISC_R_NOMEMORY);
	if (!EVP_PKEY_set1_DSA(pkey, dsa)) {
		EVP_PKEY_free(pkey);
		return (ISC_R_FAILURE);
	}
	sigbuf = malloc(EVP_PKEY_size(pkey));
	if (sigbuf == NULL) {
		EVP_PKEY_free(pkey);
		return (ISC_R_NOMEMORY);
	}
	if (!EVP_SignFinal(evp_md_ctx, sigbuf, &siglen, pkey)) {
		EVP_PKEY_free(pkey);
		free(sigbuf);
172
173
		return (dst__openssl_toresult3(dctx->category,
					       "EVP_SignFinal",
174
					       ISC_R_FAILURE));
Francis Dupont's avatar
Francis Dupont committed
175
176
177
178
179
180
181
182
183
184
185
186
	}
	INSIST(EVP_PKEY_size(pkey) >= (int) siglen);
	EVP_PKEY_free(pkey);
	/* Convert from Dss-Sig-Value (RFC2459). */
	dsasig = DSA_SIG_new();
	if (dsasig == NULL) {
		free(sigbuf);
		return (ISC_R_NOMEMORY);
	}
	sb = sigbuf;
	if (d2i_DSA_SIG(&dsasig, &sb, (long) siglen) == NULL) {
		free(sigbuf);
187
188
189
		return (dst__openssl_toresult3(dctx->category,
					       "d2i_DSA_SIG",
					       ISC_R_FAILURE));
Francis Dupont's avatar
Francis Dupont committed
190
191
	}
	free(sigbuf);
192

Francis Dupont's avatar
Francis Dupont committed
193
194
195
#elif 0
	/* Only use EVP for the Digest */
	if (!EVP_DigestFinal_ex(evp_md_ctx, digest, &siglen)) {
196
197
		return (dst__openssl_toresult3(dctx->category,
					       "EVP_DigestFinal_ex",
198
					       ISC_R_FAILURE));
Francis Dupont's avatar
Francis Dupont committed
199
200
201
	}
	dsasig = DSA_do_sign(digest, ISC_SHA1_DIGESTLENGTH, dsa);
	if (dsasig == NULL)
202
203
		return (dst__openssl_toresult3(dctx->category,
					       "DSA_do_sign",
204
					       DST_R_SIGNFAILURE));
Francis Dupont's avatar
Francis Dupont committed
205
#else
206
207
208
209
	isc_sha1_final(sha1ctx, digest);

	dsasig = DSA_do_sign(digest, ISC_SHA1_DIGESTLENGTH, dsa);
	if (dsasig == NULL)
210
211
		return (dst__openssl_toresult3(dctx->category,
					       "DSA_do_sign",
212
					       DST_R_SIGNFAILURE));
Francis Dupont's avatar
Francis Dupont committed
213
#endif
214
215
216
217
218
219
220

	klen = (key->key_size - 512)/64;
	if (klen > 255)
		return (ISC_R_FAILURE);
	*r.base = klen;
	isc_region_consume(&r, 1);

221
	BN_bn2bin_fixed(dsasig->r, r.base, ISC_SHA1_DIGESTLENGTH);
222
	isc_region_consume(&r, ISC_SHA1_DIGESTLENGTH);
223
	BN_bn2bin_fixed(dsasig->s, r.base, ISC_SHA1_DIGESTLENGTH);
224
	isc_region_consume(&r, ISC_SHA1_DIGESTLENGTH);
225
226
227
228
229
230
231
232
233
	DSA_SIG_free(dsasig);
	isc_buffer_add(sig, ISC_SHA1_DIGESTLENGTH * 2 + 1);

	return (ISC_R_SUCCESS);
}

static isc_result_t
openssldsa_verify(dst_context_t *dctx, const isc_region_t *sig) {
	dst_key_t *key = dctx->key;
234
	DSA *dsa = key->keydata.dsa;
235
236
	int status = 0;
	unsigned char *cp = sig->base;
Francis Dupont's avatar
Francis Dupont committed
237
238
239
240
241
242
243
244
245
246
247
248
	DSA_SIG *dsasig;
#if USE_EVP
	EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
#if 0
	EVP_PKEY *pkey;
	unsigned char *sigbuf;
#endif
	unsigned int siglen;
#else
	isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
#endif
	unsigned char digest[ISC_SHA1_DIGESTLENGTH];
249

Francis Dupont's avatar
Francis Dupont committed
250
251
252
253
254
255
256
257
258

#if USE_EVP
#if 1
	/* Only use EVP for the digest */
	if (!EVP_DigestFinal_ex(evp_md_ctx, digest, &siglen)) {
		return (ISC_R_FAILURE);
	}
#endif
#else
259
	isc_sha1_final(sha1ctx, digest);
Francis Dupont's avatar
Francis Dupont committed
260
#endif
261

Francis Dupont's avatar
Francis Dupont committed
262
	if (sig->length != 2 * ISC_SHA1_DIGESTLENGTH + 1) {
263
		return (DST_R_VERIFYFAILURE);
Francis Dupont's avatar
Francis Dupont committed
264
	}
265

266
	cp++;	/*%< Skip T */
267
	dsasig = DSA_SIG_new();
Francis Dupont's avatar
Francis Dupont committed
268
269
	if (dsasig == NULL)
		return (ISC_R_NOMEMORY);
270
271
272
273
	dsasig->r = BN_bin2bn(cp, ISC_SHA1_DIGESTLENGTH, NULL);
	cp += ISC_SHA1_DIGESTLENGTH;
	dsasig->s = BN_bin2bn(cp, ISC_SHA1_DIGESTLENGTH, NULL);

Francis Dupont's avatar
Francis Dupont committed
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#if 0
	pkey = EVP_PKEY_new();
	if (pkey == NULL)
		return (ISC_R_NOMEMORY);
	if (!EVP_PKEY_set1_DSA(pkey, dsa)) {
		EVP_PKEY_free(pkey);
		return (ISC_R_FAILURE);
	}
	/* Convert to Dss-Sig-Value (RFC2459). */
	sigbuf = malloc(EVP_PKEY_size(pkey) + 50);
	if (sigbuf == NULL) {
		EVP_PKEY_free(pkey);
		return (ISC_R_NOMEMORY);
	}
	siglen = (unsigned) i2d_DSA_SIG(dsasig, &sigbuf);
	INSIST(EVP_PKEY_size(pkey) >= (int) siglen);
	status = EVP_VerifyFinal(evp_md_ctx, sigbuf, siglen, pkey);
	EVP_PKEY_free(pkey);
	free(sigbuf);
#else
294
	status = DSA_do_verify(digest, ISC_SHA1_DIGESTLENGTH, dsasig, dsa);
Francis Dupont's avatar
Francis Dupont committed
295
#endif
296
	DSA_SIG_free(dsasig);
297
298
299
300
	switch (status) {
	case 1:
		return (ISC_R_SUCCESS);
	case 0:
301
		return (dst__openssl_toresult(DST_R_VERIFYFAILURE));
302
	default:
303
304
		return (dst__openssl_toresult3(dctx->category,
					       "DSA_do_verify",
305
306
					       DST_R_VERIFYFAILURE));
	}
307
308
309
310
311
312
313
}

static isc_boolean_t
openssldsa_compare(const dst_key_t *key1, const dst_key_t *key2) {
	int status;
	DSA *dsa1, *dsa2;

314
315
	dsa1 = key1->keydata.dsa;
	dsa2 = key2->keydata.dsa;
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338

	if (dsa1 == NULL && dsa2 == NULL)
		return (ISC_TRUE);
	else if (dsa1 == NULL || dsa2 == NULL)
		return (ISC_FALSE);

	status = BN_cmp(dsa1->p, dsa2->p) ||
		 BN_cmp(dsa1->q, dsa2->q) ||
		 BN_cmp(dsa1->g, dsa2->g) ||
		 BN_cmp(dsa1->pub_key, dsa2->pub_key);

	if (status != 0)
		return (ISC_FALSE);

	if (dsa1->priv_key != NULL || dsa2->priv_key != NULL) {
		if (dsa1->priv_key == NULL || dsa2->priv_key == NULL)
			return (ISC_FALSE);
		if (BN_cmp(dsa1->priv_key, dsa2->priv_key))
			return (ISC_FALSE);
	}
	return (ISC_TRUE);
}

339
#if OPENSSL_VERSION_NUMBER > 0x00908000L
340
341
342
static int
progress_cb(int p, int n, BN_GENCB *cb)
{
343
344
345
346
	union {
		void *dptr;
		void (*fptr)(int);
	} u;
347
348

	UNUSED(n);
349

350
	u.dptr = BN_GENCB_get_arg(cb);
351
352
	if (u.fptr != NULL)
		u.fptr(p);
353
354
	return (1);
}
355
#endif
356
357
358

static isc_result_t
openssldsa_generate(dst_key_t *key, int unused, void (*callback)(int)) {
359
360
361
	DSA *dsa;
	unsigned char rand_array[ISC_SHA1_DIGESTLENGTH];
	isc_result_t result;
362
#if OPENSSL_VERSION_NUMBER > 0x00908000L
363
	BN_GENCB *cb;
364
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
365
366
	BN_GENCB _cb;
#endif
367
368
369
370
	union {
		void *dptr;
		void (*fptr)(int);
	} u;
371

372
#else
373

374
375
	UNUSED(callback);
#endif
376
377
378
379
380
381
382
	UNUSED(unused);

	result = dst__entropy_getdata(rand_array, sizeof(rand_array),
				      ISC_FALSE);
	if (result != ISC_R_SUCCESS)
		return (result);

383
#if OPENSSL_VERSION_NUMBER > 0x00908000L
Automatic Updater's avatar
Automatic Updater committed
384
	dsa = DSA_new();
385
386
	if (dsa == NULL)
		return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
387
	cb = BN_GENCB_new();
388
#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
389
390
391
392
	if (cb == NULL) {
		DSA_free(dsa);
		return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
	}
Mark Andrews's avatar
Mark Andrews committed
393
#endif
394
	if (callback == NULL) {
395
		BN_GENCB_set_old(cb, NULL, NULL);
396
	} else {
397
		u.fptr = callback;
398
		BN_GENCB_set(cb, &progress_cb, u.dptr);
399
	}
Automatic Updater's avatar
Automatic Updater committed
400

401
402
	if (!DSA_generate_parameters_ex(dsa, key->key_size, rand_array,
					ISC_SHA1_DIGESTLENGTH,  NULL, NULL,
403
					cb))
404
	{
405
		DSA_free(dsa);
406
		BN_GENCB_free(cb);
407
408
		return (dst__openssl_toresult2("DSA_generate_parameters_ex",
					       DST_R_OPENSSLFAILURE));
409
	}
410
	BN_GENCB_free(cb);
411
#else
412
413
414
415
	dsa = DSA_generate_parameters(key->key_size, rand_array,
				      ISC_SHA1_DIGESTLENGTH, NULL, NULL,
				      NULL, NULL);
	if (dsa == NULL)
416
417
		return (dst__openssl_toresult2("DSA_generate_parameters",
					       DST_R_OPENSSLFAILURE));
418
#endif
419
420
421

	if (DSA_generate_key(dsa) == 0) {
		DSA_free(dsa);
422
423
		return (dst__openssl_toresult2("DSA_generate_key",
					       DST_R_OPENSSLFAILURE));
424
425
426
	}
	dsa->flags &= ~DSA_FLAG_CACHE_MONT_P;

427
	key->keydata.dsa = dsa;
428
429
430
431
432
433

	return (ISC_R_SUCCESS);
}

static isc_boolean_t
openssldsa_isprivate(const dst_key_t *key) {
434
	DSA *dsa = key->keydata.dsa;
Brian Wellington's avatar
Brian Wellington committed
435
	return (ISC_TF(dsa != NULL && dsa->priv_key != NULL));
436
437
438
439
}

static void
openssldsa_destroy(dst_key_t *key) {
440
	DSA *dsa = key->keydata.dsa;
441
	DSA_free(dsa);
442
	key->keydata.dsa = NULL;
443
444
445
446
447
448
449
450
451
452
}


static isc_result_t
openssldsa_todns(const dst_key_t *key, isc_buffer_t *data) {
	DSA *dsa;
	isc_region_t r;
	int dnslen;
	unsigned int t, p_bytes;

453
	REQUIRE(key->keydata.dsa != NULL);
454

455
	dsa = key->keydata.dsa;
456
457
458
459
460
461
462
463
464
465
466
467

	isc_buffer_availableregion(data, &r);

	t = (BN_num_bytes(dsa->p) - 64) / 8;
	if (t > 8)
		return (DST_R_INVALIDPUBLICKEY);
	p_bytes = 64 + 8 * t;

	dnslen = 1 + (key->key_size * 3)/8 + ISC_SHA1_DIGESTLENGTH;
	if (r.length < (unsigned int) dnslen)
		return (ISC_R_NOSPACE);

468
469
	*r.base = t;
	isc_region_consume(&r, 1);
470
	BN_bn2bin_fixed(dsa->q, r.base, ISC_SHA1_DIGESTLENGTH);
471
	isc_region_consume(&r, ISC_SHA1_DIGESTLENGTH);
472
	BN_bn2bin_fixed(dsa->p, r.base, key->key_size/8);
473
	isc_region_consume(&r, p_bytes);
474
	BN_bn2bin_fixed(dsa->g, r.base, key->key_size/8);
475
	isc_region_consume(&r, p_bytes);
476
	BN_bn2bin_fixed(dsa->pub_key, r.base, key->key_size/8);
477
	isc_region_consume(&r, p_bytes);
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501

	isc_buffer_add(data, dnslen);

	return (ISC_R_SUCCESS);
}

static isc_result_t
openssldsa_fromdns(dst_key_t *key, isc_buffer_t *data) {
	DSA *dsa;
	isc_region_t r;
	unsigned int t, p_bytes;
	isc_mem_t *mctx = key->mctx;

	UNUSED(mctx);

	isc_buffer_remainingregion(data, &r);
	if (r.length == 0)
		return (ISC_R_SUCCESS);

	dsa = DSA_new();
	if (dsa == NULL)
		return (ISC_R_NOMEMORY);
	dsa->flags &= ~DSA_FLAG_CACHE_MONT_P;

502
503
	t = (unsigned int) *r.base;
	isc_region_consume(&r, 1);
504
505
506
507
508
509
	if (t > 8) {
		DSA_free(dsa);
		return (DST_R_INVALIDPUBLICKEY);
	}
	p_bytes = 64 + 8 * t;

510
	if (r.length < ISC_SHA1_DIGESTLENGTH + 3 * p_bytes) {
511
512
513
514
515
		DSA_free(dsa);
		return (DST_R_INVALIDPUBLICKEY);
	}

	dsa->q = BN_bin2bn(r.base, ISC_SHA1_DIGESTLENGTH, NULL);
516
	isc_region_consume(&r, ISC_SHA1_DIGESTLENGTH);
517
518

	dsa->p = BN_bin2bn(r.base, p_bytes, NULL);
519
	isc_region_consume(&r, p_bytes);
520
521

	dsa->g = BN_bin2bn(r.base, p_bytes, NULL);
522
	isc_region_consume(&r, p_bytes);
523
524

	dsa->pub_key = BN_bin2bn(r.base, p_bytes, NULL);
525
	isc_region_consume(&r, p_bytes);
526
527
528
529
530

	key->key_size = p_bytes * 8;

	isc_buffer_forward(data, 1 + ISC_SHA1_DIGESTLENGTH + 3 * p_bytes);

531
	key->keydata.dsa = dsa;
532
533
534
535
536
537
538
539
540
541
542
543

	return (ISC_R_SUCCESS);
}


static isc_result_t
openssldsa_tofile(const dst_key_t *key, const char *directory) {
	int cnt = 0;
	DSA *dsa;
	dst_private_t priv;
	unsigned char bufs[5][128];

544
	if (key->keydata.dsa == NULL)
545
		return (DST_R_NULLKEY);
Tinderbox User's avatar
Tinderbox User committed
546

547
548
549
550
	if (key->external) {
		priv.nelements = 0;
		return (dst__privstruct_writefile(key, &priv, directory));
	}
551

552
	dsa = key->keydata.dsa;
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588

	priv.elements[cnt].tag = TAG_DSA_PRIME;
	priv.elements[cnt].length = BN_num_bytes(dsa->p);
	BN_bn2bin(dsa->p, bufs[cnt]);
	priv.elements[cnt].data = bufs[cnt];
	cnt++;

	priv.elements[cnt].tag = TAG_DSA_SUBPRIME;
	priv.elements[cnt].length = BN_num_bytes(dsa->q);
	BN_bn2bin(dsa->q, bufs[cnt]);
	priv.elements[cnt].data = bufs[cnt];
	cnt++;

	priv.elements[cnt].tag = TAG_DSA_BASE;
	priv.elements[cnt].length = BN_num_bytes(dsa->g);
	BN_bn2bin(dsa->g, bufs[cnt]);
	priv.elements[cnt].data = bufs[cnt];
	cnt++;

	priv.elements[cnt].tag = TAG_DSA_PRIVATE;
	priv.elements[cnt].length = BN_num_bytes(dsa->priv_key);
	BN_bn2bin(dsa->priv_key, bufs[cnt]);
	priv.elements[cnt].data = bufs[cnt];
	cnt++;

	priv.elements[cnt].tag = TAG_DSA_PUBLIC;
	priv.elements[cnt].length = BN_num_bytes(dsa->pub_key);
	BN_bn2bin(dsa->pub_key, bufs[cnt]);
	priv.elements[cnt].data = bufs[cnt];
	cnt++;

	priv.nelements = cnt;
	return (dst__privstruct_writefile(key, &priv, directory));
}

static isc_result_t
589
openssldsa_parse(dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub) {
590
591
592
593
594
595
596
597
	dst_private_t priv;
	isc_result_t ret;
	int i;
	DSA *dsa = NULL;
	isc_mem_t *mctx = key->mctx;
#define DST_RET(a) {ret = a; goto err;}

	/* read private key file */
598
	ret = dst__privstruct_parse(key, DST_ALG_DSA, lexer, mctx, &priv);
599
600
601
	if (ret != ISC_R_SUCCESS)
		return (ret);

602
603
604
605
606
607
608
609
610
611
612
613
614
	if (key->external) {
		if (priv.nelements != 0)
			DST_RET(DST_R_INVALIDPRIVATEKEY);
		if (pub == NULL)
			DST_RET(DST_R_INVALIDPRIVATEKEY);
		key->keydata.pkey = pub->keydata.pkey;
		pub->keydata.pkey = NULL;
		key->key_size = pub->key_size;
		dst__privstruct_free(&priv, mctx);
		memset(&priv, 0, sizeof(priv));
		return (ISC_R_SUCCESS);
	}

615
616
617
618
	dsa = DSA_new();
	if (dsa == NULL)
		DST_RET(ISC_R_NOMEMORY);
	dsa->flags &= ~DSA_FLAG_CACHE_MONT_P;
619
	key->keydata.dsa = dsa;
620

Evan Hunt's avatar
Evan Hunt committed
621
	for (i = 0; i < priv.nelements; i++) {
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
		BIGNUM *bn;
		bn = BN_bin2bn(priv.elements[i].data,
			       priv.elements[i].length, NULL);
		if (bn == NULL)
			DST_RET(ISC_R_NOMEMORY);

		switch (priv.elements[i].tag) {
			case TAG_DSA_PRIME:
				dsa->p = bn;
				break;
			case TAG_DSA_SUBPRIME:
				dsa->q = bn;
				break;
			case TAG_DSA_BASE:
				dsa->g = bn;
				break;
			case TAG_DSA_PRIVATE:
				dsa->priv_key = bn;
				break;
			case TAG_DSA_PUBLIC:
				dsa->pub_key = bn;
				break;
Brian Wellington's avatar
Brian Wellington committed
644
		}
645
646
	}
	dst__privstruct_free(&priv, mctx);
647
	memset(&priv, 0, sizeof(priv));
648
649
650
651
652
653
654
655
656
657
658
659
	key->key_size = BN_num_bits(dsa->p);
	return (ISC_R_SUCCESS);

 err:
	openssldsa_destroy(key);
	dst__privstruct_free(&priv, mctx);
	memset(&priv, 0, sizeof(priv));
	return (ret);
}

static dst_func_t openssldsa_functions = {
	openssldsa_createctx,
Evan Hunt's avatar
Evan Hunt committed
660
	NULL, /*%< createctx2 */
661
662
663
664
	openssldsa_destroyctx,
	openssldsa_adddata,
	openssldsa_sign,
	openssldsa_verify,
665
	NULL, /*%< verify2 */
666
	NULL, /*%< computesecret */
667
	openssldsa_compare,
668
	NULL, /*%< paramcompare */
669
670
671
672
673
674
	openssldsa_generate,
	openssldsa_isprivate,
	openssldsa_destroy,
	openssldsa_todns,
	openssldsa_fromdns,
	openssldsa_tofile,
675
	openssldsa_parse,
676
	NULL, /*%< cleanup */
Francis Dupont's avatar
Francis Dupont committed
677
	NULL, /*%< fromlabel */
678
679
	NULL, /*%< dump */
	NULL, /*%< restore */
680
681
682
683
};

isc_result_t
dst__openssldsa_init(dst_func_t **funcp) {
684
685
686
	REQUIRE(funcp != NULL);
	if (*funcp == NULL)
		*funcp = &openssldsa_functions;
687
688
689
	return (ISC_R_SUCCESS);
}

690
691
692
693
#else /* OPENSSL */

#include <isc/util.h>

Mark Andrews's avatar
Mark Andrews committed
694
EMPTY_TRANSLATION_UNIT
695

696
#endif /* OPENSSL */
697
/*! \file */