1 /* $KAME: crypto_openssl.c,v 1.73 2003/04/24 02:21:22 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/types.h>
33 #include <sys/param.h>
40 /* get openssl/ssleay version number */
41 #ifdef HAVE_OPENSSL_OPENSSLV_H
42 # include <openssl/opensslv.h>
44 # error no opensslv.h found.
47 #ifndef OPENSSL_VERSION_NUMBER
48 #error OPENSSL_VERSION_NUMBER is not defined. OpenSSL0.9.4 or later required.
51 #ifdef HAVE_OPENSSL_PEM_H
52 #include <openssl/pem.h>
54 #ifdef HAVE_OPENSSL_EVP_H
55 #include <openssl/evp.h>
57 #ifdef HAVE_OPENSSL_X509_H
58 #include <openssl/x509.h>
59 #include <openssl/x509v3.h>
60 #include <openssl/x509_vfy.h>
62 #include <openssl/bn.h>
63 #include <openssl/dh.h>
64 #include <openssl/md5.h>
65 #include <openssl/sha.h>
66 #include <openssl/hmac.h>
67 #include <openssl/des.h>
68 #include <openssl/crypto.h>
69 #ifdef HAVE_OPENSSL_IDEA_H
70 #include <openssl/idea.h>
72 #include <openssl/blowfish.h>
73 #ifdef HAVE_OPENSSL_RC5_H
74 #include <openssl/rc5.h>
76 #include <openssl/cast.h>
77 #include <openssl/err.h>
78 #ifdef HAVE_OPENSSL_RIJNDAEL_H
79 #include <openssl/rijndael.h>
81 #include "rijndael-api-fst.h"
83 #ifdef HAVE_OPENSSL_SHA2_H
84 #include <openssl/sha2.h>
93 #include "crypto_openssl.h"
98 * I hate to cast every parameter to des_xx into void *, but it is
99 * necessary for SSLeay/OpenSSL portability. It sucks.
102 #ifdef HAVE_SIGNING_C
103 static int cb_check_cert_local
__P((int, X509_STORE_CTX
*));
104 static int cb_check_cert_remote
__P((int, X509_STORE_CTX
*));
105 static void eay_setgentype
__P((char *, int *));
106 static X509
*mem2x509
__P((vchar_t
*));
109 static caddr_t eay_hmac_init
__P((vchar_t
*, const EVP_MD
*));
111 #ifdef HAVE_SIGNING_C
112 /* X509 Certificate */
114 * convert the string of the subject name into DER
115 * e.g. str = "C=JP, ST=Kanagawa";
118 eay_str2asn1dn(str
, len
)
129 buf
= racoon_malloc(len
+ 1);
131 printf("failed to allocate buffer\n");
134 memcpy(buf
, str
, len
);
136 name
= X509_NAME_new();
140 for (i
= 0; i
< len
; i
++) {
141 if (!value
&& buf
[i
] == '=') {
145 } else if (buf
[i
] == ',' || buf
[i
] == '/') {
148 printf("[%s][%s]\n", field
, value
);
150 if (!X509_NAME_add_entry_by_txt(name
, field
,
151 MBSTRING_ASC
, value
, -1, -1, 0))
153 for (j
= i
+ 1; j
< len
; j
++) {
164 printf("[%s][%s]\n", field
, value
);
166 if (!X509_NAME_add_entry_by_txt(name
, field
,
167 MBSTRING_ASC
, value
, -1, -1, 0))
170 i
= i2d_X509_NAME(name
, NULL
);
177 i
= i2d_X509_NAME(name
, (unsigned char **)&p
);
187 X509_NAME_free(name
);
192 * compare two subjectNames.
198 eay_cmp_asn1dn(n1
, n2
)
201 X509_NAME
*a
= NULL
, *b
= NULL
;
206 if (!d2i_X509_NAME(&a
, (unsigned char **)&p
, n1
->l
))
209 if (!d2i_X509_NAME(&b
, (unsigned char **)&p
, n2
->l
))
212 i
= X509_NAME_cmp(a
, b
);
223 * this functions is derived from apps/verify.c in OpenSSL0.9.5
226 eay_check_x509cert(cert
, CApath
, local
)
231 X509_STORE
*cert_ctx
= NULL
;
232 X509_LOOKUP
*lookup
= NULL
;
234 #if OPENSSL_VERSION_NUMBER >= 0x00905100L
241 /* XXX define only functions required. */
242 #if OPENSSL_VERSION_NUMBER >= 0x00905100L
243 OpenSSL_add_all_algorithms();
245 SSLeay_add_all_algorithms();
248 cert_ctx
= X509_STORE_new();
249 if (cert_ctx
== NULL
)
253 X509_STORE_set_verify_cb_func(cert_ctx
, cb_check_cert_local
);
255 X509_STORE_set_verify_cb_func(cert_ctx
, cb_check_cert_remote
);
257 lookup
= X509_STORE_add_lookup(cert_ctx
, X509_LOOKUP_file());
260 X509_LOOKUP_load_file(lookup
, NULL
, X509_FILETYPE_DEFAULT
); /* XXX */
262 lookup
= X509_STORE_add_lookup(cert_ctx
, X509_LOOKUP_hash_dir());
265 error
= X509_LOOKUP_add_dir(lookup
, CApath
, X509_FILETYPE_PEM
);
270 error
= -1; /* initialized */
272 /* read the certificate to be verified */
273 x509
= mem2x509(cert
);
277 #if OPENSSL_VERSION_NUMBER >= 0x00905100L
278 csc
= X509_STORE_CTX_new();
281 X509_STORE_CTX_init(csc
, cert_ctx
, x509
, NULL
);
283 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
284 X509_STORE_CTX_set_flags(csc
, X509_V_FLAG_CRL_CHECK
);
285 X509_STORE_CTX_set_flags(csc
, X509_V_FLAG_CRL_CHECK_ALL
);
288 error
= X509_verify_cert(csc
);
289 X509_STORE_CTX_cleanup(csc
);
291 X509_STORE_CTX_init(&csc
, cert_ctx
, x509
, NULL
);
292 error
= X509_verify_cert(&csc
);
293 X509_STORE_CTX_cleanup(&csc
);
297 * if x509_verify_cert() is successful then the value of error is
300 error
= error
? 0 : -1;
304 printf("%s\n", eay_strerror());
305 if (cert_ctx
!= NULL
)
306 X509_STORE_free(cert_ctx
);
314 * callback function for verifing certificate.
315 * Derived from cb() in openssl/apps/s_server.c
317 * This one is called for certificates obtained from
318 * 'peers_certfile' directive.
321 cb_check_cert_local(ok
, ctx
)
330 X509_get_subject_name(ctx
->current_cert
),
334 * since we are just checking the certificates, it is
335 * ok if they are self signed. But we should still warn
338 switch (ctx
->error
) {
339 case X509_V_ERR_CERT_HAS_EXPIRED
:
340 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
:
341 #if OPENSSL_VERSION_NUMBER >= 0x00905100L
342 case X509_V_ERR_INVALID_PURPOSE
:
343 case X509_V_ERR_UNABLE_TO_GET_CRL
:
346 log_tag
= LLV_WARNING
;
355 plog(log_tag
, LOCATION
, NULL
,
356 "%s(%d) at depth:%d SubjectName:%s\n",
357 X509_verify_cert_error_string(ctx
->error
),
362 printf("%d: %s(%d) at depth:%d SubjectName:%s\n",
364 X509_verify_cert_error_string(ctx
->error
),
376 * Similar to cb_check_cert_local() but this one is called
377 * for certificates obtained from the IKE payload.
380 cb_check_cert_remote(ok
, ctx
)
389 X509_get_subject_name(ctx
->current_cert
),
392 switch (ctx
->error
) {
393 case X509_V_ERR_UNABLE_TO_GET_CRL
:
395 log_tag
= LLV_WARNING
;
401 plog(log_tag
, LOCATION
, NULL
,
402 "%s(%d) at depth:%d SubjectName:%s\n",
403 X509_verify_cert_error_string(ctx
->error
),
408 printf("%d: %s(%d) at depth:%d SubjectName:%s\n",
410 X509_verify_cert_error_string(ctx
->error
),
422 * get a subjectAltName from X509 certificate.
425 eay_get_x509asn1subjectname(cert
)
430 vchar_t
*name
= NULL
;
436 x509
= mem2x509(cert
);
440 /* get the length of the name */
441 len
= i2d_X509_NAME(x509
->cert_info
->subject
, NULL
);
447 len
= i2d_X509_NAME(x509
->cert_info
->subject
, &bp
);
454 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
456 printf("%s\n", eay_strerror());
470 * get the subjectAltName from X509 certificate.
471 * the name is terminated by '\0'.
473 #include <openssl/x509v3.h>
475 eay_get_x509subjectaltname(cert
, altname
, type
, pos
)
483 X509V3_EXT_METHOD
*method
= NULL
;
484 STACK_OF(GENERAL_NAME
) *name
;
485 CONF_VALUE
*cval
= NULL
;
486 STACK_OF(CONF_VALUE
) *nval
= NULL
;
492 *type
= GENT_OTHERNAME
;
496 x509
= mem2x509(cert
);
500 i
= X509_get_ext_by_NID(x509
, NID_subject_alt_name
, -1);
503 ext
= X509_get_ext(x509
, i
);
504 method
= X509V3_EXT_get(ext
);
508 bp
= ext
->value
->data
;
509 name
= method
->d2i(NULL
, &bp
, ext
->value
->length
);
513 nval
= method
->i2v(method
, name
, NULL
);
514 method
->ext_free(name
);
519 for (i
= 0; i
< sk_CONF_VALUE_num(nval
); i
++) {
523 cval
= sk_CONF_VALUE_value(nval
, i
);
524 len
= strlen(cval
->value
) + 1; /* '\0' included */
525 *altname
= racoon_malloc(len
);
527 sk_CONF_VALUE_pop_free(nval
, X509V3_conf_free
);
530 strlcpy(*altname
, cval
->value
, len
);
532 /* set type of the name */
533 eay_setgentype(cval
->name
, type
);
536 sk_CONF_VALUE_pop_free(nval
, X509V3_conf_free
);
543 racoon_free(*altname
);
547 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
549 printf("%s\n", eay_strerror());
559 eay_setgentype(name
, type
)
563 /* XXX It's needed effective code */
564 if(!memcmp(name
, "email", strlen("email"))) {
566 } else if(!memcmp(name
, "URI", strlen("URI"))) {
568 } else if(!memcmp(name
, "DNS", strlen("DNS"))) {
570 } else if(!memcmp(name
, "RID", strlen("RID"))) {
572 } else if(!memcmp(name
, "IP", strlen("IP"))) {
575 *type
= GENT_OTHERNAME
;
580 * decode a X509 certificate and make a readable text terminated '\n'.
581 * return the buffer allocated, so must free it later.
584 eay_get_x509text(cert
)
594 x509
= mem2x509(cert
);
598 bio
= BIO_new(BIO_s_mem());
602 error
= X509_print(bio
, x509
);
608 len
= BIO_get_mem_data(bio
, &bp
);
609 text
= racoon_malloc(len
+ 1);
612 memcpy(text
, bp
, len
);
624 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
626 printf("%s\n", eay_strerror());
637 /* get X509 structure from buffer. */
650 x509
= d2i_X509(NULL
, &bp
, cert
->l
);
657 bio
= BIO_new(BIO_s_mem());
660 len
= BIO_write(bio
, cert
->v
, cert
->l
);
663 x509
= PEM_read_bio_X509(bio
, NULL
, NULL
, NULL
);
671 * get a X509 certificate from local file.
672 * a certificate must be PEM format.
674 * path to a certificate.
676 * NULL if error occured
680 eay_get_x509cert(path
)
690 /* Read private key */
691 fp
= fopen(path
, "r");
694 #if OPENSSL_VERSION_NUMBER >= 0x00904100L
695 x509
= PEM_read_X509(fp
, NULL
, NULL
, NULL
);
697 x509
= PEM_read_X509(fp
, NULL
, NULL
);
704 len
= i2d_X509(x509
, NULL
);
711 error
= i2d_X509(x509
, &bp
);
721 * sign a souce by X509 signature.
722 * XXX: to be get hash type from my cert ?
723 * to be handled EVP_dss().
726 eay_get_x509sign(source
, privkey
, cert
)
733 sig
= eay_rsa_sign(source
, privkey
);
739 * check a X509 signature
740 * XXX: to be get hash type from my cert ?
741 * to be handled EVP_dss().
742 * OUT: return -1 when error.
746 eay_check_x509sign(source
, sig
, cert
)
757 x509
= d2i_X509(NULL
, &bp
, cert
->l
);
760 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
765 evp
= X509_get_pubkey(x509
);
768 plog(LLV_ERROR
, LOCATION
, NULL
, "X509_get_pubkey: %s\n", eay_strerror());
773 return eay_rsa_verify(source
, sig
, evp
);
777 * check a signature by signed with PKCS7 certificate.
778 * XXX: to be get hash type from my cert ?
779 * to be handled EVP_dss().
780 * OUT: return -1 when error.
784 eay_check_pkcs7sign(source
, sig
, cert
)
793 BIO
*bio
= BIO_new(BIO_s_mem());
798 error
= BIO_write(bio
, cert
->v
, cert
->l
);
799 if (error
!= cert
->l
)
803 x509
= PEM_read_bio_X509(bio
, NULL
, NULL
, NULL
);
808 evp
= X509_get_pubkey(x509
);
813 /* Verify the signature */
814 /* XXX: to be handled EVP_dss() */
815 EVP_VerifyInit(&md_ctx
, EVP_sha1());
816 EVP_VerifyUpdate(&md_ctx
, source
->v
, source
->l
);
817 error
= EVP_VerifyFinal(&md_ctx
, sig
->v
, sig
->l
, evp
);
828 * get PKCS#1 Private Key of PEM format from local file.
831 eay_get_pkcs1privkey(path
)
835 EVP_PKEY
*evp
= NULL
;
836 vchar_t
*pkey
= NULL
;
841 /* Read private key */
842 fp
= fopen(path
, "r");
846 #if OPENSSL_VERSION_NUMBER >= 0x00904100L
847 evp
= PEM_read_PrivateKey(fp
, NULL
, NULL
, NULL
);
849 evp
= PEM_read_PrivateKey(fp
, NULL
, NULL
);
856 pkeylen
= i2d_PrivateKey(evp
, NULL
);
859 pkey
= vmalloc(pkeylen
);
863 pkeylen
= i2d_PrivateKey(evp
, &bp
);
872 if (error
!= 0 && pkey
!= NULL
) {
881 * get PKCS#1 Public Key of PEM format from local file.
884 eay_get_pkcs1pubkey(path
)
888 EVP_PKEY
*evp
= NULL
;
889 vchar_t
*pkey
= NULL
;
895 /* Read private key */
896 fp
= fopen(path
, "r");
900 #if OPENSSL_VERSION_NUMBER >= 0x00904100L
901 x509
= PEM_read_X509(fp
, NULL
, NULL
, NULL
);
903 x509
= PEM_read_X509(fp
, NULL
, NULL
);
910 /* Get public key - eay */
911 evp
= X509_get_pubkey(x509
);
915 pkeylen
= i2d_PublicKey(evp
, NULL
);
918 pkey
= vmalloc(pkeylen
);
922 pkeylen
= i2d_PublicKey(evp
, &bp
);
930 if (error
!= 0 && pkey
!= NULL
) {
940 eay_rsa_sign(src
, privkey
)
941 vchar_t
*src
, *privkey
;
944 u_char
*bp
= privkey
->v
;
947 int pad
= RSA_PKCS1_PADDING
;
949 /* XXX to be handled EVP_PKEY_DSA */
950 evp
= d2i_PrivateKey(EVP_PKEY_RSA
, NULL
, &bp
, privkey
->l
);
954 /* XXX: to be handled EVP_dss() */
955 /* XXX: Where can I get such parameters ? From my cert ? */
957 len
= RSA_size(evp
->pkey
.rsa
);
963 len
= RSA_private_encrypt(src
->l
, src
->v
, sig
->v
, evp
->pkey
.rsa
, pad
);
965 if (len
== 0 || len
!= sig
->l
) {
974 eay_rsa_verify(src
, sig
, evp
)
978 vchar_t
*xbuf
= NULL
;
979 int pad
= RSA_PKCS1_PADDING
;
983 len
= RSA_size(evp
->pkey
.rsa
);
988 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
994 len
= RSA_public_decrypt(sig
->l
, sig
->v
, xbuf
->v
, evp
->pkey
.rsa
, pad
);
996 if (len
== 0 || len
!= src
->l
)
997 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
1000 if (len
== 0 || len
!= src
->l
) {
1005 error
= memcmp(src
->v
, xbuf
->v
, src
->l
);
1015 * MUST load ERR_load_crypto_strings() first.
1020 static char ebuf
[512];
1024 #if OPENSSL_VERSION_NUMBER >= 0x00904100L
1025 const char *file
, *data
;
1032 es
= CRYPTO_thread_id();
1034 while ((l
= ERR_get_error_line_data(&file
, &line
, &data
, &flags
)) != 0){
1035 n
= snprintf(ebuf
+ len
, sizeof(ebuf
) - len
,
1037 es
, ERR_error_string(l
, buf
), file
, line
,
1038 (flags
& ERR_TXT_STRING
) ? data
: "");
1039 if (n
< 0 || n
>= sizeof(ebuf
) - len
)
1042 if (sizeof(ebuf
) < len
)
1052 ERR_load_crypto_strings();
1059 eay_des_encrypt(data
, key
, iv
)
1060 vchar_t
*data
, *key
, *iv
;
1063 des_key_schedule ks
;
1065 if (des_key_sched((void *)key
->v
, ks
) != 0)
1068 /* allocate buffer for result */
1069 if ((res
= vmalloc(data
->l
)) == NULL
)
1072 /* decryption data */
1073 des_cbc_encrypt((void *)data
->v
, (void *)res
->v
, data
->l
,
1074 ks
, (void *)iv
->v
, DES_ENCRYPT
);
1080 eay_des_decrypt(data
, key
, iv
)
1081 vchar_t
*data
, *key
, *iv
;
1084 des_key_schedule ks
;
1086 if (des_key_sched((void *)key
->v
, ks
) != 0)
1089 /* allocate buffer for result */
1090 if ((res
= vmalloc(data
->l
)) == NULL
)
1093 /* decryption data */
1094 des_cbc_encrypt((void *)data
->v
, (void *)res
->v
, data
->l
,
1095 ks
, (void *)iv
->v
, DES_DECRYPT
);
1101 eay_des_weakkey(key
)
1104 return des_is_weak_key((void *)key
->v
);
1111 if (len
!= 0 && len
!= 64)
1116 #ifdef HAVE_OPENSSL_IDEA_H
1121 eay_idea_encrypt(data
, key
, iv
)
1122 vchar_t
*data
, *key
, *iv
;
1125 IDEA_KEY_SCHEDULE ks
;
1127 idea_set_encrypt_key(key
->v
, &ks
);
1129 /* allocate buffer for result */
1130 if ((res
= vmalloc(data
->l
)) == NULL
)
1133 /* decryption data */
1134 idea_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1135 &ks
, iv
->v
, IDEA_ENCRYPT
);
1141 eay_idea_decrypt(data
, key
, iv
)
1142 vchar_t
*data
, *key
, *iv
;
1145 IDEA_KEY_SCHEDULE ks
, dks
;
1147 idea_set_encrypt_key(key
->v
, &ks
);
1148 idea_set_decrypt_key(&ks
, &dks
);
1150 /* allocate buffer for result */
1151 if ((res
= vmalloc(data
->l
)) == NULL
)
1154 /* decryption data */
1155 idea_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1156 &dks
, iv
->v
, IDEA_DECRYPT
);
1162 eay_idea_weakkey(key
)
1169 eay_idea_keylen(len
)
1172 if (len
!= 0 && len
!= 128)
1182 eay_bf_encrypt(data
, key
, iv
)
1183 vchar_t
*data
, *key
, *iv
;
1188 BF_set_key(&ks
, key
->l
, key
->v
);
1190 /* allocate buffer for result */
1191 if ((res
= vmalloc(data
->l
)) == NULL
)
1194 /* decryption data */
1195 BF_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1196 &ks
, iv
->v
, BF_ENCRYPT
);
1202 eay_bf_decrypt(data
, key
, iv
)
1203 vchar_t
*data
, *key
, *iv
;
1208 BF_set_key(&ks
, key
->l
, key
->v
);
1210 /* allocate buffer for result */
1211 if ((res
= vmalloc(data
->l
)) == NULL
)
1214 /* decryption data */
1215 BF_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1216 &ks
, iv
->v
, BF_DECRYPT
);
1225 return 0; /* XXX to be done. refer to RFC 2451 */
1234 if (len
< 40 || len
> 448)
1236 return (len
+ 7) / 8;
1239 #ifdef HAVE_OPENSSL_RC5_H
1244 eay_rc5_encrypt(data
, key
, iv
)
1245 vchar_t
*data
, *key
, *iv
;
1250 /* in RFC 2451, there is information about the number of round. */
1251 RC5_32_set_key(&ks
, key
->l
, key
->v
, 16);
1253 /* allocate buffer for result */
1254 if ((res
= vmalloc(data
->l
)) == NULL
)
1257 /* decryption data */
1258 RC5_32_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1259 &ks
, iv
->v
, RC5_ENCRYPT
);
1265 eay_rc5_decrypt(data
, key
, iv
)
1266 vchar_t
*data
, *key
, *iv
;
1271 /* in RFC 2451, there is information about the number of round. */
1272 RC5_32_set_key(&ks
, key
->l
, key
->v
, 16);
1274 /* allocate buffer for result */
1275 if ((res
= vmalloc(data
->l
)) == NULL
)
1278 /* decryption data */
1279 RC5_32_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1280 &ks
, iv
->v
, RC5_DECRYPT
);
1286 eay_rc5_weakkey(key
)
1289 return 0; /* No known weak keys when used with 16 rounds. */
1299 if (len
< 40 || len
> 2040)
1301 return (len
+ 7) / 8;
1309 eay_3des_encrypt(data
, key
, iv
)
1310 vchar_t
*data
, *key
, *iv
;
1313 des_key_schedule ks1
, ks2
, ks3
;
1318 if (des_key_sched((void *)key
->v
, ks1
) != 0)
1320 if (des_key_sched((void *)(key
->v
+ 8), ks2
) != 0)
1322 if (des_key_sched((void *)(key
->v
+ 16), ks3
) != 0)
1325 /* allocate buffer for result */
1326 if ((res
= vmalloc(data
->l
)) == NULL
)
1329 /* decryption data */
1330 des_ede3_cbc_encrypt((void *)data
->v
, (void *)res
->v
, data
->l
,
1331 ks1
, ks2
, ks3
, (void *)iv
->v
, DES_ENCRYPT
);
1337 eay_3des_decrypt(data
, key
, iv
)
1338 vchar_t
*data
, *key
, *iv
;
1341 des_key_schedule ks1
, ks2
, ks3
;
1346 if (des_key_sched((void *)key
->v
, ks1
) != 0)
1348 if (des_key_sched((void *)(key
->v
+ 8), ks2
) != 0)
1350 if (des_key_sched((void *)(key
->v
+ 16), ks3
) != 0)
1353 /* allocate buffer for result */
1354 if ((res
= vmalloc(data
->l
)) == NULL
)
1357 /* decryption data */
1358 des_ede3_cbc_encrypt((void *)data
->v
, (void *)res
->v
, data
->l
,
1359 ks1
, ks2
, ks3
, (void *)iv
->v
, DES_DECRYPT
);
1365 eay_3des_weakkey(key
)
1371 return (des_is_weak_key((void *)key
->v
)
1372 || des_is_weak_key((void *)(key
->v
+ 8))
1373 || des_is_weak_key((void *)(key
->v
+ 16)));
1377 eay_3des_keylen(len
)
1380 if (len
!= 0 && len
!= 192)
1389 eay_cast_encrypt(data
, key
, iv
)
1390 vchar_t
*data
, *key
, *iv
;
1395 CAST_set_key(&ks
, key
->l
, key
->v
);
1397 /* allocate buffer for result */
1398 if ((res
= vmalloc(data
->l
)) == NULL
)
1401 /* decryption data */
1402 CAST_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1403 &ks
, iv
->v
, DES_ENCRYPT
);
1409 eay_cast_decrypt(data
, key
, iv
)
1410 vchar_t
*data
, *key
, *iv
;
1415 CAST_set_key(&ks
, key
->l
, key
->v
);
1417 /* allocate buffer for result */
1418 if ((res
= vmalloc(data
->l
)) == NULL
)
1421 /* decryption data */
1422 CAST_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1423 &ks
, iv
->v
, DES_DECRYPT
);
1429 eay_cast_weakkey(key
)
1432 return 0; /* No known weak keys. */
1436 eay_cast_keylen(len
)
1441 if (len
< 40 || len
> 128)
1443 return (len
+ 7) / 8;
1450 eay_aes_encrypt(data
, key
, iv
)
1451 vchar_t
*data
, *key
, *iv
;
1457 memset(&k
, 0, sizeof(k
));
1458 if (rijndael_makeKey(&k
, DIR_ENCRYPT
, key
->l
<< 3, key
->v
) < 0)
1461 /* allocate buffer for result */
1462 if ((res
= vmalloc(data
->l
)) == NULL
)
1465 /* encryption data */
1466 memset(&c
, 0, sizeof(c
));
1467 if (rijndael_cipherInit(&c
, MODE_CBC
, iv
->v
) < 0)
1469 if (rijndael_blockEncrypt(&c
, &k
, data
->v
, data
->l
<< 3, res
->v
) < 0)
1476 eay_aes_decrypt(data
, key
, iv
)
1477 vchar_t
*data
, *key
, *iv
;
1483 memset(&k
, 0, sizeof(k
));
1484 if (rijndael_makeKey(&k
, DIR_DECRYPT
, key
->l
<< 3, key
->v
) < 0)
1487 /* allocate buffer for result */
1488 if ((res
= vmalloc(data
->l
)) == NULL
)
1491 /* decryption data */
1492 memset(&c
, 0, sizeof(c
));
1493 if (rijndael_cipherInit(&c
, MODE_CBC
, iv
->v
) < 0)
1495 if (rijndael_blockDecrypt(&c
, &k
, data
->v
, data
->l
<< 3, res
->v
) < 0)
1502 eay_aes_weakkey(key
)
1514 if (len
!= 128 && len
!= 192 && len
!= 256)
1519 /* for ipsec part */
1533 eay_twofish_keylen(len
)
1536 if (len
< 0 || len
> 256)
1542 eay_null_keylen(len
)
1552 eay_hmac_init(key
, md
)
1556 HMAC_CTX
*c
= racoon_malloc(sizeof(*c
));
1559 HMAC_Init(c
, key
->v
, key
->l
, md
);
1568 eay_hmacsha2_512_one(key
, data
)
1569 vchar_t
*key
, *data
;
1574 ctx
= eay_hmacsha2_512_init(key
);
1575 eay_hmacsha2_512_update(ctx
, data
);
1576 res
= eay_hmacsha2_512_final(ctx
);
1582 eay_hmacsha2_512_init(key
)
1585 return eay_hmac_init(key
, EVP_sha2_512());
1589 eay_hmacsha2_512_update(c
, data
)
1593 HMAC_Update((HMAC_CTX
*)c
, data
->v
, data
->l
);
1597 eay_hmacsha2_512_final(c
)
1603 if ((res
= vmalloc(SHA512_DIGEST_LENGTH
)) == 0)
1606 HMAC_Final((HMAC_CTX
*)c
, res
->v
, &l
);
1608 HMAC_CTX_cleanup(c
);
1609 (void)racoon_free(c
);
1611 if (SHA512_DIGEST_LENGTH
!= res
->l
) {
1613 plog(LLV_ERROR
, LOCATION
, NULL
,
1614 "hmac sha2_512 length mismatch %d.\n", res
->l
);
1616 printf("hmac sha2_512 length mismatch %d.\n", res
->l
);
1629 eay_hmacsha2_384_one(key
, data
)
1630 vchar_t
*key
, *data
;
1635 ctx
= eay_hmacsha2_384_init(key
);
1636 eay_hmacsha2_384_update(ctx
, data
);
1637 res
= eay_hmacsha2_384_final(ctx
);
1643 eay_hmacsha2_384_init(key
)
1646 return eay_hmac_init(key
, EVP_sha2_384());
1650 eay_hmacsha2_384_update(c
, data
)
1654 HMAC_Update((HMAC_CTX
*)c
, data
->v
, data
->l
);
1658 eay_hmacsha2_384_final(c
)
1664 if ((res
= vmalloc(SHA384_DIGEST_LENGTH
)) == 0)
1667 HMAC_Final((HMAC_CTX
*)c
, res
->v
, &l
);
1669 HMAC_CTX_cleanup(c
);
1670 (void)racoon_free(c
);
1672 if (SHA384_DIGEST_LENGTH
!= res
->l
) {
1674 plog(LLV_ERROR
, LOCATION
, NULL
,
1675 "hmac sha2_384 length mismatch %d.\n", res
->l
);
1677 printf("hmac sha2_384 length mismatch %d.\n", res
->l
);
1690 eay_hmacsha2_256_one(key
, data
)
1691 vchar_t
*key
, *data
;
1696 ctx
= eay_hmacsha2_256_init(key
);
1697 eay_hmacsha2_256_update(ctx
, data
);
1698 res
= eay_hmacsha2_256_final(ctx
);
1704 eay_hmacsha2_256_init(key
)
1707 return eay_hmac_init(key
, EVP_sha2_256());
1711 eay_hmacsha2_256_update(c
, data
)
1715 HMAC_Update((HMAC_CTX
*)c
, data
->v
, data
->l
);
1719 eay_hmacsha2_256_final(c
)
1725 if ((res
= vmalloc(SHA256_DIGEST_LENGTH
)) == 0)
1728 HMAC_Final((HMAC_CTX
*)c
, res
->v
, &l
);
1730 HMAC_CTX_cleanup(c
);
1731 (void)racoon_free(c
);
1733 if (SHA256_DIGEST_LENGTH
!= res
->l
) {
1735 plog(LLV_ERROR
, LOCATION
, NULL
,
1736 "hmac sha2_256 length mismatch %d.\n", res
->l
);
1738 printf("hmac sha2_256 length mismatch %d.\n", res
->l
);
1751 eay_hmacsha1_one(key
, data
)
1752 vchar_t
*key
, *data
;
1757 ctx
= eay_hmacsha1_init(key
);
1758 eay_hmacsha1_update(ctx
, data
);
1759 res
= eay_hmacsha1_final(ctx
);
1765 eay_hmacsha1_init(key
)
1768 return eay_hmac_init(key
, EVP_sha1());
1772 eay_hmacsha1_update(c
, data
)
1776 HMAC_Update((HMAC_CTX
*)c
, data
->v
, data
->l
);
1780 eay_hmacsha1_final(c
)
1786 if ((res
= vmalloc(SHA_DIGEST_LENGTH
)) == 0)
1789 HMAC_Final((HMAC_CTX
*)c
, res
->v
, &l
);
1791 HMAC_CTX_cleanup(c
);
1792 (void)racoon_free(c
);
1794 if (SHA_DIGEST_LENGTH
!= res
->l
) {
1796 plog(LLV_ERROR
, LOCATION
, NULL
,
1797 "hmac sha1 length mismatch %d.\n", res
->l
);
1799 printf("hmac sha1 length mismatch %d.\n", res
->l
);
1812 eay_hmacmd5_one(key
, data
)
1813 vchar_t
*key
, *data
;
1818 ctx
= eay_hmacmd5_init(key
);
1819 eay_hmacmd5_update(ctx
, data
);
1820 res
= eay_hmacmd5_final(ctx
);
1826 eay_hmacmd5_init(key
)
1829 return eay_hmac_init(key
, EVP_md5());
1833 eay_hmacmd5_update(c
, data
)
1837 HMAC_Update((HMAC_CTX
*)c
, data
->v
, data
->l
);
1841 eay_hmacmd5_final(c
)
1847 if ((res
= vmalloc(MD5_DIGEST_LENGTH
)) == 0)
1850 HMAC_Final((HMAC_CTX
*)c
, res
->v
, &l
);
1852 HMAC_CTX_cleanup(c
);
1853 (void)racoon_free(c
);
1855 if (MD5_DIGEST_LENGTH
!= res
->l
) {
1857 plog(LLV_ERROR
, LOCATION
, NULL
,
1858 "hmac md5 length mismatch %d.\n", res
->l
);
1860 printf("hmac md5 length mismatch %d.\n", res
->l
);
1870 * SHA2-512 functions
1875 SHA512_CTX
*c
= racoon_malloc(sizeof(*c
));
1883 eay_sha2_512_update(c
, data
)
1887 SHA512_Update((SHA512_CTX
*)c
, data
->v
, data
->l
);
1893 eay_sha2_512_final(c
)
1898 if ((res
= vmalloc(SHA512_DIGEST_LENGTH
)) == 0)
1901 SHA512_Final(res
->v
, (SHA512_CTX
*)c
);
1902 (void)racoon_free(c
);
1908 eay_sha2_512_one(data
)
1914 ctx
= eay_sha2_512_init();
1915 eay_sha2_512_update(ctx
, data
);
1916 res
= eay_sha2_512_final(ctx
);
1922 eay_sha2_512_hashlen()
1924 return SHA512_DIGEST_LENGTH
<< 3;
1928 * SHA2-384 functions
1933 SHA384_CTX
*c
= racoon_malloc(sizeof(*c
));
1941 eay_sha2_384_update(c
, data
)
1945 SHA384_Update((SHA384_CTX
*)c
, data
->v
, data
->l
);
1951 eay_sha2_384_final(c
)
1956 if ((res
= vmalloc(SHA384_DIGEST_LENGTH
)) == 0)
1959 SHA384_Final(res
->v
, (SHA384_CTX
*)c
);
1960 (void)racoon_free(c
);
1966 eay_sha2_384_one(data
)
1972 ctx
= eay_sha2_384_init();
1973 eay_sha2_384_update(ctx
, data
);
1974 res
= eay_sha2_384_final(ctx
);
1980 eay_sha2_384_hashlen()
1982 return SHA384_DIGEST_LENGTH
<< 3;
1986 * SHA2-256 functions
1991 SHA256_CTX
*c
= racoon_malloc(sizeof(*c
));
1999 eay_sha2_256_update(c
, data
)
2003 SHA256_Update((SHA256_CTX
*)c
, data
->v
, data
->l
);
2009 eay_sha2_256_final(c
)
2014 if ((res
= vmalloc(SHA256_DIGEST_LENGTH
)) == 0)
2017 SHA256_Final(res
->v
, (SHA256_CTX
*)c
);
2018 (void)racoon_free(c
);
2024 eay_sha2_256_one(data
)
2030 ctx
= eay_sha2_256_init();
2031 eay_sha2_256_update(ctx
, data
);
2032 res
= eay_sha2_256_final(ctx
);
2038 eay_sha2_256_hashlen()
2040 return SHA256_DIGEST_LENGTH
<< 3;
2049 SHA_CTX
*c
= racoon_malloc(sizeof(*c
));
2057 eay_sha1_update(c
, data
)
2061 SHA1_Update((SHA_CTX
*)c
, data
->v
, data
->l
);
2072 if ((res
= vmalloc(SHA_DIGEST_LENGTH
)) == 0)
2075 SHA1_Final(res
->v
, (SHA_CTX
*)c
);
2076 (void)racoon_free(c
);
2088 ctx
= eay_sha1_init();
2089 eay_sha1_update(ctx
, data
);
2090 res
= eay_sha1_final(ctx
);
2098 return SHA_DIGEST_LENGTH
<< 3;
2107 MD5_CTX
*c
= racoon_malloc(sizeof(*c
));
2115 eay_md5_update(c
, data
)
2119 MD5_Update((MD5_CTX
*)c
, data
->v
, data
->l
);
2130 if ((res
= vmalloc(MD5_DIGEST_LENGTH
)) == 0)
2133 MD5_Final(res
->v
, (MD5_CTX
*)c
);
2134 (void)racoon_free(c
);
2146 ctx
= eay_md5_init();
2147 eay_md5_update(ctx
, data
);
2148 res
= eay_md5_final(ctx
);
2156 return MD5_DIGEST_LENGTH
<< 3;
2161 * size: number of bytes.
2164 eay_set_random(size
)
2170 if ((r
= BN_new()) == NULL
)
2172 BN_rand(r
, size
* 8, 0, 0);
2183 eay_dh_generate(prime
, g
, publen
, pub
, priv
)
2184 vchar_t
*prime
, **pub
, **priv
;
2193 /* pre-process to generate number */
2194 if (eay_v2bn(&p
, prime
) < 0)
2197 if ((dh
= DH_new()) == NULL
)
2200 p
= NULL
; /* p is now part of dh structure */
2202 if ((dh
->g
= BN_new()) == NULL
)
2204 if (!BN_set_word(dh
->g
, g
))
2208 dh
->length
= publen
;
2210 /* generate public and private number */
2211 if (!DH_generate_key(dh
))
2214 /* copy results to buffers */
2215 if (eay_bn2v(pub
, dh
->pub_key
) < 0)
2217 if (eay_bn2v(priv
, dh
->priv_key
) < 0) {
2233 eay_dh_compute(prime
, g
, pub
, priv
, pub2
, key
)
2234 vchar_t
*prime
, *pub
, *priv
, *pub2
, **key
;
2237 BIGNUM
*dh_pub
= NULL
;
2243 /* make public number to compute */
2244 if (eay_v2bn(&dh_pub
, pub2
) < 0)
2247 /* make DH structure */
2248 if ((dh
= DH_new()) == NULL
)
2250 if (eay_v2bn(&dh
->p
, prime
) < 0)
2252 if (eay_v2bn(&dh
->pub_key
, pub
) < 0)
2254 if (eay_v2bn(&dh
->priv_key
, priv
) < 0)
2256 dh
->length
= pub2
->l
* 8;
2259 if ((dh
->g
= BN_new()) == NULL
)
2261 if (!BN_set_word(dh
->g
, g
))
2264 if ((v
= (caddr_t
)racoon_calloc(prime
->l
, sizeof(u_char
))) == NULL
)
2266 if ((l
= DH_compute_key(v
, dh_pub
, dh
)) == -1)
2268 memcpy((*key
)->v
+ (prime
->l
- l
), v
, l
);
2288 if ((*bn
= BN_bin2bn(var
->v
, var
->l
, NULL
)) == NULL
)
2295 * convert vchar_t <-> BIGNUM.
2297 * vchar_t: unit is u_char, network endian, most significant byte first.
2298 * BIGNUM: unit is BN_ULONG, each of BN_ULONG is in host endian,
2299 * least significant BN_ULONG must come first.
2301 * hex value of "0x3ffe050104" is represented as follows:
2302 * vchar_t: 3f fe 05 01 04
2303 * BIGNUM (BN_ULONG = u_int8_t): 04 01 05 fe 3f
2304 * BIGNUM (BN_ULONG = u_int16_t): 0x0104 0xfe05 0x003f
2305 * BIGNUM (BN_ULONG = u_int32_t_t): 0xfe050104 0x0000003f
2321 l
= (var
->l
* 8 + BN_BITS2
- 1) / BN_BITS2
;
2322 if (bn_expand(*bn
, l
* BN_BITS2
) == NULL
)
2326 /* scan from least significant byte */
2327 p
= (u_char
*)var
->v
;
2328 q
= (u_char
*)(var
->v
+ var
->l
);
2334 num
= num
| ((BN_ULONG
)*q
<< (l
++ * 8));
2335 if (l
== BN_BYTES
) {
2357 *var
= vmalloc(bn
->top
* BN_BYTES
);
2361 (*var
)->l
= BN_bn2bin(bn
, (*var
)->v
);
2369 return SSLeay_version(SSLEAY_VERSION
);