]> git.saurik.com Git - apple/network_cmds.git/blob - eaytest.tproj/crypto_openssl.c
network_cmds-176.tar.gz
[apple/network_cmds.git] / eaytest.tproj / crypto_openssl.c
1 /* $KAME: crypto_openssl.c,v 1.69 2001/09/11 13:25:00 sakane Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/types.h>
33 #include <sys/param.h>
34
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <limits.h>
38 #include <string.h>
39
40 /* get openssl/ssleay version number */
41 #ifdef HAVE_OPENSSL_OPENSSLV_H
42 # include <openssl/opensslv.h>
43 #else
44 # error no opensslv.h found.
45 #endif
46
47 #ifndef OPENSSL_VERSION_NUMBER
48 #error OPENSSL_VERSION_NUMBER is not defined. OpenSSL0.9.4 or later required.
49 #endif
50
51 #ifdef HAVE_OPENSSL_PEM_H
52 #include <openssl/pem.h>
53 #endif
54 #ifdef HAVE_OPENSSL_EVP_H
55 #include <openssl/evp.h>
56 #endif
57 #ifdef HAVE_OPENSSL_X509_H
58 #include <openssl/x509.h>
59 #include <openssl/x509_vfy.h>
60 #endif
61 #include <openssl/bn.h>
62 #include <openssl/dh.h>
63 #include <openssl/md5.h>
64 #include <openssl/sha.h>
65 #include <openssl/hmac.h>
66 #include <openssl/des.h>
67 #include <openssl/crypto.h>
68 #ifdef HAVE_OPENSSL_IDEA_H
69 #include <openssl/idea.h>
70 #endif
71 #include <openssl/blowfish.h>
72 #ifdef HAVE_OPENSSL_RC5_H
73 #include <openssl/rc5.h>
74 #endif
75 #include <openssl/cast.h>
76 #include <openssl/err.h>
77 #ifdef HAVE_OPENSSL_RIJNDAEL_H
78 #include <openssl/rijndael.h>
79 #else
80 #include "rijndael-api-fst.h"
81 #endif
82 #ifdef HAVE_OPENSSL_SHA2_H
83 #include <openssl/sha2.h>
84 #else
85 #include "sha2.h"
86 #endif
87
88 #include "var.h"
89 #include "misc.h"
90 #include "vmbuf.h"
91 #include "plog.h"
92 #include "crypto_openssl.h"
93 #include "debug.h"
94 #include "gcmalloc.h"
95
96 /*
97 * I hate to cast every parameter to des_xx into void *, but it is
98 * necessary for SSLeay/OpenSSL portability. It sucks.
99 */
100
101 #ifdef HAVE_SIGNING_C
102 static int cb_check_cert __P((int, X509_STORE_CTX *));
103 static void eay_setgentype __P((char *, int *));
104 static X509 *mem2x509 __P((vchar_t *));
105 #endif
106
107 static caddr_t eay_hmac_init __P((vchar_t *, const EVP_MD *));
108
109 #ifdef HAVE_SIGNING_C
110 /* X509 Certificate */
111 /*
112 * convert the string of the subject name into DER
113 * e.g. str = "C=JP, ST=Kanagawa";
114 */
115 vchar_t *
116 eay_str2asn1dn(str, len)
117 char *str;
118 int len;
119 {
120 X509_NAME *name;
121 char *buf;
122 char *field, *value;
123 int i, j;
124 vchar_t *ret;
125 caddr_t p;
126
127 buf = racoon_malloc(len + 1);
128 if (!buf) {
129 printf("failed to allocate buffer\n");
130 return NULL;
131 }
132 memcpy(buf, str, len);
133
134 name = X509_NAME_new();
135
136 field = &buf[0];
137 value = NULL;
138 for (i = 0; i < len; i++) {
139 if (!value && buf[i] == '=') {
140 buf[i] = '\0';
141 value = &buf[i + 1];
142 continue;
143 } else if (buf[i] == ',' || buf[i] == '/') {
144 buf[i] = '\0';
145 #if 0
146 printf("[%s][%s]\n", field, value);
147 #endif
148 if (!X509_NAME_add_entry_by_txt(name, field,
149 MBSTRING_ASC, value, -1, -1, 0))
150 goto err;
151 for (j = i + 1; j < len; j++) {
152 if (buf[j] != ' ')
153 break;
154 }
155 field = &buf[j];
156 value = NULL;
157 continue;
158 }
159 }
160 buf[len] = '\0';
161 #if 0
162 printf("[%s][%s]\n", field, value);
163 #endif
164 if (!X509_NAME_add_entry_by_txt(name, field,
165 MBSTRING_ASC, value, -1, -1, 0))
166 goto err;
167
168 i = i2d_X509_NAME(name, NULL);
169 if (!i)
170 goto err;
171 ret = vmalloc(i);
172 if (!ret)
173 goto err;
174 p = ret->v;
175 i = i2d_X509_NAME(name, (unsigned char **)&p);
176 if (!i)
177 goto err;
178
179 return ret;
180
181 err:
182 if (buf)
183 racoon_free(buf);
184 if (name)
185 X509_NAME_free(name);
186 return NULL;
187 }
188
189 /*
190 * compare two subjectNames.
191 * OUT: 0: equal
192 * positive:
193 * -1: other error.
194 */
195 int
196 eay_cmp_asn1dn(n1, n2)
197 vchar_t *n1, *n2;
198 {
199 X509_NAME *a = NULL, *b = NULL;
200 caddr_t p;
201 int i = -1;
202
203 p = n1->v;
204 if (!d2i_X509_NAME(&a, (unsigned char **)&p, n1->l))
205 goto end;
206 p = n2->v;
207 if (!d2i_X509_NAME(&b, (unsigned char **)&p, n2->l))
208 goto end;
209
210 i = X509_NAME_cmp(a, b);
211
212 end:
213 if (a)
214 X509_NAME_free(a);
215 if (b)
216 X509_NAME_free(b);
217 return i;
218 }
219
220 /*
221 * this functions is derived from apps/verify.c in OpenSSL0.9.5
222 */
223 int
224 eay_check_x509cert(cert, CApath)
225 vchar_t *cert;
226 char *CApath;
227 {
228 X509_STORE *cert_ctx = NULL;
229 X509_LOOKUP *lookup = NULL;
230 X509 *x509 = NULL;
231 #if OPENSSL_VERSION_NUMBER >= 0x00905100L
232 X509_STORE_CTX *csc;
233 #else
234 X509_STORE_CTX csc;
235 #endif
236 int error = -1;
237
238 /* XXX define only functions required. */
239 #if OPENSSL_VERSION_NUMBER >= 0x00905100L
240 OpenSSL_add_all_algorithms();
241 #else
242 SSLeay_add_all_algorithms();
243 #endif
244
245 cert_ctx = X509_STORE_new();
246 if (cert_ctx == NULL)
247 goto end;
248 X509_STORE_set_verify_cb_func(cert_ctx, cb_check_cert);
249
250 lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_file());
251 if (lookup == NULL)
252 goto end;
253 X509_LOOKUP_load_file(lookup, NULL, X509_FILETYPE_DEFAULT); /* XXX */
254
255 lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_hash_dir());
256 if (lookup == NULL)
257 goto end;
258 error = X509_LOOKUP_add_dir(lookup, CApath, X509_FILETYPE_PEM);
259 if(!error) {
260 error = -1;
261 goto end;
262 }
263 error = -1; /* initialized */
264
265 /* read the certificate to be verified */
266 x509 = mem2x509(cert);
267 if (x509 == NULL)
268 goto end;
269
270 #if OPENSSL_VERSION_NUMBER >= 0x00905100L
271 csc = X509_STORE_CTX_new();
272 if (csc == NULL)
273 goto end;
274 X509_STORE_CTX_init(csc, cert_ctx, x509, NULL);
275 error = X509_verify_cert(csc);
276 X509_STORE_CTX_cleanup(csc);
277 #else
278 X509_STORE_CTX_init(&csc, cert_ctx, x509, NULL);
279 error = X509_verify_cert(&csc);
280 X509_STORE_CTX_cleanup(&csc);
281 #endif
282
283 /*
284 * if x509_verify_cert() is successful then the value of error is
285 * set non-zero.
286 */
287 error = error ? 0 : -1;
288
289 end:
290 if (error)
291 printf("%s\n", eay_strerror());
292 if (cert_ctx != NULL)
293 X509_STORE_free(cert_ctx);
294 if (x509 != NULL)
295 X509_free(x509);
296
297 return(error);
298 }
299
300 /*
301 * callback function for verifing certificate.
302 * this function is derived from cb() in openssl/apps/s_server.c
303 */
304 static int
305 cb_check_cert(ok, ctx)
306 int ok;
307 X509_STORE_CTX *ctx;
308 {
309 char buf[256];
310 int log_tag;
311
312 if (!ok) {
313 X509_NAME_oneline(
314 X509_get_subject_name(ctx->current_cert),
315 buf,
316 256);
317 /*
318 * since we are just checking the certificates, it is
319 * ok if they are self signed. But we should still warn
320 * the user.
321 */
322 switch (ctx->error) {
323 case X509_V_ERR_CERT_HAS_EXPIRED:
324 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
325 #if OPENSSL_VERSION_NUMBER >= 0x00905100L
326 case X509_V_ERR_INVALID_CA:
327 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
328 case X509_V_ERR_INVALID_PURPOSE:
329 #endif
330 ok = 1;
331 log_tag = LLV_WARNING;
332 break;
333 default:
334 log_tag = LLV_ERROR;
335 }
336 #ifndef EAYDEBUG
337 plog(log_tag, LOCATION, NULL,
338 "%s(%d) at depth:%d SubjectName:%s\n",
339 X509_verify_cert_error_string(ctx->error),
340 ctx->error,
341 ctx->error_depth,
342 buf);
343 #else
344 printf("%d: %s(%d) at depth:%d SubjectName:%s\n",
345 log_tag,
346 X509_verify_cert_error_string(ctx->error),
347 ctx->error,
348 ctx->error_depth,
349 buf);
350 #endif
351 }
352 ERR_clear_error();
353
354 return ok;
355 }
356
357 /*
358 * get a subjectAltName from X509 certificate.
359 */
360 vchar_t *
361 eay_get_x509asn1subjectname(cert)
362 vchar_t *cert;
363 {
364 X509 *x509 = NULL;
365 u_char *bp;
366 vchar_t *name = NULL;
367 int len;
368 int error = -1;
369
370 bp = cert->v;
371
372 x509 = mem2x509(cert);
373 if (x509 == NULL)
374 goto end;
375
376 /* get the length of the name */
377 len = i2d_X509_NAME(x509->cert_info->subject, NULL);
378 name = vmalloc(len);
379 if (!name)
380 goto end;
381 /* get the name */
382 bp = name->v;
383 len = i2d_X509_NAME(x509->cert_info->subject, &bp);
384
385 error = 0;
386
387 end:
388 if (error) {
389 #ifndef EAYDEBUG
390 plog(LLV_ERROR, LOCATION, NULL, "%s\n", eay_strerror());
391 #else
392 printf("%s\n", eay_strerror());
393 #endif
394 if (name) {
395 vfree(name);
396 name = NULL;
397 }
398 }
399 if (x509)
400 X509_free(x509);
401
402 return name;
403 }
404
405 /*
406 * get the subjectAltName from X509 certificate.
407 * the name is terminated by '\0'.
408 */
409 #include <openssl/x509v3.h>
410 int
411 eay_get_x509subjectaltname(cert, altname, type, pos)
412 vchar_t *cert;
413 char **altname;
414 int *type;
415 int pos;
416 {
417 X509 *x509 = NULL;
418 X509_EXTENSION *ext;
419 X509V3_EXT_METHOD *method = NULL;
420 STACK_OF(GENERAL_NAME) *name;
421 CONF_VALUE *cval = NULL;
422 STACK_OF(CONF_VALUE) *nval = NULL;
423 u_char *bp;
424 int i, len;
425 int error = -1;
426
427 *altname = NULL;
428 *type = GENT_OTHERNAME;
429
430 bp = cert->v;
431
432 x509 = mem2x509(cert);
433 if (x509 == NULL)
434 goto end;
435
436 i = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1);
437 if (i < 0)
438 goto end;
439 ext = X509_get_ext(x509, i);
440 method = X509V3_EXT_get(ext);
441 if(!method)
442 goto end;
443
444 bp = ext->value->data;
445 name = method->d2i(NULL, &bp, ext->value->length);
446 if(!name)
447 goto end;
448
449 nval = method->i2v(method, name, NULL);
450 method->ext_free(name);
451 name = NULL;
452 if(!nval)
453 goto end;
454
455 for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
456 /* skip the name */
457 if (i + 1 != pos)
458 continue;
459 cval = sk_CONF_VALUE_value(nval, i);
460 len = strlen(cval->value) + 1; /* '\0' included */
461 *altname = racoon_malloc(len);
462 if (!*altname) {
463 sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
464 goto end;
465 }
466 strcpy(*altname, cval->value);
467
468 /* set type of the name */
469 eay_setgentype(cval->name, type);
470 }
471
472 sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
473
474 error = 0;
475
476 end:
477 if (error) {
478 if (*altname) {
479 racoon_free(*altname);
480 *altname = NULL;
481 }
482 #ifndef EAYDEBUG
483 plog(LLV_ERROR, LOCATION, NULL, "%s\n", eay_strerror());
484 #else
485 printf("%s\n", eay_strerror());
486 #endif
487 }
488 if (x509)
489 X509_free(x509);
490
491 return error;
492 }
493
494 static void
495 eay_setgentype(name, type)
496 char *name;
497 int *type;
498 {
499 /* XXX It's needed effective code */
500 if(!memcmp(name, "email", strlen("email"))) {
501 *type = GENT_EMAIL;
502 } else if(!memcmp(name, "URI", strlen("URI"))) {
503 *type = GENT_URI;
504 } else if(!memcmp(name, "DNS", strlen("DNS"))) {
505 *type = GENT_DNS;
506 } else if(!memcmp(name, "RID", strlen("RID"))) {
507 *type = GENT_RID;
508 } else if(!memcmp(name, "IP", strlen("IP"))) {
509 *type = GENT_IPADD;
510 } else {
511 *type = GENT_OTHERNAME;
512 }
513 }
514
515 /*
516 * decode a X509 certificate and make a readable text terminated '\n'.
517 * return the buffer allocated, so must free it later.
518 */
519 char *
520 eay_get_x509text(cert)
521 vchar_t *cert;
522 {
523 X509 *x509 = NULL;
524 BIO *bio = NULL;
525 char *text = NULL;
526 u_char *bp = NULL;
527 int len = 0;
528 int error = -1;
529
530 x509 = mem2x509(cert);
531 if (x509 == NULL)
532 goto end;
533
534 bio = BIO_new(BIO_s_mem());
535 if (bio == NULL)
536 goto end;
537
538 error = X509_print(bio, x509);
539 if (error != 1) {
540 error = -1;
541 goto end;
542 }
543
544 len = BIO_get_mem_data(bio, &bp);
545 text = racoon_malloc(len + 1);
546 if (text == NULL)
547 goto end;
548 memcpy(text, bp, len);
549 text[len] = '\0';
550
551 error = 0;
552
553 end:
554 if (error) {
555 if (text) {
556 racoon_free(text);
557 text = NULL;
558 }
559 #ifndef EAYDEBUG
560 plog(LLV_ERROR, LOCATION, NULL, "%s\n", eay_strerror());
561 #else
562 printf("%s\n", eay_strerror());
563 #endif
564 }
565 if (bio)
566 BIO_free(bio);
567 if (x509)
568 X509_free(x509);
569
570 return text;
571 }
572
573 /* get X509 structure from buffer. */
574 static X509 *
575 mem2x509(cert)
576 vchar_t *cert;
577 {
578 X509 *x509;
579
580 #ifndef EAYDEBUG
581 {
582 u_char *bp;
583
584 bp = cert->v;
585
586 x509 = d2i_X509(NULL, &bp, cert->l);
587 }
588 #else
589 {
590 BIO *bio;
591 int len;
592
593 bio = BIO_new(BIO_s_mem());
594 if (bio == NULL)
595 return NULL;
596 len = BIO_write(bio, cert->v, cert->l);
597 if (len == -1)
598 return NULL;
599 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
600 BIO_free(bio);
601 }
602 #endif
603 return x509;
604 }
605
606 /*
607 * get a X509 certificate from local file.
608 * a certificate must be PEM format.
609 * Input:
610 * path to a certificate.
611 * Output:
612 * NULL if error occured
613 * other is the cert.
614 */
615 vchar_t *
616 eay_get_x509cert(path)
617 char *path;
618 {
619 FILE *fp;
620 X509 *x509;
621 vchar_t *cert;
622 u_char *bp;
623 int len;
624 int error;
625
626 /* Read private key */
627 fp = fopen(path, "r");
628 if (fp == NULL)
629 return NULL;
630 #if OPENSSL_VERSION_NUMBER >= 0x00904100L
631 x509 = PEM_read_X509(fp, NULL, NULL, NULL);
632 #else
633 x509 = PEM_read_X509(fp, NULL, NULL);
634 #endif
635 fclose (fp);
636
637 if (x509 == NULL)
638 return NULL;
639
640 len = i2d_X509(x509, NULL);
641 cert = vmalloc(len);
642 if (cert == NULL) {
643 X509_free(x509);
644 return NULL;
645 }
646 bp = cert->v;
647 error = i2d_X509(x509, &bp);
648 X509_free(x509);
649
650 if (error == 0)
651 return NULL;
652
653 return cert;
654 }
655
656 /*
657 * sign a souce by X509 signature.
658 * XXX: to be get hash type from my cert ?
659 * to be handled EVP_dss().
660 */
661 vchar_t *
662 eay_get_x509sign(source, privkey, cert)
663 vchar_t *source;
664 vchar_t *privkey;
665 vchar_t *cert;
666 {
667 vchar_t *sig = NULL;
668
669 sig = eay_rsa_sign(source, privkey);
670
671 return sig;
672 }
673
674 /*
675 * check a X509 signature
676 * XXX: to be get hash type from my cert ?
677 * to be handled EVP_dss().
678 * OUT: return -1 when error.
679 * 0
680 */
681 int
682 eay_check_x509sign(source, sig, cert)
683 vchar_t *source;
684 vchar_t *sig;
685 vchar_t *cert;
686 {
687 X509 *x509;
688 u_char *bp;
689 vchar_t pubkey;
690
691 bp = cert->v;
692
693 x509 = d2i_X509(NULL, &bp, cert->l);
694 if (x509 == NULL) {
695 #ifndef EAYDEBUG
696 plog(LLV_ERROR, LOCATION, NULL, "%s\n", eay_strerror());
697 #endif
698 return -1;
699 }
700
701 pubkey.v = x509->cert_info->key->public_key->data;
702 pubkey.l = x509->cert_info->key->public_key->length;
703
704 return eay_rsa_verify(source, sig, &pubkey);
705 }
706
707 /*
708 * check a signature by signed with PKCS7 certificate.
709 * XXX: to be get hash type from my cert ?
710 * to be handled EVP_dss().
711 * OUT: return -1 when error.
712 * 0
713 */
714 int
715 eay_check_pkcs7sign(source, sig, cert)
716 vchar_t *source;
717 vchar_t *sig;
718 vchar_t *cert;
719 {
720 X509 *x509;
721 EVP_MD_CTX md_ctx;
722 EVP_PKEY *evp;
723 int error;
724 BIO *bio = BIO_new(BIO_s_mem());
725 char *bp;
726
727 if (bio == NULL)
728 return -1;
729 error = BIO_write(bio, cert->v, cert->l);
730 if (error != cert->l)
731 return -1;
732
733 bp = cert->v;
734 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
735 BIO_free(bio);
736 if (x509 == NULL)
737 return -1;
738
739 evp = X509_get_pubkey(x509);
740 X509_free(x509);
741 if (evp == NULL)
742 return -1;
743
744 /* Verify the signature */
745 /* XXX: to be handled EVP_dss() */
746 EVP_VerifyInit(&md_ctx, EVP_sha1());
747 EVP_VerifyUpdate(&md_ctx, source->v, source->l);
748 error = EVP_VerifyFinal(&md_ctx, sig->v, sig->l, evp);
749
750 EVP_PKEY_free(evp);
751
752 if (error != 1)
753 return -1;
754
755 return 0;
756 }
757
758 /*
759 * get PKCS#1 Private Key of PEM format from local file.
760 */
761 vchar_t *
762 eay_get_pkcs1privkey(path)
763 char *path;
764 {
765 FILE *fp;
766 EVP_PKEY *evp = NULL;
767 vchar_t *pkey = NULL;
768 u_char *bp;
769 int pkeylen;
770 int error = -1;
771
772 /* Read private key */
773 fp = fopen(path, "r");
774 if (fp == NULL)
775 return NULL;
776
777 #if OPENSSL_VERSION_NUMBER >= 0x00904100L
778 evp = PEM_read_PrivateKey(fp, NULL, NULL, NULL);
779 #else
780 evp = PEM_read_PrivateKey(fp, NULL, NULL);
781 #endif
782 fclose (fp);
783
784 if (evp == NULL)
785 return NULL;
786
787 pkeylen = i2d_PrivateKey(evp, NULL);
788 if (pkeylen == 0)
789 goto end;
790 pkey = vmalloc(pkeylen);
791 if (pkey == NULL)
792 goto end;
793 bp = pkey->v;
794 pkeylen = i2d_PrivateKey(evp, &bp);
795 if (pkeylen == 0)
796 goto end;
797
798 error = 0;
799
800 end:
801 if (evp != NULL)
802 EVP_PKEY_free(evp);
803 if (error != 0 && pkey != NULL) {
804 vfree(pkey);
805 pkey = NULL;
806 }
807
808 return pkey;
809 }
810
811 /*
812 * get PKCS#1 Public Key of PEM format from local file.
813 */
814 vchar_t *
815 eay_get_pkcs1pubkey(path)
816 char *path;
817 {
818 FILE *fp;
819 EVP_PKEY *evp = NULL;
820 vchar_t *pkey = NULL;
821 X509 *x509 = NULL;
822 u_char *bp;
823 int pkeylen;
824 int error = -1;
825
826 /* Read private key */
827 fp = fopen(path, "r");
828 if (fp == NULL)
829 return NULL;
830
831 #if OPENSSL_VERSION_NUMBER >= 0x00904100L
832 x509 = PEM_read_X509(fp, NULL, NULL, NULL);
833 #else
834 x509 = PEM_read_X509(fp, NULL, NULL);
835 #endif
836 fclose (fp);
837
838 if (x509 == NULL)
839 return NULL;
840
841 /* Get public key - eay */
842 evp = X509_get_pubkey(x509);
843 if (evp == NULL)
844 return NULL;
845
846 pkeylen = i2d_PublicKey(evp, NULL);
847 if (pkeylen == 0)
848 goto end;
849 pkey = vmalloc(pkeylen);
850 if (pkey == NULL)
851 goto end;
852 bp = pkey->v;
853 pkeylen = i2d_PublicKey(evp, &bp);
854 if (pkeylen == 0)
855 goto end;
856
857 error = 0;
858 end:
859 if (evp != NULL)
860 EVP_PKEY_free(evp);
861 if (error != 0 && pkey != NULL) {
862 vfree(pkey);
863 pkey = NULL;
864 }
865
866 return pkey;
867 }
868 #endif
869
870 vchar_t *
871 eay_rsa_sign(src, privkey)
872 vchar_t *src, *privkey;
873 {
874 EVP_PKEY *evp;
875 u_char *bp = privkey->v;
876 vchar_t *sig = NULL;
877 int len;
878 int pad = RSA_PKCS1_PADDING;
879
880 /* XXX to be handled EVP_PKEY_DSA */
881 evp = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &bp, privkey->l);
882 if (evp == NULL)
883 return NULL;
884
885 /* XXX: to be handled EVP_dss() */
886 /* XXX: Where can I get such parameters ? From my cert ? */
887
888 len = RSA_size(evp->pkey.rsa);
889
890 sig = vmalloc(len);
891 if (sig == NULL)
892 return NULL;
893
894 len = RSA_private_encrypt(src->l, src->v, sig->v, evp->pkey.rsa, pad);
895 EVP_PKEY_free(evp);
896 if (len == 0 || len != sig->l) {
897 vfree(sig);
898 sig = NULL;
899 }
900
901 return sig;
902 }
903
904 int
905 eay_rsa_verify(src, sig, pubkey)
906 vchar_t *src, *sig, *pubkey;
907 {
908 EVP_PKEY *evp;
909 u_char *bp = pubkey->v;
910 vchar_t *xbuf = NULL;
911 int pad = RSA_PKCS1_PADDING;
912 int len = 0;
913 int error;
914
915 evp = d2i_PUBKEY(NULL, &bp, pubkey->l);
916 if (evp == NULL)
917 #ifndef EAYDEBUG
918 return NULL;
919 #endif
920
921 len = RSA_size(evp->pkey.rsa);
922
923 xbuf = vmalloc(len);
924 if (xbuf == NULL) {
925 #ifndef EAYDEBUG
926 plog(LLV_ERROR, LOCATION, NULL, "%s\n", eay_strerror());
927 #endif
928 EVP_PKEY_free(evp);
929 return -1;
930 }
931
932 len = RSA_public_decrypt(sig->l, sig->v, xbuf->v, evp->pkey.rsa, pad);
933 #ifndef EAYDEBUG
934 if (len == 0 || len != src->l)
935 plog(LLV_ERROR, LOCATION, NULL, "%s\n", eay_strerror());
936 #endif
937 EVP_PKEY_free(evp);
938 if (len == 0 || len != src->l) {
939 vfree(xbuf);
940 return -1;
941 }
942
943 error = memcmp(src->v, xbuf->v, src->l);
944 vfree(xbuf);
945 if (error != 0)
946 return -1;
947
948 return 0;
949 }
950
951 /*
952 * get error string
953 * MUST load ERR_load_crypto_strings() first.
954 */
955 char *
956 eay_strerror()
957 {
958 static char ebuf[512];
959 int len = 0, n;
960 unsigned long l;
961 char buf[200];
962 #if OPENSSL_VERSION_NUMBER >= 0x00904100L
963 const char *file, *data;
964 #else
965 char *file, *data;
966 #endif
967 int line, flags;
968 unsigned long es;
969
970 es = CRYPTO_thread_id();
971
972 while ((l = ERR_get_error_line_data(&file, &line, &data, &flags)) != 0){
973 n = snprintf(ebuf + len, sizeof(ebuf) - len,
974 "%lu:%s:%s:%d:%s ",
975 es, ERR_error_string(l, buf), file, line,
976 (flags & ERR_TXT_STRING) ? data : "");
977 if (n < 0 || n >= sizeof(ebuf) - len)
978 break;
979 len += n;
980 if (sizeof(ebuf) < len)
981 break;
982 }
983
984 return ebuf;
985 }
986
987 void
988 eay_init_error()
989 {
990 ERR_load_crypto_strings();
991 }
992
993 /*
994 * DES-CBC
995 */
996 vchar_t *
997 eay_des_encrypt(data, key, iv)
998 vchar_t *data, *key, *iv;
999 {
1000 vchar_t *res;
1001 des_key_schedule ks;
1002
1003 if (des_key_sched((void *)key->v, ks) != 0)
1004 return NULL;
1005
1006 /* allocate buffer for result */
1007 if ((res = vmalloc(data->l)) == NULL)
1008 return NULL;
1009
1010 /* decryption data */
1011 des_cbc_encrypt((void *)data->v, (void *)res->v, data->l,
1012 ks, (void *)iv->v, DES_ENCRYPT);
1013
1014 return res;
1015 }
1016
1017 vchar_t *
1018 eay_des_decrypt(data, key, iv)
1019 vchar_t *data, *key, *iv;
1020 {
1021 vchar_t *res;
1022 des_key_schedule ks;
1023
1024 if (des_key_sched((void *)key->v, ks) != 0)
1025 return NULL;
1026
1027 /* allocate buffer for result */
1028 if ((res = vmalloc(data->l)) == NULL)
1029 return NULL;
1030
1031 /* decryption data */
1032 des_cbc_encrypt((void *)data->v, (void *)res->v, data->l,
1033 ks, (void *)iv->v, DES_DECRYPT);
1034
1035 return res;
1036 }
1037
1038 int
1039 eay_des_weakkey(key)
1040 vchar_t *key;
1041 {
1042 return des_is_weak_key((void *)key->v);
1043 }
1044
1045 int
1046 eay_des_keylen(len)
1047 int len;
1048 {
1049 if (len != 0 && len != 64)
1050 return -1;
1051 return 64;
1052 }
1053
1054 #ifdef HAVE_OPENSSL_IDEA_H
1055 /*
1056 * IDEA-CBC
1057 */
1058 vchar_t *
1059 eay_idea_encrypt(data, key, iv)
1060 vchar_t *data, *key, *iv;
1061 {
1062 vchar_t *res;
1063 IDEA_KEY_SCHEDULE ks;
1064
1065 idea_set_encrypt_key(key->v, &ks);
1066
1067 /* allocate buffer for result */
1068 if ((res = vmalloc(data->l)) == NULL)
1069 return NULL;
1070
1071 /* decryption data */
1072 idea_cbc_encrypt(data->v, res->v, data->l,
1073 &ks, iv->v, IDEA_ENCRYPT);
1074
1075 return res;
1076 }
1077
1078 vchar_t *
1079 eay_idea_decrypt(data, key, iv)
1080 vchar_t *data, *key, *iv;
1081 {
1082 vchar_t *res;
1083 IDEA_KEY_SCHEDULE ks, dks;
1084
1085 idea_set_encrypt_key(key->v, &ks);
1086 idea_set_decrypt_key(&ks, &dks);
1087
1088 /* allocate buffer for result */
1089 if ((res = vmalloc(data->l)) == NULL)
1090 return NULL;
1091
1092 /* decryption data */
1093 idea_cbc_encrypt(data->v, res->v, data->l,
1094 &dks, iv->v, IDEA_DECRYPT);
1095
1096 return res;
1097 }
1098
1099 int
1100 eay_idea_weakkey(key)
1101 vchar_t *key;
1102 {
1103 return 0; /* XXX */
1104 }
1105
1106 int
1107 eay_idea_keylen(len)
1108 int len;
1109 {
1110 if (len != 0 && len != 128)
1111 return -1;
1112 return 128;
1113 }
1114 #endif
1115
1116 /*
1117 * BLOWFISH-CBC
1118 */
1119 vchar_t *
1120 eay_bf_encrypt(data, key, iv)
1121 vchar_t *data, *key, *iv;
1122 {
1123 vchar_t *res;
1124 BF_KEY ks;
1125
1126 BF_set_key(&ks, key->l, key->v);
1127
1128 /* allocate buffer for result */
1129 if ((res = vmalloc(data->l)) == NULL)
1130 return NULL;
1131
1132 /* decryption data */
1133 BF_cbc_encrypt(data->v, res->v, data->l,
1134 &ks, iv->v, BF_ENCRYPT);
1135
1136 return res;
1137 }
1138
1139 vchar_t *
1140 eay_bf_decrypt(data, key, iv)
1141 vchar_t *data, *key, *iv;
1142 {
1143 vchar_t *res;
1144 BF_KEY ks;
1145
1146 BF_set_key(&ks, key->l, key->v);
1147
1148 /* allocate buffer for result */
1149 if ((res = vmalloc(data->l)) == NULL)
1150 return NULL;
1151
1152 /* decryption data */
1153 BF_cbc_encrypt(data->v, res->v, data->l,
1154 &ks, iv->v, BF_DECRYPT);
1155
1156 return res;
1157 }
1158
1159 int
1160 eay_bf_weakkey(key)
1161 vchar_t *key;
1162 {
1163 return 0; /* XXX to be done. refer to RFC 2451 */
1164 }
1165
1166 int
1167 eay_bf_keylen(len)
1168 int len;
1169 {
1170 if (len == 0)
1171 return 448;
1172 if (len < 40 || len > 448)
1173 return -1;
1174 return len + 7 / 8;
1175 }
1176
1177 #ifdef HAVE_OPENSSL_RC5_H
1178 /*
1179 * RC5-CBC
1180 */
1181 vchar_t *
1182 eay_rc5_encrypt(data, key, iv)
1183 vchar_t *data, *key, *iv;
1184 {
1185 vchar_t *res;
1186 RC5_32_KEY ks;
1187
1188 /* in RFC 2451, there is information about the number of round. */
1189 RC5_32_set_key(&ks, key->l, key->v, 16);
1190
1191 /* allocate buffer for result */
1192 if ((res = vmalloc(data->l)) == NULL)
1193 return NULL;
1194
1195 /* decryption data */
1196 RC5_32_cbc_encrypt(data->v, res->v, data->l,
1197 &ks, iv->v, RC5_ENCRYPT);
1198
1199 return res;
1200 }
1201
1202 vchar_t *
1203 eay_rc5_decrypt(data, key, iv)
1204 vchar_t *data, *key, *iv;
1205 {
1206 vchar_t *res;
1207 RC5_32_KEY ks;
1208
1209 /* in RFC 2451, there is information about the number of round. */
1210 RC5_32_set_key(&ks, key->l, key->v, 16);
1211
1212 /* allocate buffer for result */
1213 if ((res = vmalloc(data->l)) == NULL)
1214 return NULL;
1215
1216 /* decryption data */
1217 RC5_32_cbc_encrypt(data->v, res->v, data->l,
1218 &ks, iv->v, RC5_DECRYPT);
1219
1220 return res;
1221 }
1222
1223 int
1224 eay_rc5_weakkey(key)
1225 vchar_t *key;
1226 {
1227 return 0; /* No known weak keys when used with 16 rounds. */
1228
1229 }
1230
1231 int
1232 eay_rc5_keylen(len)
1233 int len;
1234 {
1235 if (len == 0)
1236 return 128;
1237 if (len < 40 || len > 2040)
1238 return -1;
1239 return len + 7 / 8;
1240 }
1241 #endif
1242
1243 /*
1244 * 3DES-CBC
1245 */
1246 vchar_t *
1247 eay_3des_encrypt(data, key, iv)
1248 vchar_t *data, *key, *iv;
1249 {
1250 vchar_t *res;
1251 des_key_schedule ks1, ks2, ks3;
1252
1253 if (key->l < 24)
1254 return NULL;
1255
1256 if (des_key_sched((void *)key->v, ks1) != 0)
1257 return NULL;
1258 if (des_key_sched((void *)(key->v + 8), ks2) != 0)
1259 return NULL;
1260 if (des_key_sched((void *)(key->v + 16), ks3) != 0)
1261 return NULL;
1262
1263 /* allocate buffer for result */
1264 if ((res = vmalloc(data->l)) == NULL)
1265 return NULL;
1266
1267 /* decryption data */
1268 des_ede3_cbc_encrypt((void *)data->v, (void *)res->v, data->l,
1269 ks1, ks2, ks3, (void *)iv->v, DES_ENCRYPT);
1270
1271 return res;
1272 }
1273
1274 vchar_t *
1275 eay_3des_decrypt(data, key, iv)
1276 vchar_t *data, *key, *iv;
1277 {
1278 vchar_t *res;
1279 des_key_schedule ks1, ks2, ks3;
1280
1281 if (key->l < 24)
1282 return NULL;
1283
1284 if (des_key_sched((void *)key->v, ks1) != 0)
1285 return NULL;
1286 if (des_key_sched((void *)(key->v + 8), ks2) != 0)
1287 return NULL;
1288 if (des_key_sched((void *)(key->v + 16), ks3) != 0)
1289 return NULL;
1290
1291 /* allocate buffer for result */
1292 if ((res = vmalloc(data->l)) == NULL)
1293 return NULL;
1294
1295 /* decryption data */
1296 des_ede3_cbc_encrypt((void *)data->v, (void *)res->v, data->l,
1297 ks1, ks2, ks3, (void *)iv->v, DES_DECRYPT);
1298
1299 return res;
1300 }
1301
1302 int
1303 eay_3des_weakkey(key)
1304 vchar_t *key;
1305 {
1306 if (key->l < 24)
1307 return NULL;
1308
1309 return (des_is_weak_key((void *)key->v)
1310 || des_is_weak_key((void *)(key->v + 8))
1311 || des_is_weak_key((void *)(key->v + 16)));
1312 }
1313
1314 int
1315 eay_3des_keylen(len)
1316 int len;
1317 {
1318 if (len != 0 && len != 192)
1319 return -1;
1320 return 192;
1321 }
1322
1323 /*
1324 * CAST-CBC
1325 */
1326 vchar_t *
1327 eay_cast_encrypt(data, key, iv)
1328 vchar_t *data, *key, *iv;
1329 {
1330 vchar_t *res;
1331 CAST_KEY ks;
1332
1333 CAST_set_key(&ks, key->l, key->v);
1334
1335 /* allocate buffer for result */
1336 if ((res = vmalloc(data->l)) == NULL)
1337 return NULL;
1338
1339 /* decryption data */
1340 CAST_cbc_encrypt(data->v, res->v, data->l,
1341 &ks, iv->v, DES_ENCRYPT);
1342
1343 return res;
1344 }
1345
1346 vchar_t *
1347 eay_cast_decrypt(data, key, iv)
1348 vchar_t *data, *key, *iv;
1349 {
1350 vchar_t *res;
1351 CAST_KEY ks;
1352
1353 CAST_set_key(&ks, key->l, key->v);
1354
1355 /* allocate buffer for result */
1356 if ((res = vmalloc(data->l)) == NULL)
1357 return NULL;
1358
1359 /* decryption data */
1360 CAST_cbc_encrypt(data->v, res->v, data->l,
1361 &ks, iv->v, DES_DECRYPT);
1362
1363 return res;
1364 }
1365
1366 int
1367 eay_cast_weakkey(key)
1368 vchar_t *key;
1369 {
1370 return 0; /* No known weak keys. */
1371 }
1372
1373 int
1374 eay_cast_keylen(len)
1375 int len;
1376 {
1377 if (len == 0)
1378 return 128;
1379 if (len < 40 || len > 128)
1380 return -1;
1381 return len + 7 / 8;
1382 }
1383
1384 /*
1385 * AES(RIJNDAEL)-CBC
1386 */
1387 vchar_t *
1388 eay_aes_encrypt(data, key, iv)
1389 vchar_t *data, *key, *iv;
1390 {
1391 vchar_t *res;
1392 keyInstance k;
1393 cipherInstance c;
1394
1395 memset(&k, 0, sizeof(k));
1396 if (rijndael_makeKey(&k, DIR_ENCRYPT, key->l << 3, key->v) < 0)
1397 return NULL;
1398
1399 /* allocate buffer for result */
1400 if ((res = vmalloc(data->l)) == NULL)
1401 return NULL;
1402
1403 /* encryption data */
1404 memset(&c, 0, sizeof(c));
1405 if (rijndael_cipherInit(&c, MODE_CBC, iv->v) < 0)
1406 return NULL;
1407 if (rijndael_blockEncrypt(&c, &k, data->v, data->l << 3, res->v) < 0)
1408 return NULL;
1409
1410 return res;
1411 }
1412
1413 vchar_t *
1414 eay_aes_decrypt(data, key, iv)
1415 vchar_t *data, *key, *iv;
1416 {
1417 vchar_t *res;
1418 keyInstance k;
1419 cipherInstance c;
1420
1421 memset(&k, 0, sizeof(k));
1422 if (rijndael_makeKey(&k, DIR_DECRYPT, key->l << 3, key->v) < 0)
1423 return NULL;
1424
1425 /* allocate buffer for result */
1426 if ((res = vmalloc(data->l)) == NULL)
1427 return NULL;
1428
1429 /* decryption data */
1430 memset(&c, 0, sizeof(c));
1431 if (rijndael_cipherInit(&c, MODE_CBC, iv->v) < 0)
1432 return NULL;
1433 if (rijndael_blockDecrypt(&c, &k, data->v, data->l << 3, res->v) < 0)
1434 return NULL;
1435
1436 return res;
1437 }
1438
1439 int
1440 eay_aes_weakkey(key)
1441 vchar_t *key;
1442 {
1443 return 0;
1444 }
1445
1446 int
1447 eay_aes_keylen(len)
1448 int len;
1449 {
1450 if (len == 0)
1451 return 128;
1452 if (len != 128 && len != 192 && len != 256)
1453 return -1;
1454 return len;
1455 }
1456
1457 /* for ipsec part */
1458 int
1459 eay_null_hashlen()
1460 {
1461 return 0;
1462 }
1463
1464 int
1465 eay_kpdk_hashlen()
1466 {
1467 return 0;
1468 }
1469
1470 int
1471 eay_twofish_keylen(len)
1472 int len;
1473 {
1474 if (len < 0 || len > 256)
1475 return -1;
1476 return len;
1477 }
1478
1479 /*
1480 * HMAC functions
1481 */
1482 static caddr_t
1483 eay_hmac_init(key, md)
1484 vchar_t *key;
1485 const EVP_MD *md;
1486 {
1487 HMAC_CTX *c = racoon_malloc(sizeof(*c));
1488
1489 HMAC_Init(c, key->v, key->l, md);
1490
1491 return (caddr_t)c;
1492 }
1493
1494 /*
1495 * HMAC SHA2-512
1496 */
1497 vchar_t *
1498 eay_hmacsha2_512_one(key, data)
1499 vchar_t *key, *data;
1500 {
1501 vchar_t *res;
1502 caddr_t ctx;
1503
1504 ctx = eay_hmacsha2_512_init(key);
1505 eay_hmacsha2_512_update(ctx, data);
1506 res = eay_hmacsha2_512_final(ctx);
1507
1508 return(res);
1509 }
1510
1511 caddr_t
1512 eay_hmacsha2_512_init(key)
1513 vchar_t *key;
1514 {
1515 return eay_hmac_init(key, EVP_sha2_512());
1516 }
1517
1518 void
1519 eay_hmacsha2_512_update(c, data)
1520 caddr_t c;
1521 vchar_t *data;
1522 {
1523 HMAC_Update((HMAC_CTX *)c, data->v, data->l);
1524 }
1525
1526 vchar_t *
1527 eay_hmacsha2_512_final(c)
1528 caddr_t c;
1529 {
1530 vchar_t *res;
1531 unsigned int l;
1532
1533 if ((res = vmalloc(SHA512_DIGEST_LENGTH)) == 0)
1534 return NULL;
1535
1536 HMAC_Final((HMAC_CTX *)c, res->v, &l);
1537 res->l = l;
1538 (void)racoon_free(c);
1539
1540 if (SHA512_DIGEST_LENGTH != res->l) {
1541 #ifndef EAYDEBUG
1542 plog(LLV_ERROR, LOCATION, NULL,
1543 "hmac sha2_512 length mismatch %d.\n", res->l);
1544 #else
1545 printf("hmac sha2_512 length mismatch %d.\n", res->l);
1546 #endif
1547 vfree(res);
1548 return NULL;
1549 }
1550
1551 return(res);
1552 }
1553
1554 /*
1555 * HMAC SHA2-384
1556 */
1557 vchar_t *
1558 eay_hmacsha2_384_one(key, data)
1559 vchar_t *key, *data;
1560 {
1561 vchar_t *res;
1562 caddr_t ctx;
1563
1564 ctx = eay_hmacsha2_384_init(key);
1565 eay_hmacsha2_384_update(ctx, data);
1566 res = eay_hmacsha2_384_final(ctx);
1567
1568 return(res);
1569 }
1570
1571 caddr_t
1572 eay_hmacsha2_384_init(key)
1573 vchar_t *key;
1574 {
1575 return eay_hmac_init(key, EVP_sha2_384());
1576 }
1577
1578 void
1579 eay_hmacsha2_384_update(c, data)
1580 caddr_t c;
1581 vchar_t *data;
1582 {
1583 HMAC_Update((HMAC_CTX *)c, data->v, data->l);
1584 }
1585
1586 vchar_t *
1587 eay_hmacsha2_384_final(c)
1588 caddr_t c;
1589 {
1590 vchar_t *res;
1591 unsigned int l;
1592
1593 if ((res = vmalloc(SHA384_DIGEST_LENGTH)) == 0)
1594 return NULL;
1595
1596 HMAC_Final((HMAC_CTX *)c, res->v, &l);
1597 res->l = l;
1598 (void)racoon_free(c);
1599
1600 if (SHA384_DIGEST_LENGTH != res->l) {
1601 #ifndef EAYDEBUG
1602 plog(LLV_ERROR, LOCATION, NULL,
1603 "hmac sha2_384 length mismatch %d.\n", res->l);
1604 #else
1605 printf("hmac sha2_384 length mismatch %d.\n", res->l);
1606 #endif
1607 vfree(res);
1608 return NULL;
1609 }
1610
1611 return(res);
1612 }
1613
1614 /*
1615 * HMAC SHA2-256
1616 */
1617 vchar_t *
1618 eay_hmacsha2_256_one(key, data)
1619 vchar_t *key, *data;
1620 {
1621 vchar_t *res;
1622 caddr_t ctx;
1623
1624 ctx = eay_hmacsha2_256_init(key);
1625 eay_hmacsha2_256_update(ctx, data);
1626 res = eay_hmacsha2_256_final(ctx);
1627
1628 return(res);
1629 }
1630
1631 caddr_t
1632 eay_hmacsha2_256_init(key)
1633 vchar_t *key;
1634 {
1635 return eay_hmac_init(key, EVP_sha2_256());
1636 }
1637
1638 void
1639 eay_hmacsha2_256_update(c, data)
1640 caddr_t c;
1641 vchar_t *data;
1642 {
1643 HMAC_Update((HMAC_CTX *)c, data->v, data->l);
1644 }
1645
1646 vchar_t *
1647 eay_hmacsha2_256_final(c)
1648 caddr_t c;
1649 {
1650 vchar_t *res;
1651 unsigned int l;
1652
1653 if ((res = vmalloc(SHA256_DIGEST_LENGTH)) == 0)
1654 return NULL;
1655
1656 HMAC_Final((HMAC_CTX *)c, res->v, &l);
1657 res->l = l;
1658 (void)racoon_free(c);
1659
1660 if (SHA256_DIGEST_LENGTH != res->l) {
1661 #ifndef EAYDEBUG
1662 plog(LLV_ERROR, LOCATION, NULL,
1663 "hmac sha2_256 length mismatch %d.\n", res->l);
1664 #else
1665 printf("hmac sha2_256 length mismatch %d.\n", res->l);
1666 #endif
1667 vfree(res);
1668 return NULL;
1669 }
1670
1671 return(res);
1672 }
1673
1674 /*
1675 * HMAC SHA1
1676 */
1677 vchar_t *
1678 eay_hmacsha1_one(key, data)
1679 vchar_t *key, *data;
1680 {
1681 vchar_t *res;
1682 caddr_t ctx;
1683
1684 ctx = eay_hmacsha1_init(key);
1685 eay_hmacsha1_update(ctx, data);
1686 res = eay_hmacsha1_final(ctx);
1687
1688 return(res);
1689 }
1690
1691 caddr_t
1692 eay_hmacsha1_init(key)
1693 vchar_t *key;
1694 {
1695 return eay_hmac_init(key, EVP_sha1());
1696 }
1697
1698 void
1699 eay_hmacsha1_update(c, data)
1700 caddr_t c;
1701 vchar_t *data;
1702 {
1703 HMAC_Update((HMAC_CTX *)c, data->v, data->l);
1704 }
1705
1706 vchar_t *
1707 eay_hmacsha1_final(c)
1708 caddr_t c;
1709 {
1710 vchar_t *res;
1711 unsigned int l;
1712
1713 if ((res = vmalloc(SHA_DIGEST_LENGTH)) == 0)
1714 return NULL;
1715
1716 HMAC_Final((HMAC_CTX *)c, res->v, &l);
1717 res->l = l;
1718 (void)racoon_free(c);
1719
1720 if (SHA_DIGEST_LENGTH != res->l) {
1721 #ifndef EAYDEBUG
1722 plog(LLV_ERROR, LOCATION, NULL,
1723 "hmac sha1 length mismatch %d.\n", res->l);
1724 #else
1725 printf("hmac sha1 length mismatch %d.\n", res->l);
1726 #endif
1727 vfree(res);
1728 return NULL;
1729 }
1730
1731 return(res);
1732 }
1733
1734 /*
1735 * HMAC MD5
1736 */
1737 vchar_t *
1738 eay_hmacmd5_one(key, data)
1739 vchar_t *key, *data;
1740 {
1741 vchar_t *res;
1742 caddr_t ctx;
1743
1744 ctx = eay_hmacmd5_init(key);
1745 eay_hmacmd5_update(ctx, data);
1746 res = eay_hmacmd5_final(ctx);
1747
1748 return(res);
1749 }
1750
1751 caddr_t
1752 eay_hmacmd5_init(key)
1753 vchar_t *key;
1754 {
1755 return eay_hmac_init(key, EVP_md5());
1756 }
1757
1758 void
1759 eay_hmacmd5_update(c, data)
1760 caddr_t c;
1761 vchar_t *data;
1762 {
1763 HMAC_Update((HMAC_CTX *)c, data->v, data->l);
1764 }
1765
1766 vchar_t *
1767 eay_hmacmd5_final(c)
1768 caddr_t c;
1769 {
1770 vchar_t *res;
1771 unsigned int l;
1772
1773 if ((res = vmalloc(MD5_DIGEST_LENGTH)) == 0)
1774 return NULL;
1775
1776 HMAC_Final((HMAC_CTX *)c, res->v, &l);
1777 res->l = l;
1778 (void)racoon_free(c);
1779
1780 if (MD5_DIGEST_LENGTH != res->l) {
1781 #ifndef EAYDEBUG
1782 plog(LLV_ERROR, LOCATION, NULL,
1783 "hmac md5 length mismatch %d.\n", res->l);
1784 #else
1785 printf("hmac md5 length mismatch %d.\n", res->l);
1786 #endif
1787 vfree(res);
1788 return NULL;
1789 }
1790
1791 return(res);
1792 }
1793
1794 /*
1795 * SHA2-512 functions
1796 */
1797 caddr_t
1798 eay_sha2_512_init()
1799 {
1800 SHA512_CTX *c = racoon_malloc(sizeof(*c));
1801
1802 SHA512_Init(c);
1803
1804 return((caddr_t)c);
1805 }
1806
1807 void
1808 eay_sha2_512_update(c, data)
1809 caddr_t c;
1810 vchar_t *data;
1811 {
1812 SHA512_Update((SHA512_CTX *)c, data->v, data->l);
1813
1814 return;
1815 }
1816
1817 vchar_t *
1818 eay_sha2_512_final(c)
1819 caddr_t c;
1820 {
1821 vchar_t *res;
1822
1823 if ((res = vmalloc(SHA512_DIGEST_LENGTH)) == 0)
1824 return(0);
1825
1826 SHA512_Final(res->v, (SHA512_CTX *)c);
1827 (void)racoon_free(c);
1828
1829 return(res);
1830 }
1831
1832 vchar_t *
1833 eay_sha2_512_one(data)
1834 vchar_t *data;
1835 {
1836 caddr_t ctx;
1837 vchar_t *res;
1838
1839 ctx = eay_sha2_512_init();
1840 eay_sha2_512_update(ctx, data);
1841 res = eay_sha2_512_final(ctx);
1842
1843 return(res);
1844 }
1845
1846 int
1847 eay_sha2_512_hashlen()
1848 {
1849 return SHA512_DIGEST_LENGTH << 3;
1850 }
1851
1852 /*
1853 * SHA2-384 functions
1854 */
1855 caddr_t
1856 eay_sha2_384_init()
1857 {
1858 SHA384_CTX *c = racoon_malloc(sizeof(*c));
1859
1860 SHA384_Init(c);
1861
1862 return((caddr_t)c);
1863 }
1864
1865 void
1866 eay_sha2_384_update(c, data)
1867 caddr_t c;
1868 vchar_t *data;
1869 {
1870 SHA384_Update((SHA384_CTX *)c, data->v, data->l);
1871
1872 return;
1873 }
1874
1875 vchar_t *
1876 eay_sha2_384_final(c)
1877 caddr_t c;
1878 {
1879 vchar_t *res;
1880
1881 if ((res = vmalloc(SHA384_DIGEST_LENGTH)) == 0)
1882 return(0);
1883
1884 SHA384_Final(res->v, (SHA384_CTX *)c);
1885 (void)racoon_free(c);
1886
1887 return(res);
1888 }
1889
1890 vchar_t *
1891 eay_sha2_384_one(data)
1892 vchar_t *data;
1893 {
1894 caddr_t ctx;
1895 vchar_t *res;
1896
1897 ctx = eay_sha2_384_init();
1898 eay_sha2_384_update(ctx, data);
1899 res = eay_sha2_384_final(ctx);
1900
1901 return(res);
1902 }
1903
1904 int
1905 eay_sha2_384_hashlen()
1906 {
1907 return SHA384_DIGEST_LENGTH << 3;
1908 }
1909
1910 /*
1911 * SHA2-256 functions
1912 */
1913 caddr_t
1914 eay_sha2_256_init()
1915 {
1916 SHA256_CTX *c = racoon_malloc(sizeof(*c));
1917
1918 SHA256_Init(c);
1919
1920 return((caddr_t)c);
1921 }
1922
1923 void
1924 eay_sha2_256_update(c, data)
1925 caddr_t c;
1926 vchar_t *data;
1927 {
1928 SHA256_Update((SHA256_CTX *)c, data->v, data->l);
1929
1930 return;
1931 }
1932
1933 vchar_t *
1934 eay_sha2_256_final(c)
1935 caddr_t c;
1936 {
1937 vchar_t *res;
1938
1939 if ((res = vmalloc(SHA256_DIGEST_LENGTH)) == 0)
1940 return(0);
1941
1942 SHA256_Final(res->v, (SHA256_CTX *)c);
1943 (void)racoon_free(c);
1944
1945 return(res);
1946 }
1947
1948 vchar_t *
1949 eay_sha2_256_one(data)
1950 vchar_t *data;
1951 {
1952 caddr_t ctx;
1953 vchar_t *res;
1954
1955 ctx = eay_sha2_256_init();
1956 eay_sha2_256_update(ctx, data);
1957 res = eay_sha2_256_final(ctx);
1958
1959 return(res);
1960 }
1961
1962 int
1963 eay_sha2_256_hashlen()
1964 {
1965 return SHA256_DIGEST_LENGTH << 3;
1966 }
1967
1968 /*
1969 * SHA functions
1970 */
1971 caddr_t
1972 eay_sha1_init()
1973 {
1974 SHA_CTX *c = racoon_malloc(sizeof(*c));
1975
1976 SHA1_Init(c);
1977
1978 return((caddr_t)c);
1979 }
1980
1981 void
1982 eay_sha1_update(c, data)
1983 caddr_t c;
1984 vchar_t *data;
1985 {
1986 SHA1_Update((SHA_CTX *)c, data->v, data->l);
1987
1988 return;
1989 }
1990
1991 vchar_t *
1992 eay_sha1_final(c)
1993 caddr_t c;
1994 {
1995 vchar_t *res;
1996
1997 if ((res = vmalloc(SHA_DIGEST_LENGTH)) == 0)
1998 return(0);
1999
2000 SHA1_Final(res->v, (SHA_CTX *)c);
2001 (void)racoon_free(c);
2002
2003 return(res);
2004 }
2005
2006 vchar_t *
2007 eay_sha1_one(data)
2008 vchar_t *data;
2009 {
2010 caddr_t ctx;
2011 vchar_t *res;
2012
2013 ctx = eay_sha1_init();
2014 eay_sha1_update(ctx, data);
2015 res = eay_sha1_final(ctx);
2016
2017 return(res);
2018 }
2019
2020 int
2021 eay_sha1_hashlen()
2022 {
2023 return SHA_DIGEST_LENGTH << 3;
2024 }
2025
2026 /*
2027 * MD5 functions
2028 */
2029 caddr_t
2030 eay_md5_init()
2031 {
2032 MD5_CTX *c = racoon_malloc(sizeof(*c));
2033
2034 MD5_Init(c);
2035
2036 return((caddr_t)c);
2037 }
2038
2039 void
2040 eay_md5_update(c, data)
2041 caddr_t c;
2042 vchar_t *data;
2043 {
2044 MD5_Update((MD5_CTX *)c, data->v, data->l);
2045
2046 return;
2047 }
2048
2049 vchar_t *
2050 eay_md5_final(c)
2051 caddr_t c;
2052 {
2053 vchar_t *res;
2054
2055 if ((res = vmalloc(MD5_DIGEST_LENGTH)) == 0)
2056 return(0);
2057
2058 MD5_Final(res->v, (MD5_CTX *)c);
2059 (void)racoon_free(c);
2060
2061 return(res);
2062 }
2063
2064 vchar_t *
2065 eay_md5_one(data)
2066 vchar_t *data;
2067 {
2068 caddr_t ctx;
2069 vchar_t *res;
2070
2071 ctx = eay_md5_init();
2072 eay_md5_update(ctx, data);
2073 res = eay_md5_final(ctx);
2074
2075 return(res);
2076 }
2077
2078 int
2079 eay_md5_hashlen()
2080 {
2081 return MD5_DIGEST_LENGTH << 3;
2082 }
2083
2084 /*
2085 * eay_set_random
2086 * size: number of bytes.
2087 */
2088 vchar_t *
2089 eay_set_random(size)
2090 u_int32_t size;
2091 {
2092 BIGNUM *r = NULL;
2093 vchar_t *res = 0;
2094
2095 if ((r = BN_new()) == NULL)
2096 goto end;
2097 BN_rand(r, size * 8, 0, 0);
2098 eay_bn2v(&res, r);
2099
2100 end:
2101 if (r)
2102 BN_free(r);
2103 return(res);
2104 }
2105
2106 /* DH */
2107 int
2108 eay_dh_generate(prime, g, publen, pub, priv)
2109 vchar_t *prime, **pub, **priv;
2110 u_int publen;
2111 u_int32_t g;
2112 {
2113 BIGNUM *p = NULL;
2114 DH *dh = NULL;
2115 int error = -1;
2116
2117 /* initialize */
2118 /* pre-process to generate number */
2119 if (eay_v2bn(&p, prime) < 0)
2120 goto end;
2121
2122 if ((dh = DH_new()) == NULL)
2123 goto end;
2124 dh->p = p;
2125 p = NULL; /* p is now part of dh structure */
2126 dh->g = NULL;
2127 if ((dh->g = BN_new()) == NULL)
2128 goto end;
2129 if (!BN_set_word(dh->g, g))
2130 goto end;
2131
2132 if (publen != 0)
2133 dh->length = publen;
2134
2135 /* generate public and private number */
2136 if (!DH_generate_key(dh))
2137 goto end;
2138
2139 /* copy results to buffers */
2140 if (eay_bn2v(pub, dh->pub_key) < 0)
2141 goto end;
2142 if (eay_bn2v(priv, dh->priv_key) < 0) {
2143 vfree(*pub);
2144 goto end;
2145 }
2146
2147 error = 0;
2148
2149 end:
2150 if (dh != NULL)
2151 DH_free(dh);
2152 if (p != 0)
2153 BN_free(p);
2154 return(error);
2155 }
2156
2157 int
2158 eay_dh_compute(prime, g, pub, priv, pub2, key)
2159 vchar_t *prime, *pub, *priv, *pub2, **key;
2160 u_int32_t g;
2161 {
2162 BIGNUM *dh_pub = NULL;
2163 DH *dh = NULL;
2164 int l;
2165 caddr_t v = NULL;
2166 int error = -1;
2167
2168 /* make public number to compute */
2169 if (eay_v2bn(&dh_pub, pub2) < 0)
2170 goto end;
2171
2172 /* make DH structure */
2173 if ((dh = DH_new()) == NULL)
2174 goto end;
2175 if (eay_v2bn(&dh->p, prime) < 0)
2176 goto end;
2177 if (eay_v2bn(&dh->pub_key, pub) < 0)
2178 goto end;
2179 if (eay_v2bn(&dh->priv_key, priv) < 0)
2180 goto end;
2181 dh->length = pub2->l * 8;
2182
2183 dh->g = NULL;
2184 if ((dh->g = BN_new()) == NULL)
2185 goto end;
2186 if (!BN_set_word(dh->g, g))
2187 goto end;
2188
2189 if ((v = (caddr_t)racoon_calloc(prime->l, sizeof(u_char))) == NULL)
2190 goto end;
2191 if ((l = DH_compute_key(v, dh_pub, dh)) == -1)
2192 goto end;
2193 memcpy((*key)->v + (prime->l - l), v, l);
2194
2195 error = 0;
2196
2197 end:
2198 if (dh_pub != NULL)
2199 BN_free(dh_pub);
2200 if (dh != NULL)
2201 DH_free(dh);
2202 if (v != NULL)
2203 racoon_free(v);
2204 return(error);
2205 }
2206
2207 #if 1
2208 int
2209 eay_v2bn(bn, var)
2210 BIGNUM **bn;
2211 vchar_t *var;
2212 {
2213 if ((*bn = BN_bin2bn(var->v, var->l, NULL)) == NULL)
2214 return -1;
2215
2216 return 0;
2217 }
2218 #else
2219 /*
2220 * convert vchar_t <-> BIGNUM.
2221 *
2222 * vchar_t: unit is u_char, network endian, most significant byte first.
2223 * BIGNUM: unit is BN_ULONG, each of BN_ULONG is in host endian,
2224 * least significant BN_ULONG must come first.
2225 *
2226 * hex value of "0x3ffe050104" is represented as follows:
2227 * vchar_t: 3f fe 05 01 04
2228 * BIGNUM (BN_ULONG = u_int8_t): 04 01 05 fe 3f
2229 * BIGNUM (BN_ULONG = u_int16_t): 0x0104 0xfe05 0x003f
2230 * BIGNUM (BN_ULONG = u_int32_t_t): 0xfe050104 0x0000003f
2231 */
2232 int
2233 eay_v2bn(bn, var)
2234 BIGNUM **bn;
2235 vchar_t *var;
2236 {
2237 u_char *p;
2238 u_char *q;
2239 BN_ULONG *r;
2240 int l;
2241 BN_ULONG num;
2242
2243 *bn = BN_new();
2244 if (*bn == NULL)
2245 goto err;
2246 l = (var->l * 8 + BN_BITS2 - 1) / BN_BITS2;
2247 if (bn_expand(*bn, l * BN_BITS2) == NULL)
2248 goto err;
2249 (*bn)->top = l;
2250
2251 /* scan from least significant byte */
2252 p = (u_char *)var->v;
2253 q = (u_char *)(var->v + var->l);
2254 r = (*bn)->d;
2255 num = 0;
2256 l = 0;
2257 do {
2258 q--;
2259 num = num | ((BN_ULONG)*q << (l++ * 8));
2260 if (l == BN_BYTES) {
2261 *r++ = num;
2262 num = 0;
2263 l = 0;
2264 }
2265 } while (p < q);
2266 if (l)
2267 *r = num;
2268 return 0;
2269
2270 err:
2271 if (*bn)
2272 BN_free(*bn);
2273 return -1;
2274 }
2275 #endif
2276
2277 int
2278 eay_bn2v(var, bn)
2279 vchar_t **var;
2280 BIGNUM *bn;
2281 {
2282 *var = vmalloc(bn->top * BN_BYTES);
2283 if (*var == NULL)
2284 return(-1);
2285
2286 (*var)->l = BN_bn2bin(bn, (*var)->v);
2287
2288 return 0;
2289 }
2290
2291 const char *
2292 eay_version()
2293 {
2294 return SSLeay_version(SSLEAY_VERSION);
2295 }