1 /* $NetBSD: crypto_openssl.c,v 1.11.6.1 2006/12/18 10:18:10 vanhu Exp $ */
3 /* Id: crypto_openssl.c,v 1.47 2006/05/06 20:42:09 manubsd Exp */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #define COMMON_DIGEST_FOR_OPENSSL 1
40 #include <sys/types.h>
41 #include <sys/param.h>
48 /* get openssl/ssleay version number */
49 #include <openssl/opensslv.h>
51 #if !defined(OPENSSL_VERSION_NUMBER) || (OPENSSL_VERSION_NUMBER < 0x0090602fL)
52 #error OpenSSL version 0.9.6 or later required.
55 #include <openssl/pem.h>
56 #include <openssl/evp.h>
57 #include <openssl/x509.h>
58 #include <openssl/x509v3.h>
59 #include <openssl/x509_vfy.h>
60 #include <openssl/bn.h>
61 #include <openssl/dh.h>
63 #include <CommonCrypto/CommonDigest.h>
64 #include <CommonCrypto/CommonHMAC.h>
65 #include <CommonCrypto/CommonCryptor.h>
67 #include <openssl/md5.h>
68 #include <openssl/sha.h>
69 #include <openssl/hmac.h>
71 #include <openssl/des.h>
72 #include <openssl/crypto.h>
73 #ifdef HAVE_OPENSSL_ENGINE_H
74 #include <openssl/engine.h>
76 #include <openssl/blowfish.h>
77 #include <openssl/cast.h>
78 #include <openssl/err.h>
79 #ifdef HAVE_OPENSSL_RC5_H
80 #include <openssl/rc5.h>
82 #ifdef HAVE_OPENSSL_IDEA_H
83 #include <openssl/idea.h>
85 #if defined(HAVE_OPENSSL_AES_H)
86 #include <openssl/aes.h>
87 #elif defined(HAVE_OPENSSL_RIJNDAEL_H)
88 #include <openssl/rijndael.h>
90 #include "crypto/rijndael/rijndael-api-fst.h"
94 #ifdef HAVE_OPENSSL_SHA2_H
95 #include <openssl/sha2.h>
101 #if OPENSSL_VERSION_NUMBER < 0x0090700fL
102 typedef STACK_OF(GENERAL_NAME
) GENERAL_NAMES
;
104 #define USE_NEW_DES_API
107 #define OpenSSL_BUG() do { plog(LLV_ERROR, LOCATION, NULL, "OpenSSL function failed\n"); } while(0)
113 #include "crypto_openssl.h"
115 #include "gcmalloc.h"
119 * I hate to cast every parameter to des_xx into void *, but it is
120 * necessary for SSLeay/OpenSSL portability. It sucks.
123 static int cb_check_cert_local
__P((int, X509_STORE_CTX
*));
124 static int cb_check_cert_remote
__P((int, X509_STORE_CTX
*));
125 static X509
*mem2x509
__P((vchar_t
*));
128 static caddr_t eay_hmac_init
__P((vchar_t
*, CCHmacAlgorithm
));
130 static caddr_t eay_hmac_init
__P((vchar_t
*, const EVP_MD
*));
133 /* X509 Certificate */
135 * convert the string of the subject name into DER
136 * e.g. str = "C=JP, ST=Kanagawa";
139 eay_str2asn1dn(str
, len
)
153 buf
= racoon_malloc(len
+ 1);
155 plog(LLV_WARNING
, LOCATION
, NULL
,"failed to allocate buffer\n");
158 memcpy(buf
, str
, len
);
160 name
= X509_NAME_new();
164 for (i
= 0; i
< len
; i
++) {
165 if (!value
&& buf
[i
] == '=') {
169 } else if (buf
[i
] == ',' || buf
[i
] == '/') {
172 plog(LLV_DEBUG
, LOCATION
, NULL
, "DN: %s=%s\n",
175 if (!value
) goto err
;
176 if (!X509_NAME_add_entry_by_txt(name
, field
,
177 (value
[0] == '*' && value
[1] == 0) ?
178 V_ASN1_PRINTABLESTRING
: MBSTRING_ASC
,
179 (unsigned char *) value
, -1, -1, 0)) {
180 plog(LLV_ERROR
, LOCATION
, NULL
,
181 "Invalid DN field: %s=%s\n",
183 plog(LLV_ERROR
, LOCATION
, NULL
,
184 "%s\n", eay_strerror());
187 for (j
= i
+ 1; j
< len
; j
++) {
198 plog(LLV_DEBUG
, LOCATION
, NULL
, "DN: %s=%s\n",
201 if (!value
) goto err
;
202 if (!X509_NAME_add_entry_by_txt(name
, field
,
203 (value
[0] == '*' && value
[1] == 0) ?
204 V_ASN1_PRINTABLESTRING
: MBSTRING_ASC
,
205 (unsigned char *) value
, -1, -1, 0)) {
206 plog(LLV_ERROR
, LOCATION
, NULL
,
207 "Invalid DN field: %s=%s\n",
209 plog(LLV_ERROR
, LOCATION
, NULL
,
210 "%s\n", eay_strerror());
214 i
= i2d_X509_NAME(name
, NULL
);
221 i
= i2d_X509_NAME(name
, (void *)&p
);
231 X509_NAME_free(name
);
238 * convert the hex string of the subject name into DER
241 eay_hex2asn1dn(const char *hex
, int len
)
243 BIGNUM
*bn
= BN_new();
251 if (BN_hex2bn(&bn
, hex
) != len
) {
252 plog(LLV_ERROR
, LOCATION
, NULL
,
253 "conversion of Hex-encoded ASN1 string to binary failed: %s\n",
258 binlen
= BN_num_bytes(bn
);
259 ret
= vmalloc(binlen
);
261 plog(LLV_WARNING
, LOCATION
, NULL
,"failed to allocate buffer\n");
266 BN_bn2bin(bn
, (unsigned char *) binbuf
);
275 * The following are derived from code in crypto/x509/x509_cmp.c
277 * X509_NAME_wildcmp() adds wildcard matching to the original
278 * X509_NAME_cmp(), nocase_cmp() and nocase_spacenorm_cmp() are as is.
281 /* Case insensitive string comparision */
282 static int nocase_cmp(const ASN1_STRING
*a
, const ASN1_STRING
*b
)
286 if (a
->length
!= b
->length
)
287 return (a
->length
- b
->length
);
289 for (i
=0; i
<a
->length
; i
++)
293 ca
= tolower(a
->data
[i
]);
294 cb
= tolower(b
->data
[i
]);
302 /* Case insensitive string comparision with space normalization
303 * Space normalization - ignore leading, trailing spaces,
304 * multiple spaces between characters are replaced by single space
306 static int nocase_spacenorm_cmp(const ASN1_STRING
*a
, const ASN1_STRING
*b
)
308 unsigned char *pa
= NULL
, *pb
= NULL
;
316 /* skip leading spaces */
317 while (la
> 0 && isspace(*pa
))
322 while (lb
> 0 && isspace(*pb
))
328 /* skip trailing spaces */
329 while (la
> 0 && isspace(pa
[la
-1]))
331 while (lb
> 0 && isspace(pb
[lb
-1]))
334 /* compare strings with space normalization */
335 while (la
> 0 && lb
> 0)
339 /* compare character */
348 if (la
<= 0 || lb
<= 0)
351 /* is white space next character ? */
352 if (isspace(*pa
) && isspace(*pb
))
354 /* skip remaining white spaces */
355 while (la
> 0 && isspace(*pa
))
360 while (lb
> 0 && isspace(*pb
))
367 if (la
> 0 || lb
> 0)
373 static int X509_NAME_wildcmp(const X509_NAME
*a
, const X509_NAME
*b
)
376 X509_NAME_ENTRY
*na
,*nb
;
378 if (sk_X509_NAME_ENTRY_num(a
->entries
)
379 != sk_X509_NAME_ENTRY_num(b
->entries
))
380 return sk_X509_NAME_ENTRY_num(a
->entries
)
381 -sk_X509_NAME_ENTRY_num(b
->entries
);
382 for (i
=sk_X509_NAME_ENTRY_num(a
->entries
)-1; i
>=0; i
--)
384 na
=sk_X509_NAME_ENTRY_value(a
->entries
,i
);
385 nb
=sk_X509_NAME_ENTRY_value(b
->entries
,i
);
386 j
=OBJ_cmp(na
->object
,nb
->object
);
388 if ((na
->value
->length
== 1 && na
->value
->data
[0] == '*')
389 || (nb
->value
->length
== 1 && nb
->value
->data
[0] == '*'))
391 j
=na
->value
->type
-nb
->value
->type
;
393 if (na
->value
->type
== V_ASN1_PRINTABLESTRING
)
394 j
=nocase_spacenorm_cmp(na
->value
, nb
->value
);
395 else if (na
->value
->type
== V_ASN1_IA5STRING
396 && OBJ_obj2nid(na
->object
) == NID_pkcs9_emailAddress
)
397 j
=nocase_cmp(na
->value
, nb
->value
);
400 j
=na
->value
->length
-nb
->value
->length
;
402 j
=memcmp(na
->value
->data
,nb
->value
->data
,
414 * compare two subjectNames.
420 eay_cmp_asn1dn(n1
, n2
)
423 X509_NAME
*a
= NULL
, *b
= NULL
;
428 if (!d2i_X509_NAME(&a
, (void *)&p
, n1
->l
))
431 if (!d2i_X509_NAME(&b
, (void *)&p
, n2
->l
))
434 i
= X509_NAME_wildcmp(a
, b
);
445 * this functions is derived from apps/verify.c in OpenSSL0.9.5
448 eay_check_x509cert(cert
, CApath
, CAfile
, local
)
454 X509_STORE
*cert_ctx
= NULL
;
455 X509_LOOKUP
*lookup
= NULL
;
460 cert_ctx
= X509_STORE_new();
461 if (cert_ctx
== NULL
)
465 X509_STORE_set_verify_cb_func(cert_ctx
, cb_check_cert_local
);
467 X509_STORE_set_verify_cb_func(cert_ctx
, cb_check_cert_remote
);
469 lookup
= X509_STORE_add_lookup(cert_ctx
, X509_LOOKUP_file());
473 X509_LOOKUP_load_file(lookup
, CAfile
,
474 (CAfile
== NULL
) ? X509_FILETYPE_DEFAULT
: X509_FILETYPE_PEM
);
476 lookup
= X509_STORE_add_lookup(cert_ctx
, X509_LOOKUP_hash_dir());
479 error
= X509_LOOKUP_add_dir(lookup
, CApath
, X509_FILETYPE_PEM
);
484 error
= -1; /* initialized */
486 /* read the certificate to be verified */
487 x509
= mem2x509(cert
);
491 csc
= X509_STORE_CTX_new();
494 X509_STORE_CTX_init(csc
, cert_ctx
, x509
, NULL
);
495 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
496 X509_STORE_CTX_set_flags (csc
, X509_V_FLAG_CRL_CHECK
);
497 X509_STORE_CTX_set_flags (csc
, X509_V_FLAG_CRL_CHECK_ALL
);
499 error
= X509_verify_cert(csc
);
500 X509_STORE_CTX_free(csc
);
503 * if x509_verify_cert() is successful then the value of error is
506 error
= error
? 0 : -1;
510 plog(LLV_WARNING
, LOCATION
, NULL
,"%s\n", eay_strerror());
511 if (cert_ctx
!= NULL
)
512 X509_STORE_free(cert_ctx
);
520 * callback function for verifing certificate.
521 * this function is derived from cb() in openssl/apps/s_server.c
524 cb_check_cert_local(ok
, ctx
)
533 X509_get_subject_name(ctx
->current_cert
),
537 * since we are just checking the certificates, it is
538 * ok if they are self signed. But we should still warn
541 switch (ctx
->error
) {
542 case X509_V_ERR_CERT_HAS_EXPIRED
:
543 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
:
544 case X509_V_ERR_INVALID_CA
:
545 case X509_V_ERR_PATH_LENGTH_EXCEEDED
:
546 case X509_V_ERR_INVALID_PURPOSE
:
547 case X509_V_ERR_UNABLE_TO_GET_CRL
:
549 log_tag
= LLV_WARNING
;
554 plog(log_tag
, LOCATION
, NULL
,
555 "%s(%d) at depth:%d SubjectName:%s\n",
556 X509_verify_cert_error_string(ctx
->error
),
567 * callback function for verifing remote certificates.
568 * this function is derived from cb() in openssl/apps/s_server.c
571 cb_check_cert_remote(ok
, ctx
)
580 X509_get_subject_name(ctx
->current_cert
),
583 switch (ctx
->error
) {
584 case X509_V_ERR_UNABLE_TO_GET_CRL
:
586 log_tag
= LLV_WARNING
;
591 plog(log_tag
, LOCATION
, NULL
,
592 "%s(%d) at depth:%d SubjectName:%s\n",
593 X509_verify_cert_error_string(ctx
->error
),
604 * get a subjectAltName from X509 certificate.
607 eay_get_x509asn1subjectname(cert
)
612 vchar_t
*name
= NULL
;
615 bp
= (unsigned char *) cert
->v
;
617 x509
= mem2x509(cert
);
621 /* get the length of the name */
622 len
= i2d_X509_NAME(x509
->cert_info
->subject
, NULL
);
627 bp
= (unsigned char *) name
->v
;
628 len
= i2d_X509_NAME(x509
->cert_info
->subject
, &bp
);
635 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
649 * Get the common name from a cert
651 #define EAY_MAX_CN_LEN 256
653 eay_get_x509_common_name(cert
)
658 vchar_t
*commonName
= NULL
;
660 commonName
= vmalloc(EAY_MAX_CN_LEN
);
661 if (commonName
== NULL
) {
662 plog(LLV_ERROR
, LOCATION
, NULL
, "no memory\n");
666 x509
= mem2x509(cert
);
672 name
= X509_get_subject_name(x509
);
673 X509_NAME_get_text_by_NID(name
, NID_commonName
, commonName
->v
, EAY_MAX_CN_LEN
);
675 commonName
->l
= strlen(commonName
->v
);
683 * get the subjectAltName from X509 certificate.
684 * the name must be terminated by '\0'.
687 eay_get_x509subjectaltname(cert
, altname
, type
, pos
, len
)
696 GENERAL_NAMES
*gens
= NULL
;
701 *type
= GENT_OTHERNAME
;
703 x509
= mem2x509(cert
);
707 gens
= X509_get_ext_d2i(x509
, NID_subject_alt_name
, NULL
, NULL
);
711 for (i
= 0; i
< sk_GENERAL_NAME_num(gens
); i
++) {
717 /* there is no data at "pos" */
718 if (i
== sk_GENERAL_NAME_num(gens
))
721 gen
= sk_GENERAL_NAME_value(gens
, i
);
723 /* make sure the data is terminated by '\0'. */
724 if (gen
->d
.ia5
->data
[gen
->d
.ia5
->length
] != '\0') {
725 plog(LLV_ERROR
, LOCATION
, NULL
,
726 "data is not terminated by 0.");
727 hexdump(gen
->d
.ia5
->data
, gen
->d
.ia5
->length
+ 1);
731 /* read DNSName / Email */
732 if (gen
->type
== GEN_DNS
||
733 gen
->type
== GEN_EMAIL
||
734 gen
->type
== GEN_URI
) {
736 *len
= gen
->d
.ia5
->length
+ 1;
737 *altname
= racoon_malloc(*len
);
741 strlcpy(*altname
, (const char *)gen
->d
.ia5
->data
, *len
);
745 } else if (gen
->type
== GEN_IPADD
) {
747 *len
= gen
->d
.ia5
->length
+ 1;
748 *altname
= racoon_malloc(*len
);
752 memcpy(*altname
, (const char *)gen
->d
.ia5
->data
, *len
);
761 racoon_free(*altname
);
765 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
767 printf("%s\n", eay_strerror());
771 /* free the whole stack. */
772 sk_GENERAL_NAME_pop_free(gens
, GENERAL_NAME_free
);
779 #else /* __APPLE__ */
782 * get the subjectAltName from X509 certificate.
783 * the name must be terminated by '\0'.
786 eay_get_x509subjectaltname(cert
, altname
, type
, pos
)
793 GENERAL_NAMES
*gens
= NULL
;
799 *type
= GENT_OTHERNAME
;
801 x509
= mem2x509(cert
);
805 gens
= X509_get_ext_d2i(x509
, NID_subject_alt_name
, NULL
, NULL
);
809 /* there is no data at "pos" */
810 if (pos
> sk_GENERAL_NAME_num(gens
))
813 gen
= sk_GENERAL_NAME_value(gens
, pos
- 1);
815 /* read DNSName / Email */
816 if (gen
->type
== GEN_DNS
||
817 gen
->type
== GEN_EMAIL
||
818 gen
->type
== GEN_URI
)
820 /* make sure if the data is terminated by '\0'. */
821 if (gen
->d
.ia5
->data
[gen
->d
.ia5
->length
] != '\0')
823 plog(LLV_ERROR
, LOCATION
, NULL
,
824 "data is not terminated by NUL.");
825 hexdump(gen
->d
.ia5
->data
, gen
->d
.ia5
->length
+ 1);
829 len
= gen
->d
.ia5
->length
+ 1;
830 *altname
= racoon_malloc(len
);
834 strlcpy(*altname
, (char *) gen
->d
.ia5
->data
, len
);
838 /* read IP address */
839 else if (gen
->type
== GEN_IPADD
)
841 unsigned char p
[5], *ip
;
842 const int maxaltnamelen
= 20;
845 /* only support IPv4 */
846 if (gen
->d
.ip
->length
!= 4)
849 /* convert Octet String to String
852 /*i2d_ASN1_OCTET_STRING(gen->d.ip,&ip);*/
853 ip
= gen
->d
.ip
->data
;
855 /* XXX Magic, enough for an IPv4 address
857 *altname
= racoon_malloc(maxaltnamelen
);
861 snprintf(*altname
, maxaltnamelen
, "%u.%u.%u.%u", ip
[0], ip
[1], ip
[2], ip
[3]);
865 /* XXX other possible types ?
866 * For now, error will be -1 if unsupported type
872 racoon_free(*altname
);
875 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
880 /* free the whole stack. */
881 sk_GENERAL_NAME_pop_free(gens
, GENERAL_NAME_free
);
889 * decode a X509 certificate and make a readable text terminated '\n'.
890 * return the buffer allocated, so must free it later.
893 eay_get_x509text(cert
)
903 x509
= mem2x509(cert
);
907 bio
= BIO_new(BIO_s_mem());
911 error
= X509_print(bio
, x509
);
917 len
= BIO_get_mem_data(bio
, &bp
);
918 text
= racoon_malloc(len
+ 1);
921 memcpy(text
, bp
, len
);
932 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
942 /* get X509 structure from buffer. */
953 bp
= (unsigned char *) cert
->v
;
955 x509
= d2i_X509(NULL
, (void *)&bp
, cert
->l
);
962 bio
= BIO_new(BIO_s_mem());
965 len
= BIO_write(bio
, cert
->v
, cert
->l
);
968 x509
= PEM_read_bio_X509(bio
, NULL
, NULL
, NULL
);
976 * get a X509 certificate from local file.
977 * a certificate must be PEM format.
979 * path to a certificate.
981 * NULL if error occured
985 eay_get_x509cert(path
)
995 /* Read private key */
996 fp
= fopen(path
, "r");
999 x509
= PEM_read_X509(fp
, NULL
, NULL
, NULL
);
1005 len
= i2d_X509(x509
, NULL
);
1006 cert
= vmalloc(len
);
1011 bp
= (unsigned char *) cert
->v
;
1012 error
= i2d_X509(x509
, &bp
);
1024 * check a X509 signature
1025 * XXX: to be get hash type from my cert ?
1026 * to be handled EVP_dss().
1027 * OUT: return -1 when error.
1031 eay_check_x509sign(source
, sig
, cert
)
1041 bp
= (unsigned char *) cert
->v
;
1043 x509
= d2i_X509(NULL
, (void *)&bp
, cert
->l
);
1045 plog(LLV_ERROR
, LOCATION
, NULL
, "d2i_X509(): %s\n", eay_strerror());
1049 evp
= X509_get_pubkey(x509
);
1051 plog(LLV_ERROR
, LOCATION
, NULL
, "X509_get_pubkey(): %s\n", eay_strerror());
1056 res
= eay_rsa_verify(source
, sig
, evp
->pkey
.rsa
);
1065 * check RSA signature
1066 * OUT: return -1 when error.
1070 eay_check_rsasign(source
, sig
, rsa
)
1075 return eay_rsa_verify(source
, sig
, rsa
);
1079 * get PKCS#1 Private Key of PEM format from local file.
1082 eay_get_pkcs1privkey(path
)
1086 EVP_PKEY
*evp
= NULL
;
1087 vchar_t
*pkey
= NULL
;
1092 /* Read private key */
1093 fp
= fopen(path
, "r");
1097 evp
= PEM_read_PrivateKey(fp
, NULL
, NULL
, NULL
);
1104 pkeylen
= i2d_PrivateKey(evp
, NULL
);
1107 pkey
= vmalloc(pkeylen
);
1110 bp
= (unsigned char *) pkey
->v
;
1111 pkeylen
= i2d_PrivateKey(evp
, &bp
);
1120 if (error
!= 0 && pkey
!= NULL
) {
1129 * get PKCS#1 Public Key of PEM format from local file.
1132 eay_get_pkcs1pubkey(path
)
1136 EVP_PKEY
*evp
= NULL
;
1137 vchar_t
*pkey
= NULL
;
1143 /* Read private key */
1144 fp
= fopen(path
, "r");
1148 x509
= PEM_read_X509(fp
, NULL
, NULL
, NULL
);
1155 /* Get public key - eay */
1156 evp
= X509_get_pubkey(x509
);
1160 pkeylen
= i2d_PublicKey(evp
, NULL
);
1163 pkey
= vmalloc(pkeylen
);
1166 bp
= (unsigned char *) pkey
->v
;
1167 pkeylen
= i2d_PublicKey(evp
, &bp
);
1175 if (error
!= 0 && pkey
!= NULL
) {
1184 eay_get_x509sign(src
, privkey
)
1185 vchar_t
*src
, *privkey
;
1188 u_char
*bp
= (unsigned char *) privkey
->v
;
1189 vchar_t
*sig
= NULL
;
1191 int pad
= RSA_PKCS1_PADDING
;
1193 /* XXX to be handled EVP_PKEY_DSA */
1194 evp
= d2i_PrivateKey(EVP_PKEY_RSA
, NULL
, (void *)&bp
, privkey
->l
);
1198 sig
= eay_rsa_sign(src
, evp
->pkey
.rsa
);
1206 eay_get_rsasign(src
, rsa
)
1210 return eay_rsa_sign(src
, rsa
);
1214 eay_rsa_sign(vchar_t
*src
, RSA
*rsa
)
1217 vchar_t
*sig
= NULL
;
1218 int pad
= RSA_PKCS1_PADDING
;
1220 len
= RSA_size(rsa
);
1226 len
= RSA_private_encrypt(src
->l
, (unsigned char *) src
->v
,
1227 (unsigned char *) sig
->v
, rsa
, pad
);
1229 if (len
== 0 || len
!= sig
->l
) {
1238 eay_rsa_verify(src
, sig
, rsa
)
1242 vchar_t
*xbuf
= NULL
;
1243 int pad
= RSA_PKCS1_PADDING
;
1247 len
= RSA_size(rsa
);
1248 xbuf
= vmalloc(len
);
1250 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
1254 len
= RSA_public_decrypt(sig
->l
, (unsigned char *) sig
->v
,
1255 (unsigned char *) xbuf
->v
, rsa
, pad
);
1256 if (len
== 0 || len
!= src
->l
) {
1257 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
1262 error
= memcmp(src
->v
, xbuf
->v
, src
->l
);
1272 * MUST load ERR_load_crypto_strings() first.
1277 static char ebuf
[512];
1281 const char *file
, *data
;
1285 es
= CRYPTO_thread_id();
1287 while ((l
= ERR_get_error_line_data(&file
, &line
, &data
, &flags
)) != 0){
1288 n
= snprintf(ebuf
+ len
, sizeof(ebuf
) - len
,
1290 es
, ERR_error_string(l
, buf
), file
, line
,
1291 (flags
& ERR_TXT_STRING
) ? data
: "");
1292 if (n
< 0 || n
>= sizeof(ebuf
) - len
)
1295 if (sizeof(ebuf
) < len
)
1303 evp_crypt(vchar_t
*data
, vchar_t
*key
, vchar_t
*iv
, const EVP_CIPHER
*e
, int enc
)
1311 if (data
->l
% EVP_CIPHER_block_size(e
))
1314 if ((res
= vmalloc(data
->l
)) == NULL
)
1317 EVP_CIPHER_CTX_init(&ctx
);
1319 switch(EVP_CIPHER_nid(e
)){
1326 case NID_cast5_cfb64
:
1327 case NID_cast5_ofb64
:
1328 /* XXX: can we do that also for algos with a fixed key size ?
1330 /* init context without key/iv
1332 if (!EVP_CipherInit(&ctx
, e
, NULL
, NULL
, enc
))
1341 if (!EVP_CIPHER_CTX_set_key_length(&ctx
, key
->l
))
1348 /* finalize context init with desired key size
1350 if (!EVP_CipherInit(&ctx
, NULL
, (u_char
*) key
->v
,
1351 (u_char
*) iv
->v
, enc
))
1359 if (!EVP_CipherInit(&ctx
, e
, (u_char
*) key
->v
,
1360 (u_char
*) iv
->v
, enc
)) {
1367 /* disable openssl padding */
1368 EVP_CIPHER_CTX_set_padding(&ctx
, 0);
1370 if (!EVP_Cipher(&ctx
, (u_char
*) res
->v
, (u_char
*) data
->v
, data
->l
)) {
1376 EVP_CIPHER_CTX_cleanup(&ctx
);
1382 evp_weakkey(vchar_t
*key
, const EVP_CIPHER
*e
)
1388 evp_keylen(int len
, const EVP_CIPHER
*e
)
1392 /* EVP functions return lengths in bytes, ipsec-tools
1393 * uses lengths in bits, therefore conversion is required. --AK
1395 if (len
!= 0 && len
!= (EVP_CIPHER_key_length(e
) << 3))
1398 return EVP_CIPHER_key_length(e
) << 3;
1402 eay_CCCrypt(CCOperation oper
,
1411 CCCryptorStatus status
;
1413 /* allocate buffer for result */
1414 if ((res
= vmalloc(data
->l
)) == NULL
)
1417 status
= CCCrypt(oper
,
1423 res
->v
, res
->l
, &res_len
);
1424 if (status
== kCCSuccess
) {
1425 if (res
->l
!= res_len
) {
1426 plog(LLV_ERROR
, LOCATION
, NULL
,
1427 "crypt %d %d length mismatch. expected: %zd. got: %zd.\n",
1428 oper
, algo
, res
->l
, res_len
);
1432 plog(LLV_ERROR
, LOCATION
, NULL
,
1433 "crypt %d %d error. status %d.\n",
1434 oper
, algo
, (int)status
);
1444 eay_des_encrypt(data
, key
, iv
)
1445 vchar_t
*data
, *key
, *iv
;
1448 return(eay_CCCrypt(kCCEncrypt
, kCCAlgorithmDES
, 0 /* CBC */, data
, key
, iv
));
1450 return evp_crypt(data
, key
, iv
, EVP_des_cbc(), 1);
1451 #endif /* __APPLE__ */
1455 eay_des_decrypt(data
, key
, iv
)
1456 vchar_t
*data
, *key
, *iv
;
1459 return(eay_CCCrypt(kCCDecrypt
, kCCAlgorithmDES
, 0 /* CBC */, data
, key
, iv
));
1461 return evp_crypt(data
, key
, iv
, EVP_des_cbc(), 0);
1462 #endif /* __APPLE__ */
1466 eay_des_weakkey(key
)
1469 #ifdef USE_NEW_DES_API
1470 return DES_is_weak_key((void *)key
->v
);
1472 return des_is_weak_key((void *)key
->v
);
1481 /* CommonCrypto return lengths in bytes, ipsec-tools
1482 * uses lengths in bits, therefore conversion is required.
1484 if (len
!= 0 && len
!= (kCCKeySizeDES
<< 3))
1487 return kCCKeySizeDES
<< 3;
1489 return evp_keylen(len
, EVP_des_cbc());
1490 #endif /* __APPLE__ */
1493 #ifdef HAVE_OPENSSL_IDEA_H
1498 eay_idea_encrypt(data
, key
, iv
)
1499 vchar_t
*data
, *key
, *iv
;
1502 IDEA_KEY_SCHEDULE ks
;
1504 idea_set_encrypt_key(key
->v
, &ks
);
1506 /* allocate buffer for result */
1507 if ((res
= vmalloc(data
->l
)) == NULL
)
1510 /* decryption data */
1511 idea_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1512 &ks
, iv
->v
, IDEA_ENCRYPT
);
1518 eay_idea_decrypt(data
, key
, iv
)
1519 vchar_t
*data
, *key
, *iv
;
1522 IDEA_KEY_SCHEDULE ks
, dks
;
1524 idea_set_encrypt_key(key
->v
, &ks
);
1525 idea_set_decrypt_key(&ks
, &dks
);
1527 /* allocate buffer for result */
1528 if ((res
= vmalloc(data
->l
)) == NULL
)
1531 /* decryption data */
1532 idea_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1533 &dks
, iv
->v
, IDEA_DECRYPT
);
1539 eay_idea_weakkey(key
)
1546 eay_idea_keylen(len
)
1549 if (len
!= 0 && len
!= 128)
1559 eay_bf_encrypt(data
, key
, iv
)
1560 vchar_t
*data
, *key
, *iv
;
1562 return evp_crypt(data
, key
, iv
, EVP_bf_cbc(), 1);
1566 eay_bf_decrypt(data
, key
, iv
)
1567 vchar_t
*data
, *key
, *iv
;
1569 return evp_crypt(data
, key
, iv
, EVP_bf_cbc(), 0);
1576 return 0; /* XXX to be done. refer to RFC 2451 */
1585 if (len
< 40 || len
> 448)
1590 #ifdef HAVE_OPENSSL_RC5_H
1595 eay_rc5_encrypt(data
, key
, iv
)
1596 vchar_t
*data
, *key
, *iv
;
1601 /* in RFC 2451, there is information about the number of round. */
1602 RC5_32_set_key(&ks
, key
->l
, key
->v
, 16);
1604 /* allocate buffer for result */
1605 if ((res
= vmalloc(data
->l
)) == NULL
)
1608 /* decryption data */
1609 RC5_32_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1610 &ks
, iv
->v
, RC5_ENCRYPT
);
1616 eay_rc5_decrypt(data
, key
, iv
)
1617 vchar_t
*data
, *key
, *iv
;
1622 /* in RFC 2451, there is information about the number of round. */
1623 RC5_32_set_key(&ks
, key
->l
, key
->v
, 16);
1625 /* allocate buffer for result */
1626 if ((res
= vmalloc(data
->l
)) == NULL
)
1629 /* decryption data */
1630 RC5_32_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1631 &ks
, iv
->v
, RC5_DECRYPT
);
1637 eay_rc5_weakkey(key
)
1640 return 0; /* No known weak keys when used with 16 rounds. */
1650 if (len
< 40 || len
> 2040)
1660 eay_3des_encrypt(data
, key
, iv
)
1661 vchar_t
*data
, *key
, *iv
;
1664 return(eay_CCCrypt(kCCEncrypt
, kCCAlgorithm3DES
, 0 /* CBC */, data
, key
, iv
));
1666 return evp_crypt(data
, key
, iv
, EVP_des_ede3_cbc(), 1);
1667 #endif /* __APPLE__ */
1671 eay_3des_decrypt(data
, key
, iv
)
1672 vchar_t
*data
, *key
, *iv
;
1675 return(eay_CCCrypt(kCCDecrypt
, kCCAlgorithm3DES
, 0 /* CBC */, data
, key
, iv
));
1677 return evp_crypt(data
, key
, iv
, EVP_des_ede3_cbc(), 0);
1678 #endif /* __APPLE__ */
1682 eay_3des_weakkey(key
)
1685 #ifdef USE_NEW_DES_API
1686 return (DES_is_weak_key((void *)key
->v
) ||
1687 DES_is_weak_key((void *)(key
->v
+ 8)) ||
1688 DES_is_weak_key((void *)(key
->v
+ 16)));
1693 return (des_is_weak_key((void *)key
->v
) ||
1694 des_is_weak_key((void *)(key
->v
+ 8)) ||
1695 des_is_weak_key((void *)(key
->v
+ 16)));
1700 eay_3des_keylen(len
)
1704 /* CommonCrypto return lengths in bytes, ipsec-tools
1705 * uses lengths in bits, therefore conversion is required.
1707 if (len
!= 0 && len
!= (kCCKeySize3DES
<< 3))
1710 return kCCKeySize3DES
<< 3;
1712 if (len
!= 0 && len
!= 192)
1715 #endif /* __APPLE__ */
1722 eay_cast_encrypt(data
, key
, iv
)
1723 vchar_t
*data
, *key
, *iv
;
1725 return evp_crypt(data
, key
, iv
, EVP_cast5_cbc(), 1);
1729 eay_cast_decrypt(data
, key
, iv
)
1730 vchar_t
*data
, *key
, *iv
;
1732 return evp_crypt(data
, key
, iv
, EVP_cast5_cbc(), 0);
1736 eay_cast_weakkey(key
)
1739 return 0; /* No known weak keys. */
1743 eay_cast_keylen(len
)
1748 if (len
< 40 || len
> 128)
1758 eay_aes_encrypt(data
, key
, iv
)
1759 vchar_t
*data
, *key
, *iv
;
1761 return(eay_CCCrypt(kCCEncrypt
, kCCAlgorithmAES128
/* adapts to AES-192, or AES-256 depending on the key size*/, 0 /* CBC */, data
, key
, iv
));
1765 eay_aes_decrypt(data
, key
, iv
)
1766 vchar_t
*data
, *key
, *iv
;
1768 return(eay_CCCrypt(kCCDecrypt
, kCCAlgorithmAES128
/* adapts to AES-192, or AES-256 depending on the key size*/, 0 /* CBC */, data
, key
, iv
));
1775 /* CommonCrypto return lengths in bytes, ipsec-tools
1776 * uses lengths in bits, therefore conversion is required.
1779 if (len
!= (kCCKeySizeAES128
<< 3) &&
1780 len
!= (kCCKeySizeAES192
<< 3) &&
1781 len
!= (kCCKeySizeAES256
<< 3))
1784 return kCCKeySizeAES128
<< 3;
1791 #ifndef HAVE_OPENSSL_AES_H
1793 eay_aes_encrypt(data
, key
, iv
)
1794 vchar_t
*data
, *key
, *iv
;
1800 memset(&k
, 0, sizeof(k
));
1801 if (rijndael_makeKey(&k
, DIR_ENCRYPT
, key
->l
<< 3, key
->v
) < 0)
1804 /* allocate buffer for result */
1805 if ((res
= vmalloc(data
->l
)) == NULL
)
1808 /* encryption data */
1809 memset(&c
, 0, sizeof(c
));
1810 if (rijndael_cipherInit(&c
, MODE_CBC
, iv
->v
) < 0){
1814 if (rijndael_blockEncrypt(&c
, &k
, data
->v
, data
->l
<< 3, res
->v
) < 0){
1823 eay_aes_decrypt(data
, key
, iv
)
1824 vchar_t
*data
, *key
, *iv
;
1830 memset(&k
, 0, sizeof(k
));
1831 if (rijndael_makeKey(&k
, DIR_DECRYPT
, key
->l
<< 3, key
->v
) < 0)
1834 /* allocate buffer for result */
1835 if ((res
= vmalloc(data
->l
)) == NULL
)
1838 /* decryption data */
1839 memset(&c
, 0, sizeof(c
));
1840 if (rijndael_cipherInit(&c
, MODE_CBC
, iv
->v
) < 0){
1844 if (rijndael_blockDecrypt(&c
, &k
, data
->v
, data
->l
<< 3, res
->v
) < 0){
1852 static inline const EVP_CIPHER
*
1853 aes_evp_by_keylen(int keylen
)
1858 return EVP_aes_128_cbc();
1861 return EVP_aes_192_cbc();
1864 return EVP_aes_256_cbc();
1871 eay_aes_encrypt(data
, key
, iv
)
1872 vchar_t
*data
, *key
, *iv
;
1874 return evp_crypt(data
, key
, iv
, aes_evp_by_keylen(key
->l
), 1);
1878 eay_aes_decrypt(data
, key
, iv
)
1879 vchar_t
*data
, *key
, *iv
;
1881 return evp_crypt(data
, key
, iv
, aes_evp_by_keylen(key
->l
), 0);
1883 #endif /* HAVE_OPENSSL_AES_H */
1891 if (len
!= 128 && len
!= 192 && len
!= 256)
1895 #endif /* __APPLE__ */
1898 eay_aes_weakkey(key
)
1904 /* for ipsec part */
1918 eay_twofish_keylen(len
)
1921 if (len
< 0 || len
> 256)
1927 eay_null_keylen(len
)
1939 eay_hmac_init(key
, algorithm
)
1941 CCHmacAlgorithm algorithm
;
1943 CCHmacContext
*c
= racoon_malloc(sizeof(*c
));
1945 CCHmacInit(c
, algorithm
, key
->v
, key
->l
);
1951 eay_hmac_init(key
, md
)
1955 HMAC_CTX
*c
= racoon_malloc(sizeof(*c
));
1957 HMAC_Init(c
, key
->v
, key
->l
, md
);
1961 #endif /* __APPLE__ */
1968 eay_hmacsha2_512_one(key
, data
)
1969 vchar_t
*key
, *data
;
1974 ctx
= eay_hmacsha2_512_init(key
);
1975 eay_hmacsha2_512_update(ctx
, data
);
1976 res
= eay_hmacsha2_512_final(ctx
);
1982 eay_hmacsha2_512_init(key
)
1986 return eay_hmac_init(key
, kCCHmacAlgSHA512
);
1988 return eay_hmac_init(key
, EVP_sha2_512());
1993 eay_hmacsha2_512_update(c
, data
)
1998 CCHmacUpdate((CCHmacContext
*)c
, data
->v
, data
->l
);
2000 HMAC_Update((HMAC_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
2006 eay_hmacsha2_512_final(c
)
2011 if ((res
= vmalloc(CC_SHA512_DIGEST_LENGTH
)) == 0)
2014 CCHmacFinal((CCHmacContext
*)c
, res
->v
);
2015 res
->l
= CC_SHA512_DIGEST_LENGTH
;
2017 (void)racoon_free(c
);
2022 eay_hmacsha2_512_final(c
)
2028 if ((res
= vmalloc(SHA512_DIGEST_LENGTH
)) == 0)
2031 HMAC_Final((HMAC_CTX
*)c
, (unsigned char *) res
->v
, &l
);
2033 HMAC_cleanup((HMAC_CTX
*)c
);
2035 (void)racoon_free(c
);
2037 if (SHA512_DIGEST_LENGTH
!= res
->l
) {
2038 plog(LLV_ERROR
, LOCATION
, NULL
,
2039 "hmac sha2_512 length mismatch %zd.\n", res
->l
);
2046 #endif /* __APPLE__ */
2052 eay_hmacsha2_384_one(key
, data
)
2053 vchar_t
*key
, *data
;
2058 ctx
= eay_hmacsha2_384_init(key
);
2059 eay_hmacsha2_384_update(ctx
, data
);
2060 res
= eay_hmacsha2_384_final(ctx
);
2066 eay_hmacsha2_384_init(key
)
2070 return eay_hmac_init(key
, kCCHmacAlgSHA384
);
2072 return eay_hmac_init(key
, EVP_sha2_384());
2077 eay_hmacsha2_384_update(c
, data
)
2082 CCHmacUpdate((CCHmacContext
*)c
, data
->v
, data
->l
);
2084 HMAC_Update((HMAC_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
2090 eay_hmacsha2_384_final(c
)
2095 if ((res
= vmalloc(CC_SHA384_DIGEST_LENGTH
)) == 0)
2098 CCHmacFinal((CCHmacContext
*)c
, res
->v
);
2099 res
->l
= CC_SHA384_DIGEST_LENGTH
;
2101 (void)racoon_free(c
);
2106 eay_hmacsha2_384_final(c
)
2112 if ((res
= vmalloc(SHA384_DIGEST_LENGTH
)) == 0)
2115 HMAC_Final((HMAC_CTX
*)c
, (unsigned char *) res
->v
, &l
);
2117 HMAC_cleanup((HMAC_CTX
*)c
);
2119 (void)racoon_free(c
);
2121 if (SHA384_DIGEST_LENGTH
!= res
->l
) {
2122 plog(LLV_ERROR
, LOCATION
, NULL
,
2123 "hmac sha2_384 length mismatch %zd.\n", res
->l
);
2130 #endif /* __APPLE__ */
2136 eay_hmacsha2_256_one(key
, data
)
2137 vchar_t
*key
, *data
;
2142 ctx
= eay_hmacsha2_256_init(key
);
2143 eay_hmacsha2_256_update(ctx
, data
);
2144 res
= eay_hmacsha2_256_final(ctx
);
2150 eay_hmacsha2_256_init(key
)
2154 return eay_hmac_init(key
, kCCHmacAlgSHA256
);
2156 return eay_hmac_init(key
, EVP_sha2_256());
2161 eay_hmacsha2_256_update(c
, data
)
2166 CCHmacUpdate((CCHmacContext
*)c
, data
->v
, data
->l
);
2168 HMAC_Update((HMAC_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
2174 eay_hmacsha2_256_final(c
)
2179 if ((res
= vmalloc(CC_SHA256_DIGEST_LENGTH
)) == 0)
2182 CCHmacFinal((CCHmacContext
*)c
, res
->v
);
2183 res
->l
= CC_SHA256_DIGEST_LENGTH
;
2185 (void)racoon_free(c
);
2190 eay_hmacsha2_256_final(c
)
2196 if ((res
= vmalloc(SHA256_DIGEST_LENGTH
)) == 0)
2199 HMAC_Final((HMAC_CTX
*)c
, (unsigned char *) res
->v
, &l
);
2201 HMAC_cleanup((HMAC_CTX
*)c
);
2203 (void)racoon_free(c
);
2205 if (SHA256_DIGEST_LENGTH
!= res
->l
) {
2206 plog(LLV_ERROR
, LOCATION
, NULL
,
2207 "hmac sha2_256 length mismatch %zd.\n", res
->l
);
2214 #endif /* __APPLE__ */
2215 #endif /* WITH_SHA2 */
2221 eay_hmacsha1_one(key
, data
)
2222 vchar_t
*key
, *data
;
2227 ctx
= eay_hmacsha1_init(key
);
2228 eay_hmacsha1_update(ctx
, data
);
2229 res
= eay_hmacsha1_final(ctx
);
2235 eay_hmacsha1_init(key
)
2239 return eay_hmac_init(key
, kCCHmacAlgSHA1
);
2241 return eay_hmac_init(key
, EVP_sha1());
2246 eay_hmacsha1_update(c
, data
)
2251 CCHmacUpdate((CCHmacContext
*)c
, data
->v
, data
->l
);
2253 HMAC_Update((HMAC_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
2259 eay_hmacsha1_final(c
)
2264 if ((res
= vmalloc(CC_SHA1_DIGEST_LENGTH
)) == 0)
2267 CCHmacFinal((CCHmacContext
*)c
, res
->v
);
2268 res
->l
= CC_SHA1_DIGEST_LENGTH
;
2270 (void)racoon_free(c
);
2275 eay_hmacsha1_final(c
)
2281 if ((res
= vmalloc(SHA_DIGEST_LENGTH
)) == 0)
2284 HMAC_Final((HMAC_CTX
*)c
, (unsigned char *) res
->v
, &l
);
2286 HMAC_cleanup((HMAC_CTX
*)c
);
2288 (void)racoon_free(c
);
2290 if (SHA_DIGEST_LENGTH
!= res
->l
) {
2291 plog(LLV_ERROR
, LOCATION
, NULL
,
2292 "hmac sha1 length mismatch %zd.\n", res
->l
);
2299 #endif /* __APPLE__ */
2305 eay_hmacmd5_one(key
, data
)
2306 vchar_t
*key
, *data
;
2311 ctx
= eay_hmacmd5_init(key
);
2312 eay_hmacmd5_update(ctx
, data
);
2313 res
= eay_hmacmd5_final(ctx
);
2319 eay_hmacmd5_init(key
)
2323 return eay_hmac_init(key
, kCCHmacAlgMD5
);
2325 return eay_hmac_init(key
, EVP_md5());
2330 eay_hmacmd5_update(c
, data
)
2335 CCHmacUpdate((CCHmacContext
*)c
, data
->v
, data
->l
);
2337 HMAC_Update((HMAC_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
2343 eay_hmacmd5_final(c
)
2348 if ((res
= vmalloc(CC_MD5_DIGEST_LENGTH
)) == 0)
2351 CCHmacFinal((CCHmacContext
*)c
, res
->v
);
2352 res
->l
= CC_MD5_DIGEST_LENGTH
;
2353 (void)racoon_free(c
);
2359 eay_hmacmd5_final(c
)
2365 if ((res
= vmalloc(MD5_DIGEST_LENGTH
)) == 0)
2368 HMAC_Final((HMAC_CTX
*)c
, (unsigned char *) res
->v
, &l
);
2370 HMAC_cleanup((HMAC_CTX
*)c
);
2372 (void)racoon_free(c
);
2374 if (MD5_DIGEST_LENGTH
!= res
->l
) {
2375 plog(LLV_ERROR
, LOCATION
, NULL
,
2376 "hmac md5 length mismatch %zd.\n", res
->l
);
2383 #endif /* __APPLE__ */
2387 * SHA2-512 functions
2392 SHA512_CTX
*c
= racoon_malloc(sizeof(*c
));
2400 eay_sha2_512_update(c
, data
)
2404 SHA512_Update((SHA512_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
2410 eay_sha2_512_final(c
)
2415 if ((res
= vmalloc(SHA512_DIGEST_LENGTH
)) == 0)
2418 SHA512_Final((unsigned char *) res
->v
, (SHA512_CTX
*)c
);
2419 (void)racoon_free(c
);
2425 eay_sha2_512_one(data
)
2431 ctx
= eay_sha2_512_init();
2432 eay_sha2_512_update(ctx
, data
);
2433 res
= eay_sha2_512_final(ctx
);
2439 eay_sha2_512_hashlen()
2441 return SHA512_DIGEST_LENGTH
<< 3;
2447 * SHA2-384 functions
2451 typedef SHA512_CTX SHA384_CTX
;
2457 SHA384_CTX
*c
= racoon_malloc(sizeof(*c
));
2465 eay_sha2_384_update(c
, data
)
2469 SHA384_Update((SHA384_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
2475 eay_sha2_384_final(c
)
2480 if ((res
= vmalloc(SHA384_DIGEST_LENGTH
)) == 0)
2483 SHA384_Final((unsigned char *) res
->v
, (SHA384_CTX
*)c
);
2484 (void)racoon_free(c
);
2490 eay_sha2_384_one(data
)
2496 ctx
= eay_sha2_384_init();
2497 eay_sha2_384_update(ctx
, data
);
2498 res
= eay_sha2_384_final(ctx
);
2504 eay_sha2_384_hashlen()
2506 return SHA384_DIGEST_LENGTH
<< 3;
2512 * SHA2-256 functions
2517 SHA256_CTX
*c
= racoon_malloc(sizeof(*c
));
2525 eay_sha2_256_update(c
, data
)
2529 SHA256_Update((SHA256_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
2535 eay_sha2_256_final(c
)
2540 if ((res
= vmalloc(SHA256_DIGEST_LENGTH
)) == 0)
2543 SHA256_Final((unsigned char *) res
->v
, (SHA256_CTX
*)c
);
2544 (void)racoon_free(c
);
2550 eay_sha2_256_one(data
)
2556 ctx
= eay_sha2_256_init();
2557 eay_sha2_256_update(ctx
, data
);
2558 res
= eay_sha2_256_final(ctx
);
2564 eay_sha2_256_hashlen()
2566 return SHA256_DIGEST_LENGTH
<< 3;
2576 SHA_CTX
*c
= racoon_malloc(sizeof(*c
));
2584 eay_sha1_update(c
, data
)
2588 SHA1_Update((SHA_CTX
*)c
, data
->v
, data
->l
);
2599 if ((res
= vmalloc(SHA_DIGEST_LENGTH
)) == 0)
2602 SHA1_Final((unsigned char *) res
->v
, (SHA_CTX
*)c
);
2603 (void)racoon_free(c
);
2615 ctx
= eay_sha1_init();
2616 eay_sha1_update(ctx
, data
);
2617 res
= eay_sha1_final(ctx
);
2625 return SHA_DIGEST_LENGTH
<< 3;
2634 MD5_CTX
*c
= racoon_malloc(sizeof(*c
));
2642 eay_md5_update(c
, data
)
2646 MD5_Update((MD5_CTX
*)c
, data
->v
, data
->l
);
2657 if ((res
= vmalloc(MD5_DIGEST_LENGTH
)) == 0)
2660 MD5_Final((unsigned char *) res
->v
, (MD5_CTX
*)c
);
2661 (void)racoon_free(c
);
2673 ctx
= eay_md5_init();
2674 eay_md5_update(ctx
, data
);
2675 res
= eay_md5_final(ctx
);
2683 return MD5_DIGEST_LENGTH
<< 3;
2688 * size: number of bytes.
2691 eay_set_random(size
)
2697 if ((r
= BN_new()) == NULL
)
2699 BN_rand(r
, size
* 8, 0, 0);
2710 eay_dh_generate(prime
, g
, publen
, pub
, priv
)
2711 vchar_t
*prime
, **pub
, **priv
;
2720 /* pre-process to generate number */
2721 if (eay_v2bn(&p
, prime
) < 0)
2724 if ((dh
= DH_new()) == NULL
)
2727 p
= NULL
; /* p is now part of dh structure */
2729 if ((dh
->g
= BN_new()) == NULL
)
2731 if (!BN_set_word(dh
->g
, g
))
2735 dh
->length
= publen
;
2737 /* generate public and private number */
2738 if (!DH_generate_key(dh
))
2741 /* copy results to buffers */
2742 if (eay_bn2v(pub
, dh
->pub_key
) < 0)
2744 if (eay_bn2v(priv
, dh
->priv_key
) < 0) {
2760 eay_dh_compute(prime
, g
, pub
, priv
, pub2
, key
)
2761 vchar_t
*prime
, *pub
, *priv
, *pub2
, **key
;
2764 BIGNUM
*dh_pub
= NULL
;
2767 unsigned char *v
= NULL
;
2770 /* make public number to compute */
2771 if (eay_v2bn(&dh_pub
, pub2
) < 0)
2774 /* make DH structure */
2775 if ((dh
= DH_new()) == NULL
)
2777 if (eay_v2bn(&dh
->p
, prime
) < 0)
2779 if (eay_v2bn(&dh
->pub_key
, pub
) < 0)
2781 if (eay_v2bn(&dh
->priv_key
, priv
) < 0)
2783 dh
->length
= pub2
->l
* 8;
2786 if ((dh
->g
= BN_new()) == NULL
)
2788 if (!BN_set_word(dh
->g
, g
))
2791 if ((v
= racoon_calloc(prime
->l
, sizeof(u_char
))) == NULL
)
2793 if ((l
= DH_compute_key(v
, dh_pub
, dh
)) == -1)
2795 memcpy((*key
)->v
+ (prime
->l
- l
), v
, l
);
2810 * convert vchar_t <-> BIGNUM.
2812 * vchar_t: unit is u_char, network endian, most significant byte first.
2813 * BIGNUM: unit is BN_ULONG, each of BN_ULONG is in host endian,
2814 * least significant BN_ULONG must come first.
2816 * hex value of "0x3ffe050104" is represented as follows:
2817 * vchar_t: 3f fe 05 01 04
2818 * BIGNUM (BN_ULONG = u_int8_t): 04 01 05 fe 3f
2819 * BIGNUM (BN_ULONG = u_int16_t): 0x0104 0xfe05 0x003f
2820 * BIGNUM (BN_ULONG = u_int32_t_t): 0xfe050104 0x0000003f
2827 if ((*bn
= BN_bin2bn((unsigned char *) var
->v
, var
->l
, NULL
)) == NULL
)
2838 *var
= vmalloc(bn
->top
* BN_BYTES
);
2842 (*var
)->l
= BN_bn2bin(bn
, (unsigned char *) (*var
)->v
);
2850 OpenSSL_add_all_algorithms();
2851 ERR_load_crypto_strings();
2852 #ifdef HAVE_OPENSSL_ENGINE_H
2853 ENGINE_load_builtin_engines();
2854 ENGINE_register_all_complete();
2859 base64_decode(char *in
, long inlen
)
2861 BIO
*bio
=NULL
, *b64
=NULL
;
2862 vchar_t
*res
= NULL
;
2866 outb
= malloc(inlen
* 2);
2869 bio
= BIO_new_mem_buf(in
, inlen
);
2870 b64
= BIO_new(BIO_f_base64());
2871 BIO_set_flags(b64
, BIO_FLAGS_BASE64_NO_NL
);
2872 bio
= BIO_push(b64
, bio
);
2874 outlen
= BIO_read(bio
, outb
, inlen
* 2);
2876 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
2880 res
= vmalloc(outlen
);
2884 memcpy(res
->v
, outb
, outlen
);
2896 base64_encode(char *in
, long inlen
)
2898 BIO
*bio
=NULL
, *b64
=NULL
;
2901 vchar_t
*res
= NULL
;
2903 bio
= BIO_new(BIO_s_mem());
2904 b64
= BIO_new(BIO_f_base64());
2905 BIO_set_flags(b64
, BIO_FLAGS_BASE64_NO_NL
);
2906 bio
= BIO_push(b64
, bio
);
2908 BIO_write(bio
, in
, inlen
);
2911 plen
= BIO_get_mem_data(bio
, &ptr
);
2912 res
= vmalloc(plen
+1);
2916 memcpy (res
->v
, ptr
, plen
);
2917 res
->v
[plen
] = '\0';
2927 binbuf_pubkey2rsa(vchar_t
*binbuf
)
2930 RSA
*rsa_pub
= NULL
;
2932 if (binbuf
->v
[0] > binbuf
->l
- 1) {
2933 plog(LLV_ERROR
, LOCATION
, NULL
, "Plain RSA pubkey format error: decoded string doesn't make sense.\n");
2937 exp
= BN_bin2bn((unsigned char *) (binbuf
->v
+ 1), binbuf
->v
[0], NULL
);
2938 mod
= BN_bin2bn((unsigned char *) (binbuf
->v
+ binbuf
->v
[0] + 1),
2939 binbuf
->l
- binbuf
->v
[0] - 1, NULL
);
2940 rsa_pub
= RSA_new();
2942 if (!exp
|| !mod
|| !rsa_pub
) {
2943 plog(LLV_ERROR
, LOCATION
, NULL
, "Plain RSA pubkey parsing error: %s\n", eay_strerror());
2962 base64_pubkey2rsa(char *in
)
2965 RSA
*rsa_pub
= NULL
;
2968 if (strncmp(in
, "0s", 2) != 0) {
2969 plog(LLV_ERROR
, LOCATION
, NULL
, "Plain RSA pubkey format error: doesn't start with '0s'\n");
2973 binbuf
= base64_decode(in
+ 2, strlen(in
+ 2));
2975 plog(LLV_ERROR
, LOCATION
, NULL
, "Plain RSA pubkey format error: Base64 decoding failed.\n");
2979 if (binbuf
->v
[0] > binbuf
->l
- 1) {
2980 plog(LLV_ERROR
, LOCATION
, NULL
, "Plain RSA pubkey format error: decoded string doesn't make sense.\n");
2984 rsa_pub
= binbuf_pubkey2rsa(binbuf
);
2994 bignum_pubkey2rsa(BIGNUM
*in
)
2996 RSA
*rsa_pub
= NULL
;
2999 binbuf
= vmalloc(BN_num_bytes(in
));
3001 plog(LLV_ERROR
, LOCATION
, NULL
, "Plain RSA pubkey conversion: memory allocation failed..\n");
3005 BN_bn2bin(in
, (unsigned char *) binbuf
->v
);
3007 rsa_pub
= binbuf_pubkey2rsa(binbuf
);
3022 vrand
= eay_set_random(sizeof(result
));
3023 memcpy(&result
, vrand
->v
, sizeof(result
));
3032 return SSLeay_version(SSLEAY_VERSION
);