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
36 #define COMMON_DIGEST_FOR_OPENSSL 1
38 #include <sys/types.h>
39 #include <sys/param.h>
47 /* get openssl/ssleay version number */
48 #include <openssl/opensslv.h>
50 #if !defined(OPENSSL_VERSION_NUMBER) || (OPENSSL_VERSION_NUMBER < 0x0090602fL)
51 #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>
60 #include <openssl/des.h>
61 #include <openssl/crypto.h>
62 #ifdef HAVE_OPENSSL_ENGINE_H
63 #include <openssl/engine.h>
65 #include <openssl/blowfish.h>
66 #include <openssl/cast.h>
67 #include <openssl/err.h>
68 #ifdef HAVE_OPENSSL_RC5_H
69 #include <openssl/rc5.h>
71 #ifdef HAVE_OPENSSL_IDEA_H
72 #include <openssl/idea.h>
74 #if defined(HAVE_OPENSSL_AES_H)
75 #include <openssl/aes.h>
76 #elif defined(HAVE_OPENSSL_RIJNDAEL_H)
77 #include <openssl/rijndael.h>
79 #else /* HAVE_OPENSSL */
80 #include <Security/SecDH.h>
81 #include <Security/SecRandom.h>
82 #endif /* HAVE_OPENSSL */
84 #include <CommonCrypto/CommonDigest.h>
85 #include <CommonCrypto/CommonHMAC.h>
86 #include <CommonCrypto/CommonCryptor.h>
90 #if OPENSSL_VERSION_NUMBER < 0x0090700fL
91 typedef STACK_OF(GENERAL_NAME
) GENERAL_NAMES
;
93 #define USE_NEW_DES_API
96 #define OpenSSL_BUG() do { plog(LLV_ERROR, LOCATION, NULL, "OpenSSL function failed\n"); } while(0)
99 #include "crypto_openssl.h"
105 #include "gcmalloc.h"
109 * I hate to cast every parameter to des_xx into void *, but it is
110 * necessary for SSLeay/OpenSSL portability. It sucks.
114 static int cb_check_cert_local
__P((int, X509_STORE_CTX
*));
115 static int cb_check_cert_remote
__P((int, X509_STORE_CTX
*));
116 static X509
*mem2x509
__P((vchar_t
*));
118 static caddr_t eay_hmac_init
__P((vchar_t
*, CCHmacAlgorithm
));
122 /* X509 Certificate */
124 * convert the string of the subject name into DER
125 * e.g. str = "C=JP, ST=Kanagawa";
128 eay_str2asn1dn(str
, len
)
142 buf
= racoon_malloc(len
+ 1);
144 plog(LLV_WARNING
, LOCATION
, NULL
,"failed to allocate buffer\n");
147 memcpy(buf
, str
, len
);
149 name
= X509_NAME_new();
153 for (i
= 0; i
< len
; i
++) {
154 if (!value
&& buf
[i
] == '=') {
158 } else if (buf
[i
] == ',' || buf
[i
] == '/') {
161 plog(LLV_DEBUG
, LOCATION
, NULL
, "DN: %s=%s\n",
164 if (!value
) goto err
;
165 if (!X509_NAME_add_entry_by_txt(name
, field
,
166 (value
[0] == '*' && value
[1] == 0) ?
167 V_ASN1_PRINTABLESTRING
: MBSTRING_ASC
,
168 (unsigned char *) value
, -1, -1, 0)) {
169 plog(LLV_ERROR
, LOCATION
, NULL
,
170 "Invalid DN field: %s=%s\n",
172 plog(LLV_ERROR
, LOCATION
, NULL
,
173 "%s\n", eay_strerror());
176 for (j
= i
+ 1; j
< len
; j
++) {
187 plog(LLV_DEBUG
, LOCATION
, NULL
, "DN: %s=%s\n",
190 if (!value
) goto err
;
191 if (!X509_NAME_add_entry_by_txt(name
, field
,
192 (value
[0] == '*' && value
[1] == 0) ?
193 V_ASN1_PRINTABLESTRING
: MBSTRING_ASC
,
194 (unsigned char *) value
, -1, -1, 0)) {
195 plog(LLV_ERROR
, LOCATION
, NULL
,
196 "Invalid DN field: %s=%s\n",
198 plog(LLV_ERROR
, LOCATION
, NULL
,
199 "%s\n", eay_strerror());
203 i
= i2d_X509_NAME(name
, NULL
);
210 i
= i2d_X509_NAME(name
, (void *)&p
);
220 X509_NAME_free(name
);
227 * convert the hex string of the subject name into DER
230 eay_hex2asn1dn(const char *hex
, int len
)
232 BIGNUM
*bn
= BN_new();
240 if (BN_hex2bn(&bn
, hex
) != len
) {
241 plog(LLV_ERROR
, LOCATION
, NULL
,
242 "conversion of Hex-encoded ASN1 string to binary failed: %s\n",
247 binlen
= BN_num_bytes(bn
);
248 ret
= vmalloc(binlen
);
250 plog(LLV_WARNING
, LOCATION
, NULL
,"failed to allocate buffer\n");
255 BN_bn2bin(bn
, (unsigned char *) binbuf
);
264 * The following are derived from code in crypto/x509/x509_cmp.c
266 * X509_NAME_wildcmp() adds wildcard matching to the original
267 * X509_NAME_cmp(), nocase_cmp() and nocase_spacenorm_cmp() are as is.
270 /* Case insensitive string comparision */
271 static int nocase_cmp(const ASN1_STRING
*a
, const ASN1_STRING
*b
)
275 if (a
->length
!= b
->length
)
276 return (a
->length
- b
->length
);
278 for (i
=0; i
<a
->length
; i
++)
282 ca
= tolower(a
->data
[i
]);
283 cb
= tolower(b
->data
[i
]);
291 /* Case insensitive string comparision with space normalization
292 * Space normalization - ignore leading, trailing spaces,
293 * multiple spaces between characters are replaced by single space
295 static int nocase_spacenorm_cmp(const ASN1_STRING
*a
, const ASN1_STRING
*b
)
297 unsigned char *pa
= NULL
, *pb
= NULL
;
305 /* skip leading spaces */
306 while (la
> 0 && isspace(*pa
))
311 while (lb
> 0 && isspace(*pb
))
317 /* skip trailing spaces */
318 while (la
> 0 && isspace(pa
[la
-1]))
320 while (lb
> 0 && isspace(pb
[lb
-1]))
323 /* compare strings with space normalization */
324 while (la
> 0 && lb
> 0)
328 /* compare character */
337 if (la
<= 0 || lb
<= 0)
340 /* is white space next character ? */
341 if (isspace(*pa
) && isspace(*pb
))
343 /* skip remaining white spaces */
344 while (la
> 0 && isspace(*pa
))
349 while (lb
> 0 && isspace(*pb
))
356 if (la
> 0 || lb
> 0)
362 static int X509_NAME_wildcmp(const X509_NAME
*a
, const X509_NAME
*b
)
365 X509_NAME_ENTRY
*na
,*nb
;
367 if (sk_X509_NAME_ENTRY_num(a
->entries
)
368 != sk_X509_NAME_ENTRY_num(b
->entries
))
369 return sk_X509_NAME_ENTRY_num(a
->entries
)
370 -sk_X509_NAME_ENTRY_num(b
->entries
);
371 for (i
=sk_X509_NAME_ENTRY_num(a
->entries
)-1; i
>=0; i
--)
373 na
=sk_X509_NAME_ENTRY_value(a
->entries
,i
);
374 nb
=sk_X509_NAME_ENTRY_value(b
->entries
,i
);
375 j
=OBJ_cmp(na
->object
,nb
->object
);
377 if ((na
->value
->length
== 1 && na
->value
->data
[0] == '*')
378 || (nb
->value
->length
== 1 && nb
->value
->data
[0] == '*'))
380 j
=na
->value
->type
-nb
->value
->type
;
382 if (na
->value
->type
== V_ASN1_PRINTABLESTRING
)
383 j
=nocase_spacenorm_cmp(na
->value
, nb
->value
);
384 else if (na
->value
->type
== V_ASN1_IA5STRING
385 && OBJ_obj2nid(na
->object
) == NID_pkcs9_emailAddress
)
386 j
=nocase_cmp(na
->value
, nb
->value
);
389 j
=na
->value
->length
-nb
->value
->length
;
391 j
=memcmp(na
->value
->data
,nb
->value
->data
,
403 * compare two subjectNames.
409 eay_cmp_asn1dn(n1
, n2
)
412 X509_NAME
*a
= NULL
, *b
= NULL
;
417 if (!d2i_X509_NAME(&a
, (void *)&p
, n1
->l
))
420 if (!d2i_X509_NAME(&b
, (void *)&p
, n2
->l
))
423 i
= X509_NAME_wildcmp(a
, b
);
434 * this functions is derived from apps/verify.c in OpenSSL0.9.5
437 eay_check_x509cert(cert
, CApath
, CAfile
, local
)
443 X509_STORE
*cert_ctx
= NULL
;
444 X509_LOOKUP
*lookup
= NULL
;
449 cert_ctx
= X509_STORE_new();
450 if (cert_ctx
== NULL
)
454 X509_STORE_set_verify_cb_func(cert_ctx
, cb_check_cert_local
);
456 X509_STORE_set_verify_cb_func(cert_ctx
, cb_check_cert_remote
);
458 lookup
= X509_STORE_add_lookup(cert_ctx
, X509_LOOKUP_file());
462 X509_LOOKUP_load_file(lookup
, CAfile
,
463 (CAfile
== NULL
) ? X509_FILETYPE_DEFAULT
: X509_FILETYPE_PEM
);
465 lookup
= X509_STORE_add_lookup(cert_ctx
, X509_LOOKUP_hash_dir());
468 error
= X509_LOOKUP_add_dir(lookup
, CApath
, X509_FILETYPE_PEM
);
473 error
= -1; /* initialized */
475 /* read the certificate to be verified */
476 x509
= mem2x509(cert
);
480 csc
= X509_STORE_CTX_new();
483 X509_STORE_CTX_init(csc
, cert_ctx
, x509
, NULL
);
484 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
485 X509_STORE_CTX_set_flags (csc
, X509_V_FLAG_CRL_CHECK
);
486 X509_STORE_CTX_set_flags (csc
, X509_V_FLAG_CRL_CHECK_ALL
);
488 error
= X509_verify_cert(csc
);
489 X509_STORE_CTX_free(csc
);
492 * if x509_verify_cert() is successful then the value of error is
495 error
= error
? 0 : -1;
499 plog(LLV_WARNING
, LOCATION
, NULL
,"%s\n", eay_strerror());
500 if (cert_ctx
!= NULL
)
501 X509_STORE_free(cert_ctx
);
509 * callback function for verifing certificate.
510 * this function is derived from cb() in openssl/apps/s_server.c
513 cb_check_cert_local(ok
, ctx
)
522 X509_get_subject_name(ctx
->current_cert
),
526 * since we are just checking the certificates, it is
527 * ok if they are self signed. But we should still warn
530 switch (ctx
->error
) {
531 case X509_V_ERR_CERT_HAS_EXPIRED
:
532 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
:
533 case X509_V_ERR_INVALID_CA
:
534 case X509_V_ERR_PATH_LENGTH_EXCEEDED
:
535 case X509_V_ERR_INVALID_PURPOSE
:
536 case X509_V_ERR_UNABLE_TO_GET_CRL
:
538 log_tag
= LLV_WARNING
;
543 plog(log_tag
, LOCATION
, NULL
,
544 "%s(%d) at depth:%d SubjectName:%s\n",
545 X509_verify_cert_error_string(ctx
->error
),
556 * callback function for verifing remote certificates.
557 * this function is derived from cb() in openssl/apps/s_server.c
560 cb_check_cert_remote(ok
, ctx
)
569 X509_get_subject_name(ctx
->current_cert
),
572 switch (ctx
->error
) {
573 case X509_V_ERR_UNABLE_TO_GET_CRL
:
575 log_tag
= LLV_WARNING
;
580 plog(log_tag
, LOCATION
, NULL
,
581 "%s(%d) at depth:%d SubjectName:%s\n",
582 X509_verify_cert_error_string(ctx
->error
),
593 * get a subjectAltName from X509 certificate.
596 eay_get_x509asn1subjectname(cert
)
601 vchar_t
*name
= NULL
;
604 bp
= (unsigned char *) cert
->v
;
606 x509
= mem2x509(cert
);
610 /* get the length of the name */
611 len
= i2d_X509_NAME(x509
->cert_info
->subject
, NULL
);
616 bp
= (unsigned char *) name
->v
;
617 len
= i2d_X509_NAME(x509
->cert_info
->subject
, &bp
);
624 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
636 * Get the common name from a cert
638 #define EAY_MAX_CN_LEN 256
640 eay_get_x509_common_name(cert
)
645 vchar_t
*commonName
= NULL
;
647 commonName
= vmalloc(EAY_MAX_CN_LEN
);
648 if (commonName
== NULL
) {
649 plog(LLV_ERROR
, LOCATION
, NULL
, "no memory\n");
653 x509
= mem2x509(cert
);
659 name
= X509_get_subject_name(x509
);
660 X509_NAME_get_text_by_NID(name
, NID_commonName
, commonName
->v
, EAY_MAX_CN_LEN
);
662 commonName
->l
= strlen(commonName
->v
);
670 * get the subjectAltName from X509 certificate.
671 * the name must be terminated by '\0'.
674 eay_get_x509subjectaltname(cert
, altname
, type
, pos
, len
)
683 GENERAL_NAMES
*gens
= NULL
;
688 *type
= GENT_OTHERNAME
;
690 x509
= mem2x509(cert
);
694 gens
= X509_get_ext_d2i(x509
, NID_subject_alt_name
, NULL
, NULL
);
698 for (i
= 0; i
< sk_GENERAL_NAME_num(gens
); i
++) {
704 /* there is no data at "pos" */
705 if (i
== sk_GENERAL_NAME_num(gens
))
708 gen
= sk_GENERAL_NAME_value(gens
, i
);
710 /* make sure the data is terminated by '\0'. */
711 if (gen
->d
.ia5
->data
[gen
->d
.ia5
->length
] != '\0') {
712 plog(LLV_ERROR
, LOCATION
, NULL
,
713 "data is not terminated by 0.");
714 hexdump(gen
->d
.ia5
->data
, gen
->d
.ia5
->length
+ 1);
718 /* read DNSName / Email */
719 if (gen
->type
== GEN_DNS
||
720 gen
->type
== GEN_EMAIL
||
721 gen
->type
== GEN_URI
) {
723 *len
= gen
->d
.ia5
->length
+ 1;
724 *altname
= racoon_malloc(*len
);
728 strlcpy(*altname
, (const char *)gen
->d
.ia5
->data
, *len
);
732 } else if (gen
->type
== GEN_IPADD
) {
734 *len
= gen
->d
.ia5
->length
+ 1;
735 *altname
= racoon_malloc(*len
);
739 memcpy(*altname
, (const char *)gen
->d
.ia5
->data
, *len
);
748 racoon_free(*altname
);
752 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
754 printf("%s\n", eay_strerror());
758 /* free the whole stack. */
759 sk_GENERAL_NAME_pop_free(gens
, GENERAL_NAME_free
);
767 * decode a X509 certificate and make a readable text terminated '\n'.
768 * return the buffer allocated, so must free it later.
771 eay_get_x509text(cert
)
781 x509
= mem2x509(cert
);
785 bio
= BIO_new(BIO_s_mem());
789 error
= X509_print(bio
, x509
);
795 len
= BIO_get_mem_data(bio
, &bp
);
796 text
= racoon_malloc(len
+ 1);
799 memcpy(text
, bp
, len
);
810 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
820 /* get X509 structure from buffer. */
831 bp
= (unsigned char *) cert
->v
;
833 x509
= d2i_X509(NULL
, (void *)&bp
, cert
->l
);
840 bio
= BIO_new(BIO_s_mem());
843 len
= BIO_write(bio
, cert
->v
, cert
->l
);
846 x509
= PEM_read_bio_X509(bio
, NULL
, NULL
, NULL
);
854 * get a X509 certificate from local file.
855 * a certificate must be PEM format.
857 * path to a certificate.
859 * NULL if error occured
863 eay_get_x509cert(path
)
873 /* Read private key */
874 fp
= fopen(path
, "r");
877 x509
= PEM_read_X509(fp
, NULL
, NULL
, NULL
);
883 len
= i2d_X509(x509
, NULL
);
889 bp
= (unsigned char *) cert
->v
;
890 error
= i2d_X509(x509
, &bp
);
902 * check a X509 signature
903 * XXX: to be get hash type from my cert ?
904 * to be handled EVP_dss().
905 * OUT: return -1 when error.
909 eay_check_x509sign(source
, sig
, cert
)
919 bp
= (unsigned char *) cert
->v
;
921 x509
= d2i_X509(NULL
, (void *)&bp
, cert
->l
);
923 plog(LLV_ERROR
, LOCATION
, NULL
, "d2i_X509(): %s\n", eay_strerror());
927 evp
= X509_get_pubkey(x509
);
929 plog(LLV_ERROR
, LOCATION
, NULL
, "X509_get_pubkey(): %s\n", eay_strerror());
934 res
= eay_rsa_verify(source
, sig
, evp
->pkey
.rsa
);
943 * check RSA signature
944 * OUT: return -1 when error.
948 eay_check_rsasign(source
, sig
, rsa
)
953 return eay_rsa_verify(source
, sig
, rsa
);
957 * get PKCS#1 Private Key of PEM format from local file.
960 eay_get_pkcs1privkey(path
)
964 EVP_PKEY
*evp
= NULL
;
965 vchar_t
*pkey
= NULL
;
970 /* Read private key */
971 fp
= fopen(path
, "r");
975 evp
= PEM_read_PrivateKey(fp
, NULL
, NULL
, NULL
);
982 pkeylen
= i2d_PrivateKey(evp
, NULL
);
985 pkey
= vmalloc(pkeylen
);
988 bp
= (unsigned char *) pkey
->v
;
989 pkeylen
= i2d_PrivateKey(evp
, &bp
);
998 if (error
!= 0 && pkey
!= NULL
) {
1007 * get PKCS#1 Public Key of PEM format from local file.
1010 eay_get_pkcs1pubkey(path
)
1014 EVP_PKEY
*evp
= NULL
;
1015 vchar_t
*pkey
= NULL
;
1021 /* Read private key */
1022 fp
= fopen(path
, "r");
1026 x509
= PEM_read_X509(fp
, NULL
, NULL
, NULL
);
1033 /* Get public key - eay */
1034 evp
= X509_get_pubkey(x509
);
1038 pkeylen
= i2d_PublicKey(evp
, NULL
);
1041 pkey
= vmalloc(pkeylen
);
1044 bp
= (unsigned char *) pkey
->v
;
1045 pkeylen
= i2d_PublicKey(evp
, &bp
);
1053 if (error
!= 0 && pkey
!= NULL
) {
1062 eay_get_x509sign(src
, privkey
)
1063 vchar_t
*src
, *privkey
;
1066 u_char
*bp
= (unsigned char *) privkey
->v
;
1067 vchar_t
*sig
= NULL
;
1069 int pad
= RSA_PKCS1_PADDING
;
1071 /* XXX to be handled EVP_PKEY_DSA */
1072 evp
= d2i_PrivateKey(EVP_PKEY_RSA
, NULL
, (void *)&bp
, privkey
->l
);
1076 sig
= eay_rsa_sign(src
, evp
->pkey
.rsa
);
1084 eay_get_rsasign(src
, rsa
)
1088 return eay_rsa_sign(src
, rsa
);
1092 eay_rsa_sign(vchar_t
*src
, RSA
*rsa
)
1095 vchar_t
*sig
= NULL
;
1096 int pad
= RSA_PKCS1_PADDING
;
1098 len
= RSA_size(rsa
);
1104 len
= RSA_private_encrypt(src
->l
, (unsigned char *) src
->v
,
1105 (unsigned char *) sig
->v
, rsa
, pad
);
1107 if (len
== 0 || len
!= sig
->l
) {
1116 eay_rsa_verify(src
, sig
, rsa
)
1120 vchar_t
*xbuf
= NULL
;
1121 int pad
= RSA_PKCS1_PADDING
;
1125 len
= RSA_size(rsa
);
1126 xbuf
= vmalloc(len
);
1128 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
1132 len
= RSA_public_decrypt(sig
->l
, (unsigned char *) sig
->v
,
1133 (unsigned char *) xbuf
->v
, rsa
, pad
);
1134 if (len
== 0 || len
!= src
->l
) {
1135 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
1140 error
= memcmp(src
->v
, xbuf
->v
, src
->l
);
1150 * MUST load ERR_load_crypto_strings() first.
1155 static char ebuf
[512];
1159 const char *file
, *data
;
1163 es
= CRYPTO_thread_id();
1165 while ((l
= ERR_get_error_line_data(&file
, &line
, &data
, &flags
)) != 0){
1166 n
= snprintf(ebuf
+ len
, sizeof(ebuf
) - len
,
1168 es
, ERR_error_string(l
, buf
), file
, line
,
1169 (flags
& ERR_TXT_STRING
) ? data
: "");
1170 if (n
< 0 || n
>= sizeof(ebuf
) - len
)
1173 if (sizeof(ebuf
) < len
)
1181 evp_crypt(vchar_t
*data
, vchar_t
*key
, vchar_t
*iv
, const EVP_CIPHER
*e
, int enc
)
1189 if (data
->l
% EVP_CIPHER_block_size(e
))
1192 if ((res
= vmalloc(data
->l
)) == NULL
)
1195 EVP_CIPHER_CTX_init(&ctx
);
1197 switch(EVP_CIPHER_nid(e
)){
1204 case NID_cast5_cfb64
:
1205 case NID_cast5_ofb64
:
1206 /* XXX: can we do that also for algos with a fixed key size ?
1208 /* init context without key/iv
1210 if (!EVP_CipherInit(&ctx
, e
, NULL
, NULL
, enc
))
1219 if (!EVP_CIPHER_CTX_set_key_length(&ctx
, key
->l
))
1226 /* finalize context init with desired key size
1228 if (!EVP_CipherInit(&ctx
, NULL
, (u_char
*) key
->v
,
1229 (u_char
*) iv
->v
, enc
))
1237 if (!EVP_CipherInit(&ctx
, e
, (u_char
*) key
->v
,
1238 (u_char
*) iv
->v
, enc
)) {
1245 /* disable openssl padding */
1246 EVP_CIPHER_CTX_set_padding(&ctx
, 0);
1248 if (!EVP_Cipher(&ctx
, (u_char
*) res
->v
, (u_char
*) data
->v
, data
->l
)) {
1254 EVP_CIPHER_CTX_cleanup(&ctx
);
1260 evp_weakkey(vchar_t
*key
, const EVP_CIPHER
*e
)
1266 evp_keylen(int len
, const EVP_CIPHER
*e
)
1270 /* EVP functions return lengths in bytes, ipsec-tools
1271 * uses lengths in bits, therefore conversion is required. --AK
1273 if (len
!= 0 && len
!= (EVP_CIPHER_key_length(e
) << 3))
1276 return EVP_CIPHER_key_length(e
) << 3;
1278 #endif /* HAVE_OPENSSL */
1281 eay_CCCrypt(CCOperation oper
,
1290 CCCryptorStatus status
;
1292 /* allocate buffer for result */
1293 if ((res
= vmalloc(data
->l
)) == NULL
)
1296 status
= CCCrypt(oper
,
1302 res
->v
, res
->l
, &res_len
);
1303 if (status
== kCCSuccess
) {
1304 if (res
->l
!= res_len
) {
1305 plog(LLV_ERROR
, LOCATION
, NULL
,
1306 "crypt %d %d length mismatch. expected: %zd. got: %zd.\n",
1307 oper
, algo
, res
->l
, res_len
);
1311 plog(LLV_ERROR
, LOCATION
, NULL
,
1312 "crypt %d %d error. status %d.\n",
1313 oper
, algo
, (int)status
);
1323 eay_des_encrypt(data
, key
, iv
)
1324 vchar_t
*data
, *key
, *iv
;
1326 return(eay_CCCrypt(kCCEncrypt
, kCCAlgorithmDES
, 0 /* CBC */, data
, key
, iv
));
1330 eay_des_decrypt(data
, key
, iv
)
1331 vchar_t
*data
, *key
, *iv
;
1333 return(eay_CCCrypt(kCCDecrypt
, kCCAlgorithmDES
, 0 /* CBC */, data
, key
, iv
));
1337 eay_des_weakkey(key
)
1341 #ifdef USE_NEW_DES_API
1342 return DES_is_weak_key((void *)key
->v
);
1344 return des_is_weak_key((void *)key
->v
);
1355 /* CommonCrypto return lengths in bytes, ipsec-tools
1356 * uses lengths in bits, therefore conversion is required.
1358 if (len
!= 0 && len
!= (kCCKeySizeDES
<< 3))
1361 return kCCKeySizeDES
<< 3;
1364 #ifdef HAVE_OPENSSL_IDEA_H
1369 eay_idea_encrypt(data
, key
, iv
)
1370 vchar_t
*data
, *key
, *iv
;
1373 IDEA_KEY_SCHEDULE ks
;
1375 idea_set_encrypt_key(key
->v
, &ks
);
1377 /* allocate buffer for result */
1378 if ((res
= vmalloc(data
->l
)) == NULL
)
1381 /* decryption data */
1382 idea_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1383 &ks
, iv
->v
, IDEA_ENCRYPT
);
1389 eay_idea_decrypt(data
, key
, iv
)
1390 vchar_t
*data
, *key
, *iv
;
1393 IDEA_KEY_SCHEDULE ks
, dks
;
1395 idea_set_encrypt_key(key
->v
, &ks
);
1396 idea_set_decrypt_key(&ks
, &dks
);
1398 /* allocate buffer for result */
1399 if ((res
= vmalloc(data
->l
)) == NULL
)
1402 /* decryption data */
1403 idea_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1404 &dks
, iv
->v
, IDEA_DECRYPT
);
1410 eay_idea_weakkey(key
)
1417 eay_idea_keylen(len
)
1420 if (len
!= 0 && len
!= 128)
1431 eay_bf_encrypt(data
, key
, iv
)
1432 vchar_t
*data
, *key
, *iv
;
1434 return evp_crypt(data
, key
, iv
, EVP_bf_cbc(), 1);
1438 eay_bf_decrypt(data
, key
, iv
)
1439 vchar_t
*data
, *key
, *iv
;
1441 return evp_crypt(data
, key
, iv
, EVP_bf_cbc(), 0);
1448 return 0; /* XXX to be done. refer to RFC 2451 */
1457 if (len
< 40 || len
> 448)
1463 #ifdef HAVE_OPENSSL_RC5_H
1468 eay_rc5_encrypt(data
, key
, iv
)
1469 vchar_t
*data
, *key
, *iv
;
1474 /* in RFC 2451, there is information about the number of round. */
1475 RC5_32_set_key(&ks
, key
->l
, key
->v
, 16);
1477 /* allocate buffer for result */
1478 if ((res
= vmalloc(data
->l
)) == NULL
)
1481 /* decryption data */
1482 RC5_32_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1483 &ks
, iv
->v
, RC5_ENCRYPT
);
1489 eay_rc5_decrypt(data
, key
, iv
)
1490 vchar_t
*data
, *key
, *iv
;
1495 /* in RFC 2451, there is information about the number of round. */
1496 RC5_32_set_key(&ks
, key
->l
, key
->v
, 16);
1498 /* allocate buffer for result */
1499 if ((res
= vmalloc(data
->l
)) == NULL
)
1502 /* decryption data */
1503 RC5_32_cbc_encrypt(data
->v
, res
->v
, data
->l
,
1504 &ks
, iv
->v
, RC5_DECRYPT
);
1510 eay_rc5_weakkey(key
)
1513 return 0; /* No known weak keys when used with 16 rounds. */
1523 if (len
< 40 || len
> 2040)
1533 eay_3des_encrypt(data
, key
, iv
)
1534 vchar_t
*data
, *key
, *iv
;
1536 return(eay_CCCrypt(kCCEncrypt
, kCCAlgorithm3DES
, 0 /* CBC */, data
, key
, iv
));
1540 eay_3des_decrypt(data
, key
, iv
)
1541 vchar_t
*data
, *key
, *iv
;
1543 return(eay_CCCrypt(kCCDecrypt
, kCCAlgorithm3DES
, 0 /* CBC */, data
, key
, iv
));
1547 eay_3des_weakkey(key
)
1551 #ifdef USE_NEW_DES_API
1552 return (DES_is_weak_key((void *)key
->v
) ||
1553 DES_is_weak_key((void *)(key
->v
+ 8)) ||
1554 DES_is_weak_key((void *)(key
->v
+ 16)));
1559 return (des_is_weak_key((void *)key
->v
) ||
1560 des_is_weak_key((void *)(key
->v
+ 8)) ||
1561 des_is_weak_key((void *)(key
->v
+ 16)));
1563 #else /* HAVE_OPENSSL */
1570 eay_3des_keylen(len
)
1573 /* CommonCrypto return lengths in bytes, ipsec-tools
1574 * uses lengths in bits, therefore conversion is required.
1576 if (len
!= 0 && len
!= (kCCKeySize3DES
<< 3))
1579 return kCCKeySize3DES
<< 3;
1587 eay_cast_encrypt(data
, key
, iv
)
1588 vchar_t
*data
, *key
, *iv
;
1590 return evp_crypt(data
, key
, iv
, EVP_cast5_cbc(), 1);
1594 eay_cast_decrypt(data
, key
, iv
)
1595 vchar_t
*data
, *key
, *iv
;
1597 return evp_crypt(data
, key
, iv
, EVP_cast5_cbc(), 0);
1601 eay_cast_weakkey(key
)
1604 return 0; /* No known weak keys. */
1608 eay_cast_keylen(len
)
1613 if (len
< 40 || len
> 128)
1623 eay_aes_encrypt(data
, key
, iv
)
1624 vchar_t
*data
, *key
, *iv
;
1626 return(eay_CCCrypt(kCCEncrypt
, kCCAlgorithmAES128
/* adapts to AES-192, or AES-256 depending on the key size*/, 0 /* CBC */, data
, key
, iv
));
1630 eay_aes_decrypt(data
, key
, iv
)
1631 vchar_t
*data
, *key
, *iv
;
1633 return(eay_CCCrypt(kCCDecrypt
, kCCAlgorithmAES128
/* adapts to AES-192, or AES-256 depending on the key size*/, 0 /* CBC */, data
, key
, iv
));
1640 /* CommonCrypto return lengths in bytes, ipsec-tools
1641 * uses lengths in bits, therefore conversion is required.
1644 if (len
!= (kCCKeySizeAES128
<< 3) &&
1645 len
!= (kCCKeySizeAES192
<< 3) &&
1646 len
!= (kCCKeySizeAES256
<< 3))
1649 return kCCKeySizeAES128
<< 3;
1656 eay_aes_weakkey(key
)
1662 /* for ipsec part */
1677 eay_twofish_keylen(len
)
1680 if (len
< 0 || len
> 256)
1687 eay_null_keylen(len
)
1697 eay_hmac_init(key
, algorithm
)
1699 CCHmacAlgorithm algorithm
;
1701 CCHmacContext
*c
= racoon_malloc(sizeof(*c
));
1703 CCHmacInit(c
, algorithm
, key
->v
, key
->l
);
1713 eay_hmacsha2_512_one(key
, data
)
1714 vchar_t
*key
, *data
;
1719 ctx
= eay_hmacsha2_512_init(key
);
1720 eay_hmacsha2_512_update(ctx
, data
);
1721 res
= eay_hmacsha2_512_final(ctx
);
1727 eay_hmacsha2_512_init(key
)
1730 return eay_hmac_init(key
, kCCHmacAlgSHA512
);
1734 eay_hmacsha2_512_update(c
, data
)
1738 CCHmacUpdate((CCHmacContext
*)c
, data
->v
, data
->l
);
1742 eay_hmacsha2_512_final(c
)
1747 if ((res
= vmalloc(CC_SHA512_DIGEST_LENGTH
)) == 0)
1750 CCHmacFinal((CCHmacContext
*)c
, res
->v
);
1751 res
->l
= CC_SHA512_DIGEST_LENGTH
;
1753 (void)racoon_free(c
);
1761 eay_hmacsha2_384_one(key
, data
)
1762 vchar_t
*key
, *data
;
1767 ctx
= eay_hmacsha2_384_init(key
);
1768 eay_hmacsha2_384_update(ctx
, data
);
1769 res
= eay_hmacsha2_384_final(ctx
);
1775 eay_hmacsha2_384_init(key
)
1778 return eay_hmac_init(key
, kCCHmacAlgSHA384
);
1782 eay_hmacsha2_384_update(c
, data
)
1786 CCHmacUpdate((CCHmacContext
*)c
, data
->v
, data
->l
);
1790 eay_hmacsha2_384_final(c
)
1795 if ((res
= vmalloc(CC_SHA384_DIGEST_LENGTH
)) == 0)
1798 CCHmacFinal((CCHmacContext
*)c
, res
->v
);
1799 res
->l
= CC_SHA384_DIGEST_LENGTH
;
1801 (void)racoon_free(c
);
1809 eay_hmacsha2_256_one(key
, data
)
1810 vchar_t
*key
, *data
;
1815 ctx
= eay_hmacsha2_256_init(key
);
1816 eay_hmacsha2_256_update(ctx
, data
);
1817 res
= eay_hmacsha2_256_final(ctx
);
1823 eay_hmacsha2_256_init(key
)
1826 return eay_hmac_init(key
, kCCHmacAlgSHA256
);
1830 eay_hmacsha2_256_update(c
, data
)
1834 CCHmacUpdate((CCHmacContext
*)c
, data
->v
, data
->l
);
1838 eay_hmacsha2_256_final(c
)
1843 if ((res
= vmalloc(CC_SHA256_DIGEST_LENGTH
)) == 0)
1846 CCHmacFinal((CCHmacContext
*)c
, res
->v
);
1847 res
->l
= CC_SHA256_DIGEST_LENGTH
;
1849 (void)racoon_free(c
);
1852 #endif /* WITH_SHA2 */
1858 eay_hmacsha1_one(key
, data
)
1859 vchar_t
*key
, *data
;
1864 ctx
= eay_hmacsha1_init(key
);
1865 eay_hmacsha1_update(ctx
, data
);
1866 res
= eay_hmacsha1_final(ctx
);
1872 eay_hmacsha1_init(key
)
1875 return eay_hmac_init(key
, kCCHmacAlgSHA1
);
1879 eay_hmacsha1_update(c
, data
)
1883 CCHmacUpdate((CCHmacContext
*)c
, data
->v
, data
->l
);
1887 eay_hmacsha1_final(c
)
1892 if ((res
= vmalloc(CC_SHA1_DIGEST_LENGTH
)) == 0)
1895 CCHmacFinal((CCHmacContext
*)c
, res
->v
);
1896 res
->l
= CC_SHA1_DIGEST_LENGTH
;
1898 (void)racoon_free(c
);
1906 eay_hmacmd5_one(key
, data
)
1907 vchar_t
*key
, *data
;
1912 ctx
= eay_hmacmd5_init(key
);
1913 eay_hmacmd5_update(ctx
, data
);
1914 res
= eay_hmacmd5_final(ctx
);
1920 eay_hmacmd5_init(key
)
1923 return eay_hmac_init(key
, kCCHmacAlgMD5
);
1927 eay_hmacmd5_update(c
, data
)
1931 CCHmacUpdate((CCHmacContext
*)c
, data
->v
, data
->l
);
1935 eay_hmacmd5_final(c
)
1940 if ((res
= vmalloc(CC_MD5_DIGEST_LENGTH
)) == 0)
1943 CCHmacFinal((CCHmacContext
*)c
, res
->v
);
1944 res
->l
= CC_MD5_DIGEST_LENGTH
;
1945 (void)racoon_free(c
);
1953 * SHA2-512 functions
1958 SHA512_CTX
*c
= racoon_malloc(sizeof(*c
));
1966 eay_sha2_512_update(c
, data
)
1970 SHA512_Update((SHA512_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
1976 eay_sha2_512_final(c
)
1981 if ((res
= vmalloc(SHA512_DIGEST_LENGTH
)) == 0)
1984 SHA512_Final((unsigned char *) res
->v
, (SHA512_CTX
*)c
);
1985 (void)racoon_free(c
);
1991 eay_sha2_512_one(data
)
1997 ctx
= eay_sha2_512_init();
1998 eay_sha2_512_update(ctx
, data
);
1999 res
= eay_sha2_512_final(ctx
);
2005 eay_sha2_512_hashlen()
2007 return SHA512_DIGEST_LENGTH
<< 3;
2013 * SHA2-384 functions
2016 typedef SHA512_CTX SHA384_CTX
;
2021 SHA384_CTX
*c
= racoon_malloc(sizeof(*c
));
2029 eay_sha2_384_update(c
, data
)
2033 SHA384_Update((SHA384_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
2039 eay_sha2_384_final(c
)
2044 if ((res
= vmalloc(SHA384_DIGEST_LENGTH
)) == 0)
2047 SHA384_Final((unsigned char *) res
->v
, (SHA384_CTX
*)c
);
2048 (void)racoon_free(c
);
2054 eay_sha2_384_one(data
)
2060 ctx
= eay_sha2_384_init();
2061 eay_sha2_384_update(ctx
, data
);
2062 res
= eay_sha2_384_final(ctx
);
2068 eay_sha2_384_hashlen()
2070 return SHA384_DIGEST_LENGTH
<< 3;
2076 * SHA2-256 functions
2081 SHA256_CTX
*c
= racoon_malloc(sizeof(*c
));
2089 eay_sha2_256_update(c
, data
)
2093 SHA256_Update((SHA256_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
2099 eay_sha2_256_final(c
)
2104 if ((res
= vmalloc(SHA256_DIGEST_LENGTH
)) == 0)
2107 SHA256_Final((unsigned char *) res
->v
, (SHA256_CTX
*)c
);
2108 (void)racoon_free(c
);
2114 eay_sha2_256_one(data
)
2120 ctx
= eay_sha2_256_init();
2121 eay_sha2_256_update(ctx
, data
);
2122 res
= eay_sha2_256_final(ctx
);
2128 eay_sha2_256_hashlen()
2130 return SHA256_DIGEST_LENGTH
<< 3;
2140 SHA_CTX
*c
= racoon_malloc(sizeof(*c
));
2148 eay_sha1_update(c
, data
)
2152 SHA1_Update((SHA_CTX
*)c
, data
->v
, data
->l
);
2163 if ((res
= vmalloc(SHA_DIGEST_LENGTH
)) == 0)
2166 SHA1_Final((unsigned char *) res
->v
, (SHA_CTX
*)c
);
2167 (void)racoon_free(c
);
2179 ctx
= eay_sha1_init();
2180 eay_sha1_update(ctx
, data
);
2181 res
= eay_sha1_final(ctx
);
2189 return SHA_DIGEST_LENGTH
<< 3;
2198 MD5_CTX
*c
= racoon_malloc(sizeof(*c
));
2206 eay_md5_update(c
, data
)
2210 MD5_Update((MD5_CTX
*)c
, data
->v
, data
->l
);
2221 if ((res
= vmalloc(MD5_DIGEST_LENGTH
)) == 0)
2224 MD5_Final((unsigned char *) res
->v
, (MD5_CTX
*)c
);
2225 (void)racoon_free(c
);
2237 ctx
= eay_md5_init();
2238 eay_md5_update(ctx
, data
);
2239 res
= eay_md5_final(ctx
);
2247 return MD5_DIGEST_LENGTH
<< 3;
2254 * size: number of bytes.
2257 eay_set_random(size
)
2263 if ((r
= BN_new()) == NULL
)
2265 BN_rand(r
, size
* 8, 0, 0);
2275 eay_set_random(u_int32_t size
)
2277 vchar_t
*res
= vmalloc(size
);
2282 if (SecRandomCopyBytes(kSecRandomDefault
, size
, res
->v
)) {
2294 eay_dh_generate(prime
, g
, publen
, pub
, priv
)
2295 vchar_t
*prime
, **pub
, **priv
;
2304 /* pre-process to generate number */
2305 if (eay_v2bn(&p
, prime
) < 0)
2308 if ((dh
= DH_new()) == NULL
)
2311 p
= NULL
; /* p is now part of dh structure */
2313 if ((dh
->g
= BN_new()) == NULL
)
2315 if (!BN_set_word(dh
->g
, g
))
2319 dh
->length
= publen
;
2321 /* generate public and private number */
2322 if (!DH_generate_key(dh
))
2325 /* copy results to buffers */
2326 if (eay_bn2v(pub
, dh
->pub_key
) < 0)
2328 if (eay_bn2v(priv
, dh
->priv_key
) < 0) {
2344 eay_dh_compute(prime
, g
, pub
, priv
, pub2
, key
)
2345 vchar_t
*prime
, *pub
, *priv
, *pub2
, **key
;
2348 BIGNUM
*dh_pub
= NULL
;
2351 unsigned char *v
= NULL
;
2354 /* make public number to compute */
2355 if (eay_v2bn(&dh_pub
, pub2
) < 0)
2358 /* make DH structure */
2359 if ((dh
= DH_new()) == NULL
)
2361 if (eay_v2bn(&dh
->p
, prime
) < 0)
2363 if (eay_v2bn(&dh
->pub_key
, pub
) < 0)
2365 if (eay_v2bn(&dh
->priv_key
, priv
) < 0)
2367 dh
->length
= pub2
->l
* 8;
2370 if ((dh
->g
= BN_new()) == NULL
)
2372 if (!BN_set_word(dh
->g
, g
))
2375 if ((v
= racoon_calloc(prime
->l
, sizeof(u_char
))) == NULL
)
2377 if ((l
= DH_compute_key(v
, dh_pub
, dh
)) == -1)
2379 memcpy((*key
)->v
+ (prime
->l
- l
), v
, l
);
2394 * convert vchar_t <-> BIGNUM.
2396 * vchar_t: unit is u_char, network endian, most significant byte first.
2397 * BIGNUM: unit is BN_ULONG, each of BN_ULONG is in host endian,
2398 * least significant BN_ULONG must come first.
2400 * hex value of "0x3ffe050104" is represented as follows:
2401 * vchar_t: 3f fe 05 01 04
2402 * BIGNUM (BN_ULONG = u_int8_t): 04 01 05 fe 3f
2403 * BIGNUM (BN_ULONG = u_int16_t): 0x0104 0xfe05 0x003f
2404 * BIGNUM (BN_ULONG = u_int32_t_t): 0xfe050104 0x0000003f
2411 if ((*bn
= BN_bin2bn((unsigned char *) var
->v
, var
->l
, NULL
)) == NULL
)
2422 *var
= vmalloc(bn
->top
* BN_BYTES
);
2426 (*var
)->l
= BN_bn2bin(bn
, (unsigned char *) (*var
)->v
);
2434 OpenSSL_add_all_algorithms();
2435 ERR_load_crypto_strings();
2436 #ifdef HAVE_OPENSSL_ENGINE_H
2437 ENGINE_load_builtin_engines();
2438 ENGINE_register_all_complete();
2443 base64_decode(char *in
, long inlen
)
2445 BIO
*bio
=NULL
, *b64
=NULL
;
2446 vchar_t
*res
= NULL
;
2450 outb
= malloc(inlen
* 2);
2453 bio
= BIO_new_mem_buf(in
, inlen
);
2454 b64
= BIO_new(BIO_f_base64());
2455 BIO_set_flags(b64
, BIO_FLAGS_BASE64_NO_NL
);
2456 bio
= BIO_push(b64
, bio
);
2458 outlen
= BIO_read(bio
, outb
, inlen
* 2);
2460 plog(LLV_ERROR
, LOCATION
, NULL
, "%s\n", eay_strerror());
2464 res
= vmalloc(outlen
);
2468 memcpy(res
->v
, outb
, outlen
);
2480 base64_encode(char *in
, long inlen
)
2482 BIO
*bio
=NULL
, *b64
=NULL
;
2485 vchar_t
*res
= NULL
;
2487 bio
= BIO_new(BIO_s_mem());
2488 b64
= BIO_new(BIO_f_base64());
2489 BIO_set_flags(b64
, BIO_FLAGS_BASE64_NO_NL
);
2490 bio
= BIO_push(b64
, bio
);
2492 BIO_write(bio
, in
, inlen
);
2495 plen
= BIO_get_mem_data(bio
, &ptr
);
2496 res
= vmalloc(plen
+1);
2500 memcpy (res
->v
, ptr
, plen
);
2501 res
->v
[plen
] = '\0';
2511 binbuf_pubkey2rsa(vchar_t
*binbuf
)
2514 RSA
*rsa_pub
= NULL
;
2516 if (binbuf
->v
[0] > binbuf
->l
- 1) {
2517 plog(LLV_ERROR
, LOCATION
, NULL
, "Plain RSA pubkey format error: decoded string doesn't make sense.\n");
2521 exp
= BN_bin2bn((unsigned char *) (binbuf
->v
+ 1), binbuf
->v
[0], NULL
);
2522 mod
= BN_bin2bn((unsigned char *) (binbuf
->v
+ binbuf
->v
[0] + 1),
2523 binbuf
->l
- binbuf
->v
[0] - 1, NULL
);
2524 rsa_pub
= RSA_new();
2526 if (!exp
|| !mod
|| !rsa_pub
) {
2527 plog(LLV_ERROR
, LOCATION
, NULL
, "Plain RSA pubkey parsing error: %s\n", eay_strerror());
2546 base64_pubkey2rsa(char *in
)
2549 RSA
*rsa_pub
= NULL
;
2552 if (strncmp(in
, "0s", 2) != 0) {
2553 plog(LLV_ERROR
, LOCATION
, NULL
, "Plain RSA pubkey format error: doesn't start with '0s'\n");
2557 binbuf
= base64_decode(in
+ 2, strlen(in
+ 2));
2559 plog(LLV_ERROR
, LOCATION
, NULL
, "Plain RSA pubkey format error: Base64 decoding failed.\n");
2563 if (binbuf
->v
[0] > binbuf
->l
- 1) {
2564 plog(LLV_ERROR
, LOCATION
, NULL
, "Plain RSA pubkey format error: decoded string doesn't make sense.\n");
2568 rsa_pub
= binbuf_pubkey2rsa(binbuf
);
2578 bignum_pubkey2rsa(BIGNUM
*in
)
2580 RSA
*rsa_pub
= NULL
;
2583 binbuf
= vmalloc(BN_num_bytes(in
));
2585 plog(LLV_ERROR
, LOCATION
, NULL
, "Plain RSA pubkey conversion: memory allocation failed..\n");
2589 BN_bn2bin(in
, (unsigned char *) binbuf
->v
);
2591 rsa_pub
= binbuf_pubkey2rsa(binbuf
);
2599 #endif /* HAVE_OPENSSL */
2607 vrand
= eay_set_random(sizeof(result
));
2608 memcpy(&result
, vrand
->v
, sizeof(result
));
2618 return SSLeay_version(SSLEAY_VERSION
);