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