opensslrsa_link.c 30.8 KB
Newer Older
1
/*
Automatic Updater's avatar
Automatic Updater committed
2
 * Copyright (C) 2004-2009  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 * Copyright (C) 2000-2003  Internet Software Consortium.
4
 *
Automatic Updater's avatar
Automatic Updater committed
5
 * Permission to use, copy, modify, and/or distribute this software for any
6 7 8
 * 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
9 10 11 12 13 14 15
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC 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.
16 17 18 19
 */

/*
 * Principal Author: Brian Wellington
Automatic Updater's avatar
Automatic Updater committed
20
 * $Id: opensslrsa_link.c,v 1.35 2009/10/27 23:47:45 tbox Exp $
21
 */
22
#ifdef OPENSSL
23 24
#include <config.h>

Francis Dupont's avatar
Francis Dupont committed
25
#ifndef USE_EVP
26 27 28
#if !defined(HAVE_EVP_SHA256) || !defined(HAVE_EVP_SHA512)
#define USE_EVP 0
#else
Francis Dupont's avatar
Francis Dupont committed
29 30
#define USE_EVP 1
#endif
31
#endif
32 33 34 35 36


#include <isc/entropy.h>
#include <isc/md5.h>
#include <isc/sha1.h>
37
#include <isc/sha2.h>
38 39 40 41 42 43 44
#include <isc/mem.h>
#include <isc/string.h>
#include <isc/util.h>

#include <dst/result.h>

#include "dst_internal.h"
45
#include "dst_openssl.h"
46 47 48 49 50
#include "dst_parse.h"

#include <openssl/err.h>
#include <openssl/objects.h>
#include <openssl/rsa.h>
51 52 53
#if OPENSSL_VERSION_NUMBER > 0x00908000L
#include <openssl/bn.h>
#endif
Francis Dupont's avatar
Francis Dupont committed
54
#include <openssl/engine.h>
55

56 57 58 59
/*
 * We don't use configure for windows so enforce the OpenSSL version
 * here.  Unlike with configure we don't support overriding this test.
 */
Mark Andrews's avatar
Mark Andrews committed
60
#ifdef WIN32
61
#if !((OPENSSL_VERSION_NUMBER >= 0x009070cfL && \
62
       OPENSSL_VERSION_NUMBER < 0x00908000L) || \
Automatic Updater's avatar
Automatic Updater committed
63
      OPENSSL_VERSION_NUMBER >= 0x0090804fL)
64 65 66 67 68
#error Please upgrade OpenSSL to 0.9.8d/0.9.7l or greater.
#endif
#endif


69
	/*
Francis Dupont's avatar
Francis Dupont committed
70 71
	 * XXXMPA  Temporarily disable RSA_BLINDING as it requires
	 * good quality random data that cannot currently be guaranteed.
72 73 74 75 76
	 * XXXMPA  Find which versions of openssl use pseudo random data
	 * and set RSA_FLAG_BLINDING for those.
	 */

#if 0
77 78
#if OPENSSL_VERSION_NUMBER < 0x0090601fL
#define SET_FLAGS(rsa) \
79 80 81 82
	do { \
	(rsa)->flags &= ~(RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE); \
	(rsa)->flags |= RSA_FLAG_BLINDING; \
	} while (0)
83
#else
84 85 86 87
#define SET_FLAGS(rsa) \
	do { \
		(rsa)->flags |= RSA_FLAG_BLINDING; \
	} while (0)
88
#endif
89 90 91 92 93 94 95 96
#endif

#if OPENSSL_VERSION_NUMBER < 0x0090601fL
#define SET_FLAGS(rsa) \
	do { \
	(rsa)->flags &= ~(RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE); \
	(rsa)->flags &= ~RSA_FLAG_BLINDING; \
	} while (0)
97 98 99 100 101 102
#elif defined(RSA_FLAG_NO_BLINDING)
#define SET_FLAGS(rsa) \
	do { \
		(rsa)->flags &= ~RSA_FLAG_BLINDING; \
		(rsa)->flags |= RSA_FLAG_NO_BLINDING; \
	} while (0)
103 104 105 106 107 108
#else
#define SET_FLAGS(rsa) \
	do { \
		(rsa)->flags &= ~RSA_FLAG_BLINDING; \
	} while (0)
#endif
109

Francis Dupont's avatar
Francis Dupont committed
110 111
#define DST_RET(a) {ret = a; goto err;}

112 113 114 115
static isc_result_t opensslrsa_todns(const dst_key_t *key, isc_buffer_t *data);

static isc_result_t
opensslrsa_createctx(dst_key_t *key, dst_context_t *dctx) {
Mark Andrews's avatar
Mark Andrews committed
116 117
#if USE_EVP
	EVP_MD_CTX *evp_md_ctx;
118
	const EVP_MD *type = NULL;
Mark Andrews's avatar
Mark Andrews committed
119 120
#endif

121
	UNUSED(key);
122
	REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
123
		dctx->key->key_alg == DST_ALG_RSASHA1 ||
124 125 126
		dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
		dctx->key->key_alg == DST_ALG_RSASHA256 ||
		dctx->key->key_alg == DST_ALG_RSASHA512);
127

Francis Dupont's avatar
Francis Dupont committed
128 129 130 131 132
#if USE_EVP
	evp_md_ctx = EVP_MD_CTX_create();
	if (evp_md_ctx == NULL)
		return (ISC_R_NOMEMORY);

