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