Merge branch 'vendor/GCC'
[dragonfly.git] / contrib / bind-9.3 / lib / dns / opensslrsa_link.c
1 /*
2  * Copyright (C) 2004, 2006  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2000-2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and 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.1.4.9 2006/11/07 21:28:40 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  Temporarially disable RSA_BLINDING as it requires
61          * good quality random data that cannot currently be guarenteed.
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->opaque = 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->opaque = 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->opaque;
136
137                 if (md5ctx != NULL) {
138                         isc_md5_invalidate(md5ctx);
139                         isc_mem_put(dctx->mctx, md5ctx, sizeof(isc_md5_t));
140                 }
141         } else {
142                 isc_sha1_t *sha1ctx = dctx->opaque;
143
144                 if (sha1ctx != NULL) {
145                         isc_sha1_invalidate(sha1ctx);
146                         isc_mem_put(dctx->mctx, sha1ctx, sizeof(isc_sha1_t));
147                 }
148         }
149         dctx->opaque = NULL;
150 }
151
152 static isc_result_t
153 opensslrsa_adddata(dst_context_t *dctx, const isc_region_t *data) {
154         REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
155                 dctx->key->key_alg == DST_ALG_RSASHA1);
156
157         if (dctx->key->key_alg == DST_ALG_RSAMD5) {
158                 isc_md5_t *md5ctx = dctx->opaque;
159                 isc_md5_update(md5ctx, data->base, data->length);
160         } else {
161                 isc_sha1_t *sha1ctx = dctx->opaque;
162                 isc_sha1_update(sha1ctx, data->base, data->length);
163         }
164         return (ISC_R_SUCCESS);
165 }
166
167 static isc_result_t
168 opensslrsa_sign(dst_context_t *dctx, isc_buffer_t *sig) {
169         dst_key_t *key = dctx->key;
170         RSA *rsa = key->opaque;
171         isc_region_t r;
172         /* note: ISC_SHA1_DIGESTLENGTH > ISC_MD5_DIGESTLENGTH */
173         unsigned char digest[ISC_SHA1_DIGESTLENGTH];
174         unsigned int siglen = 0;
175         int status;
176         int type;
177         unsigned int digestlen;
178         char *message;
179         unsigned long err;
180         const char* file;
181         int line;
182
183         REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
184                 dctx->key->key_alg == DST_ALG_RSASHA1);
185
186         isc_buffer_availableregion(sig, &r);
187
188         if (r.length < (unsigned int) RSA_size(rsa))
189                 return (ISC_R_NOSPACE);
190
191         if (dctx->key->key_alg == DST_ALG_RSAMD5) {
192                 isc_md5_t *md5ctx = dctx->opaque;
193                 isc_md5_final(md5ctx, digest);
194                 type = NID_md5;
195                 digestlen = ISC_MD5_DIGESTLENGTH;
196         } else {
197                 isc_sha1_t *sha1ctx = dctx->opaque;
198                 isc_sha1_final(sha1ctx, digest);
199                 type = NID_sha1;
200                 digestlen = ISC_SHA1_DIGESTLENGTH;
201         }
202
203         status = RSA_sign(type, digest, digestlen, r.base, &siglen, rsa);
204         if (status == 0) {
205                 err = ERR_peek_error_line(&file, &line);
206                 if (err != 0U) {
207                         message = ERR_error_string(err, NULL);
208                         fprintf(stderr, "%s:%s:%d\n", message,
209                                 file ? file : "", line);
210                 }
211                 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
212         }
213
214         isc_buffer_add(sig, siglen);
215
216         return (ISC_R_SUCCESS);
217 }
218
219 static isc_result_t
220 opensslrsa_verify(dst_context_t *dctx, const isc_region_t *sig) {
221         dst_key_t *key = dctx->key;
222         RSA *rsa = key->opaque;
223         /* note: ISC_SHA1_DIGESTLENGTH > ISC_MD5_DIGESTLENGTH */
224         unsigned char digest[ISC_SHA1_DIGESTLENGTH];
225         int status = 0;
226         int type;
227         unsigned int digestlen;
228
229         REQUIRE(dctx->key->key_alg == DST_ALG_RSAMD5 ||
230                 dctx->key->key_alg == DST_ALG_RSASHA1);
231
232         if (dctx->key->key_alg == DST_ALG_RSAMD5) {
233                 isc_md5_t *md5ctx = dctx->opaque;
234                 isc_md5_final(md5ctx, digest);
235                 type = NID_md5;
236                 digestlen = ISC_MD5_DIGESTLENGTH;
237         } else {
238                 isc_sha1_t *sha1ctx = dctx->opaque;
239                 isc_sha1_final(sha1ctx, digest);
240                 type = NID_sha1;
241                 digestlen = ISC_SHA1_DIGESTLENGTH;
242         }
243
244         if (sig->length < (unsigned int) RSA_size(rsa))
245                 return (DST_R_VERIFYFAILURE);
246
247         status = RSA_verify(type, digest, digestlen, sig->base,
248                             RSA_size(rsa), rsa);
249         if (status == 0)
250                 return (dst__openssl_toresult(DST_R_VERIFYFAILURE));
251
252         return (ISC_R_SUCCESS);
253 }
254
255 static isc_boolean_t
256 opensslrsa_compare(const dst_key_t *key1, const dst_key_t *key2) {
257         int status;
258         RSA *rsa1, *rsa2;
259
260         rsa1 = (RSA *) key1->opaque;
261         rsa2 = (RSA *) key2->opaque;
262
263         if (rsa1 == NULL && rsa2 == NULL)
264                 return (ISC_TRUE);
265         else if (rsa1 == NULL || rsa2 == NULL)
266                 return (ISC_FALSE);
267
268         status = BN_cmp(rsa1->n, rsa2->n) ||
269                  BN_cmp(rsa1->e, rsa2->e);
270
271         if (status != 0)
272                 return (ISC_FALSE);
273
274         if (rsa1->d != NULL || rsa2->d != NULL) {
275                 if (rsa1->d == NULL || rsa2->d == NULL)
276                         return (ISC_FALSE);
277                 status = BN_cmp(rsa1->d, rsa2->d) ||
278                          BN_cmp(rsa1->p, rsa2->p) ||
279                          BN_cmp(rsa1->q, rsa2->q);
280
281                 if (status != 0)
282                         return (ISC_FALSE);
283         }
284         return (ISC_TRUE);
285 }
286
287 static isc_result_t
288 opensslrsa_generate(dst_key_t *key, int exp) {
289 #if OPENSSL_VERSION_NUMBER > 0x00908000L
290         BN_GENCB cb;
291         RSA *rsa = RSA_new();
292         BIGNUM *e = BN_new();
293
294         if (rsa == NULL || e == NULL)
295                 goto err;
296
297         if (exp == 0) {
298                 /* RSA_F4 0x10001 */
299                 BN_set_bit(e, 0);
300                 BN_set_bit(e, 16);
301         } else {
302                 /* F5 0x100000001 */
303                 BN_set_bit(e, 0);
304                 BN_set_bit(e, 32);
305         }
306
307         BN_GENCB_set_old(&cb, NULL, NULL);
308
309         if (RSA_generate_key_ex(rsa, key->key_size, e, &cb)) {
310                 BN_free(e);
311                 SET_FLAGS(rsa);
312                 key->opaque = rsa;
313                 return (ISC_R_SUCCESS);
314         }
315
316 err:
317         if (e != NULL)
318                 BN_free(e);
319         if (rsa != NULL)
320                 RSA_free(rsa);
321         return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
322 #else
323         RSA *rsa;
324         unsigned long e;
325
326         if (exp == 0)
327                e = RSA_F4;
328         else
329                e = 0x40000003;
330         rsa = RSA_generate_key(key->key_size, e, NULL, NULL);
331         if (rsa == NULL)
332                return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
333         SET_FLAGS(rsa);
334         key->opaque = rsa;
335
336         return (ISC_R_SUCCESS);
337 #endif
338 }
339
340 static isc_boolean_t
341 opensslrsa_isprivate(const dst_key_t *key) {
342         RSA *rsa = (RSA *) key->opaque;
343         return (ISC_TF(rsa != NULL && rsa->d != NULL));
344 }
345
346 static void
347 opensslrsa_destroy(dst_key_t *key) {
348         RSA *rsa = key->opaque;
349         RSA_free(rsa);
350         key->opaque = NULL;
351 }
352
353
354 static isc_result_t
355 opensslrsa_todns(const dst_key_t *key, isc_buffer_t *data) {
356         RSA *rsa;
357         isc_region_t r;
358         unsigned int e_bytes;
359         unsigned int mod_bytes;
360
361         REQUIRE(key->opaque != NULL);
362
363         rsa = (RSA *) key->opaque;
364
365         isc_buffer_availableregion(data, &r);
366
367         e_bytes = BN_num_bytes(rsa->e);
368         mod_bytes = BN_num_bytes(rsa->n);
369
370         if (e_bytes < 256) {    /* key exponent is <= 2040 bits */
371                 if (r.length < 1)
372                         return (ISC_R_NOSPACE);
373                 isc_buffer_putuint8(data, (isc_uint8_t) e_bytes);
374         } else {
375                 if (r.length < 3)
376                         return (ISC_R_NOSPACE);
377                 isc_buffer_putuint8(data, 0);
378                 isc_buffer_putuint16(data, (isc_uint16_t) e_bytes);
379         }
380
381         if (r.length < e_bytes + mod_bytes)
382                 return (ISC_R_NOSPACE);
383         isc_buffer_availableregion(data, &r);
384
385         BN_bn2bin(rsa->e, r.base);
386         r.base += e_bytes;
387         BN_bn2bin(rsa->n, r.base);
388
389         isc_buffer_add(data, e_bytes + mod_bytes);
390
391         return (ISC_R_SUCCESS);
392 }
393
394 static isc_result_t
395 opensslrsa_fromdns(dst_key_t *key, isc_buffer_t *data) {
396         RSA *rsa;
397         isc_region_t r;
398         unsigned int e_bytes;
399
400         isc_buffer_remainingregion(data, &r);
401         if (r.length == 0)
402                 return (ISC_R_SUCCESS);
403
404         rsa = RSA_new();
405         if (rsa == NULL)
406                 return (ISC_R_NOMEMORY);
407         SET_FLAGS(rsa);
408
409         if (r.length < 1) {
410                 RSA_free(rsa);
411                 return (DST_R_INVALIDPUBLICKEY);
412         }
413         e_bytes = *r.base++;
414         r.length--;
415
416         if (e_bytes == 0) {
417                 if (r.length < 2) {
418                         RSA_free(rsa);
419                         return (DST_R_INVALIDPUBLICKEY);
420                 }
421                 e_bytes = ((*r.base++) << 8);
422                 e_bytes += *r.base++;
423                 r.length -= 2;
424         }
425
426         if (r.length < e_bytes) {
427                 RSA_free(rsa);
428                 return (DST_R_INVALIDPUBLICKEY);
429         }
430         rsa->e = BN_bin2bn(r.base, e_bytes, NULL);
431         r.base += e_bytes;
432         r.length -= e_bytes;
433
434         rsa->n = BN_bin2bn(r.base, r.length, NULL);
435
436         key->key_size = BN_num_bits(rsa->n);
437
438         isc_buffer_forward(data, r.length);
439
440         key->opaque = (void *) rsa;
441
442         return (ISC_R_SUCCESS);
443 }
444
445
446 static isc_result_t
447 opensslrsa_tofile(const dst_key_t *key, const char *directory) {
448         int i;
449         RSA *rsa;
450         dst_private_t priv;
451         unsigned char *bufs[8];
452         isc_result_t result;
453
454         if (key->opaque == NULL)
455                 return (DST_R_NULLKEY);
456
457         rsa = (RSA *) key->opaque;
458
459         for (i = 0; i < 8; i++) {
460                 bufs[i] = isc_mem_get(key->mctx, BN_num_bytes(rsa->n));
461                 if (bufs[i] == NULL) {
462                         result = ISC_R_NOMEMORY;
463                         goto fail;
464                 }
465         }
466
467         i = 0;
468
469         priv.elements[i].tag = TAG_RSA_MODULUS;
470         priv.elements[i].length = BN_num_bytes(rsa->n);
471         BN_bn2bin(rsa->n, bufs[i]);
472         priv.elements[i].data = bufs[i];
473         i++;
474
475         priv.elements[i].tag = TAG_RSA_PUBLICEXPONENT;
476         priv.elements[i].length = BN_num_bytes(rsa->e);
477         BN_bn2bin(rsa->e, bufs[i]);
478         priv.elements[i].data = bufs[i];
479         i++;
480
481         priv.elements[i].tag = TAG_RSA_PRIVATEEXPONENT;
482         priv.elements[i].length = BN_num_bytes(rsa->d);
483         BN_bn2bin(rsa->d, bufs[i]);
484         priv.elements[i].data = bufs[i];
485         i++;
486
487         priv.elements[i].tag = TAG_RSA_PRIME1;
488         priv.elements[i].length = BN_num_bytes(rsa->p);
489         BN_bn2bin(rsa->p, bufs[i]);
490         priv.elements[i].data = bufs[i];
491         i++;
492
493         priv.elements[i].tag = TAG_RSA_PRIME2;
494         priv.elements[i].length = BN_num_bytes(rsa->q);
495         BN_bn2bin(rsa->q, bufs[i]);
496         priv.elements[i].data = bufs[i];
497         i++;
498
499         priv.elements[i].tag = TAG_RSA_EXPONENT1;
500         priv.elements[i].length = BN_num_bytes(rsa->dmp1);
501         BN_bn2bin(rsa->dmp1, bufs[i]);
502         priv.elements[i].data = bufs[i];
503         i++;
504
505         priv.elements[i].tag = TAG_RSA_EXPONENT2;
506         priv.elements[i].length = BN_num_bytes(rsa->dmq1);
507         BN_bn2bin(rsa->dmq1, bufs[i]);
508         priv.elements[i].data = bufs[i];
509         i++;
510
511         priv.elements[i].tag = TAG_RSA_COEFFICIENT;
512         priv.elements[i].length = BN_num_bytes(rsa->iqmp);
513         BN_bn2bin(rsa->iqmp, bufs[i]);
514         priv.elements[i].data = bufs[i];
515         i++;
516
517         priv.nelements = i;
518         result =  dst__privstruct_writefile(key, &priv, directory);
519  fail:
520         for (i = 0; i < 8; i++) {
521                 if (bufs[i] == NULL)
522                         break;
523                 isc_mem_put(key->mctx, bufs[i], BN_num_bytes(rsa->n));
524         }
525         return (result);
526 }
527
528 static isc_result_t
529 opensslrsa_parse(dst_key_t *key, isc_lex_t *lexer) {
530         dst_private_t priv;
531         isc_result_t ret;
532         int i;
533         RSA *rsa = NULL;
534         isc_mem_t *mctx = key->mctx;
535 #define DST_RET(a) {ret = a; goto err;}
536
537         /* read private key file */
538         ret = dst__privstruct_parse(key, DST_ALG_RSA, lexer, mctx, &priv);
539         if (ret != ISC_R_SUCCESS)
540                 return (ret);
541
542         rsa = RSA_new();
543         if (rsa == NULL)
544                 DST_RET(ISC_R_NOMEMORY);
545         SET_FLAGS(rsa);
546         key->opaque = rsa;
547
548         for (i = 0; i < priv.nelements; i++) {
549                 BIGNUM *bn;
550                 bn = BN_bin2bn(priv.elements[i].data,
551                                priv.elements[i].length, NULL);
552                 if (bn == NULL)
553                         DST_RET(ISC_R_NOMEMORY);
554
555                 switch (priv.elements[i].tag) {
556                         case TAG_RSA_MODULUS:
557                                 rsa->n = bn;
558                                 break;
559                         case TAG_RSA_PUBLICEXPONENT:
560                                 rsa->e = bn;
561                                 break;
562                         case TAG_RSA_PRIVATEEXPONENT:
563                                 rsa->d = bn;
564                                 break;
565                         case TAG_RSA_PRIME1:
566                                 rsa->p = bn;
567                                 break;
568                         case TAG_RSA_PRIME2:
569                                 rsa->q = bn;
570                                 break;
571                         case TAG_RSA_EXPONENT1:
572                                 rsa->dmp1 = bn;
573                                 break;
574                         case TAG_RSA_EXPONENT2:
575                                 rsa->dmq1 = bn;
576                                 break;
577                         case TAG_RSA_COEFFICIENT:
578                                 rsa->iqmp = bn;
579                                 break;
580                 }
581         }
582         dst__privstruct_free(&priv, mctx);
583
584         key->key_size = BN_num_bits(rsa->n);
585
586         return (ISC_R_SUCCESS);
587
588  err:
589         opensslrsa_destroy(key);
590         dst__privstruct_free(&priv, mctx);
591         memset(&priv, 0, sizeof(priv));
592         return (ret);
593 }
594
595 static dst_func_t opensslrsa_functions = {
596         opensslrsa_createctx,
597         opensslrsa_destroyctx,
598         opensslrsa_adddata,
599         opensslrsa_sign,
600         opensslrsa_verify,
601         NULL, /* computesecret */
602         opensslrsa_compare,
603         NULL, /* paramcompare */
604         opensslrsa_generate,
605         opensslrsa_isprivate,
606         opensslrsa_destroy,
607         opensslrsa_todns,
608         opensslrsa_fromdns,
609         opensslrsa_tofile,
610         opensslrsa_parse,
611         NULL, /* cleanup */
612 };
613
614 isc_result_t
615 dst__opensslrsa_init(dst_func_t **funcp) {
616         REQUIRE(funcp != NULL);
617         if (*funcp == NULL)
618                 *funcp = &opensslrsa_functions;
619         return (ISC_R_SUCCESS);
620 }
621
622 #else /* OPENSSL */
623
624 #include <isc/util.h>
625
626 EMPTY_TRANSLATION_UNIT
627
628 #endif /* OPENSSL */