Merge branch 'vendor/LESS' into less_update
[dragonfly.git] / contrib / bind-9.5.2 / lib / dns / opensslrsa_link.c
1 /*
2  * Copyright (C) 2004-2007, 2009  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.16.128.3 2009/01/19 00:01:10 marka Exp $
21  */
22 #ifdef OPENSSL
23
24 #include <config.h>
25
26 #include <isc/entropy.h>
27 #include <isc/md5.h>
28 #include <isc/sha1.h>
29 #include <isc/mem.h>
30 #include <isc/string.h>
31 #include <isc/util.h>
32
33 #include <dst/result.h>
34
35 #include "dst_internal.h"
36 #include "dst_openssl.h"
37 #include "dst_parse.h"
38
39 #include <openssl/err.h>
40 #include <openssl/objects.h>
41 #include <openssl/rsa.h>
42 #if OPENSSL_VERSION_NUMBER > 0x00908000L
43 #include <openssl/bn.h>
44 #endif
45
46 /*
47  * We don't use configure for windows so enforce the OpenSSL version
48  * here.  Unlike with configure we don't support overriding this test.
49  */
50 #ifdef WIN32
51 #if !((OPENSSL_VERSION_NUMBER >= 0x009070cfL && \
52        OPENSSL_VERSION_NUMBER < 0x00908000L) || \
53       OPENSSL_VERSION_NUMBER >= 0x0090804fL)
54 #error Please upgrade OpenSSL to 0.9.8d/0.9.7l or greater.
55 #endif
56 #endif
57
58
59         /*
60          * XXXMPA  Temporarily disable RSA_BLINDING as it requires
61          * good quality random data that cannot currently be guaranteed.
62          * XXXMPA  Find which versions of openssl use pseudo random data
63          * and set RSA_FLAG_BLINDING for those.
64          */
65
66 #if 0
67 #if OPENSSL_VERSION_NUMBER < 0x0090601fL
68 #define SET_FLAGS(rsa) \
69         do { \
70         (rsa)->flags &= ~(RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE); \
71         (rsa)->flags |= RSA_FLAG_BLINDING; \
72         } while (0)
73 #else
74 #define SET_FLAGS(rsa) \
75         do { \
76                 (rsa)->flags |= RSA_FLAG_BLINDING; \
77         } while (0)
78 #endif
79 #endif
80
81 #if OPENSSL_VERSION_NUMBER < 0x0090601fL
82 #define SET_FLAGS(rsa) \
83         do { \
84         (rsa)->flags &= ~(RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE); \
85         (rsa)->flags &= ~RSA_FLAG_BLINDING; \
86         } while (0)
87 #elif defined(RSA_FLAG_NO_BLINDING)
88 #define SET_FLAGS(rsa) \
89         do { \
90                 (rsa)->flags &= ~RSA_FLAG_BLINDING; \
91                 (rsa)->flags |= RSA_FLAG_NO_BLINDING; \
92         } while (0)
93 #else
94 #define SET_FLAGS(rsa) \
95         do { \
96                 (rsa)->flags &= ~RSA_FLAG_BLINDING; \
97         } while (0)
98 #endif
99
100 static isc_result_t opensslrsa_todns(const dst_key_t *key, isc_buffer_t *data);
101
102 static isc_result_t
103 opensslrsa_createctx(dst_key_t *key, dst_context_t *dctx) {
104         UNUSED(key);
105         REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
106                 dctx->key->key_alg == DST_ALG_RSASHA1);
107
108         if (dctx->key->key_alg == DST_ALG_RSAMD5) {
109                 isc_md5_t *md5ctx;
110
111                 md5ctx = isc_mem_get(dctx->mctx, sizeof(isc_md5_t));
112                 if (md5ctx == NULL)
113                         return (ISC_R_NOMEMORY);
114                 isc_md5_init(md5ctx);
115                 dctx->ctxdata.md5ctx = md5ctx;
116         } else {
117                 isc_sha1_t *sha1ctx;
118
119                 sha1ctx = isc_mem_get(dctx->mctx, sizeof(isc_sha1_t));
120                 if (sha1ctx == NULL)
121                         return (ISC_R_NOMEMORY);
122                 isc_sha1_init(sha1ctx);
123                 dctx->ctxdata.sha1ctx = sha1ctx;
124         }
125
126         return (ISC_R_SUCCESS);
127 }
128
129 static void
130 opensslrsa_destroyctx(dst_context_t *dctx) {
131         REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
132                 dctx->key->key_alg == DST_ALG_RSASHA1);
133
134         if (dctx->key->key_alg == DST_ALG_RSAMD5) {
135                 isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
136
137                 if (md5ctx != NULL) {
138                         isc_md5_invalidate(md5ctx);
139                         isc_mem_put(dctx->mctx, md5ctx, sizeof(isc_md5_t));
140                         dctx->ctxdata.md5ctx = NULL;
141                 }
142         } else {
143                 isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
144
145                 if (sha1ctx != NULL) {
146                         isc_sha1_invalidate(sha1ctx);
147                         isc_mem_put(dctx->mctx, sha1ctx, sizeof(isc_sha1_t));
148                         dctx->ctxdata.sha1ctx = NULL;
149                 }
150         }
151 }
152
153 static isc_result_t
154 opensslrsa_adddata(dst_context_t *dctx, const isc_region_t *data) {
155         REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
156                 dctx->key->key_alg == DST_ALG_RSASHA1);
157
158         if (dctx->key->key_alg == DST_ALG_RSAMD5) {
159                 isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
160                 isc_md5_update(md5ctx, data->base, data->length);
161         } else {
162                 isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
163                 isc_sha1_update(sha1ctx, data->base, data->length);
164         }
165         return (ISC_R_SUCCESS);
166 }
167
168 static isc_result_t
169 opensslrsa_sign(dst_context_t *dctx, isc_buffer_t *sig) {
170         dst_key_t *key = dctx->key;
171         RSA *rsa = key->keydata.rsa;
172         isc_region_t r;
173         /* note: ISC_SHA1_DIGESTLENGTH > ISC_MD5_DIGESTLENGTH */
174         unsigned char digest[ISC_SHA1_DIGESTLENGTH];
175         unsigned int siglen = 0;
176         int status;
177         int type;
178         unsigned int digestlen;
179         char *message;
180         unsigned long err;
181         const char* file;
182         int line;
183
184         REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
185                 dctx->key->key_alg == DST_ALG_RSASHA1);
186
187         isc_buffer_availableregion(sig, &r);
188
189         if (r.length < (unsigned int) RSA_size(rsa))
190                 return (ISC_R_NOSPACE);
191
192         if (dctx->key->key_alg == DST_ALG_RSAMD5) {
193                 isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
194                 isc_md5_final(md5ctx, digest);
195                 type = NID_md5;
196                 digestlen = ISC_MD5_DIGESTLENGTH;
197         } else {
198                 isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
199                 isc_sha1_final(sha1ctx, digest);
200                 type = NID_sha1;
201                 digestlen = ISC_SHA1_DIGESTLENGTH;
202         }
203
204         status = RSA_sign(type, digest, digestlen, r.base, &siglen, rsa);
205         if (status == 0) {
206                 err = ERR_peek_error_line(&file, &line);
207                 if (err != 0U) {
208                         message = ERR_error_string(err, NULL);
209                         fprintf(stderr, "%s:%s:%d\n", message,
210                                 file ? file : "", line);
211                 }
212                 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
213         }
214
215         isc_buffer_add(sig, siglen);
216
217         return (ISC_R_SUCCESS);
218 }
219
220 static isc_result_t
221 opensslrsa_verify(dst_context_t *dctx, const isc_region_t *sig) {
222         dst_key_t *key = dctx->key;
223         RSA *rsa = key->keydata.rsa;
224         /* note: ISC_SHA1_DIGESTLENGTH > ISC_MD5_DIGESTLENGTH */
225         unsigned char digest[ISC_SHA1_DIGESTLENGTH];
226         int status = 0;
227         int type;
228         unsigned int digestlen;
229
230         REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
231                 dctx->key->key_alg == DST_ALG_RSASHA1);
232
233         if (dctx->key->key_alg == DST_ALG_RSAMD5) {
234                 isc_md5_t *md5ctx = dctx->ctxdata.md5ctx;
235                 isc_md5_final(md5ctx, digest);
236                 type = NID_md5;
237                 digestlen = ISC_MD5_DIGESTLENGTH;
238         } else {
239                 isc_sha1_t *sha1ctx = dctx->ctxdata.sha1ctx;
240                 isc_sha1_final(sha1ctx, digest);
241                 type = NID_sha1;
242                 digestlen = ISC_SHA1_DIGESTLENGTH;
243         }
244
245         if (sig->length < (unsigned int) RSA_size(rsa))
246                 return (DST_R_VERIFYFAILURE);
247
248         status = RSA_verify(type, digest, digestlen, sig->base,
249                             RSA_size(rsa), rsa);
250         if (status != 1)
251                 return (dst__openssl_toresult(DST_R_VERIFYFAILURE));
252
253         return (ISC_R_SUCCESS);
254 }
255
256 static isc_boolean_t
257 opensslrsa_compare(const dst_key_t *key1, const dst_key_t *key2) {
258         int status;
259         RSA *rsa1, *rsa2;
260
261         rsa1 = key1->keydata.rsa;
262         rsa2 = key2->keydata.rsa;
263
264         if (rsa1 == NULL && rsa2 == NULL)
265                 return (ISC_TRUE);
266         else if (rsa1 == NULL || rsa2 == NULL)
267                 return (ISC_FALSE);
268
269         status = BN_cmp(rsa1->n, rsa2->n) ||
270                  BN_cmp(rsa1->e, rsa2->e);
271
272         if (status != 0)
273                 return (ISC_FALSE);
274
275         if (rsa1->d != NULL || rsa2->d != NULL) {
276                 if (rsa1->d == NULL || rsa2->d == NULL)
277                         return (ISC_FALSE);
278                 status = BN_cmp(rsa1->d, rsa2->d) ||
279                          BN_cmp(rsa1->p, rsa2->p) ||
280                          BN_cmp(rsa1->q, rsa2->q);
281
282                 if (status != 0)
283                         return (ISC_FALSE);
284         }
285         return (ISC_TRUE);
286 }
287
288 static isc_result_t
289 opensslrsa_generate(dst_key_t *key, int exp) {
290 #if OPENSSL_VERSION_NUMBER > 0x00908000L
291         BN_GENCB cb;
292         RSA *rsa = RSA_new();
293         BIGNUM *e = BN_new();
294
295         if (rsa == NULL || e == NULL)
296                 goto err;
297
298         if (exp == 0) {
299                 /* RSA_F4 0x10001 */
300                 BN_set_bit(e, 0);
301                 BN_set_bit(e, 16);
302         } else {
303                 /* F5 0x100000001 */
304                 BN_set_bit(e, 0);
305                 BN_set_bit(e, 32);
306         }
307
308         BN_GENCB_set_old(&cb, NULL, NULL);
309
310         if (RSA_generate_key_ex(rsa, key->key_size, e, &cb)) {
311                 BN_free(e);
312                 SET_FLAGS(rsa);
313                 key->keydata.rsa = rsa;
314                 return (ISC_R_SUCCESS);
315         }
316
317 err:
318         if (e != NULL)
319                 BN_free(e);
320         if (rsa != NULL)
321                 RSA_free(rsa);
322         return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
323 #else
324         RSA *rsa;
325         unsigned long e;
326
327         if (exp == 0)
328                e = RSA_F4;
329         else
330                e = 0x40000003;
331         rsa = RSA_generate_key(key->key_size, e, NULL, NULL);
332         if (rsa == NULL)
333                return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
334         SET_FLAGS(rsa);
335         key->keydata.rsa = rsa;
336
337         return (ISC_R_SUCCESS);
338 #endif
339 }
340
341 static isc_boolean_t
342 opensslrsa_isprivate(const dst_key_t *key) {
343         RSA *rsa = (RSA *) key->keydata.rsa;
344         return (ISC_TF(rsa != NULL && rsa->d != NULL));
345 }
346
347 static void
348 opensslrsa_destroy(dst_key_t *key) {
349         RSA *rsa = key->keydata.rsa;
350         RSA_free(rsa);
351         key->keydata.rsa = NULL;
352 }
353
354
355 static isc_result_t
356 opensslrsa_todns(const dst_key_t *key, isc_buffer_t *data) {
357         RSA *rsa;
358         isc_region_t r;
359         unsigned int e_bytes;
360         unsigned int mod_bytes;
361
362         REQUIRE(key->keydata.rsa != NULL);
363
364         rsa = key->keydata.rsa;
365
366         isc_buffer_availableregion(data, &r);
367
368         e_bytes = BN_num_bytes(rsa->e);
369         mod_bytes = BN_num_bytes(rsa->n);
370
371         if (e_bytes < 256) {    /*%< key exponent is <= 2040 bits */
372                 if (r.length < 1)
373                         return (ISC_R_NOSPACE);
374                 isc_buffer_putuint8(data, (isc_uint8_t) e_bytes);
375         } else {
376                 if (r.length < 3)
377                         return (ISC_R_NOSPACE);
378                 isc_buffer_putuint8(data, 0);
379                 isc_buffer_putuint16(data, (isc_uint16_t) e_bytes);
380         }
381
382         if (r.length < e_bytes + mod_bytes)
383                 return (ISC_R_NOSPACE);
384         isc_buffer_availableregion(data, &r);
385
386         BN_bn2bin(rsa->e, r.base);
387         r.base += e_bytes;
388         BN_bn2bin(rsa->n, r.base);
389
390         isc_buffer_add(data, e_bytes + mod_bytes);
391
392         return (ISC_R_SUCCESS);
393 }
394
395 static isc_result_t
396 opensslrsa_fromdns(dst_key_t *key, isc_buffer_t *data) {
397         RSA *rsa;
398         isc_region_t r;
399         unsigned int e_bytes;
400
401         isc_buffer_remainingregion(data, &r);
402         if (r.length == 0)
403                 return (ISC_R_SUCCESS);
404
405         rsa = RSA_new();
406         if (rsa == NULL)
407                 return (dst__openssl_toresult(ISC_R_NOMEMORY));
408         SET_FLAGS(rsa);
409
410         if (r.length < 1) {
411                 RSA_free(rsa);
412                 return (DST_R_INVALIDPUBLICKEY);
413         }
414         e_bytes = *r.base++;
415         r.length--;
416
417         if (e_bytes == 0) {
418                 if (r.length < 2) {
419                         RSA_free(rsa);
420                         return (DST_R_INVALIDPUBLICKEY);
421                 }
422                 e_bytes = ((*r.base++) << 8);
423                 e_bytes += *r.base++;
424                 r.length -= 2;
425         }
426
427         if (r.length < e_bytes) {
428                 RSA_free(rsa);
429                 return (DST_R_INVALIDPUBLICKEY);
430         }
431         rsa->e = BN_bin2bn(r.base, e_bytes, NULL);
432         r.base += e_bytes;
433         r.length -= e_bytes;
434
435         rsa->n = BN_bin2bn(r.base, r.length, NULL);
436
437         key->key_size = BN_num_bits(rsa->n);
438
439         isc_buffer_forward(data, r.length);
440
441         key->keydata.rsa = rsa;
442
443         return (ISC_R_SUCCESS);
444 }
445
446
447 static isc_result_t
448 opensslrsa_tofile(const dst_key_t *key, const char *directory) {
449         int i;
450         RSA *rsa;
451         dst_private_t priv;
452         unsigned char *bufs[8];
453         isc_result_t result;
454
455         if (key->keydata.rsa == NULL)
456                 return (DST_R_NULLKEY);
457
458         rsa = key->keydata.rsa;
459
460         for (i = 0; i < 8; i++) {
461                 bufs[i] = isc_mem_get(key->mctx, BN_num_bytes(rsa->n));
462                 if (bufs[i] == NULL) {
463                         result = ISC_R_NOMEMORY;
464                         goto fail;
465                 }
466         }
467
468         i = 0;
469
470         priv.elements[i].tag = TAG_RSA_MODULUS;
471         priv.elements[i].length = BN_num_bytes(rsa->n);
472         BN_bn2bin(rsa->n, bufs[i]);
473         priv.elements[i].data = bufs[i];
474         i++;
475
476         priv.elements[i].tag = TAG_RSA_PUBLICEXPONENT;
477         priv.elements[i].length = BN_num_bytes(rsa->e);
478         BN_bn2bin(rsa->e, bufs[i]);
479         priv.elements[i].data = bufs[i];
480         i++;
481
482         priv.elements[i].tag = TAG_RSA_PRIVATEEXPONENT;
483         priv.elements[i].length = BN_num_bytes(rsa->d);
484         BN_bn2bin(rsa->d, bufs[i]);
485         priv.elements[i].data = bufs[i];
486         i++;
487
488         priv.elements[i].tag = TAG_RSA_PRIME1;
489         priv.elements[i].length = BN_num_bytes(rsa->p);
490         BN_bn2bin(rsa->p, bufs[i]);
491         priv.elements[i].data = bufs[i];
492         i++;
493
494         priv.elements[i].tag = TAG_RSA_PRIME2;
495         priv.elements[i].length = BN_num_bytes(rsa->q);
496         BN_bn2bin(rsa->q, bufs[i]);
497         priv.elements[i].data = bufs[i];
498         i++;
499
500         priv.elements[i].tag = TAG_RSA_EXPONENT1;
501         priv.elements[i].length = BN_num_bytes(rsa->dmp1);
502         BN_bn2bin(rsa->dmp1, bufs[i]);
503         priv.elements[i].data = bufs[i];
504         i++;
505
506         priv.elements[i].tag = TAG_RSA_EXPONENT2;
507         priv.elements[i].length = BN_num_bytes(rsa->dmq1);
508         BN_bn2bin(rsa->dmq1, bufs[i]);
509         priv.elements[i].data = bufs[i];
510         i++;
511
512         priv.elements[i].tag = TAG_RSA_COEFFICIENT;
513         priv.elements[i].length = BN_num_bytes(rsa->iqmp);
514         BN_bn2bin(rsa->iqmp, bufs[i]);
515         priv.elements[i].data = bufs[i];
516         i++;
517
518         priv.nelements = i;
519         result =  dst__privstruct_writefile(key, &priv, directory);
520  fail:
521         for (i = 0; i < 8; i++) {
522                 if (bufs[i] == NULL)
523                         break;
524                 isc_mem_put(key->mctx, bufs[i], BN_num_bytes(rsa->n));
525         }
526         return (result);
527 }
528
529 static isc_result_t
530 opensslrsa_parse(dst_key_t *key, isc_lex_t *lexer) {
531         dst_private_t priv;
532         isc_result_t ret;
533         int i;
534         RSA *rsa = NULL;
535         isc_mem_t *mctx = key->mctx;
536 #define DST_RET(a) {ret = a; goto err;}
537
538         /* read private key file */
539         ret = dst__privstruct_parse(key, DST_ALG_RSA, lexer, mctx, &priv);
540         if (ret != ISC_R_SUCCESS)
541                 return (ret);
542
543         rsa = RSA_new();
544         if (rsa == NULL)
545                 DST_RET(ISC_R_NOMEMORY);
546         SET_FLAGS(rsa);
547         key->keydata.rsa = rsa;
548
549         for (i = 0; i < priv.nelements; i++) {
550                 BIGNUM *bn;
551                 bn = BN_bin2bn(priv.elements[i].data,
552                                priv.elements[i].length, NULL);
553                 if (bn == NULL)
554                         DST_RET(ISC_R_NOMEMORY);
555
556                 switch (priv.elements[i].tag) {
557                         case TAG_RSA_MODULUS:
558                                 rsa->n = bn;
559                                 break;
560                         case TAG_RSA_PUBLICEXPONENT:
561                                 rsa->e = bn;
562                                 break;
563                         case TAG_RSA_PRIVATEEXPONENT:
564                                 rsa->d = bn;
565                                 break;
566                         case TAG_RSA_PRIME1:
567                                 rsa->p = bn;
568                                 break;
569                         case TAG_RSA_PRIME2:
570                                 rsa->q = bn;
571                                 break;
572                         case TAG_RSA_EXPONENT1:
573                                 rsa->dmp1 = bn;
574                                 break;
575                         case TAG_RSA_EXPONENT2:
576                                 rsa->dmq1 = bn;
577                                 break;
578                         case TAG_RSA_COEFFICIENT:
579                                 rsa->iqmp = bn;
580                                 break;
581                 }
582         }
583         dst__privstruct_free(&priv, mctx);
584
585         key->key_size = BN_num_bits(rsa->n);
586
587         return (ISC_R_SUCCESS);
588
589  err:
590         opensslrsa_destroy(key);
591         dst__privstruct_free(&priv, mctx);
592         memset(&priv, 0, sizeof(priv));
593         return (ret);
594 }
595
596 static dst_func_t opensslrsa_functions = {
597         opensslrsa_createctx,
598         opensslrsa_destroyctx,
599         opensslrsa_adddata,
600         opensslrsa_sign,
601         opensslrsa_verify,
602         NULL, /*%< computesecret */
603         opensslrsa_compare,
604         NULL, /*%< paramcompare */
605         opensslrsa_generate,
606         opensslrsa_isprivate,
607         opensslrsa_destroy,
608         opensslrsa_todns,
609         opensslrsa_fromdns,
610         opensslrsa_tofile,
611         opensslrsa_parse,
612         NULL, /*%< cleanup */
613 };
614
615 isc_result_t
616 dst__opensslrsa_init(dst_func_t **funcp) {
617         REQUIRE(funcp != NULL);
618         if (*funcp == NULL)
619                 *funcp = &opensslrsa_functions;
620         return (ISC_R_SUCCESS);
621 }
622
623 #else /* OPENSSL */
624
625 #include <isc/util.h>
626
627 EMPTY_TRANSLATION_UNIT
628
629 #endif /* OPENSSL */
630 /*! \file */