133 134
	switch (dctx->key->key_alg) {
	case DST_ALG_RSAMD5:
Francis Dupont's avatar
Francis Dupont committed
135
		type = EVP_md5();	/* MD5 + RSA */
136 137 138
		break;
	case DST_ALG_RSASHA1:
	case DST_ALG_NSEC3RSASHA1:
Francis Dupont's avatar
Francis Dupont committed
139
		type = EVP_sha1();	/* SHA1 + RSA */
140
		break;
141
#ifdef HAVE_EVP_SHA256
142 143 144
	case DST_ALG_RSASHA256:
		type = EVP_sha256();	/* SHA256 + RSA */
		break;
145 146
#endif
#ifdef HAVE_EVP_SHA512
147 148 149
	case DST_ALG_RSASHA512:
		type = EVP_sha512();
		break;
150
#endif
151 152 153
	default:
		INSIST(0);
	}
Francis Dupont's avatar
Francis Dupont committed
154 155 156 157 158 159 160

	if (!EVP_DigestInit_ex(evp_md_ctx, type, NULL)) {
		EVP_MD_CTX_destroy(evp_md_ctx);
		return (ISC_R_FAILURE);
	}
	dctx->ctxdata.evp_md_ctx = evp_md_ctx;
#else
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	switch (dctx->key->key_alg) {
	case DST_ALG_RSAMD5:
		{
			isc_md5_t *md5ctx;

			md5ctx = isc_mem_get(dctx->mctx, sizeof(isc_md5_t));
			if (md5ctx == NULL)
				return (ISC_R_NOMEMORY);
			isc_md5_init(md5ctx);
			dctx->ctxdata.md5ctx = md5ctx;
		}
		break;
	case DST_ALG_RSASHA1:
	case DST_ALG_NSEC3RSASHA1:
		{
			isc_sha1_t *sha1ctx;

			sha1ctx = isc_mem_get(dctx->mctx, sizeof(isc_sha1_t));
			if (sha1ctx == NULL)
				return (ISC_R_NOMEMORY);
			isc_sha1_init(sha1ctx);
			dctx->ctxdata.sha1ctx = sha1ctx;
		}
		break;
	case DST_ALG_RSASHA256:
		{
			isc_sha256_t *sha256ctx;

Automatic Updater's avatar
Automatic Updater committed
189
			sha256ctx = isc_mem_get(dctx->mctx,
190 191 192 193 194 195 196 197 198 199 200
						sizeof(isc_sha256_t));
			if (sha256ctx == NULL)
				return (ISC_R_NOMEMORY);
			isc_sha256_init(sha256ctx);
			dctx->ctxdata.sha256ctx = sha256ctx;
		}
		break;
	case DST_ALG_RSASHA512:
		{
			isc_sha512_t *sha512ctx;

Automatic Updater's avatar
Automatic Updater committed
201
			sha512ctx = isc_mem_get(dctx->mctx,
202 203 204 205 206 207 208 209 210
						sizeof(isc_sha512_t));
			if (sha512ctx == NULL)
				return (ISC_R_NOMEMORY);
			isc_sha512_init(sha512ctx);
			dctx->ctxdata.sha512ctx = sha512ctx;
		}
		break;
	default:
		INSIST(0);
211
	}
Francis Dupont's avatar
Francis Dupont committed
212
#endif
213 214 215 216 217 218

	return (ISC_R_SUCCESS);
}

static void
opensslrsa_destroyctx(dst_context_t *dctx) {
Mark Andrews's avatar
Mark Andrews committed
219 220 221 222
#if USE_EVP
	EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
#endif

223
	REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
224
		dctx->key->key_alg == DST_ALG_RSASHA1 ||
225 226 227
		dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
		dctx->key->key_alg == DST_ALG_RSASHA256 ||
		dctx->key->key_alg == DST_ALG_RSASHA512);
228

Francis Dupont's avatar
Francis Dupont committed
229 230 231 232 233 234
#if USE_EVP
	if (evp_md_ctx != NULL) {
		EVP_MD_CTX_destroy(evp_md_ctx);
		dctx->ctxdata.evp_md_ctx = NULL;
	}
#else
235 236 237 238 239 240 241 242 243 244 245
	switch (dctx->key->key_alg) {
	case DST_ALG_RSAMD5:
		{
			isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;

			if (md5ctx != NULL) {
				isc_md5_invalidate(md5ctx);
				isc_mem_put(dctx->mctx, md5ctx,
					    sizeof(isc_md5_t));
				dctx->ctxdata.md5ctx = NULL;
			}
246
		}
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
		break;
	case DST_ALG_RSASHA1:
	case DST_ALG_NSEC3RSASHA1:
		{
			isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;

			if (sha1ctx != NULL) {
				isc_sha1_invalidate(sha1ctx);
				isc_mem_put(dctx->mctx, sha1ctx,
					    sizeof(isc_sha1_t));
				dctx->ctxdata.sha1ctx = NULL;
			}
		}
		break;
	case DST_ALG_RSASHA256:
		{
			isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;

			if (sha256ctx != NULL) {
				isc_sha256_invalidate(sha256ctx);
				isc_mem_put(dctx->mctx, sha256ctx,
					    sizeof(isc_sha256_t));
				dctx->ctxdata.sha256ctx = NULL;
			}
		}
		break;
	case DST_ALG_RSASHA512:
		{
			isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;

			if (sha512ctx != NULL) {
				isc_sha512_invalidate(sha512ctx);
				isc_mem_put(dctx->mctx, sha512ctx,
					    sizeof(isc_sha512_t));
				dctx->ctxdata.sha512ctx = NULL;
			}
283
		}
284 285 286
		break;
	default:
		INSIST(0);
287
	}
