1 /* $NetBSD: crypto_openssl.c,v 1.18 2009/04/20 13:22:41 tteras 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
36 #include <sys/types.h>
37 #include <sys/param.h>
44 /* get openssl/ssleay version number */
45 #include <openssl/opensslv.h>
47 #if !defined(OPENSSL_VERSION_NUMBER) || (OPENSSL_VERSION_NUMBER < 0x0090602fL)
48 #error OpenSSL version 0.9.6 or later required.
51 #include <openssl/pem.h>
52 #include <openssl/evp.h>
53 #include <openssl/x509.h>
54 #include <openssl/x509v3.h>
55 #include <openssl/x509_vfy.h>
56 #include <openssl/bn.h>
57 #include <openssl/dh.h>
58 #include <openssl/md5.h>
59 #include <openssl/sha.h>
60 #include <openssl/hmac.h>
61 #include <openssl/des.h>
62 #include <openssl/crypto.h>
63 #ifdef HAVE_OPENSSL_ENGINE_H
64 #include <openssl/engine.h>
66 #include <openssl/blowfish.h>
67 #include <openssl/cast.h>
68 #include <openssl/err.h>
69 #ifdef HAVE_OPENSSL_RC5_H
70 #include <openssl/rc5.h>
72 #ifdef HAVE_OPENSSL_IDEA_H
73 #include <openssl/idea.h>
75 #if defined(HAVE_OPENSSL_AES_H)
76 #include <openssl/aes.h>
77 #elif defined(HAVE_OPENSSL_RIJNDAEL_H)
78 #include <openssl/rijndael.h>
80 #include "crypto/rijndael/rijndael-api-fst.h"
82 #if defined(HAVE_OPENSSL_CAMELLIA_H)
83 #include <openssl/camellia.h>
86 #ifdef HAVE_OPENSSL_SHA2_H
87 #include <openssl/sha2.h>
89 #include "crypto/sha2/sha2.h"
95 #if OPENSSL_VERSION_NUMBER < 0x0090700fL
96 typedef STACK_OF(GENERAL_NAME
) GENERAL_NAMES
;
98 #define USE_NEW_DES_API
101 #define OpenSSL_BUG() do { plog(LLV_ERROR, LOCATION, NULL, "OpenSSL function failed\n"); } while(0)
107 #include "crypto_openssl.h"
109 #include "gcmalloc.h"
113 * I hate to cast every parameter to des_xx into void *, but it is
114 * necessary for SSLeay/OpenSSL portability. It sucks.
117 static int cb_check_cert_local
__P((int, X509_STORE_CTX
*));
118 static int cb_check_cert_remote
__P((int, X509_STORE_CTX
*));
119 static X509
*mem2x509
__P((vchar_t
*));
121 static caddr_t eay_hmac_init
__P((vchar_t
*, const EVP_MD
*));
123 /* X509 Certificate */
125 * convert the string of the subject name into DER
126 * e.g. str = "C=JP, ST=Kanagawa";
129 eay_str2asn1dn(str
, len
)
143 buf
= racoon_malloc(len
+ 1);
145 plog(LLV_WARNING
, LOCATION
, NULL
,"failed to allocate buffer\n");
148 memcpy(buf
, str
, len
);
150 name
= X509_NAME_new();
152 dst
= field
= &buf
[0];
154 for (i
= 0; i
< len
; i
++) {
155 if (buf
[i
] == '\\') {
156 /* Escape characters specified in RFC 2253 */
158 strchr("\\,=+<>#;", buf
[i
+1]) != NULL
) {
161 } else if (i
< len
- 2) {
162 /* RFC 2253 hexpair character escape */
167 esc_str
[0] = buf
[++i
];
168 esc_str
[1] = buf
[++i
];
170 u
= strtol(esc_str
, &endptr
, 16);
171 if (*endptr
!= '\0' || u
< 0 || u
> 255)
178 if (!value
&& buf
[i
] == '=') {
180 dst
= value
= &buf
[i
+ 1];
182 } else if (buf
[i
] == ',' || buf
[i
] == '/') {
185 plog(LLV_DEBUG
, LOCATION
, NULL
, "DN: %s=%s\n",
188 if (!value
) goto err
;
189 if (!X509_NAME_add_entry_by_txt(name
, field
,
190 (value
[0] == '*' && value
[1] == 0) ?
191 V_ASN1_PRINTABLESTRING
: MBSTRING_ASC
,
192 (unsigned char *) value
, -1, -1, 0)) {
193 plog(LLV_ERROR
, LOCATION
, NULL
,
194 "Invalid DN field: %s=%s\n",
196 plog(LLV_ERROR
, LOCATION
, NULL
,
197 "%s\n", eay_strerror());
201 while (i
+ 1 < len
&& buf
[i
+ 1] == ' ') i
++;
202 dst
= field
= &buf
[i
+ 1];
211 plog(LLV_DEBUG
, LOCATION
, NULL
, "DN: %s=%s\n",
214 if (!value
) goto err
;
215 if (!X509_NAME_add_entry_by_txt(name
, field
,
216 (value
[0] == '*' && value
[1] == 0) ?
217 V_ASN1_PRINTABLESTRING
: MBSTRING_ASC
,
218 (unsigned char *) value
, -1, -1, 0)) {
219 plog(LLV_ERROR
, LOCATION
, NULL
,
220 "Invalid DN field: %s=%s\n",
222 plog(LLV_ERROR
, LOCATION
, NULL
,
223 "%s\n", eay_strerror());
227 i
= i2d_X509_NAME(name
, NULL
);
234 i
= i2d_X509_NAME(name
, (void *)&p
);
244 X509_NAME_free(name
);
251 * convert the hex string of the subject name into DER
254 eay_hex2asn1dn(const char *hex
, int len
)
256 BIGNUM
*bn
= BN_new();
264 if (BN_hex2bn(&bn
, hex
) != len
) {
265 plog(LLV_ERROR
, LOCATION
, NULL
,
266 "conversion of Hex-encoded ASN1 string to binary failed: %s\n",
271 binlen
= BN_num_bytes(bn
);
272 ret
= vmalloc(binlen
);
274 plog(LLV_WARNING
, LOCATION
, NULL
,"failed to allocate buffer\n");
279 BN_bn2bin(bn
, (unsigned char *) binbuf
);
288 * The following are derived from code in crypto/x509/x509_cmp.c
290 * X509_NAME_wildcmp() adds wildcard matching to the original
291 * X509_NAME_cmp(), nocase_cmp() and nocase_spacenorm_cmp() are as is.
294 /* Case insensitive string comparision */
295 static int nocase_cmp(const ASN1_STRING
*a
, const ASN1_STRING
*b
)
299 if (a
->length
!= b
->length
)
300 return (a
->length
- b
->length
);
302 for (i
=0; i
<a
->length
; i
++)
306 ca
= tolower(a
->data
[i
]);
307 cb
= tolower(b
->data
[i
]);
315 /* Case insensitive string comparision with space normalization
316 * Space normalization - ignore leading, trailing spaces,
317 * multiple spaces between characters are replaced by single space
319 static int nocase_spacenorm_cmp(const ASN1_STRING
*a
, const ASN1_STRING
*b
)
321 unsigned char *pa
= NULL
, *pb
= NULL
;
329 /* skip leading spaces */
330 while (la
> 0 && isspace(*pa
))
335 while (lb
> 0 && isspace(*pb
))
341 /* skip trailing spaces */
342 while (la
> 0 && isspace(pa
[la
-1]))
344 while (lb
> 0 && isspace(pb
[lb
-1]))
347 /* compare strings with space normalization */
348 while (la
> 0 && lb
> 0)
352 /* compare character */
361 if (la
<= 0 || lb
<= 0)
364 /* is white space next character ? */
365 if (isspace(*pa
) && isspace(*pb
))
367 /* skip remaining white spaces */
368 while (la
> 0 && isspace(*pa
))
373 while (lb
> 0 && isspace(*pb
))
380 if (la
> 0 || lb
> 0)
386 static int X509_NAME_wildcmp(const X509_NAME
*a
, const X509_NAME
*b
)
389 X509_NAME_ENTRY
*na
,*nb
;
391 if (sk_X509_NAME_ENTRY_num(a
->entries
)
392 != sk_X509_NAME_ENTRY_num(b
->entries
))
393 return sk_X509_NAME_ENTRY_num(a
->entries
)
394 -sk_X509_NAME_ENTRY_num(b
->entries
);
395 for (i
=sk_X509_NAME_ENTRY_num(a
->entries
)-1; i
>=0; i
--)
397 na
=sk_X509_NAME_ENTRY_value(a
->entries
,i
);
398 nb
=sk_X509_NAME_ENTRY_value(b
->entries
,i
);
399 j
=OBJ_cmp(na
->object
,nb
->object
);
401 if ((na
->value
->length
== 1 && na
->value
->data
[0] == '*')
402 || (nb
->value
->length
== 1 && nb
->value
->data
[0] == '*'))
404 j
=na
->value
->type
-nb
->value
->type
;
406 if (na
->value
->type
== V_ASN1_PRINTABLESTRING
)
407 j
=nocase_spacenorm_cmp(na
->value
, nb
->value
);
408 else if (na
->value
->type
== V_ASN1_IA5STRING
409 && OBJ_obj2nid(na
->object
) == NID_pkcs9_emailAddress
)
410 j
=nocase_cmp(na
->value
, nb
->value
);
413 j
=na
->value
->length
-nb
->value
->length
;
415 j
=memcmp(na
->value
->data
,nb
->value
->data
,
427 * compare two subjectNames.
433 eay_cmp_asn1dn(n1
, n2
)
436 X509_NAME
*a
= NULL
, *b
= NULL
;
441 if (!d2i_X509_NAME(&a
, (void *)&p
, n1
->l
))
444 if (!d2i_X509_NAME(&b
, (void *)&p
, n2
->l
))
447 i
= X509_NAME_wildcmp(a
, b
);
458 * this functions is derived from apps/verify.c in OpenSSL0.9.5
461 eay_check_x509cert(cert
, CApath
, CAfile
, local
)
467 X509_STORE
*cert_ctx
= NULL
;
468 X509_LOOKUP
*lookup
= NULL
;
473 cert_ctx
= X509_STORE_new();
474 if (cert_ctx
== NULL
)
478 X509_STORE_set_verify_cb_func(cert_ctx
, cb_check_cert_local
);
480 X509_STORE_set_verify_cb_func(cert_ctx
, cb_check_cert_remote
);
482 lookup
= X509_STORE_add_lookup(cert_ctx
, X509_LOOKUP_file());
486 X509_LOOKUP_load_file(lookup
, CAfile
,
487 (CAfile
== NULL
) ? X509_FILETYPE_DEFAULT
: X509_FILETYPE_PEM
);
489 lookup
= X509_STORE_add_lookup(cert_ctx
, X509_LOOKUP_hash_dir());
492 error
= X509_LOOKUP_add_dir(lookup
, CApath
, X509_FILETYPE_PEM
);
497 error
= -1; /* initialized */
499 /* read the certificate to be verified */
500 x509
= mem2x509(cert
);
504 csc
= X509_STORE_CTX_new();
507 X509_STORE_CTX_init(csc
, cert_ctx
, x509
, NULL
);
508 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
509 X509_STORE_CTX_set_flags (csc
, X509_V_FLAG_CRL_CHECK
);
510 X509_STORE_CTX_set_flags (csc
, X509_V_FLAG_CRL_CHECK_ALL
);
512 error
= X509_verify_cert(csc
);
513 X509_STORE_CTX_free(csc
);
516 * if x509_verify_cert() is successful then the value of error is
519 error
= error
? 0 : -1;
523 plog(LLV_WARNING
, LOCATION
, NULL
,"%s\n", eay_strerror());
524 if (cert_ctx
!= NULL
)
525 X509_STORE_free(cert_ctx
);
533 * callback function for verifing certificate.
534 * this function is derived from cb() in openssl/apps/s_server.c
537 cb_check_cert_local(ok
, ctx
)
546 X509_get_subject_name(ctx
->current_cert
),
550 * since we are just checking the certificates, it is
551 * ok if they are self signed. But we should still warn
554 switch (ctx
->error
) {
555 case X509_V_ERR_CERT_HAS_EXPIRED
:
556 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
:
557 case X509_V_ERR_INVALID_CA
:
558 case X509_V_ERR_PATH_LENGTH_EXCEEDED
:
559 case X509_V_ERR_INVALID_PURPOSE
:
560 case X509_V_ERR_UNABLE_TO_GET_CRL
:
562 log_tag
= LLV_WARNING
;
567 plog(log_tag
, LOCATION
, NULL
,
568 "%s(%d) at depth:%d SubjectName:%s\n",
569 X509_verify_cert_error_string(ctx
->error
),
580 * callback function for verifing remote certificates.
581 * this function is derived from cb() in openssl/apps/s_server.c
584 cb_check_cert_remote(ok
, ctx
)
593 X509_get_subject_name(ctx
->current_cert
),
596 switch (ctx
->error
) {
597 case X509_V_ERR_UNABLE_TO_GET_CRL
:
599 log_tag
= LLV_WARNING
;
604 plog(log_tag
, LOCATION
, NULL
,
605 "%s(%d) at depth:%d SubjectName:%s\n",
606 X509_verify_cert_error_string(ctx
->error
),
617 * get a subjectName from X509 certificate.
620 eay_get_x509asn1subjectname(cert
)
625 vchar_t
*name
= NULL
;
628 x509
= mem2x509(cert
);
632 /* get the length of the name */
633 len
= i2d_X509_NAME(x509
->cert_info
->subject
, NULL
);
638 bp
= (unsigned char *) name
->v
;
639 len
= i2d_X509_NAME(x509
->cert_info
->subject
, &bp
);
646 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
658 * get the subjectAltName from X509 certificate.
659 * the name must be terminated by '\0'.
662 eay_get_x509subjectaltname(cert
, altname
, type
, pos
)
669 GENERAL_NAMES
*gens
= NULL
;
675 *type
= GENT_OTHERNAME
;
677 x509
= mem2x509(cert
);
681 gens
= X509_get_ext_d2i(x509
, NID_subject_alt_name
, NULL
, NULL
);
685 /* there is no data at "pos" */
686 if (pos
> sk_GENERAL_NAME_num(gens
))
689 gen
= sk_GENERAL_NAME_value(gens
, pos
- 1);
691 /* read DNSName / Email */
692 if (gen
->type
== GEN_DNS
||
693 gen
->type
== GEN_EMAIL
||
694 gen
->type
== GEN_URI
)
696 /* make sure if the data is terminated by '\0'. */
697 if (gen
->d
.ia5
->data
[gen
->d
.ia5
->length
] != '\0')
699 plog(LLV_ERROR
, LOCATION
, NULL
,
700 "data is not terminated by NUL.");
701 racoon_hexdump(gen
->d
.ia5
->data
, gen
->d
.ia5
->length
+ 1);
705 len
= gen
->d
.ia5
->length
+ 1;
706 *altname
= racoon_malloc(len
);
710 strlcpy(*altname
, (char *) gen
->d
.ia5
->data
, len
);
714 /* read IP address */
715 else if (gen
->type
== GEN_IPADD
)
717 unsigned char p
[5], *ip
;
720 /* only support IPv4 */
721 if (gen
->d
.ip
->length
!= 4)
724 /* convert Octet String to String
727 /*i2d_ASN1_OCTET_STRING(gen->d.ip,&ip);*/
728 ip
= gen
->d
.ip
->data
;
730 /* XXX Magic, enough for an IPv4 address
732 *altname
= racoon_malloc(20);
736 sprintf(*altname
, "%u.%u.%u.%u", ip
[0], ip
[1], ip
[2], ip
[3]);
740 /* XXX other possible types ?
741 * For now, error will be -1 if unsupported type
747 racoon_free(*altname
);
750 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
755 /* free the whole stack. */
756 sk_GENERAL_NAME_pop_free(gens
, GENERAL_NAME_free
);
762 * get a issuerName from X509 certificate.
765 eay_get_x509asn1issuername(cert
)
770 vchar_t
*name
= NULL
;
773 x509
= mem2x509(cert
);
777 /* get the length of the name */
778 len
= i2d_X509_NAME(x509
->cert_info
->issuer
, NULL
);
784 bp
= (unsigned char *) name
->v
;
785 len
= i2d_X509_NAME(x509
->cert_info
->issuer
, &bp
);
792 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
803 * decode a X509 certificate and make a readable text terminated '\n'.
804 * return the buffer allocated, so must free it later.
807 eay_get_x509text(cert
)
817 x509
= mem2x509(cert
);
821 bio
= BIO_new(BIO_s_mem());
825 error
= X509_print(bio
, x509
);
831 len
= BIO_get_mem_data(bio
, &bp
);
832 text
= racoon_malloc(len
+ 1);
835 memcpy(text
, bp
, len
);
846 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
856 /* get X509 structure from buffer. */
867 bp
= (unsigned char *) cert
->v
+ 1;
869 x509
= d2i_X509(NULL
, (void *)&bp
, cert
->l
- 1);
876 bio
= BIO_new(BIO_s_mem());
879 len
= BIO_write(bio
, cert
->v
+ 1, cert
->l
- 1);
882 x509
= PEM_read_bio_X509(bio
, NULL
, NULL
, NULL
);
890 * get a X509 certificate from local file.
891 * a certificate must be PEM format.
893 * path to a certificate.
895 * NULL if error occured
899 eay_get_x509cert(path
)
909 /* Read private key */
910 fp
= fopen(path
, "r");
913 x509
= PEM_read_X509(fp
, NULL
, NULL
, NULL
);
919 len
= i2d_X509(x509
, NULL
);
920 cert
= vmalloc(len
+ 1);
925 cert
->v
[0] = ISAKMP_CERT_X509SIGN
;
926 bp
= (unsigned char *) &cert
->v
[1];
927 error
= i2d_X509(x509
, &bp
);
939 * check a X509 signature
940 * XXX: to be get hash type from my cert ?
941 * to be handled EVP_dss().
942 * OUT: return -1 when error.
946 eay_check_x509sign(source
, sig
, cert
)
955 x509
= mem2x509(cert
);
959 evp
= X509_get_pubkey(x509
);
961 plog(LLV_ERROR
, LOCATION
, NULL
, "X509_get_pubkey(): %s\n", eay_strerror());
966 res
= eay_rsa_verify(source
, sig
, evp
->pkey
.rsa
);
975 * check RSA signature
976 * OUT: return -1 when error.
980 eay_check_rsasign(source
, sig
, rsa
)
985 return eay_rsa_verify(source
, sig
, rsa
);
989 * get PKCS#1 Private Key of PEM format from local file.
992 eay_get_pkcs1privkey(path
)
996 EVP_PKEY
*evp
= NULL
;
997 vchar_t
*pkey
= NULL
;
1002 /* Read private key */
1003 fp
= fopen(path
, "r");
1007 evp
= PEM_read_PrivateKey(fp
, NULL
, NULL
, NULL
);
1014 pkeylen
= i2d_PrivateKey(evp
, NULL
);
1017 pkey
= vmalloc(pkeylen
);
1020 bp
= (unsigned char *) pkey
->v
;
1021 pkeylen
= i2d_PrivateKey(evp
, &bp
);
1030 if (error
!= 0 && pkey
!= NULL
) {
1039 * get PKCS#1 Public Key of PEM format from local file.
1042 eay_get_pkcs1pubkey(path
)
1046 EVP_PKEY
*evp
= NULL
;
1047 vchar_t
*pkey
= NULL
;
1053 /* Read private key */
1054 fp
= fopen(path
, "r");
1058 x509
= PEM_read_X509(fp
, NULL
, NULL
, NULL
);
1065 /* Get public key - eay */
1066 evp
= X509_get_pubkey(x509
);
1070 pkeylen
= i2d_PublicKey(evp
, NULL
);
1073 pkey
= vmalloc(pkeylen
);
1076 bp
= (unsigned char *) pkey
->v
;
1077 pkeylen
= i2d_PublicKey(evp
, &bp
);
1085 if (error
!= 0 && pkey
!= NULL
) {
1094 eay_get_x509sign(src
, privkey
)
1095 vchar_t
*src
, *privkey
;
1098 u_char
*bp
= (unsigned char *) privkey
->v
;
1099 vchar_t
*sig
= NULL
;
1101 int pad
= RSA_PKCS1_PADDING
;
1103 /* XXX to be handled EVP_PKEY_DSA */
1104 evp
= d2i_PrivateKey(EVP_PKEY_RSA
, NULL
, (void *)&bp
, privkey
->l
);
1108 sig
= eay_rsa_sign(src
, evp
->pkey
.rsa
);
1116 eay_get_rsasign(src
, rsa
)
1120 return eay_rsa_sign(src
, rsa
);
1124 eay_rsa_sign(vchar_t
*src
, RSA
*rsa
)
1127 vchar_t
*sig
= NULL
;
1128 int pad
= RSA_PKCS1_PADDING
;
1130 len
= RSA_size(rsa
);
1136 len
= RSA_private_encrypt(src
->l
, (unsigned char *) src
->v
,
1137 (unsigned char *) sig
->v
, rsa
, pad
);
1139 if (len
== 0 || len
!= sig
->l
) {
1148 eay_rsa_verify(src
, sig
, rsa
)
1152 vchar_t
*xbuf
= NULL
;
1153 int pad
= RSA_PKCS1_PADDING
;
1157 len
= RSA_size(rsa
);
1158 xbuf
= vmalloc(len
);
1160 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
1164 len
= RSA_public_decrypt(sig
->l
, (unsigned char *) sig
->v
,
1165 (unsigned char *) xbuf
->v
, rsa
, pad
);
1166 if (len
== 0 || len
!= src
->l
) {
1167 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
1172 error
= memcmp(src
->v
, xbuf
->v
, src
->l
);
1182 * MUST load ERR_load_crypto_strings() first.
1187 static char ebuf
[512];
1191 const char *file
, *data
;
1195 es
= CRYPTO_thread_id();
1197 while ((l
= ERR_get_error_line_data(&file
, &line
, &data
, &flags
)) != 0){
1198 n
= snprintf(ebuf
+ len
, sizeof(ebuf
) - len
,
1200 es
, ERR_error_string(l
, buf
), file
, line
,
1201 (flags
& ERR_TXT_STRING
) ? data
: "");
1202 if (n
< 0 || n
>= sizeof(ebuf
) - len
)
1205 if (sizeof(ebuf
) < len
)
1213 evp_crypt(vchar_t
*data
, vchar_t
*key
, vchar_t
*iv
, const EVP_CIPHER
*e
, int enc
)
1221 if (data
->l
% EVP_CIPHER_block_size(e
))
1224 if ((res
= vmalloc(data
->l
)) == NULL
)
1227 EVP_CIPHER_CTX_init(&ctx
);
1229 switch(EVP_CIPHER_nid(e
)){
1236 case NID_cast5_cfb64
:
1237 case NID_cast5_ofb64
:
1238 /* XXX: can we do that also for algos with a fixed key size ?
1240 /* init context without key/iv
1242 if (!EVP_CipherInit(&ctx
, e
, NULL
, NULL
, enc
))
1251 if (!EVP_CIPHER_CTX_set_key_length(&ctx
, key
->l
))
1258 /* finalize context init with desired key size
1260 if (!EVP_CipherInit(&ctx
, NULL
, (u_char
*) key
->v
,
1261 (u_char
*) iv
->v
, enc
))
1269 if (!EVP_CipherInit(&ctx
, e
, (u_char
*) key
->v
,
1270 (u_char
*) iv
->v
, enc
)) {
1277 /* disable openssl padding */
1278 EVP_CIPHER_CTX_set_padding(&ctx
, 0);
1280 if (!EVP_Cipher(&ctx
, (u_char
*) res
->v
, (u_char
*) data
->v
, data
->l
)) {
1286 EVP_CIPHER_CTX_cleanup(&ctx
);
1292 evp_weakkey(vchar_t
*key
, const EVP_CIPHER
*e
)
1298 evp_keylen(int len
, const EVP_CIPHER
*e
)
1302 /* EVP functions return lengths in bytes, ipsec-tools
1303 * uses lengths in bits, therefore conversion is required. --AK
1305 if (len
!= 0 && len
!= (EVP_CIPHER_key_length(e
) << 3))
1308 return EVP_CIPHER_key_length(e
) << 3;
1315 eay_des_encrypt(data
, key
, iv
)
1316 vchar_t
*data
, *key
, *iv
;
1318 return evp_crypt(data
, key
, iv
, EVP_des_cbc(), 1);
1322 eay_des_decrypt(data
, key
, iv
)
1323 vchar_t
*data
, *key
, *iv
;
1325 return evp_crypt(data
, key
, iv
, EVP_des_cbc(), 0);
1329 eay_des_weakkey(key
)
1332 #ifdef USE_NEW_DES_API
1333 return DES_is_weak_key((void *)key
->v
);
1335 return des_is_weak_key((void *)key
->v
);
1343 return evp_keylen(len
, EVP_des_cbc());
1346 #ifdef HAVE_OPENSSL_IDEA_H
1351 eay_idea_encrypt(data
, key
, iv
)
1352 vchar_t
*data
, *key
, *iv
;
1355 IDEA_KEY_SCHEDULE ks
;
1357 idea_set_encrypt_key((unsigned char *)key
->v
, &ks
);
1359 /* allocate buffer for result */
1360 if ((res
= vmalloc(data
->l
)) == NULL
)
1363 /* decryption data */
1364 idea_cbc_encrypt((unsigned char *)data
->v
, (unsigned char *)res
->v
, data
->l
,
1365 &ks
, (unsigned char *)iv
->v
, IDEA_ENCRYPT
);
1371 eay_idea_decrypt(data
, key
, iv
)
1372 vchar_t
*data
, *key
, *iv
;
1375 IDEA_KEY_SCHEDULE ks
, dks
;
1377 idea_set_encrypt_key((unsigned char *)key
->v
, &ks
);
1378 idea_set_decrypt_key(&ks
, &dks
);
1380 /* allocate buffer for result */
1381 if ((res
= vmalloc(data
->l
)) == NULL
)
1384 /* decryption data */
1385 idea_cbc_encrypt((unsigned char *)data
->v
, (unsigned char *)res
->v
, data
->l
,
1386 &dks
, (unsigned char *)iv
->v
, IDEA_DECRYPT
);
1392 eay_idea_weakkey(key
)
1399 eay_idea_keylen(len
)
1402 if (len
!= 0 && len
!= 128)
1412 eay_bf_encrypt(data
, key
, iv
)
1413 vchar_t
*data
, *key
, *iv
;
1415 return evp_crypt(data
, key
, iv
, EVP_bf_cbc(), 1);
1419 eay_bf_decrypt(data
, key
, iv
)
1420 vchar_t
*data
, *key
, *iv
;
1422 return evp_crypt(data
, key
, iv
, EVP_bf_cbc(), 0);
1429 return 0; /* XXX to be done. refer to RFC 2451 */
1438 if (len
< 40 || len
> 448)
1443 #ifdef HAVE_OPENSSL_RC5_H
1448 eay_rc5_encrypt(data
, key
, iv
)
1449 vchar_t
*data
, *key
, *iv
;
1454 /* in RFC 2451, there is information about the number of round. */
1455 RC5_32_set_key(&ks
, key
->l
, (unsigned char *)key
->v
, 16);
1457 /* allocate buffer for result */
1458 if ((res
= vmalloc(data
->l
)) == NULL
)
1461 /* decryption data */
1462 RC5_32_cbc_encrypt((unsigned char *)data
->v
, (unsigned char *)res
->v
, data
->l
,
1463 &ks
, (unsigned char *)iv
->v
, RC5_ENCRYPT
);
1469 eay_rc5_decrypt(data
, key
, iv
)
1470 vchar_t
*data
, *key
, *iv
;
1475 /* in RFC 2451, there is information about the number of round. */
1476 RC5_32_set_key(&ks
, key
->l
, (unsigned char *)key
->v
, 16);
1478 /* allocate buffer for result */
1479 if ((res
= vmalloc(data
->l
)) == NULL
)
1482 /* decryption data */
1483 RC5_32_cbc_encrypt((unsigned char *)data
->v
, (unsigned char *)res
->v
, data
->l
,
1484 &ks
, (unsigned char *)iv
->v
, RC5_DECRYPT
);
1490 eay_rc5_weakkey(key
)
1493 return 0; /* No known weak keys when used with 16 rounds. */
1503 if (len
< 40 || len
> 2040)
1513 eay_3des_encrypt(data
, key
, iv
)
1514 vchar_t
*data
, *key
, *iv
;
1516 return evp_crypt(data
, key
, iv
, EVP_des_ede3_cbc(), 1);
1520 eay_3des_decrypt(data
, key
, iv
)
1521 vchar_t
*data
, *key
, *iv
;
1523 return evp_crypt(data
, key
, iv
, EVP_des_ede3_cbc(), 0);
1527 eay_3des_weakkey(key
)
1530 #ifdef USE_NEW_DES_API
1531 return (DES_is_weak_key((void *)key
->v
) ||
1532 DES_is_weak_key((void *)(key
->v
+ 8)) ||
1533 DES_is_weak_key((void *)(key
->v
+ 16)));
1538 return (des_is_weak_key((void *)key
->v
) ||
1539 des_is_weak_key((void *)(key
->v
+ 8)) ||
1540 des_is_weak_key((void *)(key
->v
+ 16)));
1545 eay_3des_keylen(len
)
1548 if (len
!= 0 && len
!= 192)
1557 eay_cast_encrypt(data
, key
, iv
)
1558 vchar_t
*data
, *key
, *iv
;
1560 return evp_crypt(data
, key
, iv
, EVP_cast5_cbc(), 1);
1564 eay_cast_decrypt(data
, key
, iv
)
1565 vchar_t
*data
, *key
, *iv
;
1567 return evp_crypt(data
, key
, iv
, EVP_cast5_cbc(), 0);
1571 eay_cast_weakkey(key
)
1574 return 0; /* No known weak keys. */
1578 eay_cast_keylen(len
)
1583 if (len
< 40 || len
> 128)
1591 #ifndef HAVE_OPENSSL_AES_H
1593 eay_aes_encrypt(data
, key
, iv
)
1594 vchar_t
*data
, *key
, *iv
;
1600 memset(&k
, 0, sizeof(k
));
1601 if (rijndael_makeKey(&k
, DIR_ENCRYPT
, key
->l
<< 3, key
->v
) < 0)
1604 /* allocate buffer for result */
1605 if ((res
= vmalloc(data
->l
)) == NULL
)
1608 /* encryption data */
1609 memset(&c
, 0, sizeof(c
));
1610 if (rijndael_cipherInit(&c
, MODE_CBC
, iv
->v
) < 0){
1614 if (rijndael_blockEncrypt(&c
, &k
, data
->v
, data
->l
<< 3, res
->v
) < 0){
1623 eay_aes_decrypt(data
, key
, iv
)
1624 vchar_t
*data
, *key
, *iv
;
1630 memset(&k
, 0, sizeof(k
));
1631 if (rijndael_makeKey(&k
, DIR_DECRYPT
, key
->l
<< 3, key
->v
) < 0)
1634 /* allocate buffer for result */
1635 if ((res
= vmalloc(data
->l
)) == NULL
)
1638 /* decryption data */
1639 memset(&c
, 0, sizeof(c
));
1640 if (rijndael_cipherInit(&c
, MODE_CBC
, iv
->v
) < 0){
1644 if (rijndael_blockDecrypt(&c
, &k
, data
->v
, data
->l
<< 3, res
->v
) < 0){
1652 static inline const EVP_CIPHER
*
1653 aes_evp_by_keylen(int keylen
)
1658 return EVP_aes_128_cbc();
1661 return EVP_aes_192_cbc();
1664 return EVP_aes_256_cbc();
1671 eay_aes_encrypt(data
, key
, iv
)
1672 vchar_t
*data
, *key
, *iv
;
1674 return evp_crypt(data
, key
, iv
, aes_evp_by_keylen(key
->l
), 1);
1678 eay_aes_decrypt(data
, key
, iv
)
1679 vchar_t
*data
, *key
, *iv
;
1681 return evp_crypt(data
, key
, iv
, aes_evp_by_keylen(key
->l
), 0);
1686 eay_aes_weakkey(key
)
1698 if (len
!= 128 && len
!= 192 && len
!= 256)
1703 #if defined(HAVE_OPENSSL_CAMELLIA_H)
1707 static inline const EVP_CIPHER
*
1708 camellia_evp_by_keylen(int keylen
)
1713 return EVP_camellia_128_cbc();
1716 return EVP_camellia_192_cbc();
1719 return EVP_camellia_256_cbc();
1726 eay_camellia_encrypt(data
, key
, iv
)
1727 vchar_t
*data
, *key
, *iv
;
1729 return evp_crypt(data
, key
, iv
, camellia_evp_by_keylen(key
->l
), 1);
1733 eay_camellia_decrypt(data
, key
, iv
)
1734 vchar_t
*data
, *key
, *iv
;
1736 return evp_crypt(data
, key
, iv
, camellia_evp_by_keylen(key
->l
), 0);
1740 eay_camellia_weakkey(key
)
1747 eay_camellia_keylen(len
)
1752 if (len
!= 128 && len
!= 192 && len
!= 256)
1759 /* for ipsec part */
1773 eay_twofish_keylen(len
)
1776 if (len
< 0 || len
> 256)
1782 eay_null_keylen(len
)
1792 eay_hmac_init(key
, md
)
1796 HMAC_CTX
*c
= racoon_malloc(sizeof(*c
));
1798 HMAC_Init(c
, key
->v
, key
->l
, md
);
1808 eay_hmacsha2_512_one(key
, data
)
1809 vchar_t
*key
, *data
;
1814 ctx
= eay_hmacsha2_512_init(key
);
1815 eay_hmacsha2_512_update(ctx
, data
);
1816 res
= eay_hmacsha2_512_final(ctx
);
1822 eay_hmacsha2_512_init(key
)
1825 return eay_hmac_init(key
, EVP_sha2_512());
1829 eay_hmacsha2_512_update(c
, data
)
1833 HMAC_Update((HMAC_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
1837 eay_hmacsha2_512_final(c
)
1843 if ((res
= vmalloc(SHA512_DIGEST_LENGTH
)) == 0)
1846 HMAC_Final((HMAC_CTX
*)c
, (unsigned char *) res
->v
, &l
);
1848 HMAC_cleanup((HMAC_CTX
*)c
);
1849 (void)racoon_free(c
);
1851 if (SHA512_DIGEST_LENGTH
!= res
->l
) {
1852 plog(LLV_ERROR
, LOCATION
, NULL
,
1853 "hmac sha2_512 length mismatch %zd.\n", res
->l
);
1865 eay_hmacsha2_384_one(key
, data
)
1866 vchar_t
*key
, *data
;
1871 ctx
= eay_hmacsha2_384_init(key
);
1872 eay_hmacsha2_384_update(ctx
, data
);
1873 res
= eay_hmacsha2_384_final(ctx
);
1879 eay_hmacsha2_384_init(key
)
1882 return eay_hmac_init(key
, EVP_sha2_384());
1886 eay_hmacsha2_384_update(c
, data
)
1890 HMAC_Update((HMAC_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
1894 eay_hmacsha2_384_final(c
)
1900 if ((res
= vmalloc(SHA384_DIGEST_LENGTH
)) == 0)
1903 HMAC_Final((HMAC_CTX
*)c
, (unsigned char *) res
->v
, &l
);
1905 HMAC_cleanup((HMAC_CTX
*)c
);
1906 (void)racoon_free(c
);
1908 if (SHA384_DIGEST_LENGTH
!= res
->l
) {
1909 plog(LLV_ERROR
, LOCATION
, NULL
,
1910 "hmac sha2_384 length mismatch %zd.\n", res
->l
);
1922 eay_hmacsha2_256_one(key
, data
)
1923 vchar_t
*key
, *data
;
1928 ctx
= eay_hmacsha2_256_init(key
);
1929 eay_hmacsha2_256_update(ctx
, data
);
1930 res
= eay_hmacsha2_256_final(ctx
);
1936 eay_hmacsha2_256_init(key
)
1939 return eay_hmac_init(key
, EVP_sha2_256());
1943 eay_hmacsha2_256_update(c
, data
)
1947 HMAC_Update((HMAC_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
1951 eay_hmacsha2_256_final(c
)
1957 if ((res
= vmalloc(SHA256_DIGEST_LENGTH
)) == 0)
1960 HMAC_Final((HMAC_CTX
*)c
, (unsigned char *) res
->v
, &l
);
1962 HMAC_cleanup((HMAC_CTX
*)c
);
1963 (void)racoon_free(c
);
1965 if (SHA256_DIGEST_LENGTH
!= res
->l
) {
1966 plog(LLV_ERROR
, LOCATION
, NULL
,
1967 "hmac sha2_256 length mismatch %zd.\n", res
->l
);
1974 #endif /* WITH_SHA2 */
1980 eay_hmacsha1_one(key
, data
)
1981 vchar_t
*key
, *data
;
1986 ctx
= eay_hmacsha1_init(key
);
1987 eay_hmacsha1_update(ctx
, data
);
1988 res
= eay_hmacsha1_final(ctx
);
1994 eay_hmacsha1_init(key
)
1997 return eay_hmac_init(key
, EVP_sha1());
2001 eay_hmacsha1_update(c
, data
)
2005 HMAC_Update((HMAC_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
2009 eay_hmacsha1_final(c
)
2015 if ((res
= vmalloc(SHA_DIGEST_LENGTH
)) == 0)
2018 HMAC_Final((HMAC_CTX
*)c
, (unsigned char *) res
->v
, &l
);
2020 HMAC_cleanup((HMAC_CTX
*)c
);
2021 (void)racoon_free(c
);
2023 if (SHA_DIGEST_LENGTH
!= res
->l
) {
2024 plog(LLV_ERROR
, LOCATION
, NULL
,
2025 "hmac sha1 length mismatch %zd.\n", res
->l
);
2037 eay_hmacmd5_one(key
, data
)
2038 vchar_t
*key
, *data
;
2043 ctx
= eay_hmacmd5_init(key
);
2044 eay_hmacmd5_update(ctx
, data
);
2045 res
= eay_hmacmd5_final(ctx
);
2051 eay_hmacmd5_init(key
)
2054 return eay_hmac_init(key
, EVP_md5());
2058 eay_hmacmd5_update(c
, data
)
2062 HMAC_Update((HMAC_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
2066 eay_hmacmd5_final(c
)
2072 if ((res
= vmalloc(MD5_DIGEST_LENGTH
)) == 0)
2075 HMAC_Final((HMAC_CTX
*)c
, (unsigned char *) res
->v
, &l
);
2077 HMAC_cleanup((HMAC_CTX
*)c
);
2078 (void)racoon_free(c
);
2080 if (MD5_DIGEST_LENGTH
!= res
->l
) {
2081 plog(LLV_ERROR
, LOCATION
, NULL
,
2082 "hmac md5 length mismatch %zd.\n", res
->l
);
2092 * SHA2-512 functions
2097 SHA512_CTX
*c
= racoon_malloc(sizeof(*c
));
2105 eay_sha2_512_update(c
, data
)
2109 SHA512_Update((SHA512_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
2115 eay_sha2_512_final(c
)
2120 if ((res
= vmalloc(SHA512_DIGEST_LENGTH
)) == 0)
2123 SHA512_Final((unsigned char *) res
->v
, (SHA512_CTX
*)c
);
2124 (void)racoon_free(c
);
2130 eay_sha2_512_one(data
)
2136 ctx
= eay_sha2_512_init();
2137 eay_sha2_512_update(ctx
, data
);
2138 res
= eay_sha2_512_final(ctx
);
2144 eay_sha2_512_hashlen()
2146 return SHA512_DIGEST_LENGTH
<< 3;
2152 * SHA2-384 functions
2157 SHA384_CTX
*c
= racoon_malloc(sizeof(*c
));
2165 eay_sha2_384_update(c
, data
)
2169 SHA384_Update((SHA384_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
2175 eay_sha2_384_final(c
)
2180 if ((res
= vmalloc(SHA384_DIGEST_LENGTH
)) == 0)
2183 SHA384_Final((unsigned char *) res
->v
, (SHA384_CTX
*)c
);
2184 (void)racoon_free(c
);
2190 eay_sha2_384_one(data
)
2196 ctx
= eay_sha2_384_init();
2197 eay_sha2_384_update(ctx
, data
);
2198 res
= eay_sha2_384_final(ctx
);
2204 eay_sha2_384_hashlen()
2206 return SHA384_DIGEST_LENGTH
<< 3;
2212 * SHA2-256 functions
2217 SHA256_CTX
*c
= racoon_malloc(sizeof(*c
));
2225 eay_sha2_256_update(c
, data
)
2229 SHA256_Update((SHA256_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
2235 eay_sha2_256_final(c
)
2240 if ((res
= vmalloc(SHA256_DIGEST_LENGTH
)) == 0)
2243 SHA256_Final((unsigned char *) res
->v
, (SHA256_CTX
*)c
);
2244 (void)racoon_free(c
);
2250 eay_sha2_256_one(data
)
2256 ctx
= eay_sha2_256_init();
2257 eay_sha2_256_update(ctx
, data
);
2258 res
= eay_sha2_256_final(ctx
);
2264 eay_sha2_256_hashlen()
2266 return SHA256_DIGEST_LENGTH
<< 3;
2276 SHA_CTX
*c
= racoon_malloc(sizeof(*c
));
2284 eay_sha1_update(c
, data
)
2288 SHA1_Update((SHA_CTX
*)c
, data
->v
, data
->l
);
2299 if ((res
= vmalloc(SHA_DIGEST_LENGTH
)) == 0)
2302 SHA1_Final((unsigned char *) res
->v
, (SHA_CTX
*)c
);
2303 (void)racoon_free(c
);
2315 ctx
= eay_sha1_init();
2316 eay_sha1_update(ctx
, data
);
2317 res
= eay_sha1_final(ctx
);
2325 return SHA_DIGEST_LENGTH
<< 3;
2334 MD5_CTX
*c
= racoon_malloc(sizeof(*c
));
2342 eay_md5_update(c
, data
)
2346 MD5_Update((MD5_CTX
*)c
, data
->v
, data
->l
);
2357 if ((res
= vmalloc(MD5_DIGEST_LENGTH
)) == 0)
2360 MD5_Final((unsigned char *) res
->v
, (MD5_CTX
*)c
);
2361 (void)racoon_free(c
);
2373 ctx
= eay_md5_init();
2374 eay_md5_update(ctx
, data
);
2375 res
= eay_md5_final(ctx
);
2383 return MD5_DIGEST_LENGTH
<< 3;
2388 * size: number of bytes.
2391 eay_set_random(size
)
2397 if ((r
= BN_new()) == NULL
)
2399 BN_rand(r
, size
* 8, 0, 0);
2410 eay_dh_generate(prime
, g
, publen
, pub
, priv
)
2411 vchar_t
*prime
, **pub
, **priv
;
2420 /* pre-process to generate number */
2421 if (eay_v2bn(&p
, prime
) < 0)
2424 if ((dh
= DH_new()) == NULL
)
2427 p
= NULL
; /* p is now part of dh structure */
2429 if ((dh
->g
= BN_new()) == NULL
)
2431 if (!BN_set_word(dh
->g
, g
))
2435 dh
->length
= publen
;
2437 /* generate public and private number */
2438 if (!DH_generate_key(dh
))
2441 /* copy results to buffers */
2442 if (eay_bn2v(pub
, dh
->pub_key
) < 0)
2444 if (eay_bn2v(priv
, dh
->priv_key
) < 0) {
2460 eay_dh_compute(prime
, g
, pub
, priv
, pub2
, key
)
2461 vchar_t
*prime
, *pub
, *priv
, *pub2
, **key
;
2464 BIGNUM
*dh_pub
= NULL
;
2467 unsigned char *v
= NULL
;
2470 /* make public number to compute */
2471 if (eay_v2bn(&dh_pub
, pub2
) < 0)
2474 /* make DH structure */
2475 if ((dh
= DH_new()) == NULL
)
2477 if (eay_v2bn(&dh
->p
, prime
) < 0)
2479 if (eay_v2bn(&dh
->pub_key
, pub
) < 0)
2481 if (eay_v2bn(&dh
->priv_key
, priv
) < 0)
2483 dh
->length
= pub2
->l
* 8;
2486 if ((dh
->g
= BN_new()) == NULL
)
2488 if (!BN_set_word(dh
->g
, g
))
2491 if ((v
= racoon_calloc(prime
->l
, sizeof(u_char
))) == NULL
)
2493 if ((l
= DH_compute_key(v
, dh_pub
, dh
)) == -1)
2495 memcpy((*key
)->v
+ (prime
->l
- l
), v
, l
);
2510 * convert vchar_t <-> BIGNUM.
2512 * vchar_t: unit is u_char, network endian, most significant byte first.
2513 * BIGNUM: unit is BN_ULONG, each of BN_ULONG is in host endian,
2514 * least significant BN_ULONG must come first.
2516 * hex value of "0x3ffe050104" is represented as follows:
2517 * vchar_t: 3f fe 05 01 04
2518 * BIGNUM (BN_ULONG = u_int8_t): 04 01 05 fe 3f
2519 * BIGNUM (BN_ULONG = u_int16_t): 0x0104 0xfe05 0x003f
2520 * BIGNUM (BN_ULONG = u_int32_t_t): 0xfe050104 0x0000003f
2527 if ((*bn
= BN_bin2bn((unsigned char *) var
->v
, var
->l
, NULL
)) == NULL
)
2538 *var
= vmalloc(bn
->top
* BN_BYTES
);
2542 (*var
)->l
= BN_bn2bin(bn
, (unsigned char *) (*var
)->v
);
2550 OpenSSL_add_all_algorithms();
2551 ERR_load_crypto_strings();
2552 #ifdef HAVE_OPENSSL_ENGINE_H
2553 ENGINE_load_builtin_engines();
2554 ENGINE_register_all_complete();
2559 base64_decode(char *in
, long inlen
)
2561 BIO
*bio
=NULL
, *b64
=NULL
;
2562 vchar_t
*res
= NULL
;
2566 outb
= malloc(inlen
* 2);
2569 bio
= BIO_new_mem_buf(in
, inlen
);
2570 b64
= BIO_new(BIO_f_base64());
2571 BIO_set_flags(b64
, BIO_FLAGS_BASE64_NO_NL
);
2572 bio
= BIO_push(b64
, bio
);
2574 outlen
= BIO_read(bio
, outb
, inlen
* 2);
2576 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
2580 res
= vmalloc(outlen
);
2584 memcpy(res
->v
, outb
, outlen
);
2596 base64_encode(char *in
, long inlen
)
2598 BIO
*bio
=NULL
, *b64
=NULL
;
2601 vchar_t
*res
= NULL
;
2603 bio
= BIO_new(BIO_s_mem());
2604 b64
= BIO_new(BIO_f_base64());
2605 BIO_set_flags(b64
, BIO_FLAGS_BASE64_NO_NL
);
2606 bio
= BIO_push(b64
, bio
);
2608 BIO_write(bio
, in
, inlen
);
2611 plen
= BIO_get_mem_data(bio
, &ptr
);
2612 res
= vmalloc(plen
+1);
2616 memcpy (res
->v
, ptr
, plen
);
2617 res
->v
[plen
] = '\0';
2627 binbuf_pubkey2rsa(vchar_t
*binbuf
)
2630 RSA
*rsa_pub
= NULL
;
2632 if (binbuf
->v
[0] > binbuf
->l
- 1) {
2633 plog(LLV_ERROR
, LOCATION
, NULL
, "Plain RSA pubkey format error: decoded string doesn't make sense.\n");
2637 exp
= BN_bin2bn((unsigned char *) (binbuf
->v
+ 1), binbuf
->v
[0], NULL
);
2638 mod
= BN_bin2bn((unsigned char *) (binbuf
->v
+ binbuf
->v
[0] + 1),
2639 binbuf
->l
- binbuf
->v
[0] - 1, NULL
);
2640 rsa_pub
= RSA_new();
2642 if (!exp
|| !mod
|| !rsa_pub
) {
2643 plog(LLV_ERROR
, LOCATION
, NULL
, "Plain RSA pubkey parsing error: %s\n", eay_strerror());
2662 base64_pubkey2rsa(char *in
)
2665 RSA
*rsa_pub
= NULL
;
2668 if (strncmp(in
, "0s", 2) != 0) {
2669 plog(LLV_ERROR
, LOCATION
, NULL
, "Plain RSA pubkey format error: doesn't start with '0s'\n");
2673 binbuf
= base64_decode(in
+ 2, strlen(in
+ 2));
2675 plog(LLV_ERROR
, LOCATION
, NULL
, "Plain RSA pubkey format error: Base64 decoding failed.\n");
2679 if (binbuf
->v
[0] > binbuf
->l
- 1) {
2680 plog(LLV_ERROR
, LOCATION
, NULL
, "Plain RSA pubkey format error: decoded string doesn't make sense.\n");
2684 rsa_pub
= binbuf_pubkey2rsa(binbuf
);
2694 bignum_pubkey2rsa(BIGNUM
*in
)
2696 RSA
*rsa_pub
= NULL
;
2699 binbuf
= vmalloc(BN_num_bytes(in
));
2701 plog(LLV_ERROR
, LOCATION
, NULL
, "Plain RSA pubkey conversion: memory allocation failed..\n");
2705 BN_bn2bin(in
, (unsigned char *) binbuf
->v
);
2707 rsa_pub
= binbuf_pubkey2rsa(binbuf
);
2722 vrand
= eay_set_random(sizeof(result
));
2723 memcpy(&result
, vrand
->v
, sizeof(result
));
2732 return SSLeay_version(SSLEAY_VERSION
);