]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/bind9/lib/dns/opensslrsa_link.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / bind9 / lib / dns / opensslrsa_link.c
1 /*
2  * Copyright (C) 2004-2009, 2011, 2012  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2000-2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /*
19  * Principal Author: Brian Wellington
20  * $Id$
21  */
22 #ifdef OPENSSL
23 #include <config.h>
24
25 #ifndef USE_EVP
26 #if !defined(HAVE_EVP_SHA256) || !defined(HAVE_EVP_SHA512)
27 #define USE_EVP 0
28 #else
29 #define USE_EVP 1
30 #endif
31 #endif
32
33
34 #include <isc/entropy.h>
35 #include <isc/md5.h>
36 #include <isc/sha1.h>
37 #include <isc/sha2.h>
38 #include <isc/mem.h>
39 #include <isc/string.h>
40 #include <isc/util.h>
41
42 #include <dst/result.h>
43
44 #include "dst_internal.h"
45 #include "dst_openssl.h"
46 #include "dst_parse.h"
47
48 #include <openssl/err.h>
49 #include <openssl/objects.h>
50 #include <openssl/rsa.h>
51 #if OPENSSL_VERSION_NUMBER > 0x00908000L
52 #include <openssl/bn.h>
53 #endif
54 #ifdef USE_ENGINE
55 #include <openssl/engine.h>
56 #endif
57
58 /*
59  * We don't use configure for windows so enforce the OpenSSL version
60  * here.  Unlike with configure we don't support overriding this test.
61  */
62 #ifdef WIN32
63 #if !((OPENSSL_VERSION_NUMBER >= 0x009070cfL && \
64        OPENSSL_VERSION_NUMBER < 0x00908000L) || \
65       OPENSSL_VERSION_NUMBER >= 0x0090804fL)
66 #error Please upgrade OpenSSL to 0.9.8d/0.9.7l or greater.
67 #endif
68 #endif
69
70
71         /*
72          * XXXMPA  Temporarily disable RSA_BLINDING as it requires
73          * good quality random data that cannot currently be guaranteed.
74          * XXXMPA  Find which versions of openssl use pseudo random data
75          * and set RSA_FLAG_BLINDING for those.
76          */
77
78 #if 0
79 #if OPENSSL_VERSION_NUMBER < 0x0090601fL
80 #define SET_FLAGS(rsa) \
81         do { \
82         (rsa)->flags &= ~(RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE); \
83         (rsa)->flags |= RSA_FLAG_BLINDING; \
84         } while (0)
85 #else
86 #define SET_FLAGS(rsa) \
87         do { \
88                 (rsa)->flags |= RSA_FLAG_BLINDING; \
89         } while (0)
90 #endif
91 #endif
92
93 #if OPENSSL_VERSION_NUMBER < 0x0090601fL
94 #define SET_FLAGS(rsa) \
95         do { \
96         (rsa)->flags &= ~(RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE); \
97         (rsa)->flags &= ~RSA_FLAG_BLINDING; \
98         } while (0)
99 #elif defined(RSA_FLAG_NO_BLINDING)
100 #define SET_FLAGS(rsa) \
101         do { \
102                 (rsa)->flags &= ~RSA_FLAG_BLINDING; \
103                 (rsa)->flags |= RSA_FLAG_NO_BLINDING; \
104         } while (0)
105 #else
106 #define SET_FLAGS(rsa) \
107         do { \
108                 (rsa)->flags &= ~RSA_FLAG_BLINDING; \
109         } while (0)
110 #endif
111
112 #define DST_RET(a) {ret = a; goto err;}
113
114 static isc_result_t opensslrsa_todns(const dst_key_t *key, isc_buffer_t *data);
115
116 static isc_result_t
117 opensslrsa_createctx(dst_key_t *key, dst_context_t *dctx) {
118 #if USE_EVP
119         EVP_MD_CTX *evp_md_ctx;
120         const EVP_MD *type = NULL;
121 #endif
122
123         UNUSED(key);
124         REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
125                 dctx->key->key_alg == DST_ALG_RSASHA1 ||
126                 dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
127                 dctx->key->key_alg == DST_ALG_RSASHA256 ||
128                 dctx->key->key_alg == DST_ALG_RSASHA512);
129
130 #if USE_EVP
131         evp_md_ctx = EVP_MD_CTX_create();
132         if (evp_md_ctx == NULL)
133                 return (ISC_R_NOMEMORY);
134
135         switch (dctx->key->key_alg) {
136         case DST_ALG_RSAMD5:
137                 type = EVP_md5();       /* MD5 + RSA */
138                 break;
139         case DST_ALG_RSASHA1:
140         case DST_ALG_NSEC3RSASHA1:
141                 type = EVP_sha1();      /* SHA1 + RSA */
142                 break;
143 #ifdef HAVE_EVP_SHA256
144         case DST_ALG_RSASHA256:
145                 type = EVP_sha256();    /* SHA256 + RSA */
146                 break;
147 #endif
148 #ifdef HAVE_EVP_SHA512
149         case DST_ALG_RSASHA512:
150                 type = EVP_sha512();
151                 break;
152 #endif
153         default:
154                 INSIST(0);
155         }
156
157         if (!EVP_DigestInit_ex(evp_md_ctx, type, NULL)) {
158                 EVP_MD_CTX_destroy(evp_md_ctx);
159                 return (dst__openssl_toresult2("EVP_DigestInit_ex",
160                                                ISC_R_FAILURE));
161         }
162         dctx->ctxdata.evp_md_ctx = evp_md_ctx;
163 #else
164         switch (dctx->key->key_alg) {
165         case DST_ALG_RSAMD5:
166                 {
167                         isc_md5_t *md5ctx;
168
169                         md5ctx = isc_mem_get(dctx->mctx, sizeof(isc_md5_t));
170                         if (md5ctx == NULL)
171                                 return (ISC_R_NOMEMORY);
172                         isc_md5_init(md5ctx);
173                         dctx->ctxdata.md5ctx = md5ctx;
174                 }
175                 break;
176         case DST_ALG_RSASHA1:
177         case DST_ALG_NSEC3RSASHA1:
178                 {
179                         isc_sha1_t *sha1ctx;
180
181                         sha1ctx = isc_mem_get(dctx->mctx, sizeof(isc_sha1_t));
182                         if (sha1ctx == NULL)
183                                 return (ISC_R_NOMEMORY);
184                         isc_sha1_init(sha1ctx);
185                         dctx->ctxdata.sha1ctx = sha1ctx;
186                 }
187                 break;
188         case DST_ALG_RSASHA256:
189                 {
190                         isc_sha256_t *sha256ctx;
191
192                         sha256ctx = isc_mem_get(dctx->mctx,
193                                                 sizeof(isc_sha256_t));
194                         if (sha256ctx == NULL)
195                                 return (ISC_R_NOMEMORY);
196                         isc_sha256_init(sha256ctx);
197                         dctx->ctxdata.sha256ctx = sha256ctx;
198                 }
199                 break;
200         case DST_ALG_RSASHA512:
201                 {
202                         isc_sha512_t *sha512ctx;
203
204                         sha512ctx = isc_mem_get(dctx->mctx,
205                                                 sizeof(isc_sha512_t));
206                         if (sha512ctx == NULL)
207                                 return (ISC_R_NOMEMORY);
208                         isc_sha512_init(sha512ctx);
209                         dctx->ctxdata.sha512ctx = sha512ctx;
210                 }
211                 break;
212         default:
213                 INSIST(0);
214         }
215 #endif
216
217         return (ISC_R_SUCCESS);
218 }
219
220 static void
221 opensslrsa_destroyctx(dst_context_t *dctx) {
222 #if USE_EVP
223         EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
224 #endif
225
226         REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
227                 dctx->key->key_alg == DST_ALG_RSASHA1 ||
228                 dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
229                 dctx->key->key_alg == DST_ALG_RSASHA256 ||
230                 dctx->key->key_alg == DST_ALG_RSASHA512);
231
232 #if USE_EVP
233         if (evp_md_ctx != NULL) {
234                 EVP_MD_CTX_destroy(evp_md_ctx);
235                 dctx->ctxdata.evp_md_ctx = NULL;
236         }
237 #else
238         switch (dctx->key->key_alg) {
239         case DST_ALG_RSAMD5:
240                 {
241                         isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
242
243                         if (md5ctx != NULL) {
244                                 isc_md5_invalidate(md5ctx);
245                                 isc_mem_put(dctx->mctx, md5ctx,
246                                             sizeof(isc_md5_t));
247                                 dctx->ctxdata.md5ctx = NULL;
248                         }
249                 }
250                 break;
251         case DST_ALG_RSASHA1:
252         case DST_ALG_NSEC3RSASHA1:
253                 {
254                         isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
255
256                         if (sha1ctx != NULL) {
257                                 isc_sha1_invalidate(sha1ctx);
258                                 isc_mem_put(dctx->mctx, sha1ctx,
259                                             sizeof(isc_sha1_t));
260                                 dctx->ctxdata.sha1ctx = NULL;
261                         }
262                 }
263                 break;
264         case DST_ALG_RSASHA256:
265                 {
266                         isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
267
268                         if (sha256ctx != NULL) {
269                                 isc_sha256_invalidate(sha256ctx);
270                                 isc_mem_put(dctx->mctx, sha256ctx,
271                                             sizeof(isc_sha256_t));
272                                 dctx->ctxdata.sha256ctx = NULL;
273                         }
274                 }
275                 break;
276         case DST_ALG_RSASHA512:
277                 {
278                         isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
279
280                         if (sha512ctx != NULL) {
281                                 isc_sha512_invalidate(sha512ctx);
282                                 isc_mem_put(dctx->mctx, sha512ctx,
283                                             sizeof(isc_sha512_t));
284                                 dctx->ctxdata.sha512ctx = NULL;
285                         }
286                 }
287                 break;
288         default:
289                 INSIST(0);
290         }
291 #endif
292 }
293
294 static isc_result_t
295 opensslrsa_adddata(dst_context_t *dctx, const isc_region_t *data) {
296 #if USE_EVP
297         EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
298 #endif
299
300         REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
301                 dctx->key->key_alg == DST_ALG_RSASHA1 ||
302                 dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
303                 dctx->key->key_alg == DST_ALG_RSASHA256 ||
304                 dctx->key->key_alg == DST_ALG_RSASHA512);
305
306 #if USE_EVP
307         if (!EVP_DigestUpdate(evp_md_ctx, data->base, data->length)) {
308                 return (dst__openssl_toresult2("EVP_DigestUpdate",
309                                                ISC_R_FAILURE));
310         }
311 #else
312         switch (dctx->key->key_alg) {
313         case DST_ALG_RSAMD5:
314                 {
315                         isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
316
317                         isc_md5_update(md5ctx, data->base, data->length);
318                 }
319                 break;
320         case DST_ALG_RSASHA1:
321         case DST_ALG_NSEC3RSASHA1:
322                 {
323                         isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
324
325                         isc_sha1_update(sha1ctx, data->base, data->length);
326                 }
327                 break;
328         case DST_ALG_RSASHA256:
329                 {
330                         isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
331
332                         isc_sha256_update(sha256ctx, data->base, data->length);
333                 }
334                 break;
335         case DST_ALG_RSASHA512:
336                 {
337                         isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
338
339                         isc_sha512_update(sha512ctx, data->base, data->length);
340                 }
341                 break;
342         default:
343                 INSIST(0);
344         }
345 #endif
346         return (ISC_R_SUCCESS);
347 }
348
349 #if ! USE_EVP && OPENSSL_VERSION_NUMBER < 0x00908000L
350 /*
351  * Digest prefixes from RFC 5702.
352  */
353 static unsigned char sha256_prefix[] =
354          { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
355            0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20};
356 static unsigned char sha512_prefix[] =
357          { 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48,
358            0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40};
359 #define PREFIXLEN sizeof(sha512_prefix)
360 #else
361 #define PREFIXLEN 0
362 #endif
363
364 static isc_result_t
365 opensslrsa_sign(dst_context_t *dctx, isc_buffer_t *sig) {
366         dst_key_t *key = dctx->key;
367         isc_region_t r;
368         unsigned int siglen = 0;
369 #if USE_EVP
370         EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
371         EVP_PKEY *pkey = key->keydata.pkey;
372 #else
373         RSA *rsa = key->keydata.rsa;
374         /* note: ISC_SHA512_DIGESTLENGTH >= ISC_*_DIGESTLENGTH */
375         unsigned char digest[PREFIXLEN + ISC_SHA512_DIGESTLENGTH];
376         int status;
377         int type = 0;
378         unsigned int digestlen = 0;
379 #if OPENSSL_VERSION_NUMBER < 0x00908000L
380         unsigned int prefixlen = 0;
381         const unsigned char *prefix = NULL;
382 #endif
383 #endif
384
385         REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
386                 dctx->key->key_alg == DST_ALG_RSASHA1 ||
387                 dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
388                 dctx->key->key_alg == DST_ALG_RSASHA256 ||
389                 dctx->key->key_alg == DST_ALG_RSASHA512);
390
391         isc_buffer_availableregion(sig, &r);
392
393 #if USE_EVP
394         if (r.length < (unsigned int) EVP_PKEY_size(pkey))
395                 return (ISC_R_NOSPACE);
396
397         if (!EVP_SignFinal(evp_md_ctx, r.base, &siglen, pkey)) {
398                 return (dst__openssl_toresult2("EVP_SignFinal",
399                                                ISC_R_FAILURE));
400         }
401 #else
402         if (r.length < (unsigned int) RSA_size(rsa))
403                 return (ISC_R_NOSPACE);
404
405         switch (dctx->key->key_alg) {
406         case DST_ALG_RSAMD5:
407                 {
408                         isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
409
410                         isc_md5_final(md5ctx, digest);
411                         type = NID_md5;
412                         digestlen = ISC_MD5_DIGESTLENGTH;
413                 }
414                 break;
415         case DST_ALG_RSASHA1:
416         case DST_ALG_NSEC3RSASHA1:
417                 {
418                         isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
419
420                         isc_sha1_final(sha1ctx, digest);
421                         type = NID_sha1;
422                         digestlen = ISC_SHA1_DIGESTLENGTH;
423                 }
424                 break;
425         case DST_ALG_RSASHA256:
426                 {
427                         isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
428
429                         isc_sha256_final(digest, sha256ctx);
430                         digestlen = ISC_SHA256_DIGESTLENGTH;
431 #if OPENSSL_VERSION_NUMBER < 0x00908000L
432                         prefix = sha256_prefix;
433                         prefixlen = sizeof(sha256_prefix);
434 #else
435                         type = NID_sha256;
436 #endif
437                 }
438                 break;
439         case DST_ALG_RSASHA512:
440                 {
441                         isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
442
443                         isc_sha512_final(digest, sha512ctx);
444                         digestlen = ISC_SHA512_DIGESTLENGTH;
445 #if OPENSSL_VERSION_NUMBER < 0x00908000L
446                         prefix = sha512_prefix;
447                         prefixlen = sizeof(sha512_prefix);
448 #else
449                         type = NID_sha512;
450 #endif
451                 }
452                 break;
453         default:
454                 INSIST(0);
455         }
456
457 #if OPENSSL_VERSION_NUMBER < 0x00908000L
458         switch (dctx->key->key_alg) {
459         case DST_ALG_RSAMD5:
460         case DST_ALG_RSASHA1:
461         case DST_ALG_NSEC3RSASHA1:
462                 INSIST(type != 0);
463                 status = RSA_sign(type, digest, digestlen, r.base,
464                                   &siglen, rsa);
465                 break;
466
467         case DST_ALG_RSASHA256:
468         case DST_ALG_RSASHA512:
469                 INSIST(prefix != NULL);
470                 INSIST(prefixlen != 0);
471                 INSIST(prefixlen + digestlen <= sizeof(digest));
472
473                 memmove(digest + prefixlen, digest, digestlen);
474                 memcpy(digest, prefix, prefixlen);
475                 status = RSA_private_encrypt(digestlen + prefixlen,
476                                              digest, r.base, rsa,
477                                              RSA_PKCS1_PADDING);
478                 if (status < 0)
479                         status = 0;
480                 else
481                         siglen = status;
482                 break;
483
484         default:
485                 INSIST(0);
486         }
487 #else
488         INSIST(type != 0);
489         status = RSA_sign(type, digest, digestlen, r.base, &siglen, rsa);
490 #endif
491         if (status == 0)
492                 return (dst__openssl_toresult2("RSA_sign",
493                                                DST_R_OPENSSLFAILURE));
494 #endif
495
496         isc_buffer_add(sig, siglen);
497
498         return (ISC_R_SUCCESS);
499 }
500
501 static isc_result_t
502 opensslrsa_verify(dst_context_t *dctx, const isc_region_t *sig) {
503         dst_key_t *key = dctx->key;
504         int status = 0;
505 #if USE_EVP
506         EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
507         EVP_PKEY *pkey = key->keydata.pkey;
508 #else
509         /* note: ISC_SHA512_DIGESTLENGTH >= ISC_*_DIGESTLENGTH */
510         unsigned char digest[ISC_SHA512_DIGESTLENGTH];
511         int type = 0;
512         unsigned int digestlen = 0;
513         RSA *rsa = key->keydata.rsa;
514 #if OPENSSL_VERSION_NUMBER < 0x00908000L
515         unsigned int prefixlen = 0;
516         const unsigned char *prefix = NULL;
517 #endif
518 #endif
519
520         REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
521                 dctx->key->key_alg == DST_ALG_RSASHA1 ||
522                 dctx->key->key_alg == DST_ALG_NSEC3RSASHA1 ||
523                 dctx->key->key_alg == DST_ALG_RSASHA256 ||
524                 dctx->key->key_alg == DST_ALG_RSASHA512);
525
526 #if USE_EVP
527         status = EVP_VerifyFinal(evp_md_ctx, sig->base, sig->length, pkey);
528 #else
529         switch (dctx->key->key_alg) {
530         case DST_ALG_RSAMD5:
531                 {
532                         isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
533
534                         isc_md5_final(md5ctx, digest);
535                         type = NID_md5;
536                         digestlen = ISC_MD5_DIGESTLENGTH;
537                 }
538                 break;
539         case DST_ALG_RSASHA1:
540         case DST_ALG_NSEC3RSASHA1:
541                 {
542                         isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
543
544                         isc_sha1_final(sha1ctx, digest);
545                         type = NID_sha1;
546                         digestlen = ISC_SHA1_DIGESTLENGTH;
547                 }
548                 break;
549         case DST_ALG_RSASHA256:
550                 {
551                         isc_sha256_t *sha256ctx = dctx->ctxdata.sha256ctx;
552
553                         isc_sha256_final(digest, sha256ctx);
554                         digestlen = ISC_SHA256_DIGESTLENGTH;
555 #if OPENSSL_VERSION_NUMBER < 0x00908000L
556                         prefix = sha256_prefix;
557                         prefixlen = sizeof(sha256_prefix);
558 #else
559                         type = NID_sha256;
560 #endif
561                 }
562                 break;
563         case DST_ALG_RSASHA512:
564                 {
565                         isc_sha512_t *sha512ctx = dctx->ctxdata.sha512ctx;
566
567                         isc_sha512_final(digest, sha512ctx);
568                         digestlen = ISC_SHA512_DIGESTLENGTH;
569 #if OPENSSL_VERSION_NUMBER < 0x00908000L
570                         prefix = sha512_prefix;
571                         prefixlen = sizeof(sha512_prefix);
572 #else
573                         type = NID_sha512;
574 #endif
575                 }
576                 break;
577         default:
578                 INSIST(0);
579         }
580
581         if (sig->length != (unsigned int) RSA_size(rsa))
582                 return (DST_R_VERIFYFAILURE);
583
584 #if OPENSSL_VERSION_NUMBER < 0x00908000L
585         switch (dctx->key->key_alg) {
586         case DST_ALG_RSAMD5:
587         case DST_ALG_RSASHA1:
588         case DST_ALG_NSEC3RSASHA1:
589                 INSIST(type != 0);
590                 status = RSA_verify(type, digest, digestlen, sig->base,
591                                     RSA_size(rsa), rsa);
592                 break;
593
594         case DST_ALG_RSASHA256:
595         case DST_ALG_RSASHA512:
596                 {
597                         /*
598                          * 1024 is big enough for all valid RSA bit sizes
599                          * for use with DNSSEC.
600                          */
601                         unsigned char original[PREFIXLEN + 1024];
602
603                         INSIST(prefix != NULL);
604                         INSIST(prefixlen != 0U);
605
606                         if (RSA_size(rsa) > (int)sizeof(original))
607                                 return (DST_R_VERIFYFAILURE);
608
609                         status = RSA_public_decrypt(sig->length, sig->base,
610                                                     original, rsa,
611                                                     RSA_PKCS1_PADDING);
612                         if (status <= 0)
613                                 return (dst__openssl_toresult2(
614                                                 "RSA_public_decrypt",
615                                                 DST_R_VERIFYFAILURE));
616                         if (status != (int)(prefixlen + digestlen))
617                                 return (DST_R_VERIFYFAILURE);
618                         if (memcmp(original, prefix, prefixlen))
619                                 return (DST_R_VERIFYFAILURE);
620                         if (memcmp(original + prefixlen, digest, digestlen))
621                                 return (DST_R_VERIFYFAILURE);
622                         status = 1;
623                 }
624                 break;
625
626         default:
627                 INSIST(0);
628         }
629 #else
630         INSIST(type != 0);
631         status = RSA_verify(type, digest, digestlen, sig->base,
632                              RSA_size(rsa), rsa);
633 #endif
634 #endif
635         if (status != 1)
636                 return (dst__openssl_toresult2("RSA_verify",
637                                                DST_R_VERIFYFAILURE));
638
639         return (ISC_R_SUCCESS);
640 }
641
642 static isc_boolean_t
643 opensslrsa_compare(const dst_key_t *key1, const dst_key_t *key2) {
644         int status;
645         RSA *rsa1 = NULL, *rsa2 = NULL;
646 #if USE_EVP
647         EVP_PKEY *pkey1, *pkey2;
648 #endif
649
650 #if USE_EVP
651         pkey1 = key1->keydata.pkey;
652         pkey2 = key2->keydata.pkey;
653         /*
654          * The pkey reference will keep these around after
655          * the RSA_free() call.
656          */
657         if (pkey1 != NULL) {
658                 rsa1 = EVP_PKEY_get1_RSA(pkey1);
659                 RSA_free(rsa1);
660         }
661         if (pkey2 != NULL) {
662                 rsa2 = EVP_PKEY_get1_RSA(pkey2);
663                 RSA_free(rsa2);
664         }
665 #else
666         rsa1 = key1->keydata.rsa;
667         rsa2 = key2->keydata.rsa;
668 #endif
669
670         if (rsa1 == NULL && rsa2 == NULL)
671                 return (ISC_TRUE);
672         else if (rsa1 == NULL || rsa2 == NULL)
673                 return (ISC_FALSE);
674
675         status = BN_cmp(rsa1->n, rsa2->n) ||
676                  BN_cmp(rsa1->e, rsa2->e);
677
678         if (status != 0)
679                 return (ISC_FALSE);
680
681 #if USE_EVP
682         if ((rsa1->flags & RSA_FLAG_EXT_PKEY) != 0 ||
683             (rsa2->flags & RSA_FLAG_EXT_PKEY) != 0) {
684                 if ((rsa1->flags & RSA_FLAG_EXT_PKEY) == 0 ||
685                     (rsa2->flags & RSA_FLAG_EXT_PKEY) == 0)
686                         return (ISC_FALSE);
687                 /*
688                  * Can't compare private parameters, BTW does it make sense?
689                  */
690                 return (ISC_TRUE);
691         }
692 #endif
693
694         if (rsa1->d != NULL || rsa2->d != NULL) {
695                 if (rsa1->d == NULL || rsa2->d == NULL)
696                         return (ISC_FALSE);
697                 status = BN_cmp(rsa1->d, rsa2->d) ||
698                          BN_cmp(rsa1->p, rsa2->p) ||
699                          BN_cmp(rsa1->q, rsa2->q);
700
701                 if (status != 0)
702                         return (ISC_FALSE);
703         }
704         return (ISC_TRUE);
705 }
706
707 #if OPENSSL_VERSION_NUMBER > 0x00908000L
708 static int
709 progress_cb(int p, int n, BN_GENCB *cb)
710 {
711         union {
712                 void *dptr;
713                 void (*fptr)(int);
714         } u;
715
716         UNUSED(n);
717
718         u.dptr = cb->arg;
719         if (u.fptr != NULL)
720                 u.fptr(p);
721         return (1);
722 }
723 #endif
724
725 static isc_result_t
726 opensslrsa_generate(dst_key_t *key, int exp, void (*callback)(int)) {
727 #if OPENSSL_VERSION_NUMBER > 0x00908000L
728         isc_result_t ret = DST_R_OPENSSLFAILURE;
729         BN_GENCB cb;
730         union {
731                 void *dptr;
732                 void (*fptr)(int);
733         } u;
734         RSA *rsa = RSA_new();
735         BIGNUM *e = BN_new();
736 #if USE_EVP
737         EVP_PKEY *pkey = EVP_PKEY_new();
738 #endif
739
740         if (rsa == NULL || e == NULL)
741                 goto err;
742 #if USE_EVP
743         if (pkey == NULL)
744                 goto err;
745         if (!EVP_PKEY_set1_RSA(pkey, rsa))
746                 goto err;
747 #endif
748
749         if (exp == 0) {
750                 /* RSA_F4 0x10001 */
751                 BN_set_bit(e, 0);
752                 BN_set_bit(e, 16);
753         } else {
754                 /* F5 0x100000001 */
755                 BN_set_bit(e, 0);
756                 BN_set_bit(e, 32);
757         }
758
759         if (callback == NULL) {
760                 BN_GENCB_set_old(&cb, NULL, NULL);
761         } else {
762                 u.fptr = callback;
763                 BN_GENCB_set(&cb, &progress_cb, u.dptr);
764         }
765
766         if (RSA_generate_key_ex(rsa, key->key_size, e, &cb)) {
767                 BN_free(e);
768                 SET_FLAGS(rsa);
769 #if USE_EVP
770                 key->keydata.pkey = pkey;
771
772                 RSA_free(rsa);
773 #else
774                 key->keydata.rsa = rsa;
775 #endif
776                 return (ISC_R_SUCCESS);
777         }
778         ret = dst__openssl_toresult2("RSA_generate_key_ex",
779                                      DST_R_OPENSSLFAILURE);
780
781 err:
782 #if USE_EVP
783         if (pkey != NULL)
784                 EVP_PKEY_free(pkey);
785 #endif
786         if (e != NULL)
787                 BN_free(e);
788         if (rsa != NULL)
789                 RSA_free(rsa);
790         return (dst__openssl_toresult(ret));
791 #else
792         RSA *rsa;
793         unsigned long e;
794 #if USE_EVP
795         EVP_PKEY *pkey = EVP_PKEY_new();
796
797         UNUSED(callback);
798
799         if (pkey == NULL)
800                 return (ISC_R_NOMEMORY);
801 #else
802         UNUSED(callback);
803 #endif
804
805         if (exp == 0)
806                e = RSA_F4;
807         else
808                e = 0x40000003;
809         rsa = RSA_generate_key(key->key_size, e, NULL, NULL);
810         if (rsa == NULL) {
811 #if USE_EVP
812                 EVP_PKEY_free(pkey);
813 #endif
814                 return (dst__openssl_toresult2("RSA_generate_key",
815                                                DST_R_OPENSSLFAILURE));
816         }
817         SET_FLAGS(rsa);
818 #if USE_EVP
819         if (!EVP_PKEY_set1_RSA(pkey, rsa)) {
820                 EVP_PKEY_free(pkey);
821                 RSA_free(rsa);
822                 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
823         }
824         key->keydata.pkey = pkey;
825         RSA_free(rsa);
826 #else
827         key->keydata.rsa = rsa;
828 #endif
829
830         return (ISC_R_SUCCESS);
831 #endif
832 }
833
834 static isc_boolean_t
835 opensslrsa_isprivate(const dst_key_t *key) {
836 #if USE_EVP
837         RSA *rsa = EVP_PKEY_get1_RSA(key->keydata.pkey);
838         INSIST(rsa != NULL);
839         RSA_free(rsa);
840         /* key->keydata.pkey still has a reference so rsa is still valid. */
841 #else
842         RSA *rsa = key->keydata.rsa;
843 #endif
844         if (rsa != NULL && (rsa->flags & RSA_FLAG_EXT_PKEY) != 0)
845                 return (ISC_TRUE);
846         return (ISC_TF(rsa != NULL && rsa->d != NULL));
847 }
848
849 static void
850 opensslrsa_destroy(dst_key_t *key) {
851 #if USE_EVP
852         EVP_PKEY *pkey = key->keydata.pkey;
853         EVP_PKEY_free(pkey);
854         key->keydata.pkey = NULL;
855 #else
856         RSA *rsa = key->keydata.rsa;
857         RSA_free(rsa);
858         key->keydata.rsa = NULL;
859 #endif
860 }
861
862
863 static isc_result_t
864 opensslrsa_todns(const dst_key_t *key, isc_buffer_t *data) {
865         isc_region_t r;
866         unsigned int e_bytes;
867         unsigned int mod_bytes;
868         isc_result_t ret;
869         RSA *rsa;
870 #if USE_EVP
871         EVP_PKEY *pkey;
872 #endif
873
874 #if USE_EVP
875         REQUIRE(key->keydata.pkey != NULL);
876 #else
877         REQUIRE(key->keydata.rsa != NULL);
878 #endif
879
880 #if USE_EVP
881         pkey = key->keydata.pkey;
882         rsa = EVP_PKEY_get1_RSA(pkey);
883         if (rsa == NULL)
884                 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
885 #else
886         rsa = key->keydata.rsa;
887 #endif
888
889         isc_buffer_availableregion(data, &r);
890
891         e_bytes = BN_num_bytes(rsa->e);
892         mod_bytes = BN_num_bytes(rsa->n);
893
894         if (e_bytes < 256) {    /*%< key exponent is <= 2040 bits */
895                 if (r.length < 1)
896                         DST_RET(ISC_R_NOSPACE);
897                 isc_buffer_putuint8(data, (isc_uint8_t) e_bytes);
898                 isc_region_consume(&r, 1);
899         } else {
900                 if (r.length < 3)
901                         DST_RET(ISC_R_NOSPACE);
902                 isc_buffer_putuint8(data, 0);
903                 isc_buffer_putuint16(data, (isc_uint16_t) e_bytes);
904                 isc_region_consume(&r, 3);
905         }
906
907         if (r.length < e_bytes + mod_bytes)
908                 DST_RET(ISC_R_NOSPACE);
909
910         BN_bn2bin(rsa->e, r.base);
911         isc_region_consume(&r, e_bytes);
912         BN_bn2bin(rsa->n, r.base);
913
914         isc_buffer_add(data, e_bytes + mod_bytes);
915
916         ret = ISC_R_SUCCESS;
917  err:
918 #if USE_EVP
919         if (rsa != NULL)
920                 RSA_free(rsa);
921 #endif
922         return (ret);
923 }
924
925 static isc_result_t
926 opensslrsa_fromdns(dst_key_t *key, isc_buffer_t *data) {
927         RSA *rsa;
928         isc_region_t r;
929         unsigned int e_bytes;
930 #if USE_EVP
931         EVP_PKEY *pkey;
932 #endif
933
934         isc_buffer_remainingregion(data, &r);
935         if (r.length == 0)
936                 return (ISC_R_SUCCESS);
937
938         rsa = RSA_new();
939         if (rsa == NULL)
940                 return (dst__openssl_toresult(ISC_R_NOMEMORY));
941         SET_FLAGS(rsa);
942
943         if (r.length < 1) {
944                 RSA_free(rsa);
945                 return (DST_R_INVALIDPUBLICKEY);
946         }
947         e_bytes = *r.base++;
948         r.length--;
949
950         if (e_bytes == 0) {
951                 if (r.length < 2) {
952                         RSA_free(rsa);
953                         return (DST_R_INVALIDPUBLICKEY);
954                 }
955                 e_bytes = ((*r.base++) << 8);
956                 e_bytes += *r.base++;
957                 r.length -= 2;
958         }
959
960         if (r.length < e_bytes) {
961                 RSA_free(rsa);
962                 return (DST_R_INVALIDPUBLICKEY);
963         }
964         rsa->e = BN_bin2bn(r.base, e_bytes, NULL);
965         r.base += e_bytes;
966         r.length -= e_bytes;
967
968         rsa->n = BN_bin2bn(r.base, r.length, NULL);
969
970         key->key_size = BN_num_bits(rsa->n);
971
972         isc_buffer_forward(data, r.length);
973
974 #if USE_EVP
975         pkey = EVP_PKEY_new();
976         if (pkey == NULL) {
977                 RSA_free(rsa);
978                 return (ISC_R_NOMEMORY);
979         }
980         if (!EVP_PKEY_set1_RSA(pkey, rsa)) {
981                 EVP_PKEY_free(pkey);
982                 RSA_free(rsa);
983                 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
984         }
985         key->keydata.pkey = pkey;
986         RSA_free(rsa);
987 #else
988         key->keydata.rsa = rsa;
989 #endif
990
991         return (ISC_R_SUCCESS);
992 }
993
994 static isc_result_t
995 opensslrsa_tofile(const dst_key_t *key, const char *directory) {
996         int i;
997         RSA *rsa;
998         dst_private_t priv;
999         unsigned char *bufs[8];
1000         isc_result_t result;
1001
1002 #if USE_EVP
1003         if (key->keydata.pkey == NULL)
1004                 return (DST_R_NULLKEY);
1005         rsa = EVP_PKEY_get1_RSA(key->keydata.pkey);
1006         if (rsa == NULL)
1007                 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
1008 #else
1009         if (key->keydata.rsa == NULL)
1010                 return (DST_R_NULLKEY);
1011         rsa = key->keydata.rsa;
1012 #endif
1013
1014         memset(bufs, 0, sizeof(bufs));
1015         for (i = 0; i < 8; i++) {
1016                 bufs[i] = isc_mem_get(key->mctx, BN_num_bytes(rsa->n));
1017                 if (bufs[i] == NULL) {
1018                         result = ISC_R_NOMEMORY;
1019                         goto fail;
1020                 }
1021         }
1022
1023         i = 0;
1024
1025         priv.elements[i].tag = TAG_RSA_MODULUS;
1026         priv.elements[i].length = BN_num_bytes(rsa->n);
1027         BN_bn2bin(rsa->n, bufs[i]);
1028         priv.elements[i].data = bufs[i];
1029         i++;
1030
1031         priv.elements[i].tag = TAG_RSA_PUBLICEXPONENT;
1032         priv.elements[i].length = BN_num_bytes(rsa->e);
1033         BN_bn2bin(rsa->e, bufs[i]);
1034         priv.elements[i].data = bufs[i];
1035         i++;
1036
1037         if (rsa->d != NULL) {
1038                 priv.elements[i].tag = TAG_RSA_PRIVATEEXPONENT;
1039                 priv.elements[i].length = BN_num_bytes(rsa->d);
1040                 BN_bn2bin(rsa->d, bufs[i]);
1041                 priv.elements[i].data = bufs[i];
1042                 i++;
1043         }
1044
1045         if (rsa->p != NULL) {
1046                 priv.elements[i].tag = TAG_RSA_PRIME1;
1047                 priv.elements[i].length = BN_num_bytes(rsa->p);
1048                 BN_bn2bin(rsa->p, bufs[i]);
1049                 priv.elements[i].data = bufs[i];
1050                 i++;
1051         }
1052
1053         if (rsa->q != NULL) {
1054                 priv.elements[i].tag = TAG_RSA_PRIME2;
1055                 priv.elements[i].length = BN_num_bytes(rsa->q);
1056                 BN_bn2bin(rsa->q, bufs[i]);
1057                 priv.elements[i].data = bufs[i];
1058                 i++;
1059         }
1060
1061         if (rsa->dmp1 != NULL) {
1062                 priv.elements[i].tag = TAG_RSA_EXPONENT1;
1063                 priv.elements[i].length = BN_num_bytes(rsa->dmp1);
1064                 BN_bn2bin(rsa->dmp1, bufs[i]);
1065                 priv.elements[i].data = bufs[i];
1066                 i++;
1067         }
1068
1069         if (rsa->dmq1 != NULL) {
1070                 priv.elements[i].tag = TAG_RSA_EXPONENT2;
1071                 priv.elements[i].length = BN_num_bytes(rsa->dmq1);
1072                 BN_bn2bin(rsa->dmq1, bufs[i]);
1073                 priv.elements[i].data = bufs[i];
1074                 i++;
1075         }
1076
1077         if (rsa->iqmp != NULL) {
1078                 priv.elements[i].tag = TAG_RSA_COEFFICIENT;
1079                 priv.elements[i].length = BN_num_bytes(rsa->iqmp);
1080                 BN_bn2bin(rsa->iqmp, bufs[i]);
1081                 priv.elements[i].data = bufs[i];
1082                 i++;
1083         }
1084
1085         if (key->engine != NULL) {
1086                 priv.elements[i].tag = TAG_RSA_ENGINE;
1087                 priv.elements[i].length = strlen(key->engine) + 1;
1088                 priv.elements[i].data = (unsigned char *)key->engine;
1089                 i++;
1090         }
1091
1092         if (key->label != NULL) {
1093                 priv.elements[i].tag = TAG_RSA_LABEL;
1094                 priv.elements[i].length = strlen(key->label) + 1;
1095                 priv.elements[i].data = (unsigned char *)key->label;
1096                 i++;
1097         }
1098
1099
1100         priv.nelements = i;
1101         result = dst__privstruct_writefile(key, &priv, directory);
1102  fail:
1103 #if USE_EVP
1104         RSA_free(rsa);
1105 #endif
1106         for (i = 0; i < 8; i++) {
1107                 if (bufs[i] == NULL)
1108                         break;
1109                 isc_mem_put(key->mctx, bufs[i], BN_num_bytes(rsa->n));
1110         }
1111         return (result);
1112 }
1113
1114 static isc_result_t
1115 rsa_check(RSA *rsa, RSA *pub)
1116 {
1117         /* Public parameters should be the same but if they are not set
1118          * copy them from the public key. */
1119         if (pub != NULL) {
1120                 if (rsa->n != NULL) {
1121                         if (BN_cmp(rsa->n, pub->n) != 0)
1122                                 return (DST_R_INVALIDPRIVATEKEY);
1123                 } else {
1124                         rsa->n = pub->n;
1125                         pub->n = NULL;
1126                 }
1127                 if (rsa->e != NULL) {
1128                         if (BN_cmp(rsa->e, pub->e) != 0)
1129                                 return (DST_R_INVALIDPRIVATEKEY);
1130                 } else {
1131                         rsa->e = pub->e;
1132                         pub->e = NULL;
1133                 }
1134         }
1135         if (rsa->n == NULL || rsa->e == NULL)
1136                 return (DST_R_INVALIDPRIVATEKEY);
1137         return (ISC_R_SUCCESS);
1138 }
1139
1140 static isc_result_t
1141 opensslrsa_parse(dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub) {
1142         dst_private_t priv;
1143         isc_result_t ret;
1144         int i;
1145         RSA *rsa = NULL, *pubrsa = NULL;
1146 #ifdef USE_ENGINE
1147         ENGINE *e = NULL;
1148 #endif
1149         isc_mem_t *mctx = key->mctx;
1150         const char *engine = NULL, *label = NULL;
1151 #if defined(USE_ENGINE) || USE_EVP
1152         EVP_PKEY *pkey = NULL;
1153 #endif
1154
1155 #if USE_EVP
1156         if (pub != NULL && pub->keydata.pkey != NULL)
1157                 pubrsa = EVP_PKEY_get1_RSA(pub->keydata.pkey);
1158 #else
1159         if (pub != NULL && pub->keydata.rsa != NULL) {
1160                 pubrsa = pub->keydata.rsa;
1161                 pub->keydata.rsa = NULL;
1162         }
1163 #endif
1164
1165         /* read private key file */
1166         ret = dst__privstruct_parse(key, DST_ALG_RSA, lexer, mctx, &priv);
1167         if (ret != ISC_R_SUCCESS)
1168                 goto err;
1169
1170         for (i = 0; i < priv.nelements; i++) {
1171                 switch (priv.elements[i].tag) {
1172                 case TAG_RSA_ENGINE:
1173                         engine = (char *)priv.elements[i].data;
1174                         break;
1175                 case TAG_RSA_LABEL:
1176                         label = (char *)priv.elements[i].data;
1177                         break;
1178                 default:
1179                         break;
1180                 }
1181         }
1182         /*
1183          * Is this key is stored in a HSM?
1184          * See if we can fetch it.
1185          */
1186         if (label != NULL) {
1187 #ifdef USE_ENGINE
1188                 if (engine == NULL)
1189                         DST_RET(DST_R_NOENGINE);
1190                 e = dst__openssl_getengine(engine);
1191                 if (e == NULL)
1192                         DST_RET(DST_R_NOENGINE);
1193                 pkey = ENGINE_load_private_key(e, label, NULL, NULL);
1194                 if (pkey == NULL)
1195                         DST_RET(dst__openssl_toresult2(
1196                                         "ENGINE_load_private_key",
1197                                         ISC_R_NOTFOUND));
1198                 key->engine = isc_mem_strdup(key->mctx, engine);
1199                 if (key->engine == NULL)
1200                         DST_RET(ISC_R_NOMEMORY);
1201                 key->label = isc_mem_strdup(key->mctx, label);
1202                 if (key->label == NULL)
1203                         DST_RET(ISC_R_NOMEMORY);
1204                 rsa = EVP_PKEY_get1_RSA(pkey);
1205                 if (rsa == NULL)
1206                         DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
1207                 if (rsa_check(rsa, pubrsa) != ISC_R_SUCCESS)
1208                         DST_RET(DST_R_INVALIDPRIVATEKEY);
1209                 if (pubrsa != NULL)
1210                         RSA_free(pubrsa);
1211                 key->key_size = EVP_PKEY_bits(pkey);
1212 #if USE_EVP
1213                 key->keydata.pkey = pkey;
1214                 RSA_free(rsa);
1215 #else
1216                 key->keydata.rsa = rsa;
1217                 EVP_PKEY_free(pkey);
1218 #endif
1219                 dst__privstruct_free(&priv, mctx);
1220                 memset(&priv, 0, sizeof(priv));
1221                 return (ISC_R_SUCCESS);
1222 #else
1223                 DST_RET(DST_R_NOENGINE);
1224 #endif
1225         }
1226
1227         rsa = RSA_new();
1228         if (rsa == NULL)
1229                 DST_RET(ISC_R_NOMEMORY);
1230         SET_FLAGS(rsa);
1231
1232 #if USE_EVP
1233         pkey = EVP_PKEY_new();
1234         if (pkey == NULL)
1235                 DST_RET(ISC_R_NOMEMORY);
1236         if (!EVP_PKEY_set1_RSA(pkey, rsa))
1237                 DST_RET(ISC_R_FAILURE);
1238         key->keydata.pkey = pkey;
1239 #else
1240         key->keydata.rsa = rsa;
1241 #endif
1242
1243         for (i = 0; i < priv.nelements; i++) {
1244                 BIGNUM *bn;
1245                 switch (priv.elements[i].tag) {
1246                 case TAG_RSA_ENGINE:
1247                         continue;
1248                 case TAG_RSA_LABEL:
1249                         continue;
1250                 case TAG_RSA_PIN:
1251                         continue;
1252                 default:
1253                         bn = BN_bin2bn(priv.elements[i].data,
1254                                        priv.elements[i].length, NULL);
1255                         if (bn == NULL)
1256                                 DST_RET(ISC_R_NOMEMORY);
1257                 }
1258
1259                 switch (priv.elements[i].tag) {
1260                         case TAG_RSA_MODULUS:
1261                                 rsa->n = bn;
1262                                 break;
1263                         case TAG_RSA_PUBLICEXPONENT:
1264                                 rsa->e = bn;
1265                                 break;
1266                         case TAG_RSA_PRIVATEEXPONENT:
1267                                 rsa->d = bn;
1268                                 break;
1269                         case TAG_RSA_PRIME1:
1270                                 rsa->p = bn;
1271                                 break;
1272                         case TAG_RSA_PRIME2:
1273                                 rsa->q = bn;
1274                                 break;
1275                         case TAG_RSA_EXPONENT1:
1276                                 rsa->dmp1 = bn;
1277                                 break;
1278                         case TAG_RSA_EXPONENT2:
1279                                 rsa->dmq1 = bn;
1280                                 break;
1281                         case TAG_RSA_COEFFICIENT:
1282                                 rsa->iqmp = bn;
1283                                 break;
1284                 }
1285         }
1286         dst__privstruct_free(&priv, mctx);
1287         memset(&priv, 0, sizeof(priv));
1288
1289         if (rsa_check(rsa, pubrsa) != ISC_R_SUCCESS)
1290                 DST_RET(DST_R_INVALIDPRIVATEKEY);
1291         key->key_size = BN_num_bits(rsa->n);
1292         if (pubrsa != NULL)
1293                 RSA_free(pubrsa);
1294 #if USE_EVP
1295         RSA_free(rsa);
1296 #endif
1297
1298         return (ISC_R_SUCCESS);
1299
1300  err:
1301 #if USE_EVP
1302         if (pkey != NULL)
1303                 EVP_PKEY_free(pkey);
1304 #endif
1305         if (rsa != NULL)
1306                 RSA_free(rsa);
1307         if (pubrsa != NULL)
1308                 RSA_free(pubrsa);
1309         opensslrsa_destroy(key);
1310         dst__privstruct_free(&priv, mctx);
1311         memset(&priv, 0, sizeof(priv));
1312         return (ret);
1313 }
1314
1315 static isc_result_t
1316 opensslrsa_fromlabel(dst_key_t *key, const char *engine, const char *label,
1317                      const char *pin)
1318 {
1319 #ifdef USE_ENGINE
1320         ENGINE *e = NULL;
1321         isc_result_t ret;
1322         EVP_PKEY *pkey = NULL;
1323         RSA *rsa = NULL, *pubrsa = NULL;
1324         char *colon;
1325
1326         UNUSED(pin);
1327
1328         if (engine == NULL)
1329                 DST_RET(DST_R_NOENGINE);
1330         e = dst__openssl_getengine(engine);
1331         if (e == NULL)
1332                 DST_RET(DST_R_NOENGINE);
1333         pkey = ENGINE_load_public_key(e, label, NULL, NULL);
1334         if (pkey != NULL) {
1335                 pubrsa = EVP_PKEY_get1_RSA(pkey);
1336                 EVP_PKEY_free(pkey);
1337                 if (pubrsa == NULL)
1338                         DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
1339         }
1340         pkey = ENGINE_load_private_key(e, label, NULL, NULL);
1341         if (pkey == NULL)
1342                 DST_RET(dst__openssl_toresult2("ENGINE_load_private_key",
1343                                                ISC_R_NOTFOUND));
1344         if (engine != NULL) {
1345                 key->engine = isc_mem_strdup(key->mctx, engine);
1346                 if (key->engine == NULL)
1347                         DST_RET(ISC_R_NOMEMORY);
1348         } else {
1349                 key->engine = isc_mem_strdup(key->mctx, label);
1350                 if (key->engine == NULL)
1351                         DST_RET(ISC_R_NOMEMORY);
1352                 colon = strchr(key->engine, ':');
1353                 if (colon != NULL)
1354                         *colon = '\0';
1355         }
1356         key->label = isc_mem_strdup(key->mctx, label);
1357         if (key->label == NULL)
1358                 DST_RET(ISC_R_NOMEMORY);
1359         rsa = EVP_PKEY_get1_RSA(pkey);
1360         if (rsa == NULL)
1361                 DST_RET(dst__openssl_toresult(DST_R_OPENSSLFAILURE));
1362         if (rsa_check(rsa, pubrsa) != ISC_R_SUCCESS)
1363                 DST_RET(DST_R_INVALIDPRIVATEKEY);
1364         if (pubrsa != NULL)
1365                 RSA_free(pubrsa);
1366         key->key_size = EVP_PKEY_bits(pkey);
1367 #if USE_EVP
1368         key->keydata.pkey = pkey;
1369         RSA_free(rsa);
1370 #else
1371         key->keydata.rsa = rsa;
1372         EVP_PKEY_free(pkey);
1373 #endif
1374         return (ISC_R_SUCCESS);
1375
1376  err:
1377         if (rsa != NULL)
1378                 RSA_free(rsa);
1379         if (pubrsa != NULL)
1380                 RSA_free(pubrsa);
1381         if (pkey != NULL)
1382                 EVP_PKEY_free(pkey);
1383         return (ret);
1384 #else
1385         UNUSED(key);
1386         UNUSED(engine);
1387         UNUSED(label);
1388         UNUSED(pin);
1389         return(DST_R_NOENGINE);
1390 #endif
1391 }
1392
1393 static dst_func_t opensslrsa_functions = {
1394         opensslrsa_createctx,
1395         opensslrsa_destroyctx,
1396         opensslrsa_adddata,
1397         opensslrsa_sign,
1398         opensslrsa_verify,
1399         NULL, /*%< computesecret */
1400         opensslrsa_compare,
1401         NULL, /*%< paramcompare */
1402         opensslrsa_generate,
1403         opensslrsa_isprivate,
1404         opensslrsa_destroy,
1405         opensslrsa_todns,
1406         opensslrsa_fromdns,
1407         opensslrsa_tofile,
1408         opensslrsa_parse,
1409         NULL, /*%< cleanup */
1410         opensslrsa_fromlabel,
1411         NULL, /*%< dump */
1412         NULL, /*%< restore */
1413 };
1414
1415 isc_result_t
1416 dst__opensslrsa_init(dst_func_t **funcp, unsigned char algorithm) {
1417         REQUIRE(funcp != NULL);
1418
1419         if (*funcp == NULL) {
1420                 switch (algorithm) {
1421                 case DST_ALG_RSASHA256:
1422 #if defined(HAVE_EVP_SHA256) || !USE_EVP
1423                         *funcp = &opensslrsa_functions;
1424 #endif
1425                         break;
1426                 case DST_ALG_RSASHA512:
1427 #if defined(HAVE_EVP_SHA512) || !USE_EVP
1428                         *funcp = &opensslrsa_functions;
1429 #endif
1430                         break;
1431                 default:
1432                         *funcp = &opensslrsa_functions;
1433                         break;
1434                 }
1435         }
1436         return (ISC_R_SUCCESS);
1437 }
1438
1439 #else /* OPENSSL */
1440
1441 #include <isc/util.h>
1442
1443 EMPTY_TRANSLATION_UNIT
1444
1445 #endif /* OPENSSL */
1446 /*! \file */