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/x509_vfy.h>
61 #include <openssl/bn.h>
62 #include <openssl/dh.h>
63 #include <openssl/md5.h>
64 #include <openssl/sha.h>
65 #include <openssl/hmac.h>
66 #include <openssl/des.h>
67 #include <openssl/crypto.h>
68 #ifdef HAVE_OPENSSL_IDEA_H
69 #include <openssl/idea.h>
71 #include <openssl/blowfish.h>
72 #ifdef HAVE_OPENSSL_RC5_H
73 #include <openssl/rc5.h>
75 #include <openssl/cast.h>
76 #include <openssl/err.h>
77 #ifdef HAVE_OPENSSL_RIJNDAEL_H
78 #include <openssl/rijndael.h>
80 #include "rijndael-api-fst.h"
82 #ifdef HAVE_OPENSSL_SHA2_H
83 #include <openssl/sha2.h>
92 #include "crypto_openssl.h"
97 * I hate to cast every parameter to des_xx into void *, but it is
98 * necessary for SSLeay/OpenSSL portability. It sucks.
101 #ifdef HAVE_SIGNING_C
102 static int cb_check_cert
__P((int, X509_STORE_CTX
*));
103 static void eay_setgentype
__P((char *, int *));
104 static X509
*mem2x509
__P((vchar_t
*));
107 static caddr_t eay_hmac_init
__P((vchar_t
*, const EVP_MD
*));
109 #ifdef HAVE_SIGNING_C
110 /* X509 Certificate */
112 * convert the string of the subject name into DER
113 * e.g. str = "C=JP, ST=Kanagawa";
116 eay_str2asn1dn(str
, len
)
127 buf
= racoon_malloc(len
+ 1);
129 printf("failed to allocate buffer\n");
132 memcpy(buf
, str
, len
);
134 name
= X509_NAME_new();
138 for (i
= 0; i
< len
; i
++) {
139 if (!value
&& buf
[i
] == '=') {
143 } else if (buf
[i
] == ',' || buf
[i
] == '/') {
146 printf("[%s][%s]\n", field
, value
);
148 if (!X509_NAME_add_entry_by_txt(name
, field
,
149 MBSTRING_ASC
, value
, -1, -1, 0))
151 for (j
= i
+ 1; j
< len
; j
++) {
162 printf("[%s][%s]\n", field
, value
);
164 if (!X509_NAME_add_entry_by_txt(name
, field
,
165 MBSTRING_ASC
, value
, -1, -1, 0))
168 i
= i2d_X509_NAME(name
, NULL
);
175 i
= i2d_X509_NAME(name
, (unsigned char **)&p
);
185 X509_NAME_free(name
);
190 * compare two subjectNames.
196 eay_cmp_asn1dn(n1
, n2
)
199 X509_NAME
*a
= NULL
, *b
= NULL
;
204 if (!d2i_X509_NAME(&a
, (unsigned char **)&p
, n1
->l
))
207 if (!d2i_X509_NAME(&b
, (unsigned char **)&p
, n2
->l
))
210 i
= X509_NAME_cmp(a
, b
);
221 * this functions is derived from apps/verify.c in OpenSSL0.9.5
224 eay_check_x509cert(cert
, CApath
)
228 X509_STORE
*cert_ctx
= NULL
;
229 X509_LOOKUP
*lookup
= NULL
;
231 #if OPENSSL_VERSION_NUMBER >= 0x00905100L
238 /* XXX define only functions required. */
239 #if OPENSSL_VERSION_NUMBER >= 0x00905100L
240 OpenSSL_add_all_algorithms();
242 SSLeay_add_all_algorithms();
245 cert_ctx
= X509_STORE_new();
246 if (cert_ctx
== NULL
)
248 X509_STORE_set_verify_cb_func(cert_ctx
, cb_check_cert
);
250 lookup
= X509_STORE_add_lookup(cert_ctx
, X509_LOOKUP_file());
253 X509_LOOKUP_load_file(lookup
, NULL
, X509_FILETYPE_DEFAULT
); /* XXX */
255 lookup
= X509_STORE_add_lookup(cert_ctx
, X509_LOOKUP_hash_dir());
258 error
= X509_LOOKUP_add_dir(lookup
, CApath
, X509_FILETYPE_PEM
);
263 error
= -1; /* initialized */
265 /* read the certificate to be verified */
266 x509
= mem2x509(cert
);
270 #if OPENSSL_VERSION_NUMBER >= 0x00905100L
271 csc
= X509_STORE_CTX_new();
274 X509_STORE_CTX_init(csc
, cert_ctx
, x509
, NULL
);
275 error
= X509_verify_cert(csc
);
276 X509_STORE_CTX_cleanup(csc
);
278 X509_STORE_CTX_init(&csc
, cert_ctx
, x509
, NULL
);
279 error
= X509_verify_cert(&csc
);
280 X509_STORE_CTX_cleanup(&csc
);
284 * if x509_verify_cert() is successful then the value of error is
287 error
= error
? 0 : -1;
291 printf("%s\n", eay_strerror());
292 if (cert_ctx
!= NULL
)
293 X509_STORE_free(cert_ctx
);
301 * callback function for verifing certificate.
302 * this function is derived from cb() in openssl/apps/s_server.c
305 cb_check_cert(ok
, ctx
)
314 X509_get_subject_name(ctx
->current_cert
),
318 * since we are just checking the certificates, it is
319 * ok if they are self signed. But we should still warn
322 switch (ctx
->error
) {
323 case X509_V_ERR_CERT_HAS_EXPIRED
:
324 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
:
325 #if OPENSSL_VERSION_NUMBER >= 0x00905100L
326 case X509_V_ERR_INVALID_CA
:
327 case X509_V_ERR_PATH_LENGTH_EXCEEDED
:
328 case X509_V_ERR_INVALID_PURPOSE
:
331 log_tag
= LLV_WARNING
;
337 plog(log_tag
, LOCATION
, NULL
,
338 "%s(%d) at depth:%d SubjectName:%s\n",
339 X509_verify_cert_error_string(ctx
->error
),
344 printf("%d: %s(%d) at depth:%d SubjectName:%s\n",
346 X509_verify_cert_error_string(ctx
->error
),
358 * get a subjectAltName from X509 certificate.
361 eay_get_x509asn1subjectname(cert
)
366 vchar_t
*name
= NULL
;
372 x509
= mem2x509(cert
);
376 /* get the length of the name */
377 len
= i2d_X509_NAME(x509
->cert_info
->subject
, NULL
);
383 len
= i2d_X509_NAME(x509
->cert_info
->subject
, &bp
);
390 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
392 printf("%s\n", eay_strerror());
406 * get the subjectAltName from X509 certificate.
407 * the name is terminated by '\0'.
409 #include <openssl/x509v3.h>
411 eay_get_x509subjectaltname(cert
, altname
, type
, pos
)
419 X509V3_EXT_METHOD
*method
= NULL
;
420 STACK_OF(GENERAL_NAME
) *name
;
421 CONF_VALUE
*cval
= NULL
;
422 STACK_OF(CONF_VALUE
) *nval
= NULL
;
428 *type
= GENT_OTHERNAME
;
432 x509
= mem2x509(cert
);
436 i
= X509_get_ext_by_NID(x509
, NID_subject_alt_name
, -1);
439 ext
= X509_get_ext(x509
, i
);
440 method
= X509V3_EXT_get(ext
);
444 bp
= ext
->value
->data
;
445 name
= method
->d2i(NULL
, &bp
, ext
->value
->length
);
449 nval
= method
->i2v(method
, name
, NULL
);
450 method
->ext_free(name
);
455 for (i
= 0; i
< sk_CONF_VALUE_num(nval
); i
++) {
459 cval
= sk_CONF_VALUE_value(nval
, i
);
460 len
= strlen(cval
->value
) + 1; /* '\0' included */
461 *altname
= racoon_malloc(len
);
463 sk_CONF_VALUE_pop_free(nval
, X509V3_conf_free
);
466 strlcpy(*altname
, cval
->value
, len
);
468 /* set type of the name */
469 eay_setgentype(cval
->name
, type
);
472 sk_CONF_VALUE_pop_free(nval
, X509V3_conf_free
);
479 racoon_free(*altname
);
483 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
485 printf("%s\n", eay_strerror());
495 eay_setgentype(name
, type
)
499 /* XXX It's needed effective code */
500 if(!memcmp(name
, "email", strlen("email"))) {
502 } else if(!memcmp(name
, "URI", strlen("URI"))) {
504 } else if(!memcmp(name
, "DNS", strlen("DNS"))) {
506 } else if(!memcmp(name
, "RID", strlen("RID"))) {
508 } else if(!memcmp(name
, "IP", strlen("IP"))) {
511 *type
= GENT_OTHERNAME
;
516 * decode a X509 certificate and make a readable text terminated '\n'.
517 * return the buffer allocated, so must free it later.
520 eay_get_x509text(cert
)
530 x509
= mem2x509(cert
);
534 bio
= BIO_new(BIO_s_mem());
538 error
= X509_print(bio
, x509
);
544 len
= BIO_get_mem_data(bio
, &bp
);
545 text
= racoon_malloc(len
+ 1);
548 memcpy(text
, bp
, len
);
560 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
562 printf("%s\n", eay_strerror());
573 /* get X509 structure from buffer. */
586 x509
= d2i_X509(NULL
, &bp
, cert
->l
);
593 bio
= BIO_new(BIO_s_mem());
596 len
= BIO_write(bio
, cert
->v
, cert
->l
);
599 x509
= PEM_read_bio_X509(bio
, NULL
, NULL
, NULL
);
607 * get a X509 certificate from local file.
608 * a certificate must be PEM format.
610 * path to a certificate.
612 * NULL if error occured
616 eay_get_x509cert(path
)
626 /* Read private key */
627 fp
= fopen(path
, "r");
630 #if OPENSSL_VERSION_NUMBER >= 0x00904100L
631 x509
= PEM_read_X509(fp
, NULL
, NULL
, NULL
);
633 x509
= PEM_read_X509(fp
, NULL
, NULL
);
640 len
= i2d_X509(x509
, NULL
);
647 error
= i2d_X509(x509
, &bp
);
657 * sign a souce by X509 signature.
658 * XXX: to be get hash type from my cert ?
659 * to be handled EVP_dss().
662 eay_get_x509sign(source
, privkey
, cert
)
669 sig
= eay_rsa_sign(source
, privkey
);
675 * check a X509 signature
676 * XXX: to be get hash type from my cert ?
677 * to be handled EVP_dss().
678 * OUT: return -1 when error.
682 eay_check_x509sign(source
, sig
, cert
)
693 x509
= d2i_X509(NULL
, &bp
, cert
->l
);
696 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
701 evp
= X509_get_pubkey(x509
);
703 plog(LLV_ERROR
, LOCATION
, NULL
, "X509_get_pubkey: %s\n", eay_strerror());
707 return eay_rsa_verify(source
, sig
, evp
);
711 * check a signature by signed with PKCS7 certificate.
712 * XXX: to be get hash type from my cert ?
713 * to be handled EVP_dss().
714 * OUT: return -1 when error.
718 eay_check_pkcs7sign(source
, sig
, cert
)
727 BIO
*bio
= BIO_new(BIO_s_mem());
732 error
= BIO_write(bio
, cert
->v
, cert
->l
);
733 if (error
!= cert
->l
)
737 x509
= PEM_read_bio_X509(bio
, NULL
, NULL
, NULL
);
742 evp
= X509_get_pubkey(x509
);
747 /* Verify the signature */
748 /* XXX: to be handled EVP_dss() */
749 EVP_VerifyInit(&md_ctx
, EVP_sha1());
750 EVP_VerifyUpdate(&md_ctx
, source
->v
, source
->l
);
751 error
= EVP_VerifyFinal(&md_ctx
, sig
->v
, sig
->l
, evp
);
762 * get PKCS#1 Private Key of PEM format from local file.
765 eay_get_pkcs1privkey(path
)
769 EVP_PKEY
*evp
= NULL
;
770 vchar_t
*pkey
= NULL
;
775 /* Read private key */
776 fp
= fopen(path
, "r");
780 #if OPENSSL_VERSION_NUMBER >= 0x00904100L
781 evp
= PEM_read_PrivateKey(fp
, NULL
, NULL
, NULL
);
783 evp
= PEM_read_PrivateKey(fp
, NULL
, NULL
);
790 pkeylen
= i2d_PrivateKey(evp
, NULL
);
793 pkey
= vmalloc(pkeylen
);
797 pkeylen
= i2d_PrivateKey(evp
, &bp
);
806 if (error
!= 0 && pkey
!= NULL
) {
815 * get PKCS#1 Public Key of PEM format from local file.
818 eay_get_pkcs1pubkey(path
)
822 EVP_PKEY
*evp
= NULL
;
823 vchar_t
*pkey
= NULL
;
829 /* Read private key */
830 fp
= fopen(path
, "r");
834 #if OPENSSL_VERSION_NUMBER >= 0x00904100L
835 x509
= PEM_read_X509(fp
, NULL
, NULL
, NULL
);
837 x509
= PEM_read_X509(fp
, NULL
, NULL
);
844 /* Get public key - eay */
845 evp
= X509_get_pubkey(x509
);
849 pkeylen
= i2d_PublicKey(evp
, NULL
);
852 pkey
= vmalloc(pkeylen
);
856 pkeylen
= i2d_PublicKey(evp
, &bp
);
864 if (error
!= 0 && pkey
!= NULL
) {
874 eay_rsa_sign(src
, privkey
)
875 vchar_t
*src
, *privkey
;
878 u_char
*bp
= privkey
->v
;
881 int pad
= RSA_PKCS1_PADDING
;
883 /* XXX to be handled EVP_PKEY_DSA */
884 evp
= d2i_PrivateKey(EVP_PKEY_RSA
, NULL
, &bp
, privkey
->l
);
888 /* XXX: to be handled EVP_dss() */
889 /* XXX: Where can I get such parameters ? From my cert ? */
891 len
= RSA_size(evp
->pkey
.rsa
);
897 len
= RSA_private_encrypt(src
->l
, src
->v
, sig
->v
, evp
->pkey
.rsa
, pad
);
899 if (len
== 0 || len
!= sig
->l
) {
908 eay_rsa_verify(src
, sig
, evp
)
912 vchar_t
*xbuf
= NULL
;
913 int pad
= RSA_PKCS1_PADDING
;
917 len
= RSA_size(evp
->pkey
.rsa
);
922 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
928 len
= RSA_public_decrypt(sig
->l
, sig
->v
, xbuf
->v
, evp
->pkey
.rsa
, pad
);
930 if (len
== 0 || len
!= src
->l
)
931 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
934 if (len
== 0 || len
!= src
->l
) {
939 error
= memcmp(src
->v
, xbuf
->v
, src
->l
);
949 * MUST load ERR_load_crypto_strings() first.
954 static char ebuf
[512];
958 #if OPENSSL_VERSION_NUMBER >= 0x00904100L
959 const char *file
, *data
;
966 es
= CRYPTO_thread_id();
968 while ((l
= ERR_get_error_line_data(&file
, &line
, &data
, &flags
)) != 0){
969 n
= snprintf(ebuf
+ len
, sizeof(ebuf
) - len
,
971 es
, ERR_error_string(l
, buf
), file
, line
,
972 (flags
& ERR_TXT_STRING
) ? data
: "");
973 if (n
< 0 || n
>= sizeof(ebuf
) - len
)
976 if (sizeof(ebuf
) < len
)
986 ERR_load_crypto_strings();
993 eay_des_encrypt(data
, key
, iv
)
994 vchar_t
*data
, *key
, *iv
;
999 if (des_key_sched((void *)key
->v
, ks
) != 0)
1002 /* allocate buffer for result */
1003 if ((res
= vmalloc(data
->l
)) == NULL
)
1006 /* decryption data */
1007 des_cbc_encrypt((void *)data
->v
, (void *)res
->v
, data
->l
,
1008 ks
, (void *)iv
->v
, DES_ENCRYPT
);
1014 eay_des_decrypt(data
, key
, iv
)
1015 vchar_t
*data
, *key
, *iv
;
1018 des_key_schedule ks
;
1020 if (des_key_sched((void *)key
->v
, ks
) != 0)
1023 /* allocate buffer for result */
1024 if ((res
= vmalloc(data
->l
)) == NULL
)
1027 /* decryption data */
1028 des_cbc_encrypt((void *)data
->v
, (void *)res
->v
, data
->l
,
1029 ks
, (void *)iv
->v
, DES_DECRYPT
);
1035 eay_des_weakkey(key
)
1038 return des_is_weak_key((void *)key
->v
);
1045 if (len
!= 0 && len
!= 64)
1050 #ifdef HAVE_OPENSSL_IDEA_H
1055 eay_idea_encrypt(data
, key
, iv
)
1056 vchar_t
*data
, *key
, *iv
;
1059 IDEA_KEY_SCHEDULE ks
;
1061 idea_set_encrypt_key(key
->v
, &ks
);
1063 /* allocate buffer for result */
1064 if ((res
= vmalloc(data
->l
)) == NULL
)
1067 /* decryption data */
1068 idea_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1069 &ks
, iv
->v
, IDEA_ENCRYPT
);
1075 eay_idea_decrypt(data
, key
, iv
)
1076 vchar_t
*data
, *key
, *iv
;
1079 IDEA_KEY_SCHEDULE ks
, dks
;
1081 idea_set_encrypt_key(key
->v
, &ks
);
1082 idea_set_decrypt_key(&ks
, &dks
);
1084 /* allocate buffer for result */
1085 if ((res
= vmalloc(data
->l
)) == NULL
)
1088 /* decryption data */
1089 idea_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1090 &dks
, iv
->v
, IDEA_DECRYPT
);
1096 eay_idea_weakkey(key
)
1103 eay_idea_keylen(len
)
1106 if (len
!= 0 && len
!= 128)
1116 eay_bf_encrypt(data
, key
, iv
)
1117 vchar_t
*data
, *key
, *iv
;
1122 BF_set_key(&ks
, key
->l
, key
->v
);
1124 /* allocate buffer for result */
1125 if ((res
= vmalloc(data
->l
)) == NULL
)
1128 /* decryption data */
1129 BF_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1130 &ks
, iv
->v
, BF_ENCRYPT
);
1136 eay_bf_decrypt(data
, key
, iv
)
1137 vchar_t
*data
, *key
, *iv
;
1142 BF_set_key(&ks
, key
->l
, key
->v
);
1144 /* allocate buffer for result */
1145 if ((res
= vmalloc(data
->l
)) == NULL
)
1148 /* decryption data */
1149 BF_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1150 &ks
, iv
->v
, BF_DECRYPT
);
1159 return 0; /* XXX to be done. refer to RFC 2451 */
1168 if (len
< 40 || len
> 448)
1170 return (len
+ 7) / 8;
1173 #ifdef HAVE_OPENSSL_RC5_H
1178 eay_rc5_encrypt(data
, key
, iv
)
1179 vchar_t
*data
, *key
, *iv
;
1184 /* in RFC 2451, there is information about the number of round. */
1185 RC5_32_set_key(&ks
, key
->l
, key
->v
, 16);
1187 /* allocate buffer for result */
1188 if ((res
= vmalloc(data
->l
)) == NULL
)
1191 /* decryption data */
1192 RC5_32_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1193 &ks
, iv
->v
, RC5_ENCRYPT
);
1199 eay_rc5_decrypt(data
, key
, iv
)
1200 vchar_t
*data
, *key
, *iv
;
1205 /* in RFC 2451, there is information about the number of round. */
1206 RC5_32_set_key(&ks
, key
->l
, key
->v
, 16);
1208 /* allocate buffer for result */
1209 if ((res
= vmalloc(data
->l
)) == NULL
)
1212 /* decryption data */
1213 RC5_32_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1214 &ks
, iv
->v
, RC5_DECRYPT
);
1220 eay_rc5_weakkey(key
)
1223 return 0; /* No known weak keys when used with 16 rounds. */
1233 if (len
< 40 || len
> 2040)
1235 return (len
+ 7) / 8;
1243 eay_3des_encrypt(data
, key
, iv
)
1244 vchar_t
*data
, *key
, *iv
;
1247 des_key_schedule ks1
, ks2
, ks3
;
1252 if (des_key_sched((void *)key
->v
, ks1
) != 0)
1254 if (des_key_sched((void *)(key
->v
+ 8), ks2
) != 0)
1256 if (des_key_sched((void *)(key
->v
+ 16), ks3
) != 0)
1259 /* allocate buffer for result */
1260 if ((res
= vmalloc(data
->l
)) == NULL
)
1263 /* decryption data */
1264 des_ede3_cbc_encrypt((void *)data
->v
, (void *)res
->v
, data
->l
,
1265 ks1
, ks2
, ks3
, (void *)iv
->v
, DES_ENCRYPT
);
1271 eay_3des_decrypt(data
, key
, iv
)
1272 vchar_t
*data
, *key
, *iv
;
1275 des_key_schedule ks1
, ks2
, ks3
;
1280 if (des_key_sched((void *)key
->v
, ks1
) != 0)
1282 if (des_key_sched((void *)(key
->v
+ 8), ks2
) != 0)
1284 if (des_key_sched((void *)(key
->v
+ 16), ks3
) != 0)
1287 /* allocate buffer for result */
1288 if ((res
= vmalloc(data
->l
)) == NULL
)
1291 /* decryption data */
1292 des_ede3_cbc_encrypt((void *)data
->v
, (void *)res
->v
, data
->l
,
1293 ks1
, ks2
, ks3
, (void *)iv
->v
, DES_DECRYPT
);
1299 eay_3des_weakkey(key
)
1305 return (des_is_weak_key((void *)key
->v
)
1306 || des_is_weak_key((void *)(key
->v
+ 8))
1307 || des_is_weak_key((void *)(key
->v
+ 16)));
1311 eay_3des_keylen(len
)
1314 if (len
!= 0 && len
!= 192)
1323 eay_cast_encrypt(data
, key
, iv
)
1324 vchar_t
*data
, *key
, *iv
;
1329 CAST_set_key(&ks
, key
->l
, key
->v
);
1331 /* allocate buffer for result */
1332 if ((res
= vmalloc(data
->l
)) == NULL
)
1335 /* decryption data */
1336 CAST_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1337 &ks
, iv
->v
, DES_ENCRYPT
);
1343 eay_cast_decrypt(data
, key
, iv
)
1344 vchar_t
*data
, *key
, *iv
;
1349 CAST_set_key(&ks
, key
->l
, key
->v
);
1351 /* allocate buffer for result */
1352 if ((res
= vmalloc(data
->l
)) == NULL
)
1355 /* decryption data */
1356 CAST_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1357 &ks
, iv
->v
, DES_DECRYPT
);
1363 eay_cast_weakkey(key
)
1366 return 0; /* No known weak keys. */
1370 eay_cast_keylen(len
)
1375 if (len
< 40 || len
> 128)
1377 return (len
+ 7) / 8;
1384 eay_aes_encrypt(data
, key
, iv
)
1385 vchar_t
*data
, *key
, *iv
;
1391 memset(&k
, 0, sizeof(k
));
1392 if (rijndael_makeKey(&k
, DIR_ENCRYPT
, key
->l
<< 3, key
->v
) < 0)
1395 /* allocate buffer for result */
1396 if ((res
= vmalloc(data
->l
)) == NULL
)
1399 /* encryption data */
1400 memset(&c
, 0, sizeof(c
));
1401 if (rijndael_cipherInit(&c
, MODE_CBC
, iv
->v
) < 0)
1403 if (rijndael_blockEncrypt(&c
, &k
, data
->v
, data
->l
<< 3, res
->v
) < 0)
1410 eay_aes_decrypt(data
, key
, iv
)
1411 vchar_t
*data
, *key
, *iv
;
1417 memset(&k
, 0, sizeof(k
));
1418 if (rijndael_makeKey(&k
, DIR_DECRYPT
, key
->l
<< 3, key
->v
) < 0)
1421 /* allocate buffer for result */
1422 if ((res
= vmalloc(data
->l
)) == NULL
)
1425 /* decryption data */
1426 memset(&c
, 0, sizeof(c
));
1427 if (rijndael_cipherInit(&c
, MODE_CBC
, iv
->v
) < 0)
1429 if (rijndael_blockDecrypt(&c
, &k
, data
->v
, data
->l
<< 3, res
->v
) < 0)
1436 eay_aes_weakkey(key
)
1448 if (len
!= 128 && len
!= 192 && len
!= 256)
1453 /* for ipsec part */
1467 eay_twofish_keylen(len
)
1470 if (len
< 0 || len
> 256)
1476 eay_null_keylen(len
)
1486 eay_hmac_init(key
, md
)
1490 HMAC_CTX
*c
= racoon_malloc(sizeof(*c
));
1493 HMAC_Init(c
, key
->v
, key
->l
, md
);
1502 eay_hmacsha2_512_one(key
, data
)
1503 vchar_t
*key
, *data
;
1508 ctx
= eay_hmacsha2_512_init(key
);
1509 eay_hmacsha2_512_update(ctx
, data
);
1510 res
= eay_hmacsha2_512_final(ctx
);
1516 eay_hmacsha2_512_init(key
)
1519 return eay_hmac_init(key
, EVP_sha2_512());
1523 eay_hmacsha2_512_update(c
, data
)
1527 HMAC_Update((HMAC_CTX
*)c
, data
->v
, data
->l
);
1531 eay_hmacsha2_512_final(c
)
1537 if ((res
= vmalloc(SHA512_DIGEST_LENGTH
)) == 0)
1540 HMAC_Final((HMAC_CTX
*)c
, res
->v
, &l
);
1542 HMAC_CTX_cleanup(c
);
1543 (void)racoon_free(c
);
1545 if (SHA512_DIGEST_LENGTH
!= res
->l
) {
1547 plog(LLV_ERROR
, LOCATION
, NULL
,
1548 "hmac sha2_512 length mismatch %d.\n", res
->l
);
1550 printf("hmac sha2_512 length mismatch %d.\n", res
->l
);
1563 eay_hmacsha2_384_one(key
, data
)
1564 vchar_t
*key
, *data
;
1569 ctx
= eay_hmacsha2_384_init(key
);
1570 eay_hmacsha2_384_update(ctx
, data
);
1571 res
= eay_hmacsha2_384_final(ctx
);
1577 eay_hmacsha2_384_init(key
)
1580 return eay_hmac_init(key
, EVP_sha2_384());
1584 eay_hmacsha2_384_update(c
, data
)
1588 HMAC_Update((HMAC_CTX
*)c
, data
->v
, data
->l
);
1592 eay_hmacsha2_384_final(c
)
1598 if ((res
= vmalloc(SHA384_DIGEST_LENGTH
)) == 0)
1601 HMAC_Final((HMAC_CTX
*)c
, res
->v
, &l
);
1603 HMAC_CTX_cleanup(c
);
1604 (void)racoon_free(c
);
1606 if (SHA384_DIGEST_LENGTH
!= res
->l
) {
1608 plog(LLV_ERROR
, LOCATION
, NULL
,
1609 "hmac sha2_384 length mismatch %d.\n", res
->l
);
1611 printf("hmac sha2_384 length mismatch %d.\n", res
->l
);
1624 eay_hmacsha2_256_one(key
, data
)
1625 vchar_t
*key
, *data
;
1630 ctx
= eay_hmacsha2_256_init(key
);
1631 eay_hmacsha2_256_update(ctx
, data
);
1632 res
= eay_hmacsha2_256_final(ctx
);
1638 eay_hmacsha2_256_init(key
)
1641 return eay_hmac_init(key
, EVP_sha2_256());
1645 eay_hmacsha2_256_update(c
, data
)
1649 HMAC_Update((HMAC_CTX
*)c
, data
->v
, data
->l
);
1653 eay_hmacsha2_256_final(c
)
1659 if ((res
= vmalloc(SHA256_DIGEST_LENGTH
)) == 0)
1662 HMAC_Final((HMAC_CTX
*)c
, res
->v
, &l
);
1664 HMAC_CTX_cleanup(c
);
1665 (void)racoon_free(c
);
1667 if (SHA256_DIGEST_LENGTH
!= res
->l
) {
1669 plog(LLV_ERROR
, LOCATION
, NULL
,
1670 "hmac sha2_256 length mismatch %d.\n", res
->l
);
1672 printf("hmac sha2_256 length mismatch %d.\n", res
->l
);
1685 eay_hmacsha1_one(key
, data
)
1686 vchar_t
*key
, *data
;
1691 ctx
= eay_hmacsha1_init(key
);
1692 eay_hmacsha1_update(ctx
, data
);
1693 res
= eay_hmacsha1_final(ctx
);
1699 eay_hmacsha1_init(key
)
1702 return eay_hmac_init(key
, EVP_sha1());
1706 eay_hmacsha1_update(c
, data
)
1710 HMAC_Update((HMAC_CTX
*)c
, data
->v
, data
->l
);
1714 eay_hmacsha1_final(c
)
1720 if ((res
= vmalloc(SHA_DIGEST_LENGTH
)) == 0)
1723 HMAC_Final((HMAC_CTX
*)c
, res
->v
, &l
);
1725 HMAC_CTX_cleanup(c
);
1726 (void)racoon_free(c
);
1728 if (SHA_DIGEST_LENGTH
!= res
->l
) {
1730 plog(LLV_ERROR
, LOCATION
, NULL
,
1731 "hmac sha1 length mismatch %d.\n", res
->l
);
1733 printf("hmac sha1 length mismatch %d.\n", res
->l
);
1746 eay_hmacmd5_one(key
, data
)
1747 vchar_t
*key
, *data
;
1752 ctx
= eay_hmacmd5_init(key
);
1753 eay_hmacmd5_update(ctx
, data
);
1754 res
= eay_hmacmd5_final(ctx
);
1760 eay_hmacmd5_init(key
)
1763 return eay_hmac_init(key
, EVP_md5());
1767 eay_hmacmd5_update(c
, data
)
1771 HMAC_Update((HMAC_CTX
*)c
, data
->v
, data
->l
);
1775 eay_hmacmd5_final(c
)
1781 if ((res
= vmalloc(MD5_DIGEST_LENGTH
)) == 0)
1784 HMAC_Final((HMAC_CTX
*)c
, res
->v
, &l
);
1786 HMAC_CTX_cleanup(c
);
1787 (void)racoon_free(c
);
1789 if (MD5_DIGEST_LENGTH
!= res
->l
) {
1791 plog(LLV_ERROR
, LOCATION
, NULL
,
1792 "hmac md5 length mismatch %d.\n", res
->l
);
1794 printf("hmac md5 length mismatch %d.\n", res
->l
);
1804 * SHA2-512 functions
1809 SHA512_CTX
*c
= racoon_malloc(sizeof(*c
));
1817 eay_sha2_512_update(c
, data
)
1821 SHA512_Update((SHA512_CTX
*)c
, data
->v
, data
->l
);
1827 eay_sha2_512_final(c
)
1832 if ((res
= vmalloc(SHA512_DIGEST_LENGTH
)) == 0)
1835 SHA512_Final(res
->v
, (SHA512_CTX
*)c
);
1836 (void)racoon_free(c
);
1842 eay_sha2_512_one(data
)
1848 ctx
= eay_sha2_512_init();
1849 eay_sha2_512_update(ctx
, data
);
1850 res
= eay_sha2_512_final(ctx
);
1856 eay_sha2_512_hashlen()
1858 return SHA512_DIGEST_LENGTH
<< 3;
1862 * SHA2-384 functions
1867 SHA384_CTX
*c
= racoon_malloc(sizeof(*c
));
1875 eay_sha2_384_update(c
, data
)
1879 SHA384_Update((SHA384_CTX
*)c
, data
->v
, data
->l
);
1885 eay_sha2_384_final(c
)
1890 if ((res
= vmalloc(SHA384_DIGEST_LENGTH
)) == 0)
1893 SHA384_Final(res
->v
, (SHA384_CTX
*)c
);
1894 (void)racoon_free(c
);
1900 eay_sha2_384_one(data
)
1906 ctx
= eay_sha2_384_init();
1907 eay_sha2_384_update(ctx
, data
);
1908 res
= eay_sha2_384_final(ctx
);
1914 eay_sha2_384_hashlen()
1916 return SHA384_DIGEST_LENGTH
<< 3;
1920 * SHA2-256 functions
1925 SHA256_CTX
*c
= racoon_malloc(sizeof(*c
));
1933 eay_sha2_256_update(c
, data
)
1937 SHA256_Update((SHA256_CTX
*)c
, data
->v
, data
->l
);
1943 eay_sha2_256_final(c
)
1948 if ((res
= vmalloc(SHA256_DIGEST_LENGTH
)) == 0)
1951 SHA256_Final(res
->v
, (SHA256_CTX
*)c
);
1952 (void)racoon_free(c
);
1958 eay_sha2_256_one(data
)
1964 ctx
= eay_sha2_256_init();
1965 eay_sha2_256_update(ctx
, data
);
1966 res
= eay_sha2_256_final(ctx
);
1972 eay_sha2_256_hashlen()
1974 return SHA256_DIGEST_LENGTH
<< 3;
1983 SHA_CTX
*c
= racoon_malloc(sizeof(*c
));
1991 eay_sha1_update(c
, data
)
1995 SHA1_Update((SHA_CTX
*)c
, data
->v
, data
->l
);
2006 if ((res
= vmalloc(SHA_DIGEST_LENGTH
)) == 0)
2009 SHA1_Final(res
->v
, (SHA_CTX
*)c
);
2010 (void)racoon_free(c
);
2022 ctx
= eay_sha1_init();
2023 eay_sha1_update(ctx
, data
);
2024 res
= eay_sha1_final(ctx
);
2032 return SHA_DIGEST_LENGTH
<< 3;
2041 MD5_CTX
*c
= racoon_malloc(sizeof(*c
));
2049 eay_md5_update(c
, data
)
2053 MD5_Update((MD5_CTX
*)c
, data
->v
, data
->l
);
2064 if ((res
= vmalloc(MD5_DIGEST_LENGTH
)) == 0)
2067 MD5_Final(res
->v
, (MD5_CTX
*)c
);
2068 (void)racoon_free(c
);
2080 ctx
= eay_md5_init();
2081 eay_md5_update(ctx
, data
);
2082 res
= eay_md5_final(ctx
);
2090 return MD5_DIGEST_LENGTH
<< 3;
2095 * size: number of bytes.
2098 eay_set_random(size
)
2104 if ((r
= BN_new()) == NULL
)
2106 BN_rand(r
, size
* 8, 0, 0);
2117 eay_dh_generate(prime
, g
, publen
, pub
, priv
)
2118 vchar_t
*prime
, **pub
, **priv
;
2127 /* pre-process to generate number */
2128 if (eay_v2bn(&p
, prime
) < 0)
2131 if ((dh
= DH_new()) == NULL
)
2134 p
= NULL
; /* p is now part of dh structure */
2136 if ((dh
->g
= BN_new()) == NULL
)
2138 if (!BN_set_word(dh
->g
, g
))
2142 dh
->length
= publen
;
2144 /* generate public and private number */
2145 if (!DH_generate_key(dh
))
2148 /* copy results to buffers */
2149 if (eay_bn2v(pub
, dh
->pub_key
) < 0)
2151 if (eay_bn2v(priv
, dh
->priv_key
) < 0) {
2167 eay_dh_compute(prime
, g
, pub
, priv
, pub2
, key
)
2168 vchar_t
*prime
, *pub
, *priv
, *pub2
, **key
;
2171 BIGNUM
*dh_pub
= NULL
;
2177 /* make public number to compute */
2178 if (eay_v2bn(&dh_pub
, pub2
) < 0)
2181 /* make DH structure */
2182 if ((dh
= DH_new()) == NULL
)
2184 if (eay_v2bn(&dh
->p
, prime
) < 0)
2186 if (eay_v2bn(&dh
->pub_key
, pub
) < 0)
2188 if (eay_v2bn(&dh
->priv_key
, priv
) < 0)
2190 dh
->length
= pub2
->l
* 8;
2193 if ((dh
->g
= BN_new()) == NULL
)
2195 if (!BN_set_word(dh
->g
, g
))
2198 if ((v
= (caddr_t
)racoon_calloc(prime
->l
, sizeof(u_char
))) == NULL
)
2200 if ((l
= DH_compute_key(v
, dh_pub
, dh
)) == -1)
2202 memcpy((*key
)->v
+ (prime
->l
- l
), v
, l
);
2222 if ((*bn
= BN_bin2bn(var
->v
, var
->l
, NULL
)) == NULL
)
2229 * convert vchar_t <-> BIGNUM.
2231 * vchar_t: unit is u_char, network endian, most significant byte first.
2232 * BIGNUM: unit is BN_ULONG, each of BN_ULONG is in host endian,
2233 * least significant BN_ULONG must come first.
2235 * hex value of "0x3ffe050104" is represented as follows:
2236 * vchar_t: 3f fe 05 01 04
2237 * BIGNUM (BN_ULONG = u_int8_t): 04 01 05 fe 3f
2238 * BIGNUM (BN_ULONG = u_int16_t): 0x0104 0xfe05 0x003f
2239 * BIGNUM (BN_ULONG = u_int32_t_t): 0xfe050104 0x0000003f
2255 l
= (var
->l
* 8 + BN_BITS2
- 1) / BN_BITS2
;
2256 if (bn_expand(*bn
, l
* BN_BITS2
) == NULL
)
2260 /* scan from least significant byte */
2261 p
= (u_char
*)var
->v
;
2262 q
= (u_char
*)(var
->v
+ var
->l
);
2268 num
= num
| ((BN_ULONG
)*q
<< (l
++ * 8));
2269 if (l
== BN_BYTES
) {
2291 *var
= vmalloc(bn
->top
* BN_BYTES
);
2295 (*var
)->l
= BN_bn2bin(bn
, (*var
)->v
);
2303 return SSLeay_version(SSLEAY_VERSION
);