Francis Dupont's avatar
Francis Dupont committed
288
#endif
289 290 291 292
}

static isc_result_t
opensslrsa_adddata(dst_context_t *dctx, const isc_region_t *data) {
Mark Andrews's avatar
Mark Andrews committed
293 294
#if USE_EVP
	EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
Automatic Updater's avatar
Automatic Updater committed
295
#endif
Mark Andrews's avatar
Mark Andrews committed
296

297
	REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
298
		dctx->key->key_alg == DST_ALG_RSASHA1 ||
299 300 301
		dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
		dctx->key->key_alg == DST_ALG_RSASHA256 ||
		dctx->key->key_alg == DST_ALG_RSASHA512);
302

Francis Dupont's avatar
Francis Dupont committed
303 304 305 306 307
#if USE_EVP
	if (!EVP_DigestUpdate(evp_md_ctx, data->base, data->length)) {
		return (ISC_R_FAILURE);
	}
#else
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
	switch (dctx->key->key_alg) {
	case DST_ALG_RSAMD5:
		{
			isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;

			isc_md5_update(md5ctx, data->base, data->length);
		}
		break;
	case DST_ALG_RSASHA1:
	case DST_ALG_NSEC3RSASHA1:
		{
			isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;

			isc_sha1_update(sha1ctx, data->base, data->length);
		}
		break;
	case DST_ALG_RSASHA256:
		{
			isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;

			isc_sha256_update(sha256ctx, data->base, data->length);
		}
		break;
	case DST_ALG_RSASHA512:
		{
			isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;

			isc_sha512_update(sha512ctx, data->base, data->length);
		}
		break;
	default:
		INSIST(0);
340
	}
Francis Dupont's avatar
Francis Dupont committed
341
#endif
342 343 344
	return (ISC_R_SUCCESS);
}

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
#if ! USE_EVP && OPENSSL_VERSION_NUMBER < 0x00908000L
/*
 * Digest prefixes from RFC XXXX (draft-ietf-dnsext-dnssec-rsasha256-14).
 */
static unsigned char sha256_prefix[] =
	 { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
	   0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20};
static unsigned char sha512_prefix[] =
	 { 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
	   0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40};
#define PREFIXLEN sizeof(sha512_prefix)
#else
#define PREFIXLEN 0
#endif

360 361 362 363
static isc_result_t
opensslrsa_sign(dst_context_t *dctx, isc_buffer_t *sig) {
	dst_key_t *key = dctx->key;
	isc_region_t r;
Francis Dupont's avatar
Francis Dupont committed
364 365 366 367 368 369
	unsigned int siglen = 0;
#if USE_EVP
	EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
	EVP_PKEY *pkey = key->keydata.pkey;
#else
	RSA *rsa = key->keydata.rsa;
370
	/* note: ISC_SHA512_DIGESTLENGTH >= ISC_*_DIGESTLENGTH */
371
	unsigned char digest[PREFIXLEN + ISC_SHA512_DIGESTLENGTH];
372
	int status;
373 374
	int type = 0;
	unsigned int digestlen = 0;
375 376 377 378
	char *message;
	unsigned long err;
	const char* file;
	int line;
379 380 381 382
#if OPENSSL_VERSION_NUMBER < 0x00908000L
	unsigned int prefixlen = 0;
	const unsigned char *prefix = NULL;
#endif
Francis Dupont's avatar
Francis Dupont committed
383
#endif
384

385
	REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
386
		dctx->key->key_alg == DST_ALG_RSASHA1 ||
387 388 389
		dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
		dctx->key->key_alg == DST_ALG_RSASHA256 ||
		dctx->key->key_alg == DST_ALG_RSASHA512);
390

391 392
	isc_buffer_availableregion(sig, &r);

Francis Dupont's avatar
Francis Dupont committed
393 394 395 396 397 398 399 400
#if USE_EVP
	if (r.length < (unsigned int) EVP_PKEY_size(pkey))
		return (ISC_R_NOSPACE);

	if (!EVP_SignFinal(evp_md_ctx, r.base, &siglen, pkey)) {
		return (ISC_R_FAILURE);
	}
#else
401 402 403
	if (r.length < (unsigned int) RSA_size(rsa))
		return (ISC_R_NOSPACE);

404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
	switch (dctx->key->key_alg) {
	case DST_ALG_RSAMD5:
		{
			isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;

			isc_md5_final(md5ctx, digest);
			type = NID_md5;
			digestlen = ISC_MD5_DIGESTLENGTH;
		}
		break;
	case DST_ALG_RSASHA1:
	case DST_ALG_NSEC3RSASHA1:
		{
			isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;

			isc_sha1_final(sha1ctx, digest);
			type = NID_sha1;
			digestlen = ISC_SHA1_DIGESTLENGTH;
		}
		break;
	case DST_ALG_RSASHA256:
		{
			isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;

			isc_sha256_final(digest, sha256ctx);
			digestlen = ISC_SHA256_DIGESTLENGTH;
430 431 432 433 434 435
#if OPENSSL_VERSION_NUMBER < 0x00908000L
			prefix = sha256_prefix;
			prefixlen = sizeof(sha256_prefix);
#else
			type = NID_sha256;
#endif
436 437 438 439 440 441 442 443
		}
		break;
	case DST_ALG_RSASHA512:
		{
			isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;

			isc_sha512_final(digest, sha512ctx);
			digestlen = ISC_SHA512_DIGESTLENGTH;
444 445 446 447 448 449
#if OPENSSL_VERSION_NUMBER < 0x00908000L
			prefix = sha512_prefix;
			prefixlen = sizeof(sha512_prefix);
#else
			type = NID_sha512;
#endif
450 451 452 453
		}
		break;
	default:
		INSIST(0);
454 455
	}

