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
);
258 lookup
= X509_STORE_add_lookup(cert_ctx
, X509_LOOKUP_file());
261 X509_LOOKUP_load_file(lookup
, NULL
, X509_FILETYPE_DEFAULT
); /* XXX */
263 lookup
= X509_STORE_add_lookup(cert_ctx
, X509_LOOKUP_hash_dir());
266 error
= X509_LOOKUP_add_dir(lookup
, CApath
, X509_FILETYPE_PEM
);
271 error
= -1; /* initialized */
273 /* read the certificate to be verified */
274 x509
= mem2x509(cert
);
278 #if OPENSSL_VERSION_NUMBER >= 0x00905100L
279 csc
= X509_STORE_CTX_new();
282 X509_STORE_CTX_init(csc
, cert_ctx
, x509
, NULL
);
284 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
285 X509_STORE_CTX_set_flags(csc
, X509_V_FLAG_CRL_CHECK
);
286 X509_STORE_CTX_set_flags(csc
, X509_V_FLAG_CRL_CHECK_ALL
);
289 error
= X509_verify_cert(csc
);
290 X509_STORE_CTX_cleanup(csc
);
292 X509_STORE_CTX_init(&csc
, cert_ctx
, x509
, NULL
);
293 error
= X509_verify_cert(&csc
);
294 X509_STORE_CTX_cleanup(&csc
);
298 * if x509_verify_cert() is successful then the value of error is
301 error
= error
? 0 : -1;
305 printf("%s\n", eay_strerror());
306 if (cert_ctx
!= NULL
)
307 X509_STORE_free(cert_ctx
);
315 * callback function for verifing certificate.
316 * Derived from cb() in openssl/apps/s_server.c
318 * This one is called for certificates obtained from
319 * 'peers_certfile' directive.
322 cb_check_cert_local(ok
, ctx
)
331 X509_get_subject_name(ctx
->current_cert
),
335 * since we are just checking the certificates, it is
336 * ok if they are self signed. But we should still warn
339 switch (ctx
->error
) {
340 case X509_V_ERR_CERT_HAS_EXPIRED
:
341 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
:
342 #if OPENSSL_VERSION_NUMBER >= 0x00905100L
343 case X509_V_ERR_INVALID_PURPOSE
:
344 case X509_V_ERR_UNABLE_TO_GET_CRL
:
347 log_tag
= LLV_WARNING
;
356 plog(log_tag
, LOCATION
, NULL
,
357 "%s(%d) at depth:%d SubjectName:%s\n",
358 X509_verify_cert_error_string(ctx
->error
),
363 printf("%d: %s(%d) at depth:%d SubjectName:%s\n",
365 X509_verify_cert_error_string(ctx
->error
),
377 * Similar to cb_check_cert_local() but this one is called
378 * for certificates obtained from the IKE payload.
381 cb_check_cert_remote(ok
, ctx
)
390 X509_get_subject_name(ctx
->current_cert
),
393 switch (ctx
->error
) {
394 case X509_V_ERR_UNABLE_TO_GET_CRL
:
396 log_tag
= LLV_WARNING
;
402 plog(log_tag
, LOCATION
, NULL
,
403 "%s(%d) at depth:%d SubjectName:%s\n",
404 X509_verify_cert_error_string(ctx
->error
),
409 printf("%d: %s(%d) at depth:%d SubjectName:%s\n",
411 X509_verify_cert_error_string(ctx
->error
),
423 * get a subjectAltName from X509 certificate.
426 eay_get_x509asn1subjectname(cert
)
431 vchar_t
*name
= NULL
;
437 x509
= mem2x509(cert
);
441 /* get the length of the name */
442 len
= i2d_X509_NAME(x509
->cert_info
->subject
, NULL
);
448 len
= i2d_X509_NAME(x509
->cert_info
->subject
, &bp
);
455 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
457 printf("%s\n", eay_strerror());
471 * get the subjectAltName from X509 certificate.
472 * the name is terminated by '\0'.
474 #include <openssl/x509v3.h>
476 eay_get_x509subjectaltname(cert
, altname
, type
, pos
)
484 X509V3_EXT_METHOD
*method
= NULL
;
485 STACK_OF(GENERAL_NAME
) *name
;
486 CONF_VALUE
*cval
= NULL
;
487 STACK_OF(CONF_VALUE
) *nval
= NULL
;
493 *type
= GENT_OTHERNAME
;
497 x509
= mem2x509(cert
);
501 i
= X509_get_ext_by_NID(x509
, NID_subject_alt_name
, -1);
504 ext
= X509_get_ext(x509
, i
);
505 method
= X509V3_EXT_get(ext
);
509 bp
= ext
->value
->data
;
510 name
= method
->d2i(NULL
, &bp
, ext
->value
->length
);
514 nval
= method
->i2v(method
, name
, NULL
);
515 method
->ext_free(name
);
520 for (i
= 0; i
< sk_CONF_VALUE_num(nval
); i
++) {
524 cval
= sk_CONF_VALUE_value(nval
, i
);
525 len
= strlen(cval
->value
) + 1; /* '\0' included */
526 *altname
= racoon_malloc(len
);
528 sk_CONF_VALUE_pop_free(nval
, X509V3_conf_free
);
531 strlcpy(*altname
, cval
->value
, len
);
533 /* set type of the name */
534 eay_setgentype(cval
->name
, type
);
537 sk_CONF_VALUE_pop_free(nval
, X509V3_conf_free
);
544 racoon_free(*altname
);
548 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
550 printf("%s\n", eay_strerror());
560 eay_setgentype(name
, type
)
564 /* XXX It's needed effective code */
565 if(!memcmp(name
, "email", strlen("email"))) {
567 } else if(!memcmp(name
, "URI", strlen("URI"))) {
569 } else if(!memcmp(name
, "DNS", strlen("DNS"))) {
571 } else if(!memcmp(name
, "RID", strlen("RID"))) {
573 } else if(!memcmp(name
, "IP", strlen("IP"))) {
576 *type
= GENT_OTHERNAME
;
581 * decode a X509 certificate and make a readable text terminated '\n'.
582 * return the buffer allocated, so must free it later.
585 eay_get_x509text(cert
)
595 x509
= mem2x509(cert
);
599 bio
= BIO_new(BIO_s_mem());
603 error
= X509_print(bio
, x509
);
609 len
= BIO_get_mem_data(bio
, &bp
);
610 text
= racoon_malloc(len
+ 1);
613 memcpy(text
, bp
, len
);
625 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
627 printf("%s\n", eay_strerror());
638 /* get X509 structure from buffer. */
651 x509
= d2i_X509(NULL
, &bp
, cert
->l
);
658 bio
= BIO_new(BIO_s_mem());
661 len
= BIO_write(bio
, cert
->v
, cert
->l
);
664 x509
= PEM_read_bio_X509(bio
, NULL
, NULL
, NULL
);
672 * get a X509 certificate from local file.
673 * a certificate must be PEM format.
675 * path to a certificate.
677 * NULL if error occured
681 eay_get_x509cert(path
)
691 /* Read private key */
692 fp
= fopen(path
, "r");
695 #if OPENSSL_VERSION_NUMBER >= 0x00904100L
696 x509
= PEM_read_X509(fp
, NULL
, NULL
, NULL
);
698 x509
= PEM_read_X509(fp
, NULL
, NULL
);
705 len
= i2d_X509(x509
, NULL
);
712 error
= i2d_X509(x509
, &bp
);
722 * sign a souce by X509 signature.
723 * XXX: to be get hash type from my cert ?
724 * to be handled EVP_dss().
727 eay_get_x509sign(source
, privkey
, cert
)
734 sig
= eay_rsa_sign(source
, privkey
);
740 * check a X509 signature
741 * XXX: to be get hash type from my cert ?
742 * to be handled EVP_dss().
743 * OUT: return -1 when error.
747 eay_check_x509sign(source
, sig
, cert
)
758 x509
= d2i_X509(NULL
, &bp
, cert
->l
);
761 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
766 evp
= X509_get_pubkey(x509
);
769 plog(LLV_ERROR
, LOCATION
, NULL
, "X509_get_pubkey: %s\n", eay_strerror());
774 return eay_rsa_verify(source
, sig
, evp
);
778 * check a signature by signed with PKCS7 certificate.
779 * XXX: to be get hash type from my cert ?
780 * to be handled EVP_dss().
781 * OUT: return -1 when error.
785 eay_check_pkcs7sign(source
, sig
, cert
)
794 BIO
*bio
= BIO_new(BIO_s_mem());
799 error
= BIO_write(bio
, cert
->v
, cert
->l
);
800 if (error
!= cert
->l
)
804 x509
= PEM_read_bio_X509(bio
, NULL
, NULL
, NULL
);
809 evp
= X509_get_pubkey(x509
);
814 /* Verify the signature */
815 /* XXX: to be handled EVP_dss() */
816 EVP_VerifyInit(&md_ctx
, EVP_sha1());
817 EVP_VerifyUpdate(&md_ctx
, source
->v
, source
->l
);
818 error
= EVP_VerifyFinal(&md_ctx
, sig
->v
, sig
->l
, evp
);
829 * get PKCS#1 Private Key of PEM format from local file.
832 eay_get_pkcs1privkey(path
)
836 EVP_PKEY
*evp
= NULL
;
837 vchar_t
*pkey
= NULL
;
842 /* Read private key */
843 fp
= fopen(path
, "r");
847 #if OPENSSL_VERSION_NUMBER >= 0x00904100L
848 evp
= PEM_read_PrivateKey(fp
, NULL
, NULL
, NULL
);
850 evp
= PEM_read_PrivateKey(fp
, NULL
, NULL
);
857 pkeylen
= i2d_PrivateKey(evp
, NULL
);
860 pkey
= vmalloc(pkeylen
);
864 pkeylen
= i2d_PrivateKey(evp
, &bp
);
873 if (error
!= 0 && pkey
!= NULL
) {
882 * get PKCS#1 Public Key of PEM format from local file.
885 eay_get_pkcs1pubkey(path
)
889 EVP_PKEY
*evp
= NULL
;
890 vchar_t
*pkey
= NULL
;
896 /* Read private key */
897 fp
= fopen(path
, "r");
901 #if OPENSSL_VERSION_NUMBER >= 0x00904100L
902 x509
= PEM_read_X509(fp
, NULL
, NULL
, NULL
);
904 x509
= PEM_read_X509(fp
, NULL
, NULL
);
911 /* Get public key - eay */
912 evp
= X509_get_pubkey(x509
);
916 pkeylen
= i2d_PublicKey(evp
, NULL
);
919 pkey
= vmalloc(pkeylen
);
923 pkeylen
= i2d_PublicKey(evp
, &bp
);
931 if (error
!= 0 && pkey
!= NULL
) {
941 eay_rsa_sign(src
, privkey
)
942 vchar_t
*src
, *privkey
;
945 u_char
*bp
= privkey
->v
;
948 int pad
= RSA_PKCS1_PADDING
;
950 /* XXX to be handled EVP_PKEY_DSA */
951 evp
= d2i_PrivateKey(EVP_PKEY_RSA
, NULL
, &bp
, privkey
->l
);
955 /* XXX: to be handled EVP_dss() */
956 /* XXX: Where can I get such parameters ? From my cert ? */
958 len
= RSA_size(evp
->pkey
.rsa
);
964 len
= RSA_private_encrypt(src
->l
, src
->v
, sig
->v
, evp
->pkey
.rsa
, pad
);
966 if (len
== 0 || len
!= sig
->l
) {
975 eay_rsa_verify(src
, sig
, evp
)
979 vchar_t
*xbuf
= NULL
;
980 int pad
= RSA_PKCS1_PADDING
;
984 len
= RSA_size(evp
->pkey
.rsa
);
989 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
995 len
= RSA_public_decrypt(sig
->l
, sig
->v
, xbuf
->v
, evp
->pkey
.rsa
, pad
);
997 if (len
== 0 || len
!= src
->l
)
998 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
1001 if (len
== 0 || len
!= src
->l
) {
1006 error
= memcmp(src
->v
, xbuf
->v
, src
->l
);
1016 * MUST load ERR_load_crypto_strings() first.
1021 static char ebuf
[512];
1025 #if OPENSSL_VERSION_NUMBER >= 0x00904100L
1026 const char *file
, *data
;
1033 es
= CRYPTO_thread_id();
1035 while ((l
= ERR_get_error_line_data(&file
, &line
, &data
, &flags
)) != 0){
1036 n
= snprintf(ebuf
+ len
, sizeof(ebuf
) - len
,
1038 es
, ERR_error_string(l
, buf
), file
, line
,
1039 (flags
& ERR_TXT_STRING
) ? data
: "");
1040 if (n
< 0 || n
>= sizeof(ebuf
) - len
)
1043 if (sizeof(ebuf
) < len
)
1053 ERR_load_crypto_strings();
1060 eay_des_encrypt(data
, key
, iv
)
1061 vchar_t
*data
, *key
, *iv
;
1064 des_key_schedule ks
;
1066 if (des_key_sched((void *)key
->v
, ks
) != 0)
1069 /* allocate buffer for result */
1070 if ((res
= vmalloc(data
->l
)) == NULL
)
1073 /* decryption data */
1074 des_cbc_encrypt((void *)data
->v
, (void *)res
->v
, data
->l
,
1075 ks
, (void *)iv
->v
, DES_ENCRYPT
);
1081 eay_des_decrypt(data
, key
, iv
)
1082 vchar_t
*data
, *key
, *iv
;
1085 des_key_schedule ks
;
1087 if (des_key_sched((void *)key
->v
, ks
) != 0)
1090 /* allocate buffer for result */
1091 if ((res
= vmalloc(data
->l
)) == NULL
)
1094 /* decryption data */
1095 des_cbc_encrypt((void *)data
->v
, (void *)res
->v
, data
->l
,
1096 ks
, (void *)iv
->v
, DES_DECRYPT
);
1102 eay_des_weakkey(key
)
1105 return des_is_weak_key((void *)key
->v
);
1112 if (len
!= 0 && len
!= 64)
1117 #ifdef HAVE_OPENSSL_IDEA_H
1122 eay_idea_encrypt(data
, key
, iv
)
1123 vchar_t
*data
, *key
, *iv
;
1126 IDEA_KEY_SCHEDULE ks
;
1128 idea_set_encrypt_key(key
->v
, &ks
);
1130 /* allocate buffer for result */
1131 if ((res
= vmalloc(data
->l
)) == NULL
)
1134 /* decryption data */
1135 idea_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1136 &ks
, iv
->v
, IDEA_ENCRYPT
);
1142 eay_idea_decrypt(data
, key
, iv
)
1143 vchar_t
*data
, *key
, *iv
;
1146 IDEA_KEY_SCHEDULE ks
, dks
;
1148 idea_set_encrypt_key(key
->v
, &ks
);
1149 idea_set_decrypt_key(&ks
, &dks
);
1151 /* allocate buffer for result */
1152 if ((res
= vmalloc(data
->l
)) == NULL
)
1155 /* decryption data */
1156 idea_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1157 &dks
, iv
->v
, IDEA_DECRYPT
);
1163 eay_idea_weakkey(key
)
1170 eay_idea_keylen(len
)
1173 if (len
!= 0 && len
!= 128)
1183 eay_bf_encrypt(data
, key
, iv
)
1184 vchar_t
*data
, *key
, *iv
;
1189 BF_set_key(&ks
, key
->l
, key
->v
);
1191 /* allocate buffer for result */
1192 if ((res
= vmalloc(data
->l
)) == NULL
)
1195 /* decryption data */
1196 BF_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1197 &ks
, iv
->v
, BF_ENCRYPT
);
1203 eay_bf_decrypt(data
, key
, iv
)
1204 vchar_t
*data
, *key
, *iv
;
1209 BF_set_key(&ks
, key
->l
, key
->v
);
1211 /* allocate buffer for result */
1212 if ((res
= vmalloc(data
->l
)) == NULL
)
1215 /* decryption data */
1216 BF_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1217 &ks
, iv
->v
, BF_DECRYPT
);
1226 return 0; /* XXX to be done. refer to RFC 2451 */
1235 if (len
< 40 || len
> 448)
1237 return (len
+ 7) / 8;
1240 #ifdef HAVE_OPENSSL_RC5_H
1245 eay_rc5_encrypt(data
, key
, iv
)
1246 vchar_t
*data
, *key
, *iv
;
1251 /* in RFC 2451, there is information about the number of round. */
1252 RC5_32_set_key(&ks
, key
->l
, key
->v
, 16);
1254 /* allocate buffer for result */
1255 if ((res
= vmalloc(data
->l
)) == NULL
)
1258 /* decryption data */
1259 RC5_32_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1260 &ks
, iv
->v
, RC5_ENCRYPT
);
1266 eay_rc5_decrypt(data
, key
, iv
)
1267 vchar_t
*data
, *key
, *iv
;
1272 /* in RFC 2451, there is information about the number of round. */
1273 RC5_32_set_key(&ks
, key
->l
, key
->v
, 16);
1275 /* allocate buffer for result */
1276 if ((res
= vmalloc(data
->l
)) == NULL
)
1279 /* decryption data */
1280 RC5_32_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1281 &ks
, iv
->v
, RC5_DECRYPT
);
1287 eay_rc5_weakkey(key
)
1290 return 0; /* No known weak keys when used with 16 rounds. */
1300 if (len
< 40 || len
> 2040)
1302 return (len
+ 7) / 8;
1310 eay_3des_encrypt(data
, key
, iv
)
1311 vchar_t
*data
, *key
, *iv
;
1314 des_key_schedule ks1
, ks2
, ks3
;
1319 if (des_key_sched((void *)key
->v
, ks1
) != 0)
1321 if (des_key_sched((void *)(key
->v
+ 8), ks2
) != 0)
1323 if (des_key_sched((void *)(key
->v
+ 16), ks3
) != 0)
1326 /* allocate buffer for result */
1327 if ((res
= vmalloc(data
->l
)) == NULL
)
1330 /* decryption data */
1331 des_ede3_cbc_encrypt((void *)data
->v
, (void *)res
->v
, data
->l
,
1332 ks1
, ks2
, ks3
, (void *)iv
->v
, DES_ENCRYPT
);
1338 eay_3des_decrypt(data
, key
, iv
)
1339 vchar_t
*data
, *key
, *iv
;
1342 des_key_schedule ks1
, ks2
, ks3
;
1347 if (des_key_sched((void *)key
->v
, ks1
) != 0)
1349 if (des_key_sched((void *)(key
->v
+ 8), ks2
) != 0)
1351 if (des_key_sched((void *)(key
->v
+ 16), ks3
) != 0)
1354 /* allocate buffer for result */
1355 if ((res
= vmalloc(data
->l
)) == NULL
)
1358 /* decryption data */
1359 des_ede3_cbc_encrypt((void *)data
->v
, (void *)res
->v
, data
->l
,
1360 ks1
, ks2
, ks3
, (void *)iv
->v
, DES_DECRYPT
);
1366 eay_3des_weakkey(key
)
1372 return (des_is_weak_key((void *)key
->v
)
1373 || des_is_weak_key((void *)(key
->v
+ 8))
1374 || des_is_weak_key((void *)(key
->v
+ 16)));
1378 eay_3des_keylen(len
)
1381 if (len
!= 0 && len
!= 192)
1390 eay_cast_encrypt(data
, key
, iv
)
1391 vchar_t
*data
, *key
, *iv
;
1396 CAST_set_key(&ks
, key
->l
, key
->v
);
1398 /* allocate buffer for result */
1399 if ((res
= vmalloc(data
->l
)) == NULL
)
1402 /* decryption data */
1403 CAST_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1404 &ks
, iv
->v
, DES_ENCRYPT
);
1410 eay_cast_decrypt(data
, key
, iv
)
1411 vchar_t
*data
, *key
, *iv
;
1416 CAST_set_key(&ks
, key
->l
, key
->v
);
1418 /* allocate buffer for result */
1419 if ((res
= vmalloc(data
->l
)) == NULL
)
1422 /* decryption data */
1423 CAST_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1424 &ks
, iv
->v
, DES_DECRYPT
);
1430 eay_cast_weakkey(key
)
1433 return 0; /* No known weak keys. */
1437 eay_cast_keylen(len
)
1442 if (len
< 40 || len
> 128)
1444 return (len
+ 7) / 8;
1451 eay_aes_encrypt(data
, key
, iv
)
1452 vchar_t
*data
, *key
, *iv
;
1458 memset(&k
, 0, sizeof(k
));
1459 if (rijndael_makeKey(&k
, DIR_ENCRYPT
, key
->l
<< 3, key
->v
) < 0)
1462 /* allocate buffer for result */
1463 if ((res
= vmalloc(data
->l
)) == NULL
)
1466 /* encryption data */
1467 memset(&c
, 0, sizeof(c
));
1468 if (rijndael_cipherInit(&c
, MODE_CBC
, iv
->v
) < 0)
1470 if (rijndael_blockEncrypt(&c
, &k
, data
->v
, data
->l
<< 3, res
->v
) < 0)
1477 eay_aes_decrypt(data
, key
, iv
)
1478 vchar_t
*data
, *key
, *iv
;
1484 memset(&k
, 0, sizeof(k
));
1485 if (rijndael_makeKey(&k
, DIR_DECRYPT
, key
->l
<< 3, key
->v
) < 0)
1488 /* allocate buffer for result */
1489 if ((res
= vmalloc(data
->l
)) == NULL
)
1492 /* decryption data */
1493 memset(&c
, 0, sizeof(c
));
1494 if (rijndael_cipherInit(&c
, MODE_CBC
, iv
->v
) < 0)
1496 if (rijndael_blockDecrypt(&c
, &k
, data
->v
, data
->l
<< 3, res
->v
) < 0)
1503 eay_aes_weakkey(key
)
1515 if (len
!= 128 && len
!= 192 && len
!= 256)
1520 /* for ipsec part */
1534 eay_twofish_keylen(len
)
1537 if (len
< 0 || len
> 256)
1543 eay_null_keylen(len
)
1553 eay_hmac_init(key
, md
)
1557 HMAC_CTX
*c
= racoon_malloc(sizeof(*c
));
1560 HMAC_Init(c
, key
->v
, key
->l
, md
);
1569 eay_hmacsha2_512_one(key
, data
)
1570 vchar_t
*key
, *data
;
1575 ctx
= eay_hmacsha2_512_init(key
);
1576 eay_hmacsha2_512_update(ctx
, data
);
1577 res
= eay_hmacsha2_512_final(ctx
);
1583 eay_hmacsha2_512_init(key
)
1586 return eay_hmac_init(key
, EVP_sha2_512());
1590 eay_hmacsha2_512_update(c
, data
)
1594 HMAC_Update((HMAC_CTX
*)c
, data
->v
, data
->l
);
1598 eay_hmacsha2_512_final(c
)
1604 if ((res
= vmalloc(SHA512_DIGEST_LENGTH
)) == 0)
1607 HMAC_Final((HMAC_CTX
*)c
, res
->v
, &l
);
1609 HMAC_CTX_cleanup(c
);
1610 (void)racoon_free(c
);
1612 if (SHA512_DIGEST_LENGTH
!= res
->l
) {
1614 plog(LLV_ERROR
, LOCATION
, NULL
,
1615 "hmac sha2_512 length mismatch %d.\n", res
->l
);
1617 printf("hmac sha2_512 length mismatch %d.\n", res
->l
);
1630 eay_hmacsha2_384_one(key
, data
)
1631 vchar_t
*key
, *data
;
1636 ctx
= eay_hmacsha2_384_init(key
);
1637 eay_hmacsha2_384_update(ctx
, data
);
1638 res
= eay_hmacsha2_384_final(ctx
);
1644 eay_hmacsha2_384_init(key
)
1647 return eay_hmac_init(key
, EVP_sha2_384());
1651 eay_hmacsha2_384_update(c
, data
)
1655 HMAC_Update((HMAC_CTX
*)c
, data
->v
, data
->l
);
1659 eay_hmacsha2_384_final(c
)
1665 if ((res
= vmalloc(SHA384_DIGEST_LENGTH
)) == 0)
1668 HMAC_Final((HMAC_CTX
*)c
, res
->v
, &l
);
1670 HMAC_CTX_cleanup(c
);
1671 (void)racoon_free(c
);
1673 if (SHA384_DIGEST_LENGTH
!= res
->l
) {
1675 plog(LLV_ERROR
, LOCATION
, NULL
,
1676 "hmac sha2_384 length mismatch %d.\n", res
->l
);
1678 printf("hmac sha2_384 length mismatch %d.\n", res
->l
);
1691 eay_hmacsha2_256_one(key
, data
)
1692 vchar_t
*key
, *data
;
1697 ctx
= eay_hmacsha2_256_init(key
);
1698 eay_hmacsha2_256_update(ctx
, data
);
1699 res
= eay_hmacsha2_256_final(ctx
);
1705 eay_hmacsha2_256_init(key
)
1708 return eay_hmac_init(key
, EVP_sha2_256());
1712 eay_hmacsha2_256_update(c
, data
)
1716 HMAC_Update((HMAC_CTX
*)c
, data
->v
, data
->l
);
1720 eay_hmacsha2_256_final(c
)
1726 if ((res
= vmalloc(SHA256_DIGEST_LENGTH
)) == 0)
1729 HMAC_Final((HMAC_CTX
*)c
, res
->v
, &l
);
1731 HMAC_CTX_cleanup(c
);
1732 (void)racoon_free(c
);
1734 if (SHA256_DIGEST_LENGTH
!= res
->l
) {
1736 plog(LLV_ERROR
, LOCATION
, NULL
,
1737 "hmac sha2_256 length mismatch %d.\n", res
->l
);
1739 printf("hmac sha2_256 length mismatch %d.\n", res
->l
);
1752 eay_hmacsha1_one(key
, data
)
1753 vchar_t
*key
, *data
;
1758 ctx
= eay_hmacsha1_init(key
);
1759 eay_hmacsha1_update(ctx
, data
);
1760 res
= eay_hmacsha1_final(ctx
);
1766 eay_hmacsha1_init(key
)
1769 return eay_hmac_init(key
, EVP_sha1());
1773 eay_hmacsha1_update(c
, data
)
1777 HMAC_Update((HMAC_CTX
*)c
, data
->v
, data
->l
);
1781 eay_hmacsha1_final(c
)
1787 if ((res
= vmalloc(SHA_DIGEST_LENGTH
)) == 0)
1790 HMAC_Final((HMAC_CTX
*)c
, res
->v
, &l
);
1792 HMAC_CTX_cleanup(c
);
1793 (void)racoon_free(c
);
1795 if (SHA_DIGEST_LENGTH
!= res
->l
) {
1797 plog(LLV_ERROR
, LOCATION
, NULL
,
1798 "hmac sha1 length mismatch %d.\n", res
->l
);
1800 printf("hmac sha1 length mismatch %d.\n", res
->l
);
1813 eay_hmacmd5_one(key
, data
)
1814 vchar_t
*key
, *data
;
1819 ctx
= eay_hmacmd5_init(key
);
1820 eay_hmacmd5_update(ctx
, data
);
1821 res
= eay_hmacmd5_final(ctx
);
1827 eay_hmacmd5_init(key
)
1830 return eay_hmac_init(key
, EVP_md5());
1834 eay_hmacmd5_update(c
, data
)
1838 HMAC_Update((HMAC_CTX
*)c
, data
->v
, data
->l
);
1842 eay_hmacmd5_final(c
)
1848 if ((res
= vmalloc(MD5_DIGEST_LENGTH
)) == 0)
1851 HMAC_Final((HMAC_CTX
*)c
, res
->v
, &l
);
1853 HMAC_CTX_cleanup(c
);
1854 (void)racoon_free(c
);
1856 if (MD5_DIGEST_LENGTH
!= res
->l
) {
1858 plog(LLV_ERROR
, LOCATION
, NULL
,
1859 "hmac md5 length mismatch %d.\n", res
->l
);
1861 printf("hmac md5 length mismatch %d.\n", res
->l
);
1871 * SHA2-512 functions
1876 SHA512_CTX
*c
= racoon_malloc(sizeof(*c
));
1884 eay_sha2_512_update(c
, data
)
1888 SHA512_Update((SHA512_CTX
*)c
, data
->v
, data
->l
);
1894 eay_sha2_512_final(c
)
1899 if ((res
= vmalloc(SHA512_DIGEST_LENGTH
)) == 0)
1902 SHA512_Final(res
->v
, (SHA512_CTX
*)c
);
1903 (void)racoon_free(c
);
1909 eay_sha2_512_one(data
)
1915 ctx
= eay_sha2_512_init();
1916 eay_sha2_512_update(ctx
, data
);
1917 res
= eay_sha2_512_final(ctx
);
1923 eay_sha2_512_hashlen()
1925 return SHA512_DIGEST_LENGTH
<< 3;
1929 * SHA2-384 functions
1934 SHA384_CTX
*c
= racoon_malloc(sizeof(*c
));
1942 eay_sha2_384_update(c
, data
)
1946 SHA384_Update((SHA384_CTX
*)c
, data
->v
, data
->l
);
1952 eay_sha2_384_final(c
)
1957 if ((res
= vmalloc(SHA384_DIGEST_LENGTH
)) == 0)
1960 SHA384_Final(res
->v
, (SHA384_CTX
*)c
);
1961 (void)racoon_free(c
);
1967 eay_sha2_384_one(data
)
1973 ctx
= eay_sha2_384_init();
1974 eay_sha2_384_update(ctx
, data
);
1975 res
= eay_sha2_384_final(ctx
);
1981 eay_sha2_384_hashlen()
1983 return SHA384_DIGEST_LENGTH
<< 3;
1987 * SHA2-256 functions
1992 SHA256_CTX
*c
= racoon_malloc(sizeof(*c
));
2000 eay_sha2_256_update(c
, data
)
2004 SHA256_Update((SHA256_CTX
*)c
, data
->v
, data
->l
);
2010 eay_sha2_256_final(c
)
2015 if ((res
= vmalloc(SHA256_DIGEST_LENGTH
)) == 0)
2018 SHA256_Final(res
->v
, (SHA256_CTX
*)c
);
2019 (void)racoon_free(c
);
2025 eay_sha2_256_one(data
)
2031 ctx
= eay_sha2_256_init();
2032 eay_sha2_256_update(ctx
, data
);
2033 res
= eay_sha2_256_final(ctx
);
2039 eay_sha2_256_hashlen()
2041 return SHA256_DIGEST_LENGTH
<< 3;
2050 SHA_CTX
*c
= racoon_malloc(sizeof(*c
));
2058 eay_sha1_update(c
, data
)
2062 SHA1_Update((SHA_CTX
*)c
, data
->v
, data
->l
);
2073 if ((res
= vmalloc(SHA_DIGEST_LENGTH
)) == 0)
2076 SHA1_Final(res
->v
, (SHA_CTX
*)c
);
2077 (void)racoon_free(c
);
2089 ctx
= eay_sha1_init();
2090 eay_sha1_update(ctx
, data
);
2091 res
= eay_sha1_final(ctx
);
2099 return SHA_DIGEST_LENGTH
<< 3;
2108 MD5_CTX
*c
= racoon_malloc(sizeof(*c
));
2116 eay_md5_update(c
, data
)
2120 MD5_Update((MD5_CTX
*)c
, data
->v
, data
->l
);
2131 if ((res
= vmalloc(MD5_DIGEST_LENGTH
)) == 0)
2134 MD5_Final(res
->v
, (MD5_CTX
*)c
);
2135 (void)racoon_free(c
);
2147 ctx
= eay_md5_init();
2148 eay_md5_update(ctx
, data
);
2149 res
= eay_md5_final(ctx
);
2157 return MD5_DIGEST_LENGTH
<< 3;
2162 * size: number of bytes.
2165 eay_set_random(size
)
2171 if ((r
= BN_new()) == NULL
)
2173 BN_rand(r
, size
* 8, 0, 0);
2184 eay_dh_generate(prime
, g
, publen
, pub
, priv
)
2185 vchar_t
*prime
, **pub
, **priv
;
2194 /* pre-process to generate number */
2195 if (eay_v2bn(&p
, prime
) < 0)
2198 if ((dh
= DH_new()) == NULL
)
2201 p
= NULL
; /* p is now part of dh structure */
2203 if ((dh
->g
= BN_new()) == NULL
)
2205 if (!BN_set_word(dh
->g
, g
))
2209 dh
->length
= publen
;
2211 /* generate public and private number */
2212 if (!DH_generate_key(dh
))
2215 /* copy results to buffers */
2216 if (eay_bn2v(pub
, dh
->pub_key
) < 0)
2218 if (eay_bn2v(priv
, dh
->priv_key
) < 0) {
2234 eay_dh_compute(prime
, g
, pub
, priv
, pub2
, key
)
2235 vchar_t
*prime
, *pub
, *priv
, *pub2
, **key
;
2238 BIGNUM
*dh_pub
= NULL
;
2244 /* make public number to compute */
2245 if (eay_v2bn(&dh_pub
, pub2
) < 0)
2248 /* make DH structure */
2249 if ((dh
= DH_new()) == NULL
)
2251 if (eay_v2bn(&dh
->p
, prime
) < 0)
2253 if (eay_v2bn(&dh
->pub_key
, pub
) < 0)
2255 if (eay_v2bn(&dh
->priv_key
, priv
) < 0)
2257 dh
->length
= pub2
->l
* 8;
2260 if ((dh
->g
= BN_new()) == NULL
)
2262 if (!BN_set_word(dh
->g
, g
))
2265 if ((v
= (caddr_t
)racoon_calloc(prime
->l
, sizeof(u_char
))) == NULL
)
2267 if ((l
= DH_compute_key(v
, dh_pub
, dh
)) == -1)
2269 memcpy((*key
)->v
+ (prime
->l
- l
), v
, l
);
2289 if ((*bn
= BN_bin2bn(var
->v
, var
->l
, NULL
)) == NULL
)
2296 * convert vchar_t <-> BIGNUM.
2298 * vchar_t: unit is u_char, network endian, most significant byte first.
2299 * BIGNUM: unit is BN_ULONG, each of BN_ULONG is in host endian,
2300 * least significant BN_ULONG must come first.
2302 * hex value of "0x3ffe050104" is represented as follows:
2303 * vchar_t: 3f fe 05 01 04
2304 * BIGNUM (BN_ULONG = u_int8_t): 04 01 05 fe 3f
2305 * BIGNUM (BN_ULONG = u_int16_t): 0x0104 0xfe05 0x003f
2306 * BIGNUM (BN_ULONG = u_int32_t_t): 0xfe050104 0x0000003f
2322 l
= (var
->l
* 8 + BN_BITS2
- 1) / BN_BITS2
;
2323 if (bn_expand(*bn
, l
* BN_BITS2
) == NULL
)
2327 /* scan from least significant byte */
2328 p
= (u_char
*)var
->v
;
2329 q
= (u_char
*)(var
->v
+ var
->l
);
2335 num
= num
| ((BN_ULONG
)*q
<< (l
++ * 8));
2336 if (l
== BN_BYTES
) {
2358 *var
= vmalloc(bn
->top
* BN_BYTES
);
2362 (*var
)->l
= BN_bn2bin(bn
, (*var
)->v
);
2370 return SSLeay_version(SSLEAY_VERSION
);