]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/bind9/lib/dns/opensslecdsa_link.c
MFC r254651:
[FreeBSD/stable/9.git] / contrib / bind9 / lib / dns / opensslecdsa_link.c
1 /*
2  * Copyright (C) 2012, 2013  Internet Systems Consortium, Inc. ("ISC")
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14  * PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 /* $Id$ */
18
19 #include <config.h>
20
21 #ifdef HAVE_OPENSSL_ECDSA
22
23 #if !defined(HAVE_EVP_SHA256) || !defined(HAVE_EVP_SHA384)
24 #error "ECDSA without EVP for SHA2?"
25 #endif
26
27 #include <isc/entropy.h>
28 #include <isc/mem.h>
29 #include <isc/sha2.h>
30 #include <isc/string.h>
31 #include <isc/util.h>
32
33 #include <dns/keyvalues.h>
34 #include <dst/result.h>
35
36 #include "dst_internal.h"
37 #include "dst_openssl.h"
38 #include "dst_parse.h"
39
40 #include <openssl/err.h>
41 #include <openssl/objects.h>
42 #include <openssl/ecdsa.h>
43 #include <openssl/bn.h>
44
45 #ifndef NID_X9_62_prime256v1
46 #error "P-256 group is not known (NID_X9_62_prime256v1)"
47 #endif
48 #ifndef NID_secp384r1
49 #error "P-384 group is not known (NID_secp384r1)"
50 #endif
51
52 #define DST_RET(a) {ret = a; goto err;}
53
54 static isc_result_t opensslecdsa_todns(const dst_key_t *key,
55                                        isc_buffer_t *data);
56
57 static isc_result_t
58 opensslecdsa_createctx(dst_key_t *key, dst_context_t *dctx) {
59         EVP_MD_CTX *evp_md_ctx;
60         const EVP_MD *type = NULL;
61
62         UNUSED(key);
63         REQUIRE(dctx->key->key_alg == DST_ALG_ECDSA256 ||
64                 dctx->key->key_alg == DST_ALG_ECDSA384);
65
66         evp_md_ctx = EVP_MD_CTX_create();
67         if (evp_md_ctx == NULL)
68                 return (ISC_R_NOMEMORY);
69         if (dctx->key->key_alg == DST_ALG_ECDSA256)
70                 type = EVP_sha256();
71         else
72                 type = EVP_sha384();
73
74         if (!EVP_DigestInit_ex(evp_md_ctx, type, NULL)) {
75                 EVP_MD_CTX_destroy(evp_md_ctx);
76                 return (dst__openssl_toresult3(dctx->category,
77                                                "EVP_DigestInit_ex",
78                                                ISC_R_FAILURE));
79         }
80
81         dctx->ctxdata.evp_md_ctx = evp_md_ctx;
82
83         return (ISC_R_SUCCESS);
84 }
85
86 static void
87 opensslecdsa_destroyctx(dst_context_t *dctx) {
88         EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
89
90         REQUIRE(dctx->key->key_alg == DST_ALG_ECDSA256 ||
91                 dctx->key->key_alg == DST_ALG_ECDSA384);
92
93         if (evp_md_ctx != NULL) {
94                 EVP_MD_CTX_destroy(evp_md_ctx);
95                 dctx->ctxdata.evp_md_ctx = NULL;
96         }
97 }
98
99 static isc_result_t
100 opensslecdsa_adddata(dst_context_t *dctx, const isc_region_t *data) {
101         EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
102
103         REQUIRE(dctx->key->key_alg == DST_ALG_ECDSA256 ||
104                 dctx->key->key_alg == DST_ALG_ECDSA384);
105
106         if (!EVP_DigestUpdate(evp_md_ctx, data->base, data->length))
107                 return (dst__openssl_toresult3(dctx->category,
108                                                "EVP_DigestUpdate",
109                                                ISC_R_FAILURE));
110
111         return (ISC_R_SUCCESS);
112 }
113
114 static int
115 BN_bn2bin_fixed(BIGNUM *bn, unsigned char *buf, int size) {
116         int bytes = size - BN_num_bytes(bn);
117
118         while (bytes-- > 0)
119                 *buf++ = 0;
120         BN_bn2bin(bn, buf);
121         return (size);
122 }
123
124 static isc_result_t
125 opensslecdsa_sign(dst_context_t *dctx, isc_buffer_t *sig) {
126         isc_result_t ret;
127         dst_key_t *key = dctx->key;
128         isc_region_t r;
129         ECDSA_SIG *ecdsasig;
130         EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
131         EVP_PKEY *pkey = key->keydata.pkey;
132         EC_KEY *eckey = EVP_PKEY_get1_EC_KEY(pkey);
133         unsigned int dgstlen, siglen;
134         unsigned char digest[EVP_MAX_MD_SIZE];
135
136         REQUIRE(key->key_alg == DST_ALG_ECDSA256 ||
137                 key->key_alg == DST_ALG_ECDSA384);
138
139         if (eckey == NULL)
140                 return (ISC_R_FAILURE);
141
142         if (key->key_alg == DST_ALG_ECDSA256)
143                 siglen = DNS_SIG_ECDSA256SIZE;
144         else
145                 siglen = DNS_SIG_ECDSA384SIZE;
146
147         isc_buffer_availableregion(sig, &r);
148         if (r.length < siglen)
149                 DST_RET(ISC_R_NOSPACE);
150
151         if (!EVP_DigestFinal(evp_md_ctx, digest, &dgstlen))
152                 DST_RET(dst__openssl_toresult3(dctx->category,
153                                                "EVP_DigestFinal",
154                                                ISC_R_FAILURE));
155
156         ecdsasig = ECDSA_do_sign(digest, dgstlen, eckey);
157         if (ecdsasig == NULL)
158                 DST_RET(dst__openssl_toresult3(dctx->category,
159                                                "ECDSA_do_sign",
160                                                DST_R_SIGNFAILURE));
161         BN_bn2bin_fixed(ecdsasig->r, r.base, siglen / 2);
162         r.base += siglen / 2;
163         BN_bn2bin_fixed(ecdsasig->s, r.base, siglen / 2);
164         r.base += siglen / 2;
165         ECDSA_SIG_free(ecdsasig);
166         isc_buffer_add(sig, siglen);
167         ret = ISC_R_SUCCESS;
168
169  err:
170         if (eckey != NULL)
171                 EC_KEY_free(eckey);
172         return (ret);
173 }
174
175 static isc_result_t
176 opensslecdsa_verify(dst_context_t *dctx, const isc_region_t *sig) {
177         isc_result_t ret;
178         dst_key_t *key = dctx->key;
179         int status;
180         unsigned char *cp = sig->base;
181         ECDSA_SIG *ecdsasig = NULL;
182         EVP_MD_CTX *evp_md_ctx = dctx->ctxdata.evp_md_ctx;
183         EVP_PKEY *pkey = key->keydata.pkey;
184         EC_KEY *eckey = EVP_PKEY_get1_EC_KEY(pkey);
185         unsigned int dgstlen, siglen;
186         unsigned char digest[EVP_MAX_MD_SIZE];
187
188         REQUIRE(key->key_alg == DST_ALG_ECDSA256 ||
189                 key->key_alg == DST_ALG_ECDSA384);
190
191         if (eckey == NULL)
192                 return (ISC_R_FAILURE);
193
194         if (key->key_alg == DST_ALG_ECDSA256)
195                 siglen = DNS_SIG_ECDSA256SIZE;
196         else
197                 siglen = DNS_SIG_ECDSA384SIZE;
198
199         if (sig->length != siglen)
200                 return (DST_R_VERIFYFAILURE);
201
202         if (!EVP_DigestFinal_ex(evp_md_ctx, digest, &dgstlen))
203                 DST_RET (dst__openssl_toresult3(dctx->category,
204                                                 "EVP_DigestFinal_ex",
205                                                 ISC_R_FAILURE));
206
207         ecdsasig = ECDSA_SIG_new();
208         if (ecdsasig == NULL)
209                 DST_RET (ISC_R_NOMEMORY);
210         if (ecdsasig->r != NULL)
211                 BN_free(ecdsasig->r);
212         ecdsasig->r = BN_bin2bn(cp, siglen / 2, NULL);
213         cp += siglen / 2;
214         if (ecdsasig->s != NULL)
215                 BN_free(ecdsasig->s);
216         ecdsasig->s = BN_bin2bn(cp, siglen / 2, NULL);
217         /* cp += siglen / 2; */
218
219         status = ECDSA_do_verify(digest, dgstlen, ecdsasig, eckey);
220         switch (status) {
221         case 1:
222                 ret = ISC_R_SUCCESS;
223                 break;
224         case 0:
225                 ret = dst__openssl_toresult(DST_R_VERIFYFAILURE);
226                 break;
227         default:
228                 ret = dst__openssl_toresult3(dctx->category,
229                                              "ECDSA_do_verify",
230                                              DST_R_VERIFYFAILURE);
231                 break;
232         }
233
234  err:
235         if (ecdsasig != NULL)
236                 ECDSA_SIG_free(ecdsasig);
237         if (eckey != NULL)
238                 EC_KEY_free(eckey);
239         return (ret);
240 }
241
242 static isc_boolean_t
243 opensslecdsa_compare(const dst_key_t *key1, const dst_key_t *key2) {
244         isc_boolean_t ret;
245         int status;
246         EVP_PKEY *pkey1 = key1->keydata.pkey;
247         EVP_PKEY *pkey2 = key2->keydata.pkey;
248         EC_KEY *eckey1 = NULL;
249         EC_KEY *eckey2 = NULL;
250         const BIGNUM *priv1, *priv2;
251
252         if (pkey1 == NULL && pkey2 == NULL)
253                 return (ISC_TRUE);
254         else if (pkey1 == NULL || pkey2 == NULL)
255                 return (ISC_FALSE);
256
257         eckey1 = EVP_PKEY_get1_EC_KEY(pkey1);
258         eckey2 = EVP_PKEY_get1_EC_KEY(pkey2);
259         if (eckey1 == NULL && eckey2 == NULL) {
260                 DST_RET (ISC_TRUE);
261         } else if (eckey1 == NULL || eckey2 == NULL)
262                 DST_RET (ISC_FALSE);
263
264         status = EVP_PKEY_cmp(pkey1, pkey2);
265         if (status != 1)
266                 DST_RET (ISC_FALSE);
267
268         priv1 = EC_KEY_get0_private_key(eckey1);
269         priv2 = EC_KEY_get0_private_key(eckey2);
270         if (priv1 != NULL || priv2 != NULL) {
271                 if (priv1 == NULL || priv2 == NULL)
272                         DST_RET (ISC_FALSE);
273                 if (BN_cmp(priv1, priv2) != 0)
274                         DST_RET (ISC_FALSE);
275         }
276         ret = ISC_TRUE;
277
278  err:
279         if (eckey1 != NULL)
280                 EC_KEY_free(eckey1);
281         if (eckey2 != NULL)
282                 EC_KEY_free(eckey2);
283         return (ret);
284 }
285
286 static isc_result_t
287 opensslecdsa_generate(dst_key_t *key, int unused, void (*callback)(int)) {
288         isc_result_t ret;
289         EVP_PKEY *pkey;
290         EC_KEY *eckey = NULL;
291         int group_nid;
292
293         REQUIRE(key->key_alg == DST_ALG_ECDSA256 ||
294                 key->key_alg == DST_ALG_ECDSA384);
295         UNUSED(unused);
296         UNUSED(callback);
297
298         if (key->key_alg == DST_ALG_ECDSA256)
299                 group_nid = NID_X9_62_prime256v1;
300         else
301                 group_nid = NID_secp384r1;
302
303         eckey = EC_KEY_new_by_curve_name(group_nid);
304         if (eckey == NULL)
305                 return (dst__openssl_toresult2("EC_KEY_new_by_curve_name",
306                                                DST_R_OPENSSLFAILURE));
307
308         if (EC_KEY_generate_key(eckey) != 1)
309                 DST_RET (dst__openssl_toresult2("EC_KEY_generate_key",
310                                                 DST_R_OPENSSLFAILURE));
311
312         pkey = EVP_PKEY_new();
313         if (pkey == NULL)
314                 DST_RET (ISC_R_NOMEMORY);
315         if (!EVP_PKEY_set1_EC_KEY(pkey, eckey)) {
316                 EVP_PKEY_free(pkey);
317                 DST_RET (ISC_R_FAILURE);
318         }
319         key->keydata.pkey = pkey;
320         ret = ISC_R_SUCCESS;
321
322  err:
323         if (eckey != NULL)
324                 EC_KEY_free(eckey);
325         return (ret);
326 }
327
328 static isc_boolean_t
329 opensslecdsa_isprivate(const dst_key_t *key) {
330         isc_boolean_t ret;
331         EVP_PKEY *pkey = key->keydata.pkey;
332         EC_KEY *eckey = EVP_PKEY_get1_EC_KEY(pkey);
333
334         ret = ISC_TF(eckey != NULL && EC_KEY_get0_private_key(eckey) != NULL);
335         if (eckey != NULL)
336                 EC_KEY_free(eckey);
337         return (ret);
338 }
339
340 static void
341 opensslecdsa_destroy(dst_key_t *key) {
342         EVP_PKEY *pkey = key->keydata.pkey;
343
344         EVP_PKEY_free(pkey);
345         key->keydata.pkey = NULL;
346 }
347
348 static isc_result_t
349 opensslecdsa_todns(const dst_key_t *key, isc_buffer_t *data) {
350         isc_result_t ret;
351         EVP_PKEY *pkey;
352         EC_KEY *eckey = NULL;
353         isc_region_t r;
354         int len;
355         unsigned char *cp;
356         unsigned char buf[DNS_KEY_ECDSA384SIZE + 1];
357
358         REQUIRE(key->keydata.pkey != NULL);
359
360         pkey = key->keydata.pkey;
361         eckey = EVP_PKEY_get1_EC_KEY(pkey);
362         if (eckey == NULL)
363                 return (dst__openssl_toresult(ISC_R_FAILURE));
364         len = i2o_ECPublicKey(eckey, NULL);
365         /* skip form */
366         len--;
367
368         isc_buffer_availableregion(data, &r);
369         if (r.length < (unsigned int) len)
370                 DST_RET (ISC_R_NOSPACE);
371         cp = buf;
372         if (!i2o_ECPublicKey(eckey, &cp))
373                 DST_RET (dst__openssl_toresult(ISC_R_FAILURE));
374         memcpy(r.base, buf + 1, len);
375         isc_buffer_add(data, len);
376         ret = ISC_R_SUCCESS;
377
378  err:
379         if (eckey != NULL)
380                 EC_KEY_free(eckey);
381         return (ret);
382 }
383
384 static isc_result_t
385 opensslecdsa_fromdns(dst_key_t *key, isc_buffer_t *data) {
386         isc_result_t ret;
387         EVP_PKEY *pkey;
388         EC_KEY *eckey = NULL;
389         isc_region_t r;
390         int group_nid;
391         unsigned int len;
392         const unsigned char *cp;
393         unsigned char buf[DNS_KEY_ECDSA384SIZE + 1];
394
395         REQUIRE(key->key_alg == DST_ALG_ECDSA256 ||
396                 key->key_alg == DST_ALG_ECDSA384);
397
398         if (key->key_alg == DST_ALG_ECDSA256) {
399                 len = DNS_KEY_ECDSA256SIZE;
400                 group_nid = NID_X9_62_prime256v1;
401         } else {
402                 len = DNS_KEY_ECDSA384SIZE;
403                 group_nid = NID_secp384r1;
404         }
405
406         isc_buffer_remainingregion(data, &r);
407         if (r.length == 0)
408                 return (ISC_R_SUCCESS);
409         if (r.length < len)
410                 return (DST_R_INVALIDPUBLICKEY);
411
412         eckey = EC_KEY_new_by_curve_name(group_nid);
413         if (eckey == NULL)
414                 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
415
416         buf[0] = POINT_CONVERSION_UNCOMPRESSED;
417         memcpy(buf + 1, r.base, len);
418         cp = buf;
419         if (o2i_ECPublicKey(&eckey,
420                             (const unsigned char **) &cp,
421                             (long) len + 1) == NULL)
422                 DST_RET (dst__openssl_toresult(DST_R_INVALIDPUBLICKEY));
423         if (EC_KEY_check_key(eckey) != 1)
424                 DST_RET (dst__openssl_toresult(DST_R_INVALIDPUBLICKEY));
425
426         pkey = EVP_PKEY_new();
427         if (pkey == NULL)
428                 DST_RET (ISC_R_NOMEMORY);
429         if (!EVP_PKEY_set1_EC_KEY(pkey, eckey)) {
430                 EVP_PKEY_free(pkey);
431                 DST_RET (dst__openssl_toresult(ISC_R_FAILURE));
432         }
433
434         isc_buffer_forward(data, len);
435         key->keydata.pkey = pkey;
436         ret = ISC_R_SUCCESS;
437
438  err:
439         if (eckey != NULL)
440                 EC_KEY_free(eckey);
441         return (ret);
442 }
443
444 static isc_result_t
445 opensslecdsa_tofile(const dst_key_t *key, const char *directory) {
446         isc_result_t ret;
447         EVP_PKEY *pkey;
448         EC_KEY *eckey = NULL;
449         const BIGNUM *privkey;
450         dst_private_t priv;
451         unsigned char *buf = NULL;
452
453         if (key->keydata.pkey == NULL)
454                 return (DST_R_NULLKEY);
455
456         pkey = key->keydata.pkey;
457         eckey = EVP_PKEY_get1_EC_KEY(pkey);
458         if (eckey == NULL)
459                 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
460         privkey = EC_KEY_get0_private_key(eckey);
461         if (privkey == NULL)
462                 DST_RET (ISC_R_FAILURE);
463
464         buf = isc_mem_get(key->mctx, BN_num_bytes(privkey));
465         if (buf == NULL)
466                 DST_RET (ISC_R_NOMEMORY);
467
468         priv.elements[0].tag = TAG_ECDSA_PRIVATEKEY;
469         priv.elements[0].length = BN_num_bytes(privkey);
470         BN_bn2bin(privkey, buf);
471         priv.elements[0].data = buf;
472         priv.nelements = ECDSA_NTAGS;
473         ret = dst__privstruct_writefile(key, &priv, directory);
474
475  err:
476         if (eckey != NULL)
477                 EC_KEY_free(eckey);
478         if (buf != NULL)
479                 isc_mem_put(key->mctx, buf, BN_num_bytes(privkey));
480         return (ret);
481 }
482
483 static isc_result_t
484 ecdsa_check(EC_KEY *eckey, dst_key_t *pub)
485 {
486         isc_result_t ret = ISC_R_FAILURE;
487         EVP_PKEY *pkey;
488         EC_KEY *pubeckey = NULL;
489         const EC_POINT *pubkey;
490
491         if (pub == NULL)
492                 return (ISC_R_SUCCESS);
493         pkey = pub->keydata.pkey;
494         if (pkey == NULL)
495                 return (ISC_R_SUCCESS);
496         pubeckey = EVP_PKEY_get1_EC_KEY(pkey);
497         if (pubeckey == NULL)
498                 return (ISC_R_SUCCESS);
499         pubkey = EC_KEY_get0_public_key(pubeckey);
500         if (pubkey == NULL)
501                 DST_RET (ISC_R_SUCCESS);
502         if (EC_KEY_set_public_key(eckey, pubkey) != 1)
503                 DST_RET (ISC_R_SUCCESS);
504         if (EC_KEY_check_key(eckey) == 1)
505                 DST_RET (ISC_R_SUCCESS);
506
507  err:
508         if (pubeckey != NULL)
509                 EC_KEY_free(pubeckey);
510         return (ret);
511 }
512
513 static isc_result_t
514 opensslecdsa_parse(dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub) {
515         dst_private_t priv;
516         isc_result_t ret;
517         EVP_PKEY *pkey;
518         EC_KEY *eckey = NULL;
519         BIGNUM *privkey;
520         int group_nid;
521         isc_mem_t *mctx = key->mctx;
522
523         REQUIRE(key->key_alg == DST_ALG_ECDSA256 ||
524                 key->key_alg == DST_ALG_ECDSA384);
525
526         if (key->key_alg == DST_ALG_ECDSA256)
527                 group_nid = NID_X9_62_prime256v1;
528         else
529                 group_nid = NID_secp384r1;
530
531         eckey = EC_KEY_new_by_curve_name(group_nid);
532         if (eckey == NULL)
533                 return (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
534
535         /* read private key file */
536         ret = dst__privstruct_parse(key, DST_ALG_ECDSA256, lexer, mctx, &priv);
537         if (ret != ISC_R_SUCCESS)
538                 goto err;
539
540         privkey = BN_bin2bn(priv.elements[0].data,
541                             priv.elements[0].length, NULL);
542         if (privkey == NULL)
543                 DST_RET(ISC_R_NOMEMORY);
544         if (!EC_KEY_set_private_key(eckey, privkey))
545                 DST_RET(ISC_R_NOMEMORY);
546         if (ecdsa_check(eckey, pub) != ISC_R_SUCCESS)
547                 DST_RET(DST_R_INVALIDPRIVATEKEY);
548         dst__privstruct_free(&priv, mctx);
549         memset(&priv, 0, sizeof(priv));
550
551         pkey = EVP_PKEY_new();
552         if (pkey == NULL)
553                 DST_RET (ISC_R_NOMEMORY);
554         if (!EVP_PKEY_set1_EC_KEY(pkey, eckey)) {
555                 EVP_PKEY_free(pkey);
556                 DST_RET (dst__openssl_toresult(DST_R_OPENSSLFAILURE));
557         }
558         key->keydata.pkey = pkey;
559         ret = ISC_R_SUCCESS;
560
561  err:
562         if (eckey != NULL)
563                 EC_KEY_free(eckey);
564         dst__privstruct_free(&priv, mctx);
565         memset(&priv, 0, sizeof(priv));
566         return (ret);
567 }
568
569 static dst_func_t opensslecdsa_functions = {
570         opensslecdsa_createctx,
571         opensslecdsa_destroyctx,
572         opensslecdsa_adddata,
573         opensslecdsa_sign,
574         opensslecdsa_verify,
575         NULL, /*%< verify2 */
576         NULL, /*%< computesecret */
577         opensslecdsa_compare,
578         NULL, /*%< paramcompare */
579         opensslecdsa_generate,
580         opensslecdsa_isprivate,
581         opensslecdsa_destroy,
582         opensslecdsa_todns,
583         opensslecdsa_fromdns,
584         opensslecdsa_tofile,
585         opensslecdsa_parse,
586         NULL, /*%< cleanup */
587         NULL, /*%< fromlabel */
588         NULL, /*%< dump */
589         NULL, /*%< restore */
590 };
591
592 isc_result_t
593 dst__opensslecdsa_init(dst_func_t **funcp) {
594         REQUIRE(funcp != NULL);
595         if (*funcp == NULL)
596                 *funcp = &opensslecdsa_functions;
597         return (ISC_R_SUCCESS);
598 }
599
600 #else /* HAVE_OPENSSL_ECDSA */
601
602 #include <isc/util.h>
603
604 EMPTY_TRANSLATION_UNIT
605
606 #endif /* HAVE_OPENSSL_ECDSA */
607 /*! \file */