456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
#if OPENSSL_VERSION_NUMBER < 0x00908000L
	switch (dctx->key->key_alg) {
	case DST_ALG_RSAMD5:
	case DST_ALG_RSASHA1:
	case DST_ALG_NSEC3RSASHA1:
		INSIST(type != 0);
		status = RSA_sign(type, digest, digestlen, r.base,
				  &siglen, rsa);
		break;

	case DST_ALG_RSASHA256:
	case DST_ALG_RSASHA512:
		INSIST(prefix != NULL);
		INSIST(prefixlen != 0);
		INSIST(prefixlen + digestlen <= sizeof(digest));

		memmove(digest + prefixlen, digest, digestlen);
		memcpy(digest, prefix, prefixlen);
		status = RSA_private_encrypt(digestlen + prefixlen,
					     digest, r.base, rsa,
					     RSA_PKCS1_PADDING);
		if (status < 0)
			status = 0;
		else
			siglen = status;
		break;

	default:
		INSIST(0);
	}
#else
	INSIST(type != 0);
488
	status = RSA_sign(type, digest, digestlen, r.base, &siglen, rsa);
489
#endif
490 491
	if (status == 0) {
		err = ERR_peek_error_line(&file, &line);
Mark Andrews's avatar
Mark Andrews committed
492
		if (err != 0U) {
493 494
			message = ERR_error_string(err, NULL);
		}
495
		return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
496
	}
Francis Dupont's avatar
Francis Dupont committed
497
#endif
498 499 500 501 502 503 504 505 506

	isc_buffer_add(sig, siglen);

	return (ISC_R_SUCCESS);
}

static isc_result_t
opensslrsa_verify(dst_context_t *dctx, const isc_region_t *sig) {
	dst_key_t *key = dctx->key;
Francis Dupont's avatar
Francis Dupont committed
507 508 509 510 511
	int status = 0;
#if USE_EVP
	EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
	EVP_PKEY *pkey = key->keydata.pkey;
#else
512 513 514 515
	/* note: ISC_SHA512_DIGESTLENGTH >= ISC_*_DIGESTLENGTH */
	unsigned char digest[ISC_SHA512_DIGESTLENGTH];
	int type = 0;
	unsigned int digestlen = 0;
Francis Dupont's avatar
Francis Dupont committed
516
	RSA *rsa = key->keydata.rsa;
517 518 519 520
#if OPENSSL_VERSION_NUMBER < 0x00908000L
	unsigned int prefixlen = 0;
	const unsigned char *prefix = NULL;
#endif
Francis Dupont's avatar
Francis Dupont committed
521
#endif
522

523
	REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
524
		dctx->key->key_alg == DST_ALG_RSASHA1 ||
525 526 527
		dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
		dctx->key->key_alg == DST_ALG_RSASHA256 ||
		dctx->key->key_alg == DST_ALG_RSASHA512);
528

Francis Dupont's avatar
Francis Dupont committed
529 530 531
#if USE_EVP
	status = EVP_VerifyFinal(evp_md_ctx, sig->base, sig->length, pkey);
#else
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
	switch (dctx->key->key_alg) {
	case DST_ALG_RSAMD5:
		{
			isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;

			isc_md5_final(md5ctx, digest);
			type = NID_md5;
			digestlen = ISC_MD5_DIGESTLENGTH;
		}
		break;
	case DST_ALG_RSASHA1:
	case DST_ALG_NSEC3RSASHA1:
		{
			isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;

			isc_sha1_final(sha1ctx, digest);
			type = NID_sha1;
			digestlen = ISC_SHA1_DIGESTLENGTH;
		}
		break;
	case DST_ALG_RSASHA256:
		{
			isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;

			isc_sha256_final(digest, sha256ctx);
			digestlen = ISC_SHA256_DIGESTLENGTH;
558 559 560 561 562 563
#if OPENSSL_VERSION_NUMBER < 0x00908000L
			prefix = sha256_prefix;
			prefixlen = sizeof(sha256_prefix);
#else
			type = NID_sha256;
#endif
564 565 566 567 568 569 570 571
		}
		break;
	case DST_ALG_RSASHA512:
		{
			isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;

			isc_sha512_final(digest, sha512ctx);
			digestlen = ISC_SHA512_DIGESTLENGTH;
572 573 574 575 576 577
#if OPENSSL_VERSION_NUMBER < 0x00908000L
			prefix = sha512_prefix;
			prefixlen = sizeof(sha512_prefix);
#else
			type = NID_sha512;
#endif
578 579 580 581
		}
		break;
	default:
		INSIST(0);
582 583
	}

584
	if (sig->length != (unsigned int) RSA_size(rsa))
585 586
		return (DST_R_VERIFYFAILURE);

