1 /* $KAME: crypto_openssl.c,v 1.69 2001/09/11 13:25:00 sakane 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 strcpy(*altname
, cval
->value
);
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 pubkey
.v
= x509
->cert_info
->key
->public_key
->data
;
702 pubkey
.l
= x509
->cert_info
->key
->public_key
->length
;
704 return eay_rsa_verify(source
, sig
, &pubkey
);
708 * check a signature by signed with PKCS7 certificate.
709 * XXX: to be get hash type from my cert ?
710 * to be handled EVP_dss().
711 * OUT: return -1 when error.
715 eay_check_pkcs7sign(source
, sig
, cert
)
724 BIO
*bio
= BIO_new(BIO_s_mem());
729 error
= BIO_write(bio
, cert
->v
, cert
->l
);
730 if (error
!= cert
->l
)
734 x509
= PEM_read_bio_X509(bio
, NULL
, NULL
, NULL
);
739 evp
= X509_get_pubkey(x509
);
744 /* Verify the signature */
745 /* XXX: to be handled EVP_dss() */
746 EVP_VerifyInit(&md_ctx
, EVP_sha1());
747 EVP_VerifyUpdate(&md_ctx
, source
->v
, source
->l
);
748 error
= EVP_VerifyFinal(&md_ctx
, sig
->v
, sig
->l
, evp
);
759 * get PKCS#1 Private Key of PEM format from local file.
762 eay_get_pkcs1privkey(path
)
766 EVP_PKEY
*evp
= NULL
;
767 vchar_t
*pkey
= NULL
;
772 /* Read private key */
773 fp
= fopen(path
, "r");
777 #if OPENSSL_VERSION_NUMBER >= 0x00904100L
778 evp
= PEM_read_PrivateKey(fp
, NULL
, NULL
, NULL
);
780 evp
= PEM_read_PrivateKey(fp
, NULL
, NULL
);
787 pkeylen
= i2d_PrivateKey(evp
, NULL
);
790 pkey
= vmalloc(pkeylen
);
794 pkeylen
= i2d_PrivateKey(evp
, &bp
);
803 if (error
!= 0 && pkey
!= NULL
) {
812 * get PKCS#1 Public Key of PEM format from local file.
815 eay_get_pkcs1pubkey(path
)
819 EVP_PKEY
*evp
= NULL
;
820 vchar_t
*pkey
= NULL
;
826 /* Read private key */
827 fp
= fopen(path
, "r");
831 #if OPENSSL_VERSION_NUMBER >= 0x00904100L
832 x509
= PEM_read_X509(fp
, NULL
, NULL
, NULL
);
834 x509
= PEM_read_X509(fp
, NULL
, NULL
);
841 /* Get public key - eay */
842 evp
= X509_get_pubkey(x509
);
846 pkeylen
= i2d_PublicKey(evp
, NULL
);
849 pkey
= vmalloc(pkeylen
);
853 pkeylen
= i2d_PublicKey(evp
, &bp
);
861 if (error
!= 0 && pkey
!= NULL
) {
871 eay_rsa_sign(src
, privkey
)
872 vchar_t
*src
, *privkey
;
875 u_char
*bp
= privkey
->v
;
878 int pad
= RSA_PKCS1_PADDING
;
880 /* XXX to be handled EVP_PKEY_DSA */
881 evp
= d2i_PrivateKey(EVP_PKEY_RSA
, NULL
, &bp
, privkey
->l
);
885 /* XXX: to be handled EVP_dss() */
886 /* XXX: Where can I get such parameters ? From my cert ? */
888 len
= RSA_size(evp
->pkey
.rsa
);
894 len
= RSA_private_encrypt(src
->l
, src
->v
, sig
->v
, evp
->pkey
.rsa
, pad
);
896 if (len
== 0 || len
!= sig
->l
) {
905 eay_rsa_verify(src
, sig
, pubkey
)
906 vchar_t
*src
, *sig
, *pubkey
;
909 u_char
*bp
= pubkey
->v
;
910 vchar_t
*xbuf
= NULL
;
911 int pad
= RSA_PKCS1_PADDING
;
915 evp
= d2i_PUBKEY(NULL
, &bp
, pubkey
->l
);
921 len
= RSA_size(evp
->pkey
.rsa
);
926 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
932 len
= RSA_public_decrypt(sig
->l
, sig
->v
, xbuf
->v
, evp
->pkey
.rsa
, pad
);
934 if (len
== 0 || len
!= src
->l
)
935 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
938 if (len
== 0 || len
!= src
->l
) {
943 error
= memcmp(src
->v
, xbuf
->v
, src
->l
);
953 * MUST load ERR_load_crypto_strings() first.
958 static char ebuf
[512];
962 #if OPENSSL_VERSION_NUMBER >= 0x00904100L
963 const char *file
, *data
;
970 es
= CRYPTO_thread_id();
972 while ((l
= ERR_get_error_line_data(&file
, &line
, &data
, &flags
)) != 0){
973 n
= snprintf(ebuf
+ len
, sizeof(ebuf
) - len
,
975 es
, ERR_error_string(l
, buf
), file
, line
,
976 (flags
& ERR_TXT_STRING
) ? data
: "");
977 if (n
< 0 || n
>= sizeof(ebuf
) - len
)
980 if (sizeof(ebuf
) < len
)
990 ERR_load_crypto_strings();
997 eay_des_encrypt(data
, key
, iv
)
998 vchar_t
*data
, *key
, *iv
;
1001 des_key_schedule ks
;
1003 if (des_key_sched((void *)key
->v
, ks
) != 0)
1006 /* allocate buffer for result */
1007 if ((res
= vmalloc(data
->l
)) == NULL
)
1010 /* decryption data */
1011 des_cbc_encrypt((void *)data
->v
, (void *)res
->v
, data
->l
,
1012 ks
, (void *)iv
->v
, DES_ENCRYPT
);
1018 eay_des_decrypt(data
, key
, iv
)
1019 vchar_t
*data
, *key
, *iv
;
1022 des_key_schedule ks
;
1024 if (des_key_sched((void *)key
->v
, ks
) != 0)
1027 /* allocate buffer for result */
1028 if ((res
= vmalloc(data
->l
)) == NULL
)
1031 /* decryption data */
1032 des_cbc_encrypt((void *)data
->v
, (void *)res
->v
, data
->l
,
1033 ks
, (void *)iv
->v
, DES_DECRYPT
);
1039 eay_des_weakkey(key
)
1042 return des_is_weak_key((void *)key
->v
);
1049 if (len
!= 0 && len
!= 64)
1054 #ifdef HAVE_OPENSSL_IDEA_H
1059 eay_idea_encrypt(data
, key
, iv
)
1060 vchar_t
*data
, *key
, *iv
;
1063 IDEA_KEY_SCHEDULE ks
;
1065 idea_set_encrypt_key(key
->v
, &ks
);
1067 /* allocate buffer for result */
1068 if ((res
= vmalloc(data
->l
)) == NULL
)
1071 /* decryption data */
1072 idea_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1073 &ks
, iv
->v
, IDEA_ENCRYPT
);
1079 eay_idea_decrypt(data
, key
, iv
)
1080 vchar_t
*data
, *key
, *iv
;
1083 IDEA_KEY_SCHEDULE ks
, dks
;
1085 idea_set_encrypt_key(key
->v
, &ks
);
1086 idea_set_decrypt_key(&ks
, &dks
);
1088 /* allocate buffer for result */
1089 if ((res
= vmalloc(data
->l
)) == NULL
)
1092 /* decryption data */
1093 idea_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1094 &dks
, iv
->v
, IDEA_DECRYPT
);
1100 eay_idea_weakkey(key
)
1107 eay_idea_keylen(len
)
1110 if (len
!= 0 && len
!= 128)
1120 eay_bf_encrypt(data
, key
, iv
)
1121 vchar_t
*data
, *key
, *iv
;
1126 BF_set_key(&ks
, key
->l
, key
->v
);
1128 /* allocate buffer for result */
1129 if ((res
= vmalloc(data
->l
)) == NULL
)
1132 /* decryption data */
1133 BF_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1134 &ks
, iv
->v
, BF_ENCRYPT
);
1140 eay_bf_decrypt(data
, key
, iv
)
1141 vchar_t
*data
, *key
, *iv
;
1146 BF_set_key(&ks
, key
->l
, key
->v
);
1148 /* allocate buffer for result */
1149 if ((res
= vmalloc(data
->l
)) == NULL
)
1152 /* decryption data */
1153 BF_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1154 &ks
, iv
->v
, BF_DECRYPT
);
1163 return 0; /* XXX to be done. refer to RFC 2451 */
1172 if (len
< 40 || len
> 448)
1177 #ifdef HAVE_OPENSSL_RC5_H
1182 eay_rc5_encrypt(data
, key
, iv
)
1183 vchar_t
*data
, *key
, *iv
;
1188 /* in RFC 2451, there is information about the number of round. */
1189 RC5_32_set_key(&ks
, key
->l
, key
->v
, 16);
1191 /* allocate buffer for result */
1192 if ((res
= vmalloc(data
->l
)) == NULL
)
1195 /* decryption data */
1196 RC5_32_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1197 &ks
, iv
->v
, RC5_ENCRYPT
);
1203 eay_rc5_decrypt(data
, key
, iv
)
1204 vchar_t
*data
, *key
, *iv
;
1209 /* in RFC 2451, there is information about the number of round. */
1210 RC5_32_set_key(&ks
, key
->l
, key
->v
, 16);
1212 /* allocate buffer for result */
1213 if ((res
= vmalloc(data
->l
)) == NULL
)
1216 /* decryption data */
1217 RC5_32_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1218 &ks
, iv
->v
, RC5_DECRYPT
);
1224 eay_rc5_weakkey(key
)
1227 return 0; /* No known weak keys when used with 16 rounds. */
1237 if (len
< 40 || len
> 2040)
1247 eay_3des_encrypt(data
, key
, iv
)
1248 vchar_t
*data
, *key
, *iv
;
1251 des_key_schedule ks1
, ks2
, ks3
;
1256 if (des_key_sched((void *)key
->v
, ks1
) != 0)
1258 if (des_key_sched((void *)(key
->v
+ 8), ks2
) != 0)
1260 if (des_key_sched((void *)(key
->v
+ 16), ks3
) != 0)
1263 /* allocate buffer for result */
1264 if ((res
= vmalloc(data
->l
)) == NULL
)
1267 /* decryption data */
1268 des_ede3_cbc_encrypt((void *)data
->v
, (void *)res
->v
, data
->l
,
1269 ks1
, ks2
, ks3
, (void *)iv
->v
, DES_ENCRYPT
);
1275 eay_3des_decrypt(data
, key
, iv
)
1276 vchar_t
*data
, *key
, *iv
;
1279 des_key_schedule ks1
, ks2
, ks3
;
1284 if (des_key_sched((void *)key
->v
, ks1
) != 0)
1286 if (des_key_sched((void *)(key
->v
+ 8), ks2
) != 0)
1288 if (des_key_sched((void *)(key
->v
+ 16), ks3
) != 0)
1291 /* allocate buffer for result */
1292 if ((res
= vmalloc(data
->l
)) == NULL
)
1295 /* decryption data */
1296 des_ede3_cbc_encrypt((void *)data
->v
, (void *)res
->v
, data
->l
,
1297 ks1
, ks2
, ks3
, (void *)iv
->v
, DES_DECRYPT
);
1303 eay_3des_weakkey(key
)
1309 return (des_is_weak_key((void *)key
->v
)
1310 || des_is_weak_key((void *)(key
->v
+ 8))
1311 || des_is_weak_key((void *)(key
->v
+ 16)));
1315 eay_3des_keylen(len
)
1318 if (len
!= 0 && len
!= 192)
1327 eay_cast_encrypt(data
, key
, iv
)
1328 vchar_t
*data
, *key
, *iv
;
1333 CAST_set_key(&ks
, key
->l
, key
->v
);
1335 /* allocate buffer for result */
1336 if ((res
= vmalloc(data
->l
)) == NULL
)
1339 /* decryption data */
1340 CAST_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1341 &ks
, iv
->v
, DES_ENCRYPT
);
1347 eay_cast_decrypt(data
, key
, iv
)
1348 vchar_t
*data
, *key
, *iv
;
1353 CAST_set_key(&ks
, key
->l
, key
->v
);
1355 /* allocate buffer for result */
1356 if ((res
= vmalloc(data
->l
)) == NULL
)
1359 /* decryption data */
1360 CAST_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1361 &ks
, iv
->v
, DES_DECRYPT
);
1367 eay_cast_weakkey(key
)
1370 return 0; /* No known weak keys. */
1374 eay_cast_keylen(len
)
1379 if (len
< 40 || len
> 128)
1388 eay_aes_encrypt(data
, key
, iv
)
1389 vchar_t
*data
, *key
, *iv
;
1395 memset(&k
, 0, sizeof(k
));
1396 if (rijndael_makeKey(&k
, DIR_ENCRYPT
, key
->l
<< 3, key
->v
) < 0)
1399 /* allocate buffer for result */
1400 if ((res
= vmalloc(data
->l
)) == NULL
)
1403 /* encryption data */
1404 memset(&c
, 0, sizeof(c
));
1405 if (rijndael_cipherInit(&c
, MODE_CBC
, iv
->v
) < 0)
1407 if (rijndael_blockEncrypt(&c
, &k
, data
->v
, data
->l
<< 3, res
->v
) < 0)
1414 eay_aes_decrypt(data
, key
, iv
)
1415 vchar_t
*data
, *key
, *iv
;
1421 memset(&k
, 0, sizeof(k
));
1422 if (rijndael_makeKey(&k
, DIR_DECRYPT
, key
->l
<< 3, key
->v
) < 0)
1425 /* allocate buffer for result */
1426 if ((res
= vmalloc(data
->l
)) == NULL
)
1429 /* decryption data */
1430 memset(&c
, 0, sizeof(c
));
1431 if (rijndael_cipherInit(&c
, MODE_CBC
, iv
->v
) < 0)
1433 if (rijndael_blockDecrypt(&c
, &k
, data
->v
, data
->l
<< 3, res
->v
) < 0)
1440 eay_aes_weakkey(key
)
1452 if (len
!= 128 && len
!= 192 && len
!= 256)
1457 /* for ipsec part */
1471 eay_twofish_keylen(len
)
1474 if (len
< 0 || len
> 256)
1483 eay_hmac_init(key
, md
)
1487 HMAC_CTX
*c
= racoon_malloc(sizeof(*c
));
1489 HMAC_Init(c
, key
->v
, key
->l
, md
);
1498 eay_hmacsha2_512_one(key
, data
)
1499 vchar_t
*key
, *data
;
1504 ctx
= eay_hmacsha2_512_init(key
);
1505 eay_hmacsha2_512_update(ctx
, data
);
1506 res
= eay_hmacsha2_512_final(ctx
);
1512 eay_hmacsha2_512_init(key
)
1515 return eay_hmac_init(key
, EVP_sha2_512());
1519 eay_hmacsha2_512_update(c
, data
)
1523 HMAC_Update((HMAC_CTX
*)c
, data
->v
, data
->l
);
1527 eay_hmacsha2_512_final(c
)
1533 if ((res
= vmalloc(SHA512_DIGEST_LENGTH
)) == 0)
1536 HMAC_Final((HMAC_CTX
*)c
, res
->v
, &l
);
1538 (void)racoon_free(c
);
1540 if (SHA512_DIGEST_LENGTH
!= res
->l
) {
1542 plog(LLV_ERROR
, LOCATION
, NULL
,
1543 "hmac sha2_512 length mismatch %d.\n", res
->l
);
1545 printf("hmac sha2_512 length mismatch %d.\n", res
->l
);
1558 eay_hmacsha2_384_one(key
, data
)
1559 vchar_t
*key
, *data
;
1564 ctx
= eay_hmacsha2_384_init(key
);
1565 eay_hmacsha2_384_update(ctx
, data
);
1566 res
= eay_hmacsha2_384_final(ctx
);
1572 eay_hmacsha2_384_init(key
)
1575 return eay_hmac_init(key
, EVP_sha2_384());
1579 eay_hmacsha2_384_update(c
, data
)
1583 HMAC_Update((HMAC_CTX
*)c
, data
->v
, data
->l
);
1587 eay_hmacsha2_384_final(c
)
1593 if ((res
= vmalloc(SHA384_DIGEST_LENGTH
)) == 0)
1596 HMAC_Final((HMAC_CTX
*)c
, res
->v
, &l
);
1598 (void)racoon_free(c
);
1600 if (SHA384_DIGEST_LENGTH
!= res
->l
) {
1602 plog(LLV_ERROR
, LOCATION
, NULL
,
1603 "hmac sha2_384 length mismatch %d.\n", res
->l
);
1605 printf("hmac sha2_384 length mismatch %d.\n", res
->l
);
1618 eay_hmacsha2_256_one(key
, data
)
1619 vchar_t
*key
, *data
;
1624 ctx
= eay_hmacsha2_256_init(key
);
1625 eay_hmacsha2_256_update(ctx
, data
);
1626 res
= eay_hmacsha2_256_final(ctx
);
1632 eay_hmacsha2_256_init(key
)
1635 return eay_hmac_init(key
, EVP_sha2_256());
1639 eay_hmacsha2_256_update(c
, data
)
1643 HMAC_Update((HMAC_CTX
*)c
, data
->v
, data
->l
);
1647 eay_hmacsha2_256_final(c
)
1653 if ((res
= vmalloc(SHA256_DIGEST_LENGTH
)) == 0)
1656 HMAC_Final((HMAC_CTX
*)c
, res
->v
, &l
);
1658 (void)racoon_free(c
);
1660 if (SHA256_DIGEST_LENGTH
!= res
->l
) {
1662 plog(LLV_ERROR
, LOCATION
, NULL
,
1663 "hmac sha2_256 length mismatch %d.\n", res
->l
);
1665 printf("hmac sha2_256 length mismatch %d.\n", res
->l
);
1678 eay_hmacsha1_one(key
, data
)
1679 vchar_t
*key
, *data
;
1684 ctx
= eay_hmacsha1_init(key
);
1685 eay_hmacsha1_update(ctx
, data
);
1686 res
= eay_hmacsha1_final(ctx
);
1692 eay_hmacsha1_init(key
)
1695 return eay_hmac_init(key
, EVP_sha1());
1699 eay_hmacsha1_update(c
, data
)
1703 HMAC_Update((HMAC_CTX
*)c
, data
->v
, data
->l
);
1707 eay_hmacsha1_final(c
)
1713 if ((res
= vmalloc(SHA_DIGEST_LENGTH
)) == 0)
1716 HMAC_Final((HMAC_CTX
*)c
, res
->v
, &l
);
1718 (void)racoon_free(c
);
1720 if (SHA_DIGEST_LENGTH
!= res
->l
) {
1722 plog(LLV_ERROR
, LOCATION
, NULL
,
1723 "hmac sha1 length mismatch %d.\n", res
->l
);
1725 printf("hmac sha1 length mismatch %d.\n", res
->l
);
1738 eay_hmacmd5_one(key
, data
)
1739 vchar_t
*key
, *data
;
1744 ctx
= eay_hmacmd5_init(key
);
1745 eay_hmacmd5_update(ctx
, data
);
1746 res
= eay_hmacmd5_final(ctx
);
1752 eay_hmacmd5_init(key
)
1755 return eay_hmac_init(key
, EVP_md5());
1759 eay_hmacmd5_update(c
, data
)
1763 HMAC_Update((HMAC_CTX
*)c
, data
->v
, data
->l
);
1767 eay_hmacmd5_final(c
)
1773 if ((res
= vmalloc(MD5_DIGEST_LENGTH
)) == 0)
1776 HMAC_Final((HMAC_CTX
*)c
, res
->v
, &l
);
1778 (void)racoon_free(c
);
1780 if (MD5_DIGEST_LENGTH
!= res
->l
) {
1782 plog(LLV_ERROR
, LOCATION
, NULL
,
1783 "hmac md5 length mismatch %d.\n", res
->l
);
1785 printf("hmac md5 length mismatch %d.\n", res
->l
);
1795 * SHA2-512 functions
1800 SHA512_CTX
*c
= racoon_malloc(sizeof(*c
));
1808 eay_sha2_512_update(c
, data
)
1812 SHA512_Update((SHA512_CTX
*)c
, data
->v
, data
->l
);
1818 eay_sha2_512_final(c
)
1823 if ((res
= vmalloc(SHA512_DIGEST_LENGTH
)) == 0)
1826 SHA512_Final(res
->v
, (SHA512_CTX
*)c
);
1827 (void)racoon_free(c
);
1833 eay_sha2_512_one(data
)
1839 ctx
= eay_sha2_512_init();
1840 eay_sha2_512_update(ctx
, data
);
1841 res
= eay_sha2_512_final(ctx
);
1847 eay_sha2_512_hashlen()
1849 return SHA512_DIGEST_LENGTH
<< 3;
1853 * SHA2-384 functions
1858 SHA384_CTX
*c
= racoon_malloc(sizeof(*c
));
1866 eay_sha2_384_update(c
, data
)
1870 SHA384_Update((SHA384_CTX
*)c
, data
->v
, data
->l
);
1876 eay_sha2_384_final(c
)
1881 if ((res
= vmalloc(SHA384_DIGEST_LENGTH
)) == 0)
1884 SHA384_Final(res
->v
, (SHA384_CTX
*)c
);
1885 (void)racoon_free(c
);
1891 eay_sha2_384_one(data
)
1897 ctx
= eay_sha2_384_init();
1898 eay_sha2_384_update(ctx
, data
);
1899 res
= eay_sha2_384_final(ctx
);
1905 eay_sha2_384_hashlen()
1907 return SHA384_DIGEST_LENGTH
<< 3;
1911 * SHA2-256 functions
1916 SHA256_CTX
*c
= racoon_malloc(sizeof(*c
));
1924 eay_sha2_256_update(c
, data
)
1928 SHA256_Update((SHA256_CTX
*)c
, data
->v
, data
->l
);
1934 eay_sha2_256_final(c
)
1939 if ((res
= vmalloc(SHA256_DIGEST_LENGTH
)) == 0)
1942 SHA256_Final(res
->v
, (SHA256_CTX
*)c
);
1943 (void)racoon_free(c
);
1949 eay_sha2_256_one(data
)
1955 ctx
= eay_sha2_256_init();
1956 eay_sha2_256_update(ctx
, data
);
1957 res
= eay_sha2_256_final(ctx
);
1963 eay_sha2_256_hashlen()
1965 return SHA256_DIGEST_LENGTH
<< 3;
1974 SHA_CTX
*c
= racoon_malloc(sizeof(*c
));
1982 eay_sha1_update(c
, data
)
1986 SHA1_Update((SHA_CTX
*)c
, data
->v
, data
->l
);
1997 if ((res
= vmalloc(SHA_DIGEST_LENGTH
)) == 0)
2000 SHA1_Final(res
->v
, (SHA_CTX
*)c
);
2001 (void)racoon_free(c
);
2013 ctx
= eay_sha1_init();
2014 eay_sha1_update(ctx
, data
);
2015 res
= eay_sha1_final(ctx
);
2023 return SHA_DIGEST_LENGTH
<< 3;
2032 MD5_CTX
*c
= racoon_malloc(sizeof(*c
));
2040 eay_md5_update(c
, data
)
2044 MD5_Update((MD5_CTX
*)c
, data
->v
, data
->l
);
2055 if ((res
= vmalloc(MD5_DIGEST_LENGTH
)) == 0)
2058 MD5_Final(res
->v
, (MD5_CTX
*)c
);
2059 (void)racoon_free(c
);
2071 ctx
= eay_md5_init();
2072 eay_md5_update(ctx
, data
);
2073 res
= eay_md5_final(ctx
);
2081 return MD5_DIGEST_LENGTH
<< 3;
2086 * size: number of bytes.
2089 eay_set_random(size
)
2095 if ((r
= BN_new()) == NULL
)
2097 BN_rand(r
, size
* 8, 0, 0);
2108 eay_dh_generate(prime
, g
, publen
, pub
, priv
)
2109 vchar_t
*prime
, **pub
, **priv
;
2118 /* pre-process to generate number */
2119 if (eay_v2bn(&p
, prime
) < 0)
2122 if ((dh
= DH_new()) == NULL
)
2125 p
= NULL
; /* p is now part of dh structure */
2127 if ((dh
->g
= BN_new()) == NULL
)
2129 if (!BN_set_word(dh
->g
, g
))
2133 dh
->length
= publen
;
2135 /* generate public and private number */
2136 if (!DH_generate_key(dh
))
2139 /* copy results to buffers */
2140 if (eay_bn2v(pub
, dh
->pub_key
) < 0)
2142 if (eay_bn2v(priv
, dh
->priv_key
) < 0) {
2158 eay_dh_compute(prime
, g
, pub
, priv
, pub2
, key
)
2159 vchar_t
*prime
, *pub
, *priv
, *pub2
, **key
;
2162 BIGNUM
*dh_pub
= NULL
;
2168 /* make public number to compute */
2169 if (eay_v2bn(&dh_pub
, pub2
) < 0)
2172 /* make DH structure */
2173 if ((dh
= DH_new()) == NULL
)
2175 if (eay_v2bn(&dh
->p
, prime
) < 0)
2177 if (eay_v2bn(&dh
->pub_key
, pub
) < 0)
2179 if (eay_v2bn(&dh
->priv_key
, priv
) < 0)
2181 dh
->length
= pub2
->l
* 8;
2184 if ((dh
->g
= BN_new()) == NULL
)
2186 if (!BN_set_word(dh
->g
, g
))
2189 if ((v
= (caddr_t
)racoon_calloc(prime
->l
, sizeof(u_char
))) == NULL
)
2191 if ((l
= DH_compute_key(v
, dh_pub
, dh
)) == -1)
2193 memcpy((*key
)->v
+ (prime
->l
- l
), v
, l
);
2213 if ((*bn
= BN_bin2bn(var
->v
, var
->l
, NULL
)) == NULL
)
2220 * convert vchar_t <-> BIGNUM.
2222 * vchar_t: unit is u_char, network endian, most significant byte first.
2223 * BIGNUM: unit is BN_ULONG, each of BN_ULONG is in host endian,
2224 * least significant BN_ULONG must come first.
2226 * hex value of "0x3ffe050104" is represented as follows:
2227 * vchar_t: 3f fe 05 01 04
2228 * BIGNUM (BN_ULONG = u_int8_t): 04 01 05 fe 3f
2229 * BIGNUM (BN_ULONG = u_int16_t): 0x0104 0xfe05 0x003f
2230 * BIGNUM (BN_ULONG = u_int32_t_t): 0xfe050104 0x0000003f
2246 l
= (var
->l
* 8 + BN_BITS2
- 1) / BN_BITS2
;
2247 if (bn_expand(*bn
, l
* BN_BITS2
) == NULL
)
2251 /* scan from least significant byte */
2252 p
= (u_char
*)var
->v
;
2253 q
= (u_char
*)(var
->v
+ var
->l
);
2259 num
= num
| ((BN_ULONG
)*q
<< (l
++ * 8));
2260 if (l
== BN_BYTES
) {
2282 *var
= vmalloc(bn
->top
* BN_BYTES
);
2286 (*var
)->l
= BN_bn2bin(bn
, (*var
)->v
);
2294 return SSLeay_version(SSLEAY_VERSION
);