1 /* $Id: crypto_openssl.c,v 1.40.4.5 2005/07/12 11:50:15 manubsd 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
35 #define COMMON_DIGEST_FOR_OPENSSL 1
38 #include <sys/types.h>
39 #include <sys/param.h>
46 /* get openssl/ssleay version number */
47 #include <openssl/opensslv.h>
49 #if !defined(OPENSSL_VERSION_NUMBER) || (OPENSSL_VERSION_NUMBER < 0x0090602fL)
50 #error OpenSSL version 0.9.6 or later required.
53 #include <openssl/pem.h>
54 #include <openssl/evp.h>
55 #include <openssl/x509.h>
56 #include <openssl/x509v3.h>
57 #include <openssl/x509_vfy.h>
58 #include <openssl/bn.h>
59 #include <openssl/dh.h>
61 #include <CommonCrypto/CommonDigest.h>
62 #include <CommonCrypto/CommonHMAC.h>
64 #include <openssl/md5.h>
65 #include <openssl/sha.h>
66 #include <openssl/hmac.h>
68 #include <openssl/des.h>
69 #include <openssl/crypto.h>
70 #ifdef HAVE_OPENSSL_ENGINE_H
71 #include <openssl/engine.h>
73 #include <openssl/blowfish.h>
74 #include <openssl/cast.h>
75 #include <openssl/err.h>
76 #ifdef HAVE_OPENSSL_RC5_H
77 #include <openssl/rc5.h>
79 #ifdef HAVE_OPENSSL_IDEA_H
80 #include <openssl/idea.h>
82 #if defined(HAVE_OPENSSL_AES_H)
83 #include <openssl/aes.h>
84 #elif defined(HAVE_OPENSSL_RIJNDAEL_H)
85 #include <openssl/rijndael.h>
87 #include "crypto/rijndael/rijndael-api-fst.h"
91 #ifdef HAVE_OPENSSL_SHA2_H
92 #include <openssl/sha2.h>
98 #if OPENSSL_VERSION_NUMBER < 0x0090700fL
99 typedef STACK_OF(GENERAL_NAME
) GENERAL_NAMES
;
101 #define USE_NEW_DES_API
104 #define OpenSSL_BUG() do { plog(LLV_ERROR, LOCATION, NULL, "OpenSSL function failed\n"); } while(0)
110 #include "crypto_openssl.h"
112 #include "gcmalloc.h"
116 * I hate to cast every parameter to des_xx into void *, but it is
117 * necessary for SSLeay/OpenSSL portability. It sucks.
120 static int cb_check_cert_local
__P((int, X509_STORE_CTX
*));
121 static int cb_check_cert_remote
__P((int, X509_STORE_CTX
*));
122 static X509
*mem2x509
__P((vchar_t
*));
125 static caddr_t eay_hmac_init
__P((vchar_t
*, CCHmacAlgorithm
));
127 static caddr_t eay_hmac_init
__P((vchar_t
*, const EVP_MD
*));
130 /* X509 Certificate */
132 * convert the string of the subject name into DER
133 * e.g. str = "C=JP, ST=Kanagawa";
136 eay_str2asn1dn(str
, len
)
150 buf
= racoon_malloc(len
+ 1);
152 printf("failed to allocate buffer\n");
155 memcpy(buf
, str
, len
);
157 name
= X509_NAME_new();
161 for (i
= 0; i
< len
; i
++) {
162 if (!value
&& buf
[i
] == '=') {
166 } else if (buf
[i
] == ',' || buf
[i
] == '/') {
169 plog(LLV_DEBUG
, LOCATION
, NULL
, "DN: %s=%s\n",
172 if (!value
) goto err
;
173 if (!X509_NAME_add_entry_by_txt(name
, field
,
174 (value
[0] == '*' && value
[1] == 0) ?
175 V_ASN1_PRINTABLESTRING
: MBSTRING_ASC
,
176 (unsigned char *) value
, -1, -1, 0)) {
177 plog(LLV_ERROR
, LOCATION
, NULL
,
178 "Invalid DN field: %s=%s\n",
180 plog(LLV_ERROR
, LOCATION
, NULL
,
181 "%s\n", eay_strerror());
184 for (j
= i
+ 1; j
< len
; j
++) {
195 plog(LLV_DEBUG
, LOCATION
, NULL
, "DN: %s=%s\n",
198 if (!value
) goto err
;
199 if (!X509_NAME_add_entry_by_txt(name
, field
,
200 (value
[0] == '*' && value
[1] == 0) ?
201 V_ASN1_PRINTABLESTRING
: MBSTRING_ASC
,
202 (unsigned char *) value
, -1, -1, 0)) {
203 plog(LLV_ERROR
, LOCATION
, NULL
,
204 "Invalid DN field: %s=%s\n",
206 plog(LLV_ERROR
, LOCATION
, NULL
,
207 "%s\n", eay_strerror());
211 i
= i2d_X509_NAME(name
, NULL
);
218 i
= i2d_X509_NAME(name
, (void *)&p
);
228 X509_NAME_free(name
);
233 * convert the hex string of the subject name into DER
236 eay_hex2asn1dn(const char *hex
, int len
)
238 BIGNUM
*bn
= BN_new();
246 if (BN_hex2bn(&bn
, hex
) != len
) {
247 plog(LLV_ERROR
, LOCATION
, NULL
,
248 "conversion of Hex-encoded ASN1 string to binary failed: %s\n",
253 binlen
= BN_num_bytes(bn
);
254 ret
= vmalloc(binlen
);
256 printf("failed to allocate buffer\n");
261 BN_bn2bin(bn
, (unsigned char *) binbuf
);
270 * The following are derived from code in crypto/x509/x509_cmp.c
272 * X509_NAME_wildcmp() adds wildcard matching to the original
273 * X509_NAME_cmp(), nocase_cmp() and nocase_spacenorm_cmp() are as is.
276 /* Case insensitive string comparision */
277 static int nocase_cmp(const ASN1_STRING
*a
, const ASN1_STRING
*b
)
281 if (a
->length
!= b
->length
)
282 return (a
->length
- b
->length
);
284 for (i
=0; i
<a
->length
; i
++)
288 ca
= tolower(a
->data
[i
]);
289 cb
= tolower(b
->data
[i
]);
297 /* Case insensitive string comparision with space normalization
298 * Space normalization - ignore leading, trailing spaces,
299 * multiple spaces between characters are replaced by single space
301 static int nocase_spacenorm_cmp(const ASN1_STRING
*a
, const ASN1_STRING
*b
)
303 unsigned char *pa
= NULL
, *pb
= NULL
;
311 /* skip leading spaces */
312 while (la
> 0 && isspace(*pa
))
317 while (lb
> 0 && isspace(*pb
))
323 /* skip trailing spaces */
324 while (la
> 0 && isspace(pa
[la
-1]))
326 while (lb
> 0 && isspace(pb
[lb
-1]))
329 /* compare strings with space normalization */
330 while (la
> 0 && lb
> 0)
334 /* compare character */
343 if (la
<= 0 || lb
<= 0)
346 /* is white space next character ? */
347 if (isspace(*pa
) && isspace(*pb
))
349 /* skip remaining white spaces */
350 while (la
> 0 && isspace(*pa
))
355 while (lb
> 0 && isspace(*pb
))
362 if (la
> 0 || lb
> 0)
368 static int X509_NAME_wildcmp(const X509_NAME
*a
, const X509_NAME
*b
)
371 X509_NAME_ENTRY
*na
,*nb
;
373 if (sk_X509_NAME_ENTRY_num(a
->entries
)
374 != sk_X509_NAME_ENTRY_num(b
->entries
))
375 return sk_X509_NAME_ENTRY_num(a
->entries
)
376 -sk_X509_NAME_ENTRY_num(b
->entries
);
377 for (i
=sk_X509_NAME_ENTRY_num(a
->entries
)-1; i
>=0; i
--)
379 na
=sk_X509_NAME_ENTRY_value(a
->entries
,i
);
380 nb
=sk_X509_NAME_ENTRY_value(b
->entries
,i
);
381 j
=OBJ_cmp(na
->object
,nb
->object
);
383 if ((na
->value
->length
== 1 && na
->value
->data
[0] == '*')
384 || (nb
->value
->length
== 1 && nb
->value
->data
[0] == '*'))
386 j
=na
->value
->type
-nb
->value
->type
;
388 if (na
->value
->type
== V_ASN1_PRINTABLESTRING
)
389 j
=nocase_spacenorm_cmp(na
->value
, nb
->value
);
390 else if (na
->value
->type
== V_ASN1_IA5STRING
391 && OBJ_obj2nid(na
->object
) == NID_pkcs9_emailAddress
)
392 j
=nocase_cmp(na
->value
, nb
->value
);
395 j
=na
->value
->length
-nb
->value
->length
;
397 j
=memcmp(na
->value
->data
,nb
->value
->data
,
409 * compare two subjectNames.
415 eay_cmp_asn1dn(n1
, n2
)
418 X509_NAME
*a
= NULL
, *b
= NULL
;
423 if (!d2i_X509_NAME(&a
, (void *)&p
, n1
->l
))
426 if (!d2i_X509_NAME(&b
, (void *)&p
, n2
->l
))
429 i
= X509_NAME_wildcmp(a
, b
);
440 * this functions is derived from apps/verify.c in OpenSSL0.9.5
443 eay_check_x509cert(cert
, CApath
, CAfile
, local
)
449 X509_STORE
*cert_ctx
= NULL
;
450 X509_LOOKUP
*lookup
= NULL
;
455 cert_ctx
= X509_STORE_new();
456 if (cert_ctx
== NULL
)
460 X509_STORE_set_verify_cb_func(cert_ctx
, cb_check_cert_local
);
462 X509_STORE_set_verify_cb_func(cert_ctx
, cb_check_cert_remote
);
464 lookup
= X509_STORE_add_lookup(cert_ctx
, X509_LOOKUP_file());
468 X509_LOOKUP_load_file(lookup
, CAfile
,
469 (CAfile
== NULL
) ? X509_FILETYPE_DEFAULT
: X509_FILETYPE_PEM
);
471 lookup
= X509_STORE_add_lookup(cert_ctx
, X509_LOOKUP_hash_dir());
474 error
= X509_LOOKUP_add_dir(lookup
, CApath
, X509_FILETYPE_PEM
);
479 error
= -1; /* initialized */
481 /* read the certificate to be verified */
482 x509
= mem2x509(cert
);
486 csc
= X509_STORE_CTX_new();
489 X509_STORE_CTX_init(csc
, cert_ctx
, x509
, NULL
);
490 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
491 X509_STORE_CTX_set_flags (csc
, X509_V_FLAG_CRL_CHECK
);
492 X509_STORE_CTX_set_flags (csc
, X509_V_FLAG_CRL_CHECK_ALL
);
494 error
= X509_verify_cert(csc
);
495 X509_STORE_CTX_cleanup(csc
);
498 * if x509_verify_cert() is successful then the value of error is
501 error
= error
? 0 : -1;
505 printf("%s\n", eay_strerror());
506 if (cert_ctx
!= NULL
)
507 X509_STORE_free(cert_ctx
);
515 * callback function for verifing certificate.
516 * this function is derived from cb() in openssl/apps/s_server.c
519 cb_check_cert_local(ok
, ctx
)
528 X509_get_subject_name(ctx
->current_cert
),
532 * since we are just checking the certificates, it is
533 * ok if they are self signed. But we should still warn
536 switch (ctx
->error
) {
537 case X509_V_ERR_CERT_HAS_EXPIRED
:
538 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
:
539 case X509_V_ERR_INVALID_CA
:
540 case X509_V_ERR_PATH_LENGTH_EXCEEDED
:
541 case X509_V_ERR_INVALID_PURPOSE
:
542 case X509_V_ERR_UNABLE_TO_GET_CRL
:
544 log_tag
= LLV_WARNING
;
549 plog(log_tag
, LOCATION
, NULL
,
550 "%s(%d) at depth:%d SubjectName:%s\n",
551 X509_verify_cert_error_string(ctx
->error
),
562 * callback function for verifing remote certificates.
563 * this function is derived from cb() in openssl/apps/s_server.c
566 cb_check_cert_remote(ok
, ctx
)
575 X509_get_subject_name(ctx
->current_cert
),
578 switch (ctx
->error
) {
579 case X509_V_ERR_UNABLE_TO_GET_CRL
:
581 log_tag
= LLV_WARNING
;
586 plog(log_tag
, LOCATION
, NULL
,
587 "%s(%d) at depth:%d SubjectName:%s\n",
588 X509_verify_cert_error_string(ctx
->error
),
599 * get a subjectAltName from X509 certificate.
602 eay_get_x509asn1subjectname(cert
)
607 vchar_t
*name
= NULL
;
611 bp
= (unsigned char *) cert
->v
;
613 x509
= mem2x509(cert
);
617 /* get the length of the name */
618 len
= i2d_X509_NAME(x509
->cert_info
->subject
, NULL
);
623 bp
= (unsigned char *) name
->v
;
624 len
= i2d_X509_NAME(x509
->cert_info
->subject
, &bp
);
630 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
645 * Get the common name from a cert
647 #define EAY_MAX_CN_LEN 256
649 eay_get_x509_common_name(cert
)
654 vchar_t
*commonName
= NULL
;
656 commonName
= vmalloc(EAY_MAX_CN_LEN
);
657 if (commonName
== NULL
) {
658 plog(LLV_ERROR
, LOCATION
, NULL
, "no memory\n");
662 x509
= mem2x509(cert
);
668 name
= X509_get_subject_name(x509
);
669 X509_NAME_get_text_by_NID(name
, NID_commonName
, commonName
->v
, EAY_MAX_CN_LEN
);
671 commonName
->l
= strlen(commonName
->v
);
679 * get the subjectAltName from X509 certificate.
680 * the name must be terminated by '\0'.
683 eay_get_x509subjectaltname(cert
, altname
, type
, pos
, len
)
697 *type
= GENT_OTHERNAME
;
699 x509
= mem2x509(cert
);
703 gens
= X509_get_ext_d2i(x509
, NID_subject_alt_name
, NULL
, NULL
);
707 for (i
= 0; i
< sk_GENERAL_NAME_num(gens
); i
++) {
713 /* there is no data at "pos" */
714 if (i
== sk_GENERAL_NAME_num(gens
))
717 gen
= sk_GENERAL_NAME_value(gens
, i
);
719 /* make sure the data is terminated by '\0'. */
720 if (gen
->d
.ia5
->data
[gen
->d
.ia5
->length
] != '\0') {
721 plog(LLV_ERROR
, LOCATION
, NULL
,
722 "data is not terminated by 0.");
723 hexdump(gen
->d
.ia5
->data
, gen
->d
.ia5
->length
+ 1);
727 /* read DNSName / Email */
728 if (gen
->type
== GEN_DNS
||
729 gen
->type
== GEN_EMAIL
||
730 gen
->type
== GEN_URI
) {
732 *len
= gen
->d
.ia5
->length
+ 1;
733 *altname
= racoon_malloc(*len
);
737 strlcpy(*altname
, (const char *)gen
->d
.ia5
->data
, *len
);
741 } else if (gen
->type
== GEN_IPADD
) {
743 *len
= gen
->d
.ia5
->length
+ 1;
744 *altname
= racoon_malloc(*len
);
748 memcpy(*altname
, (const char *)gen
->d
.ia5
->data
, *len
);
757 racoon_free(*altname
);
761 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
763 printf("%s\n", eay_strerror());
772 #else /* __APPLE__ */
775 * get the subjectAltName from X509 certificate.
776 * the name must be terminated by '\0'.
779 eay_get_x509subjectaltname(cert
, altname
, type
, pos
)
786 GENERAL_NAMES
*gens
= NULL
;
792 *type
= GENT_OTHERNAME
;
794 x509
= mem2x509(cert
);
798 gens
= X509_get_ext_d2i(x509
, NID_subject_alt_name
, NULL
, NULL
);
802 /* there is no data at "pos" */
803 if (pos
> sk_GENERAL_NAME_num(gens
))
806 gen
= sk_GENERAL_NAME_value(gens
, pos
- 1);
808 /* read DNSName / Email */
809 if (gen
->type
== GEN_DNS
||
810 gen
->type
== GEN_EMAIL
||
811 gen
->type
== GEN_URI
)
813 /* make sure if the data is terminated by '\0'. */
814 if (gen
->d
.ia5
->data
[gen
->d
.ia5
->length
] != '\0')
816 plog(LLV_ERROR
, LOCATION
, NULL
,
817 "data is not terminated by NUL.");
818 hexdump(gen
->d
.ia5
->data
, gen
->d
.ia5
->length
+ 1);
822 len
= gen
->d
.ia5
->length
+ 1;
823 *altname
= racoon_malloc(len
);
827 strlcpy(*altname
, (char *) gen
->d
.ia5
->data
, len
);
831 /* read IP address */
832 else if (gen
->type
== GEN_IPADD
)
834 unsigned char p
[5], *ip
;
835 const int maxaltnamelen
= 20;
838 /* only support IPv4 */
839 if (gen
->d
.ip
->length
!= 4)
842 /* convert Octet String to String
845 /*i2d_ASN1_OCTET_STRING(gen->d.ip,&ip);*/
846 ip
= gen
->d
.ip
->data
;
848 /* XXX Magic, enough for an IPv4 address
850 *altname
= racoon_malloc(maxaltnamelen
);
854 snprintf(*altname
, maxaltnamelen
, "%u.%u.%u.%u", ip
[0], ip
[1], ip
[2], ip
[3]);
858 /* XXX other possible types ?
859 * For now, error will be -1 if unsupported type
865 racoon_free(*altname
);
868 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
873 /* free the whole stack. */
874 sk_GENERAL_NAME_pop_free(gens
, GENERAL_NAME_free
);
882 * decode a X509 certificate and make a readable text terminated '\n'.
883 * return the buffer allocated, so must free it later.
886 eay_get_x509text(cert
)
896 x509
= mem2x509(cert
);
900 bio
= BIO_new(BIO_s_mem());
904 error
= X509_print(bio
, x509
);
910 len
= BIO_get_mem_data(bio
, &bp
);
911 text
= racoon_malloc(len
+ 1);
914 memcpy(text
, bp
, len
);
925 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
935 /* get X509 structure from buffer. */
946 bp
= (unsigned char *) cert
->v
;
948 x509
= d2i_X509(NULL
, (void *)&bp
, cert
->l
);
955 bio
= BIO_new(BIO_s_mem());
958 len
= BIO_write(bio
, cert
->v
, cert
->l
);
961 x509
= PEM_read_bio_X509(bio
, NULL
, NULL
, NULL
);
969 * get a X509 certificate from local file.
970 * a certificate must be PEM format.
972 * path to a certificate.
974 * NULL if error occured
978 eay_get_x509cert(path
)
988 /* Read private key */
989 fp
= fopen(path
, "r");
992 x509
= PEM_read_X509(fp
, NULL
, NULL
, NULL
);
998 len
= i2d_X509(x509
, NULL
);
1004 bp
= (unsigned char *) cert
->v
;
1005 error
= i2d_X509(x509
, &bp
);
1017 * check a X509 signature
1018 * XXX: to be get hash type from my cert ?
1019 * to be handled EVP_dss().
1020 * OUT: return -1 when error.
1024 eay_check_x509sign(source
, sig
, cert
)
1034 bp
= (unsigned char *) cert
->v
;
1036 x509
= d2i_X509(NULL
, (void *)&bp
, cert
->l
);
1038 plog(LLV_ERROR
, LOCATION
, NULL
, "d2i_X509(): %s\n", eay_strerror());
1042 evp
= X509_get_pubkey(x509
);
1044 plog(LLV_ERROR
, LOCATION
, NULL
, "X509_get_pubkey(): %s\n", eay_strerror());
1048 res
= eay_rsa_verify(source
, sig
, evp
->pkey
.rsa
);
1056 * check RSA signature
1057 * OUT: return -1 when error.
1061 eay_check_rsasign(source
, sig
, rsa
)
1066 return eay_rsa_verify(source
, sig
, rsa
);
1070 * get PKCS#1 Private Key of PEM format from local file.
1073 eay_get_pkcs1privkey(path
)
1077 EVP_PKEY
*evp
= NULL
;
1078 vchar_t
*pkey
= NULL
;
1083 /* Read private key */
1084 fp
= fopen(path
, "r");
1088 evp
= PEM_read_PrivateKey(fp
, NULL
, NULL
, NULL
);
1095 pkeylen
= i2d_PrivateKey(evp
, NULL
);
1098 pkey
= vmalloc(pkeylen
);
1101 bp
= (unsigned char *) pkey
->v
;
1102 pkeylen
= i2d_PrivateKey(evp
, &bp
);
1111 if (error
!= 0 && pkey
!= NULL
) {
1120 * get PKCS#1 Public Key of PEM format from local file.
1123 eay_get_pkcs1pubkey(path
)
1127 EVP_PKEY
*evp
= NULL
;
1128 vchar_t
*pkey
= NULL
;
1134 /* Read private key */
1135 fp
= fopen(path
, "r");
1139 x509
= PEM_read_X509(fp
, NULL
, NULL
, NULL
);
1146 /* Get public key - eay */
1147 evp
= X509_get_pubkey(x509
);
1151 pkeylen
= i2d_PublicKey(evp
, NULL
);
1154 pkey
= vmalloc(pkeylen
);
1157 bp
= (unsigned char *) pkey
->v
;
1158 pkeylen
= i2d_PublicKey(evp
, &bp
);
1166 if (error
!= 0 && pkey
!= NULL
) {
1175 eay_get_x509sign(src
, privkey
)
1176 vchar_t
*src
, *privkey
;
1179 u_char
*bp
= (unsigned char *) privkey
->v
;
1180 vchar_t
*sig
= NULL
;
1182 int pad
= RSA_PKCS1_PADDING
;
1184 /* XXX to be handled EVP_PKEY_DSA */
1185 evp
= d2i_PrivateKey(EVP_PKEY_RSA
, NULL
, (void *)&bp
, privkey
->l
);
1189 sig
= eay_rsa_sign(src
, evp
->pkey
.rsa
);
1197 eay_get_rsasign(src
, rsa
)
1201 return eay_rsa_sign(src
, rsa
);
1205 eay_rsa_sign(vchar_t
*src
, RSA
*rsa
)
1208 vchar_t
*sig
= NULL
;
1209 int pad
= RSA_PKCS1_PADDING
;
1211 len
= RSA_size(rsa
);
1217 len
= RSA_private_encrypt(src
->l
, (unsigned char *) src
->v
,
1218 (unsigned char *) sig
->v
, rsa
, pad
);
1220 if (len
== 0 || len
!= sig
->l
) {
1229 eay_rsa_verify(src
, sig
, rsa
)
1233 vchar_t
*xbuf
= NULL
;
1234 int pad
= RSA_PKCS1_PADDING
;
1238 len
= RSA_size(rsa
);
1239 xbuf
= vmalloc(len
);
1241 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
1245 len
= RSA_public_decrypt(sig
->l
, (unsigned char *) sig
->v
,
1246 (unsigned char *) xbuf
->v
, rsa
, pad
);
1247 if (len
== 0 || len
!= src
->l
) {
1248 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
1253 error
= memcmp(src
->v
, xbuf
->v
, src
->l
);
1263 * MUST load ERR_load_crypto_strings() first.
1268 static char ebuf
[512];
1272 const char *file
, *data
;
1276 es
= CRYPTO_thread_id();
1278 while ((l
= ERR_get_error_line_data(&file
, &line
, &data
, &flags
)) != 0){
1279 n
= snprintf(ebuf
+ len
, sizeof(ebuf
) - len
,
1281 es
, ERR_error_string(l
, buf
), file
, line
,
1282 (flags
& ERR_TXT_STRING
) ? data
: "");
1283 if (n
< 0 || n
>= sizeof(ebuf
) - len
)
1286 if (sizeof(ebuf
) < len
)
1294 evp_crypt(vchar_t
*data
, vchar_t
*key
, vchar_t
*iv
, const EVP_CIPHER
*e
, int enc
)
1302 if (data
->l
% EVP_CIPHER_block_size(e
))
1305 if ((res
= vmalloc(data
->l
)) == NULL
)
1308 EVP_CIPHER_CTX_init(&ctx
);
1310 switch(EVP_CIPHER_nid(e
)){
1317 case NID_cast5_cfb64
:
1318 case NID_cast5_ofb64
:
1319 /* XXX: can we do that also for algos with a fixed key size ?
1321 /* init context without key/iv
1323 if (!EVP_CipherInit(&ctx
, e
, NULL
, NULL
, enc
))
1332 if (!EVP_CIPHER_CTX_set_key_length(&ctx
, key
->l
))
1339 /* finalize context init with desired key size
1341 if (!EVP_CipherInit(&ctx
, NULL
, (u_char
*) key
->v
,
1342 (u_char
*) iv
->v
, enc
))
1350 if (!EVP_CipherInit(&ctx
, e
, (u_char
*) key
->v
,
1351 (u_char
*) iv
->v
, enc
)) {
1358 /* disable openssl padding */
1359 EVP_CIPHER_CTX_set_padding(&ctx
, 0);
1361 if (!EVP_Cipher(&ctx
, (u_char
*) res
->v
, (u_char
*) data
->v
, data
->l
)) {
1367 EVP_CIPHER_CTX_cleanup(&ctx
);
1373 evp_weakkey(vchar_t
*key
, const EVP_CIPHER
*e
)
1379 evp_keylen(int len
, const EVP_CIPHER
*e
)
1383 /* EVP functions return lengths in bytes, ipsec-tools
1384 * uses lengths in bits, therefore conversion is required. --AK
1386 if (len
!= 0 && len
!= (EVP_CIPHER_key_length(e
) << 3))
1389 return EVP_CIPHER_key_length(e
) << 3;
1396 eay_des_encrypt(data
, key
, iv
)
1397 vchar_t
*data
, *key
, *iv
;
1399 return evp_crypt(data
, key
, iv
, EVP_des_cbc(), 1);
1403 eay_des_decrypt(data
, key
, iv
)
1404 vchar_t
*data
, *key
, *iv
;
1406 return evp_crypt(data
, key
, iv
, EVP_des_cbc(), 0);
1410 eay_des_weakkey(key
)
1413 #ifdef USE_NEW_DES_API
1414 return DES_is_weak_key((void *)key
->v
);
1416 return des_is_weak_key((void *)key
->v
);
1424 return evp_keylen(len
, EVP_des_cbc());
1427 #ifdef HAVE_OPENSSL_IDEA_H
1432 eay_idea_encrypt(data
, key
, iv
)
1433 vchar_t
*data
, *key
, *iv
;
1436 IDEA_KEY_SCHEDULE ks
;
1438 idea_set_encrypt_key(key
->v
, &ks
);
1440 /* allocate buffer for result */
1441 if ((res
= vmalloc(data
->l
)) == NULL
)
1444 /* decryption data */
1445 idea_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1446 &ks
, iv
->v
, IDEA_ENCRYPT
);
1452 eay_idea_decrypt(data
, key
, iv
)
1453 vchar_t
*data
, *key
, *iv
;
1456 IDEA_KEY_SCHEDULE ks
, dks
;
1458 idea_set_encrypt_key(key
->v
, &ks
);
1459 idea_set_decrypt_key(&ks
, &dks
);
1461 /* allocate buffer for result */
1462 if ((res
= vmalloc(data
->l
)) == NULL
)
1465 /* decryption data */
1466 idea_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1467 &dks
, iv
->v
, IDEA_DECRYPT
);
1473 eay_idea_weakkey(key
)
1480 eay_idea_keylen(len
)
1483 if (len
!= 0 && len
!= 128)
1493 eay_bf_encrypt(data
, key
, iv
)
1494 vchar_t
*data
, *key
, *iv
;
1496 return evp_crypt(data
, key
, iv
, EVP_bf_cbc(), 1);
1500 eay_bf_decrypt(data
, key
, iv
)
1501 vchar_t
*data
, *key
, *iv
;
1503 return evp_crypt(data
, key
, iv
, EVP_bf_cbc(), 0);
1510 return 0; /* XXX to be done. refer to RFC 2451 */
1519 if (len
< 40 || len
> 448)
1524 #ifdef HAVE_OPENSSL_RC5_H
1529 eay_rc5_encrypt(data
, key
, iv
)
1530 vchar_t
*data
, *key
, *iv
;
1535 /* in RFC 2451, there is information about the number of round. */
1536 RC5_32_set_key(&ks
, key
->l
, key
->v
, 16);
1538 /* allocate buffer for result */
1539 if ((res
= vmalloc(data
->l
)) == NULL
)
1542 /* decryption data */
1543 RC5_32_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1544 &ks
, iv
->v
, RC5_ENCRYPT
);
1550 eay_rc5_decrypt(data
, key
, iv
)
1551 vchar_t
*data
, *key
, *iv
;
1556 /* in RFC 2451, there is information about the number of round. */
1557 RC5_32_set_key(&ks
, key
->l
, key
->v
, 16);
1559 /* allocate buffer for result */
1560 if ((res
= vmalloc(data
->l
)) == NULL
)
1563 /* decryption data */
1564 RC5_32_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1565 &ks
, iv
->v
, RC5_DECRYPT
);
1571 eay_rc5_weakkey(key
)
1574 return 0; /* No known weak keys when used with 16 rounds. */
1584 if (len
< 40 || len
> 2040)
1594 eay_3des_encrypt(data
, key
, iv
)
1595 vchar_t
*data
, *key
, *iv
;
1597 return evp_crypt(data
, key
, iv
, EVP_des_ede3_cbc(), 1);
1601 eay_3des_decrypt(data
, key
, iv
)
1602 vchar_t
*data
, *key
, *iv
;
1604 return evp_crypt(data
, key
, iv
, EVP_des_ede3_cbc(), 0);
1608 eay_3des_weakkey(key
)
1611 #ifdef USE_NEW_DES_API
1612 return (DES_is_weak_key((void *)key
->v
) ||
1613 DES_is_weak_key((void *)(key
->v
+ 8)) ||
1614 DES_is_weak_key((void *)(key
->v
+ 16)));
1619 return (des_is_weak_key((void *)key
->v
) ||
1620 des_is_weak_key((void *)(key
->v
+ 8)) ||
1621 des_is_weak_key((void *)(key
->v
+ 16)));
1626 eay_3des_keylen(len
)
1629 if (len
!= 0 && len
!= 192)
1638 eay_cast_encrypt(data
, key
, iv
)
1639 vchar_t
*data
, *key
, *iv
;
1641 return evp_crypt(data
, key
, iv
, EVP_cast5_cbc(), 1);
1645 eay_cast_decrypt(data
, key
, iv
)
1646 vchar_t
*data
, *key
, *iv
;
1648 return evp_crypt(data
, key
, iv
, EVP_cast5_cbc(), 0);
1652 eay_cast_weakkey(key
)
1655 return 0; /* No known weak keys. */
1659 eay_cast_keylen(len
)
1664 if (len
< 40 || len
> 128)
1672 #ifndef HAVE_OPENSSL_AES_H
1674 eay_aes_encrypt(data
, key
, iv
)
1675 vchar_t
*data
, *key
, *iv
;
1681 memset(&k
, 0, sizeof(k
));
1682 if (rijndael_makeKey(&k
, DIR_ENCRYPT
, key
->l
<< 3, key
->v
) < 0)
1685 /* allocate buffer for result */
1686 if ((res
= vmalloc(data
->l
)) == NULL
)
1689 /* encryption data */
1690 memset(&c
, 0, sizeof(c
));
1691 if (rijndael_cipherInit(&c
, MODE_CBC
, iv
->v
) < 0){
1695 if (rijndael_blockEncrypt(&c
, &k
, data
->v
, data
->l
<< 3, res
->v
) < 0){
1704 eay_aes_decrypt(data
, key
, iv
)
1705 vchar_t
*data
, *key
, *iv
;
1711 memset(&k
, 0, sizeof(k
));
1712 if (rijndael_makeKey(&k
, DIR_DECRYPT
, key
->l
<< 3, key
->v
) < 0)
1715 /* allocate buffer for result */
1716 if ((res
= vmalloc(data
->l
)) == NULL
)
1719 /* decryption data */
1720 memset(&c
, 0, sizeof(c
));
1721 if (rijndael_cipherInit(&c
, MODE_CBC
, iv
->v
) < 0){
1725 if (rijndael_blockDecrypt(&c
, &k
, data
->v
, data
->l
<< 3, res
->v
) < 0){
1733 static inline const EVP_CIPHER
*
1734 aes_evp_by_keylen(int keylen
)
1739 return EVP_aes_128_cbc();
1742 return EVP_aes_192_cbc();
1745 return EVP_aes_256_cbc();
1752 eay_aes_encrypt(data
, key
, iv
)
1753 vchar_t
*data
, *key
, *iv
;
1755 return evp_crypt(data
, key
, iv
, aes_evp_by_keylen(key
->l
), 1);
1759 eay_aes_decrypt(data
, key
, iv
)
1760 vchar_t
*data
, *key
, *iv
;
1762 return evp_crypt(data
, key
, iv
, aes_evp_by_keylen(key
->l
), 0);
1767 eay_aes_weakkey(key
)
1779 if (len
!= 128 && len
!= 192 && len
!= 256)
1784 /* for ipsec part */
1798 eay_twofish_keylen(len
)
1801 if (len
< 0 || len
> 256)
1807 eay_null_keylen(len
)
1819 eay_hmac_init(key
, algorithm
)
1821 CCHmacAlgorithm algorithm
;
1823 CCHmacContext
*c
= racoon_malloc(sizeof(*c
));
1825 CCHmacInit(c
, algorithm
, key
->v
, key
->l
);
1831 eay_hmac_init(key
, md
)
1835 HMAC_CTX
*c
= racoon_malloc(sizeof(*c
));
1837 HMAC_Init(c
, key
->v
, key
->l
, md
);
1841 #endif /* __APPLE__ */
1848 eay_hmacsha2_512_one(key
, data
)
1849 vchar_t
*key
, *data
;
1854 ctx
= eay_hmacsha2_512_init(key
);
1855 eay_hmacsha2_512_update(ctx
, data
);
1856 res
= eay_hmacsha2_512_final(ctx
);
1862 eay_hmacsha2_512_init(key
)
1866 return eay_hmac_init(key
, kCCHmacAlgSHA512
);
1868 return eay_hmac_init(key
, EVP_sha2_512());
1873 eay_hmacsha2_512_update(c
, data
)
1878 CCHmacUpdate((CCHmacContext
*)c
, data
->v
, data
->l
);
1880 HMAC_Update((HMAC_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
1886 eay_hmacsha2_512_final(c
)
1891 if ((res
= vmalloc(CC_SHA512_DIGEST_LENGTH
)) == 0)
1894 CCHmacFinal((CCHmacContext
*)c
, res
->v
);
1895 res
->l
= CC_SHA512_DIGEST_LENGTH
;
1897 (void)racoon_free(c
);
1902 eay_hmacsha2_512_final(c
)
1908 if ((res
= vmalloc(SHA512_DIGEST_LENGTH
)) == 0)
1911 HMAC_Final((HMAC_CTX
*)c
, (unsigned char *) res
->v
, &l
);
1913 HMAC_cleanup((HMAC_CTX
*)c
);
1915 (void)racoon_free(c
);
1917 if (SHA512_DIGEST_LENGTH
!= res
->l
) {
1918 plog(LLV_ERROR
, LOCATION
, NULL
,
1919 "hmac sha2_512 length mismatch %zd.\n", res
->l
);
1926 #endif /* __APPLE__ */
1932 eay_hmacsha2_384_one(key
, data
)
1933 vchar_t
*key
, *data
;
1938 ctx
= eay_hmacsha2_384_init(key
);
1939 eay_hmacsha2_384_update(ctx
, data
);
1940 res
= eay_hmacsha2_384_final(ctx
);
1946 eay_hmacsha2_384_init(key
)
1950 return eay_hmac_init(key
, kCCHmacAlgSHA384
);
1952 return eay_hmac_init(key
, EVP_sha2_384());
1957 eay_hmacsha2_384_update(c
, data
)
1962 CCHmacUpdate((CCHmacContext
*)c
, data
->v
, data
->l
);
1964 HMAC_Update((HMAC_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
1970 eay_hmacsha2_384_final(c
)
1975 if ((res
= vmalloc(CC_SHA384_DIGEST_LENGTH
)) == 0)
1978 CCHmacFinal((CCHmacContext
*)c
, res
->v
);
1979 res
->l
= CC_SHA384_DIGEST_LENGTH
;
1981 (void)racoon_free(c
);
1986 eay_hmacsha2_384_final(c
)
1992 if ((res
= vmalloc(SHA384_DIGEST_LENGTH
)) == 0)
1995 HMAC_Final((HMAC_CTX
*)c
, (unsigned char *) res
->v
, &l
);
1997 HMAC_cleanup((HMAC_CTX
*)c
);
1999 (void)racoon_free(c
);
2001 if (SHA384_DIGEST_LENGTH
!= res
->l
) {
2002 plog(LLV_ERROR
, LOCATION
, NULL
,
2003 "hmac sha2_384 length mismatch %zd.\n", res
->l
);
2010 #endif /* __APPLE__ */
2016 eay_hmacsha2_256_one(key
, data
)
2017 vchar_t
*key
, *data
;
2022 ctx
= eay_hmacsha2_256_init(key
);
2023 eay_hmacsha2_256_update(ctx
, data
);
2024 res
= eay_hmacsha2_256_final(ctx
);
2030 eay_hmacsha2_256_init(key
)
2034 return eay_hmac_init(key
, kCCHmacAlgSHA256
);
2036 return eay_hmac_init(key
, EVP_sha2_256());
2041 eay_hmacsha2_256_update(c
, data
)
2046 CCHmacUpdate((CCHmacContext
*)c
, data
->v
, data
->l
);
2048 HMAC_Update((HMAC_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
2054 eay_hmacsha2_256_final(c
)
2059 if ((res
= vmalloc(CC_SHA256_DIGEST_LENGTH
)) == 0)
2062 CCHmacFinal((CCHmacContext
*)c
, res
->v
);
2063 res
->l
= CC_SHA256_DIGEST_LENGTH
;
2065 (void)racoon_free(c
);
2070 eay_hmacsha2_256_final(c
)
2076 if ((res
= vmalloc(SHA256_DIGEST_LENGTH
)) == 0)
2079 HMAC_Final((HMAC_CTX
*)c
, (unsigned char *) res
->v
, &l
);
2081 HMAC_cleanup((HMAC_CTX
*)c
);
2083 (void)racoon_free(c
);
2085 if (SHA256_DIGEST_LENGTH
!= res
->l
) {
2086 plog(LLV_ERROR
, LOCATION
, NULL
,
2087 "hmac sha2_256 length mismatch %zd.\n", res
->l
);
2094 #endif /* __APPLE__ */
2095 #endif /* WITH_SHA2 */
2101 eay_hmacsha1_one(key
, data
)
2102 vchar_t
*key
, *data
;
2107 ctx
= eay_hmacsha1_init(key
);
2108 eay_hmacsha1_update(ctx
, data
);
2109 res
= eay_hmacsha1_final(ctx
);
2115 eay_hmacsha1_init(key
)
2119 return eay_hmac_init(key
, kCCHmacAlgSHA1
);
2121 return eay_hmac_init(key
, EVP_sha1());
2126 eay_hmacsha1_update(c
, data
)
2131 CCHmacUpdate((CCHmacContext
*)c
, data
->v
, data
->l
);
2133 HMAC_Update((HMAC_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
2139 eay_hmacsha1_final(c
)
2144 if ((res
= vmalloc(CC_SHA1_DIGEST_LENGTH
)) == 0)
2147 CCHmacFinal((CCHmacContext
*)c
, res
->v
);
2148 res
->l
= CC_SHA1_DIGEST_LENGTH
;
2150 (void)racoon_free(c
);
2155 eay_hmacsha1_final(c
)
2161 if ((res
= vmalloc(SHA_DIGEST_LENGTH
)) == 0)
2164 HMAC_Final((HMAC_CTX
*)c
, (unsigned char *) res
->v
, &l
);
2166 HMAC_cleanup((HMAC_CTX
*)c
);
2168 (void)racoon_free(c
);
2170 if (SHA_DIGEST_LENGTH
!= res
->l
) {
2171 plog(LLV_ERROR
, LOCATION
, NULL
,
2172 "hmac sha1 length mismatch %zd.\n", res
->l
);
2179 #endif /* __APPLE__ */
2185 eay_hmacmd5_one(key
, data
)
2186 vchar_t
*key
, *data
;
2191 ctx
= eay_hmacmd5_init(key
);
2192 eay_hmacmd5_update(ctx
, data
);
2193 res
= eay_hmacmd5_final(ctx
);
2199 eay_hmacmd5_init(key
)
2203 return eay_hmac_init(key
, kCCHmacAlgMD5
);
2205 return eay_hmac_init(key
, EVP_md5());
2210 eay_hmacmd5_update(c
, data
)
2215 CCHmacUpdate((CCHmacContext
*)c
, data
->v
, data
->l
);
2217 HMAC_Update((HMAC_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
2223 eay_hmacmd5_final(c
)
2228 if ((res
= vmalloc(CC_MD5_DIGEST_LENGTH
)) == 0)
2231 CCHmacFinal((CCHmacContext
*)c
, res
->v
);
2232 res
->l
= CC_MD5_DIGEST_LENGTH
;
2233 (void)racoon_free(c
);
2239 eay_hmacmd5_final(c
)
2245 if ((res
= vmalloc(MD5_DIGEST_LENGTH
)) == 0)
2248 HMAC_Final((HMAC_CTX
*)c
, (unsigned char *) res
->v
, &l
);
2250 HMAC_cleanup((HMAC_CTX
*)c
);
2252 (void)racoon_free(c
);
2254 if (MD5_DIGEST_LENGTH
!= res
->l
) {
2255 plog(LLV_ERROR
, LOCATION
, NULL
,
2256 "hmac md5 length mismatch %zd.\n", res
->l
);
2263 #endif /* __APPLE__ */
2267 * SHA2-512 functions
2272 SHA512_CTX
*c
= racoon_malloc(sizeof(*c
));
2280 eay_sha2_512_update(c
, data
)
2284 SHA512_Update((SHA512_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
2290 eay_sha2_512_final(c
)
2295 if ((res
= vmalloc(SHA512_DIGEST_LENGTH
)) == 0)
2298 SHA512_Final((unsigned char *) res
->v
, (SHA512_CTX
*)c
);
2299 (void)racoon_free(c
);
2305 eay_sha2_512_one(data
)
2311 ctx
= eay_sha2_512_init();
2312 eay_sha2_512_update(ctx
, data
);
2313 res
= eay_sha2_512_final(ctx
);
2319 eay_sha2_512_hashlen()
2321 return SHA512_DIGEST_LENGTH
<< 3;
2327 * SHA2-384 functions
2331 typedef SHA512_CTX SHA384_CTX
;
2337 SHA384_CTX
*c
= racoon_malloc(sizeof(*c
));
2345 eay_sha2_384_update(c
, data
)
2349 SHA384_Update((SHA384_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
2355 eay_sha2_384_final(c
)
2360 if ((res
= vmalloc(SHA384_DIGEST_LENGTH
)) == 0)
2363 SHA384_Final((unsigned char *) res
->v
, (SHA384_CTX
*)c
);
2364 (void)racoon_free(c
);
2370 eay_sha2_384_one(data
)
2376 ctx
= eay_sha2_384_init();
2377 eay_sha2_384_update(ctx
, data
);
2378 res
= eay_sha2_384_final(ctx
);
2384 eay_sha2_384_hashlen()
2386 return SHA384_DIGEST_LENGTH
<< 3;
2392 * SHA2-256 functions
2397 SHA256_CTX
*c
= racoon_malloc(sizeof(*c
));
2405 eay_sha2_256_update(c
, data
)
2409 SHA256_Update((SHA256_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
2415 eay_sha2_256_final(c
)
2420 if ((res
= vmalloc(SHA256_DIGEST_LENGTH
)) == 0)
2423 SHA256_Final((unsigned char *) res
->v
, (SHA256_CTX
*)c
);
2424 (void)racoon_free(c
);
2430 eay_sha2_256_one(data
)
2436 ctx
= eay_sha2_256_init();
2437 eay_sha2_256_update(ctx
, data
);
2438 res
= eay_sha2_256_final(ctx
);
2444 eay_sha2_256_hashlen()
2446 return SHA256_DIGEST_LENGTH
<< 3;
2456 SHA_CTX
*c
= racoon_malloc(sizeof(*c
));
2464 eay_sha1_update(c
, data
)
2468 SHA1_Update((SHA_CTX
*)c
, data
->v
, data
->l
);
2479 if ((res
= vmalloc(SHA_DIGEST_LENGTH
)) == 0)
2482 SHA1_Final((unsigned char *) res
->v
, (SHA_CTX
*)c
);
2483 (void)racoon_free(c
);
2495 ctx
= eay_sha1_init();
2496 eay_sha1_update(ctx
, data
);
2497 res
= eay_sha1_final(ctx
);
2505 return SHA_DIGEST_LENGTH
<< 3;
2514 MD5_CTX
*c
= racoon_malloc(sizeof(*c
));
2522 eay_md5_update(c
, data
)
2526 MD5_Update((MD5_CTX
*)c
, data
->v
, data
->l
);
2537 if ((res
= vmalloc(MD5_DIGEST_LENGTH
)) == 0)
2540 MD5_Final((unsigned char *) res
->v
, (MD5_CTX
*)c
);
2541 (void)racoon_free(c
);
2553 ctx
= eay_md5_init();
2554 eay_md5_update(ctx
, data
);
2555 res
= eay_md5_final(ctx
);
2563 return MD5_DIGEST_LENGTH
<< 3;
2568 * size: number of bytes.
2571 eay_set_random(size
)
2577 if ((r
= BN_new()) == NULL
)
2579 BN_rand(r
, size
* 8, 0, 0);
2590 eay_dh_generate(prime
, g
, publen
, pub
, priv
)
2591 vchar_t
*prime
, **pub
, **priv
;
2600 /* pre-process to generate number */
2601 if (eay_v2bn(&p
, prime
) < 0)
2604 if ((dh
= DH_new()) == NULL
)
2607 p
= NULL
; /* p is now part of dh structure */
2609 if ((dh
->g
= BN_new()) == NULL
)
2611 if (!BN_set_word(dh
->g
, g
))
2615 dh
->length
= publen
;
2617 /* generate public and private number */
2618 if (!DH_generate_key(dh
))
2621 /* copy results to buffers */
2622 if (eay_bn2v(pub
, dh
->pub_key
) < 0)
2624 if (eay_bn2v(priv
, dh
->priv_key
) < 0) {
2640 eay_dh_compute(prime
, g
, pub
, priv
, pub2
, key
)
2641 vchar_t
*prime
, *pub
, *priv
, *pub2
, **key
;
2644 BIGNUM
*dh_pub
= NULL
;
2647 unsigned char *v
= NULL
;
2650 /* make public number to compute */
2651 if (eay_v2bn(&dh_pub
, pub2
) < 0)
2654 /* make DH structure */
2655 if ((dh
= DH_new()) == NULL
)
2657 if (eay_v2bn(&dh
->p
, prime
) < 0)
2659 if (eay_v2bn(&dh
->pub_key
, pub
) < 0)
2661 if (eay_v2bn(&dh
->priv_key
, priv
) < 0)
2663 dh
->length
= pub2
->l
* 8;
2666 if ((dh
->g
= BN_new()) == NULL
)
2668 if (!BN_set_word(dh
->g
, g
))
2671 if ((v
= racoon_calloc(prime
->l
, sizeof(u_char
))) == NULL
)
2673 if ((l
= DH_compute_key(v
, dh_pub
, dh
)) == -1)
2675 memcpy((*key
)->v
+ (prime
->l
- l
), v
, l
);
2690 * convert vchar_t <-> BIGNUM.
2692 * vchar_t: unit is u_char, network endian, most significant byte first.
2693 * BIGNUM: unit is BN_ULONG, each of BN_ULONG is in host endian,
2694 * least significant BN_ULONG must come first.
2696 * hex value of "0x3ffe050104" is represented as follows:
2697 * vchar_t: 3f fe 05 01 04
2698 * BIGNUM (BN_ULONG = u_int8_t): 04 01 05 fe 3f
2699 * BIGNUM (BN_ULONG = u_int16_t): 0x0104 0xfe05 0x003f
2700 * BIGNUM (BN_ULONG = u_int32_t_t): 0xfe050104 0x0000003f
2707 if ((*bn
= BN_bin2bn((unsigned char *) var
->v
, var
->l
, NULL
)) == NULL
)
2718 *var
= vmalloc(bn
->top
* BN_BYTES
);
2722 (*var
)->l
= BN_bn2bin(bn
, (unsigned char *) (*var
)->v
);
2730 OpenSSL_add_all_algorithms();
2731 ERR_load_crypto_strings();
2732 #ifdef HAVE_OPENSSL_ENGINE_H
2733 ENGINE_load_builtin_engines();
2734 ENGINE_register_all_complete();
2739 base64_decode(char *in
, long inlen
)
2741 BIO
*bio
=NULL
, *b64
=NULL
;
2742 vchar_t
*res
= NULL
;
2746 bio
= BIO_new_mem_buf(in
, inlen
);
2747 b64
= BIO_new(BIO_f_base64());
2748 BIO_set_flags(b64
, BIO_FLAGS_BASE64_NO_NL
);
2749 bio
= BIO_push(b64
, bio
);
2751 outlen
= BIO_read(bio
, out
, inlen
* 2);
2753 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
2757 res
= vmalloc(outlen
);
2761 memcpy(res
->v
, out
, outlen
);
2771 base64_encode(char *in
, long inlen
)
2773 BIO
*bio
=NULL
, *b64
=NULL
;
2776 vchar_t
*res
= NULL
;
2778 bio
= BIO_new(BIO_s_mem());
2779 b64
= BIO_new(BIO_f_base64());
2780 BIO_set_flags(b64
, BIO_FLAGS_BASE64_NO_NL
);
2781 bio
= BIO_push(b64
, bio
);
2783 BIO_write(bio
, in
, inlen
);
2786 plen
= BIO_get_mem_data(bio
, &ptr
);
2787 res
= vmalloc(plen
+1);
2791 memcpy (res
->v
, ptr
, plen
);
2792 res
->v
[plen
] = '\0';
2802 binbuf_pubkey2rsa(vchar_t
*binbuf
)
2805 RSA
*rsa_pub
= NULL
;
2807 if (binbuf
->v
[0] > binbuf
->l
- 1) {
2808 plog(LLV_ERROR
, LOCATION
, NULL
, "Plain RSA pubkey format error: decoded string doesn't make sense.\n");
2812 exp
= BN_bin2bn((unsigned char *) (binbuf
->v
+ 1), binbuf
->v
[0], NULL
);
2813 mod
= BN_bin2bn((unsigned char *) (binbuf
->v
+ binbuf
->v
[0] + 1),
2814 binbuf
->l
- binbuf
->v
[0] - 1, NULL
);
2815 rsa_pub
= RSA_new();
2817 if (!exp
|| !mod
|| !rsa_pub
) {
2818 plog(LLV_ERROR
, LOCATION
, NULL
, "Plain RSA pubkey parsing error: %s\n", eay_strerror());
2837 base64_pubkey2rsa(char *in
)
2840 RSA
*rsa_pub
= NULL
;
2843 if (strncmp(in
, "0s", 2) != 0) {
2844 plog(LLV_ERROR
, LOCATION
, NULL
, "Plain RSA pubkey format error: doesn't start with '0s'\n");
2848 binbuf
= base64_decode(in
+ 2, strlen(in
+ 2));
2850 plog(LLV_ERROR
, LOCATION
, NULL
, "Plain RSA pubkey format error: Base64 decoding failed.\n");
2854 if (binbuf
->v
[0] > binbuf
->l
- 1) {
2855 plog(LLV_ERROR
, LOCATION
, NULL
, "Plain RSA pubkey format error: decoded string doesn't make sense.\n");
2859 rsa_pub
= binbuf_pubkey2rsa(binbuf
);
2869 bignum_pubkey2rsa(BIGNUM
*in
)
2871 RSA
*rsa_pub
= NULL
;
2874 binbuf
= vmalloc(BN_num_bytes(in
));
2876 plog(LLV_ERROR
, LOCATION
, NULL
, "Plain RSA pubkey conversion: memory allocation failed..\n");
2880 BN_bn2bin(in
, (unsigned char *) binbuf
->v
);
2882 rsa_pub
= binbuf_pubkey2rsa(binbuf
);
2897 vrand
= eay_set_random(sizeof(result
));
2898 memcpy(&result
, vrand
->v
, sizeof(result
));
2907 return SSLeay_version(SSLEAY_VERSION
);