587 588 589 590 591 592 593 594 595 596
#if OPENSSL_VERSION_NUMBER < 0x00908000L
	switch (dctx->key->key_alg) {
	case DST_ALG_RSAMD5:
	case DST_ALG_RSASHA1:
	case DST_ALG_NSEC3RSASHA1:
		INSIST(type != 0);
		status = RSA_verify(type, digest, digestlen, sig->base,
				    RSA_size(rsa), rsa);
		break;

Automatic Updater's avatar
Automatic Updater committed
597
	case DST_ALG_RSASHA256:
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
	case DST_ALG_RSASHA512:
		{
			/*
			 * 1024 is big enough for all valid RSA bit sizes
			 * for use with DNSSEC.
			 */
			unsigned char original[PREFIXLEN + 1024];

			INSIST(prefix != NULL);
			INSIST(prefixlen != 0U);

			if (RSA_size(rsa) > (int)sizeof(original))
				return (DST_R_VERIFYFAILURE);

			status = RSA_public_decrypt(sig->length, sig->base,
						    original, rsa,
						    RSA_PKCS1_PADDING);
Automatic Updater's avatar
Automatic Updater committed
615
			if (status <= 0)
616
				return (DST_R_VERIFYFAILURE);
Automatic Updater's avatar
Automatic Updater committed
617
			if (status != (int)(prefixlen + digestlen))
618 619 620 621 622 623 624 625 626 627 628 629 630 631
				return (DST_R_VERIFYFAILURE);
			if (memcmp(original, prefix, prefixlen))
				return (DST_R_VERIFYFAILURE);
			if (memcmp(original + prefixlen, digest, digestlen))
				return (DST_R_VERIFYFAILURE);
			status = 1;
		}
		break;

	default:
		INSIST(0);
	}
#else
	INSIST(type != 0);
632
	status = RSA_verify(type, digest, digestlen, sig->base,
633 634
			     RSA_size(rsa), rsa);
#endif
Francis Dupont's avatar
Francis Dupont committed
635
#endif
Francis Dupont's avatar
Francis Dupont committed
636
	if (status != 1)
637
		return (dst__openssl_toresult(DST_R_VERIFYFAILURE));
638 639 640 641 642 643 644

	return (ISC_R_SUCCESS);
}

static isc_boolean_t
opensslrsa_compare(const dst_key_t *key1, const dst_key_t *key2) {
	int status;
Francis Dupont's avatar
Francis Dupont committed
645 646 647 648
	RSA *rsa1 = NULL, *rsa2 = NULL;
#if USE_EVP
	EVP_PKEY *pkey1, *pkey2;
#endif
649

Francis Dupont's avatar
Francis Dupont committed
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
#if USE_EVP
	pkey1 = key1->keydata.pkey;
	pkey2 = key2->keydata.pkey;
	/*
	 * The pkey reference will keep these around after
	 * the RSA_free() call.
	 */
	if (pkey1 != NULL) {
		rsa1 = EVP_PKEY_get1_RSA(pkey1);
		RSA_free(rsa1);
	}
	if (pkey2 != NULL) {
		rsa2 = EVP_PKEY_get1_RSA(pkey2);
		RSA_free(rsa2);
	}
#else
666 667
	rsa1 = key1->keydata.rsa;
	rsa2 = key2->keydata.rsa;
Francis Dupont's avatar
Francis Dupont committed
668
#endif
669 670 671 672 673 674 675 676 677 678 679 680

	if (rsa1 == NULL && rsa2 == NULL)
		return (ISC_TRUE);
	else if (rsa1 == NULL || rsa2 == NULL)
		return (ISC_FALSE);

	status = BN_cmp(rsa1->n, rsa2->n) ||
		 BN_cmp(rsa1->e, rsa2->e);

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

Francis Dupont's avatar
Francis Dupont committed
681 682 683 684 685 686 687 688 689 690 691 692 693
#if USE_EVP
	if ((rsa1->flags & RSA_FLAG_EXT_PKEY) != 0 ||
	    (rsa2->flags & RSA_FLAG_EXT_PKEY) != 0) {
		if ((rsa1->flags & RSA_FLAG_EXT_PKEY) == 0 ||
		    (rsa2->flags & RSA_FLAG_EXT_PKEY) == 0)
			return (ISC_FALSE);
		/*
		 * Can't compare private parameters, BTW does it make sense?
		 */
		return (ISC_TRUE);
	}
#endif

694 695 696 697 698 699 700 701 702 703 704 705 706
	if (rsa1->d != NULL || rsa2->d != NULL) {
		if (rsa1->d == NULL || rsa2->d == NULL)
			return (ISC_FALSE);
		status = BN_cmp(rsa1->d, rsa2->d) ||
			 BN_cmp(rsa1->p, rsa2->p) ||
			 BN_cmp(rsa1->q, rsa2->q);

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

707 708 709 710 711 712 713 714 715 716 717 718 719
#if OPENSSL_VERSION_NUMBER > 0x00908000L
static int
progress_cb(int p, int n, BN_GENCB *cb)
{
	void (*callback)(int) = cb->arg;

	UNUSED(n);
	if (callback != NULL)
		callback(p);
	return (1);
}
#endif

720
static isc_result_t
721
opensslrsa_generate(dst_key_t *key, int exp, void (*callback)(int)) {
722
#if OPENSSL_VERSION_NUMBER > 0x00908000L
723 724 725
	BN_GENCB cb;
	RSA *rsa = RSA_new();
	BIGNUM *e = BN_new();
Francis Dupont's avatar
Francis Dupont committed
726 727 728
#if USE_EVP
	EVP_PKEY *pkey = EVP_PKEY_new();
#endif
729

730
	if (rsa == NULL || e == NULL)
731
		goto err;
Francis Dupont's avatar
Francis Dupont committed
732 733 734 735 736 737
#if USE_EVP
	if (pkey == NULL)
		goto err;
	if (!EVP_PKEY_set1_RSA(pkey, rsa))
		goto err;
#endif
738

739 740 741 742 743
	if (exp == 0) {
		/* RSA_F4 0x10001 */
		BN_set_bit(e, 0);
		BN_set_bit(e, 16);
	} else {
744
		/* F5 0x100000001 */
745
		BN_set_bit(e, 0);
746
		BN_set_bit(e, 32);
747 748
	}

749 750 751 752 753
	if (callback == NULL) {
		BN_GENCB_set_old(&cb, NULL, NULL);
	} else {
		BN_GENCB_set(&cb, &progress_cb, callback);
	}
754

755 756
	if (RSA_generate_key_ex(rsa, key->key_size, e, &cb)) {
		BN_free(e);
757
		SET_FLAGS(rsa);
Francis Dupont's avatar
Francis Dupont committed
758 759
#if USE_EVP
		key->keydata.pkey = pkey;
Automatic Updater's avatar
Automatic Updater committed
760

Francis Dupont's avatar
Francis Dupont committed
761 762
		RSA_free(rsa);
#else
763
		key->keydata.rsa = rsa;
Francis Dupont's avatar
Francis Dupont committed
764
#endif
765
		return (ISC_R_SUCCESS);
766
	}
767

768
err:
Francis Dupont's avatar
Francis Dupont committed
769 770 771 772
#if USE_EVP
	if (pkey != NULL)
		EVP_PKEY_free(pkey);
#endif
773
	if (e != NULL)
774
		BN_free(e);
775
	if (rsa != NULL)
776
		RSA_free(rsa);
777
	return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
778
#else
779 780
	RSA *rsa;
	unsigned long e;
Francis Dupont's avatar
Francis Dupont committed
781 782
#if USE_EVP
	EVP_PKEY *pkey = EVP_PKEY_new();
Automatic Updater's avatar
Automatic Updater committed
783

784 785
	UNUSED(callback);

Francis Dupont's avatar
Francis Dupont committed
786 787
	if (pkey == NULL)
		return (ISC_R_NOMEMORY);
788 789
#else
	UNUSED(callback);
Francis Dupont's avatar
Francis Dupont committed
790
#endif
791 792

	if (exp == 0)
793
	       e = RSA_F4;
794
	else
795
	       e = 0x40000003;
796
	rsa = RSA_generate_key(key->key_size, e, NULL, NULL);
Francis Dupont's avatar
Francis Dupont committed
797 798 799 800 801 802
	if (rsa == NULL) {
#if USE_EVP
		EVP_PKEY_free(pkey);
#endif
		return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
	}
803
	SET_FLAGS(rsa);
Francis Dupont's avatar
Francis Dupont committed
804 805 806 807 808 809 810 811 812
#if USE_EVP
	if (!EVP_PKEY_set1_RSA(pkey, rsa)) {
		EVP_PKEY_free(pkey);
		RSA_free(rsa);
		return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
	}
	key->keydata.pkey = pkey;
	RSA_free(rsa);
#else
813
	key->keydata.rsa = rsa;
Francis Dupont's avatar
Francis Dupont committed
814
#endif
815 816

	return (ISC_R_SUCCESS);
817
#endif
818 819 820 821
}

static isc_boolean_t
opensslrsa_isprivate(const dst_key_t *key) {
Francis Dupont's avatar
Francis Dupont committed
822 823 824 825 826 827 828 829 830 831
#if USE_EVP
	RSA *rsa = EVP_PKEY_get1_RSA(key->keydata.pkey);
	INSIST(rsa != NULL);
	RSA_free(rsa);
	/* key->keydata.pkey still has a reference so rsa is still valid. */
#else
	RSA *rsa = key->keydata.rsa;
#endif
	if (rsa != NULL && (rsa->flags & RSA_FLAG_EXT_PKEY) != 0)
		return (ISC_TRUE);
Brian Wellington's avatar
Brian Wellington committed
832
	return (ISC_TF(rsa != NULL && rsa->d != NULL));
833 834 835 836
}

static void
opensslrsa_destroy(dst_key_t *key) {
Francis Dupont's avatar
Francis Dupont committed
837 838 839 840 841
#if USE_EVP
	EVP_PKEY *pkey = key->keydata.pkey;
	EVP_PKEY_free(pkey);
	key->keydata.pkey = NULL;
#else
842
	RSA *rsa = key->keydata.rsa;
843
	RSA_free(rsa);
844
	key->keydata.rsa = NULL;
Francis Dupont's avatar
Francis Dupont committed
845
#endif
846 847 848 849 850 851 852 853
}


static isc_result_t
opensslrsa_todns(const dst_key_t *key, isc_buffer_t *data) {
	isc_region_t r;
	unsigned int e_bytes;
	unsigned int mod_bytes;
Francis Dupont's avatar
Francis Dupont committed
854 855 856 857 858
	isc_result_t ret;
	RSA *rsa;
#if USE_EVP
	EVP_PKEY *pkey;
#endif
859

Francis Dupont's avatar
Francis Dupont committed
860 861 862
#if USE_EVP
	REQUIRE(key->keydata.pkey != NULL);
#else
863
	REQUIRE(key->keydata.rsa != NULL);
Francis Dupont's avatar
Francis Dupont committed
864
#endif
865

Francis Dupont's avatar
Francis Dupont committed
866 867 868 869 870 871
#if USE_EVP
	pkey = key->keydata.pkey;
	rsa = EVP_PKEY_get1_RSA(pkey);
	if (rsa == NULL)
		return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
#else
872
	rsa = key->keydata.rsa;
Francis Dupont's avatar
Francis Dupont committed
873
#endif
874 875 876 877 878 879

	isc_buffer_availableregion(data, &r);

	e_bytes = BN_num_bytes(rsa->e);
	mod_bytes = BN_num_bytes(rsa->n);

880
	if (e_bytes < 256) {	/*%< key exponent is <= 2040 bits */
881
		if (r.length < 1)
Francis Dupont's avatar
Francis Dupont committed
882
			DST_RET(ISC_R_NOSPACE);
883
		isc_buffer_putuint8(data, (isc_uint8_t) e_bytes);
884
		isc_region_consume(&r, 1);
885 886
	} else {
		if (r.length < 3)
Francis Dupont's avatar
Francis Dupont committed
887
			DST_RET(ISC_R_NOSPACE);
888 889
		isc_buffer_putuint8(data, 0);
		isc_buffer_putuint16(data, (isc_uint16_t) e_bytes);
890
		isc_region_consume(&r, 3);
891 892 893
	}

	if (r.length < e_bytes + mod_bytes)
894
		DST_RET(ISC_R_NOSPACE);
895 896

	BN_bn2bin(rsa->e, r.base);
897
	isc_region_consume(&r, e_bytes);
898 899 900 901
	BN_bn2bin(rsa->n, r.base);

	isc_buffer_add(data, e_bytes + mod_bytes);

Francis Dupont's avatar
Francis Dupont committed
902 903 904 905 906 907 908
	ret = ISC_R_SUCCESS;
 err:
#if USE_EVP
	if (rsa != NULL)
		RSA_free(rsa);
#endif
	return (ret);
909 910 911 912 913 914 915
}

static isc_result_t
opensslrsa_fromdns(dst_key_t *key, isc_buffer_t *data) {
	RSA *rsa;
	isc_region_t r;
	unsigned int e_bytes;
Francis Dupont's avatar
Francis Dupont committed
916 917 918
#if USE_EVP
	EVP_PKEY *pkey;
#endif
919 920 921 922 923 924 925

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

	rsa = RSA_new();
	if (rsa == NULL)
926
		return (dst__openssl_toresult(ISC_R_NOMEMORY));
927
	SET_FLAGS(rsa);
928

Brian Wellington's avatar
Brian Wellington committed
929 930
	if (r.length < 1) {
		RSA_free(rsa);
931
		return (DST_R_INVALIDPUBLICKEY);
Brian Wellington's avatar
Brian Wellington committed
932
	}
933 934 935 936
	e_bytes = *r.base++;
	r.length--;

	if (e_bytes == 0) {
Brian Wellington's avatar
Brian Wellington committed
937 938
		if (r.length < 2) {
			RSA_free(rsa);
939
			return (DST_R_INVALIDPUBLICKEY);
Brian Wellington's avatar
Brian Wellington committed
940
		}
941 942 943 944 945
		e_bytes = ((*r.base++) << 8);
		e_bytes += *r.base++;
		r.length -= 2;
	}

Brian Wellington's avatar
Brian Wellington committed
946 947
	if (r.length < e_bytes) {
		RSA_free(rsa);
948
		return (DST_R_INVALIDPUBLICKEY);
Brian Wellington's avatar
Brian Wellington committed
949
	}
950 951 952 953 954 955 956 957 958 959
	rsa->e = BN_bin2bn(r.base, e_bytes, NULL);
	r.base += e_bytes;
	r.length -= e_bytes;

	rsa->n = BN_bin2bn(r.base, r.length, NULL);

	key->key_size = BN_num_bits(rsa->n);

	isc_buffer_forward(data, r.length);

Francis Dupont's avatar
Francis Dupont committed
960 961 962 963 964 965 966 967 968 969 970 971 972 973
#if USE_EVP
	pkey = EVP_PKEY_new();
	if (pkey == NULL) {
		RSA_free(rsa);
		return (ISC_R_NOMEMORY);
	}
	if (!EVP_PKEY_set1_RSA(pkey, rsa)) {
		EVP_PKEY_free(pkey);
		RSA_free(rsa);
		return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
	}
	key->keydata.pkey = pkey;
	RSA_free(rsa);
#else
974
	key->keydata.rsa = rsa;
Francis Dupont's avatar
Francis Dupont committed
975
#endif
976 977 978 979 980 981

	return (ISC_R_SUCCESS);
}

static isc_result_t
opensslrsa_tofile(const dst_key_t *key, const char *directory) {
982
	int i;
983 984
	RSA *rsa;
	dst_private_t priv;
985 986
	unsigned char *bufs[8];
	isc_result_t result;
987

Francis Dupont's avatar
Francis Dupont committed
988 989 990 991 992 993 994
#if USE_EVP
	if (key->keydata.pkey == NULL)
		return (DST_R_NULLKEY);
	rsa = EVP_PKEY_get1_RSA(key->keydata.pkey);
	if (rsa == NULL)
		return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
#else
995
	if (key->keydata.rsa == NULL)
996
		return (DST_R_NULLKEY);
997
	rsa = key->keydata.rsa;
Francis Dupont's avatar
Francis Dupont committed
998
#endif
999

1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
	for (i = 0; i < 8; i++) {
		bufs[i] = isc_mem_get(key->mctx, BN_num_bytes(rsa->n));
		if (bufs[i] == NULL) {
			result = ISC_R_NOMEMORY;
			goto fail;
		}
	}

	i = 0;

	priv.elements[i].tag = TAG_RSA_MODULUS;
	priv.elements[i].length = BN_num_bytes(rsa->n);
	BN_bn2bin(rsa->n, bufs[i]);
	priv.elements[i].data = bufs[i];
	i++;

	priv.elements[i].tag = TAG_RSA_PUBLICEXPONENT;
	priv.elements[i].length = BN_num_bytes(rsa->e);
	BN_bn2bin(rsa->e, bufs[i]);
	priv.elements[i].data = bufs[i];
	i++;

Francis Dupont's avatar
Francis Dupont committed
1022 1023 1024 1025 1026 1027 1028
	if (rsa->d != NULL) {
		priv.elements[i].tag = TAG_RSA_PRIVATEEXPONENT;
		priv.elements[i].length = BN_num_bytes(rsa->d);
		BN_bn2bin(rsa->d, bufs[i]);
		priv.elements[i].data = bufs[i];
		i++;
	}
1029

Francis Dupont's avatar
Francis Dupont committed
1030 1031 1032 1033 1034 1035 1036
	if (rsa->p != NULL) {
		priv.elements[i].tag = TAG_RSA_PRIME1;
		priv.elements[i].length = BN_num_bytes(rsa->p);
		BN_bn2bin(rsa->p, bufs[i]);
		priv.elements[i].data = bufs[i];
		i++;
	}
1037

Francis Dupont's avatar
Francis Dupont committed
1038 1039 1040 1041 1042 1043 1044
	if (rsa->q != NULL) {
		priv.elements[i].tag = TAG_RSA_PRIME2;
		priv.elements[i].length = BN_num_bytes(rsa->q);
		BN_bn2bin(rsa->q, bufs[i]);
		priv.elements[i].data = bufs[i];
		i++;
	}
1045

Francis Dupont's avatar
Francis Dupont committed
1046 1047 1048 1049 1050 1051 1052
	if (rsa->dmp1 != NULL) {
		priv.elements[i].tag = TAG_RSA_EXPONENT1;
		priv.elements[i].length = BN_num_bytes(rsa->dmp1);
		BN_bn2bin(rsa->dmp1, bufs[i]);
		priv.elements[i].data = bufs[i];
		i++;
	}
1053

Francis Dupont's avatar
Francis Dupont committed
1054 1055 1056 1057 1058 1059 1060
	if (rsa->dmq1 != NULL) {
		priv.elements[i].tag = TAG_RSA_EXPONENT2;
		priv.elements[i].length = BN_num_bytes(rsa->dmq1);
		BN_bn2bin(rsa->dmq1, bufs[i]);
		priv.elements[i].data = bufs[i];
		i++;
	}
1061

Francis Dupont's avatar
Francis Dupont committed
1062 1063 1064 1065 1066 1067 1068
	if (rsa->iqmp != NULL) {
		priv.elements[i].tag = TAG_RSA_COEFFICIENT;
		priv.elements[i].length = BN_num_bytes(rsa->iqmp);
		BN_bn2bin(rsa->iqmp, bufs[i]);
		priv.elements[i].data = bufs[i];
		i++;
	}
Automatic Updater's avatar
Automatic Updater committed
1069

Francis Dupont's avatar
Francis Dupont committed
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
	if (key->engine != NULL) {
		priv.elements[i].tag = TAG_RSA_ENGINE;
		priv.elements[i].length = strlen(key->engine) + 1;
		priv.elements[i].data = (unsigned char *)key->engine;
		i++;
	}

	if (key->label != NULL) {
		priv.elements[i].tag = TAG_RSA_LABEL;
		priv.elements[i].length = strlen(key->label) + 1;
		priv.elements[i].data = (unsigned char *)key->label;
		i++;
	}
1083

1084

1085
	priv.nelements = i;
1086
	result = dst__privstruct_writefile(key, &priv, directory);
1087
 fail:
Francis Dupont's avatar
Francis Dupont committed
1088 1089 1090
#if USE_EVP
	RSA_free(rsa);
#endif
1091 1092 1093 1094 1095
	for (i = 0; i < 8; i++) {
		if (bufs[i] == NULL)
			break;
		isc_mem_put(key->mctx, bufs[i], BN_num_bytes(rsa->n));
	}
1096
	return (result);
1097 1098 1099
}

static isc_result_t
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
rsa_check(RSA *rsa, RSA *pub)
{
	/* Public parameters should be the same but if they are not set
	 * copy them from the public key. */
	if (pub != NULL) {
		if (rsa->n != NULL) {
			if (BN_cmp(rsa->n, pub->n) != 0)
				return (DST_R_INVALIDPRIVATEKEY);
		} else {
			rsa->n = pub->n;
			pub->n = NULL;
		}
		if (rsa->e != NULL) {
			if (BN_cmp(rsa->e, pub->e) != 0)
				return (DST_R_INVALIDPRIVATEKEY);
		} else {
			rsa->e = pub->e;
			pub->e = NULL;
		}
	}
	if (rsa