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/err.h>
66 #else /* HAVE_OPENSSL */
67 #include <Security/SecDH.h>
68 #include <Security/SecRandom.h>
69 #endif /* HAVE_OPENSSL */
71 #include <CommonCrypto/CommonDigest.h>
72 #include <CommonCrypto/CommonHMAC.h>
73 #include <CommonCrypto/CommonCryptor.h>
77 #if OPENSSL_VERSION_NUMBER < 0x0090700fL
78 typedef STACK_OF(GENERAL_NAME
) GENERAL_NAMES
;
80 #define USE_NEW_DES_API
83 #define OpenSSL_BUG() do { plog(ASL_LEVEL_ERR, "OpenSSL function failed\n"); } while(0)
86 #include "crypto_openssl.h"
96 * I hate to cast every parameter to des_xx into void *, but it is
97 * necessary for SSLeay/OpenSSL portability. It sucks.
101 static X509
*mem2x509(vchar_t
*);
103 static caddr_t
eay_hmac_init (vchar_t
*, CCHmacAlgorithm
);
109 * The following are derived from code in crypto/x509/x509_cmp.c
111 * X509_NAME_wildcmp() adds wildcard matching to the original
112 * X509_NAME_cmp(), nocase_cmp() and nocase_spacenorm_cmp() are as is.
115 /* Case insensitive string comparision */
116 static int nocase_cmp(const ASN1_STRING
*a
, const ASN1_STRING
*b
)
120 if (a
->length
!= b
->length
)
121 return (a
->length
- b
->length
);
123 for (i
=0; i
<a
->length
; i
++)
127 ca
= tolower(a
->data
[i
]);
128 cb
= tolower(b
->data
[i
]);
136 /* Case insensitive string comparision with space normalization
137 * Space normalization - ignore leading, trailing spaces,
138 * multiple spaces between characters are replaced by single space
140 static int nocase_spacenorm_cmp(const ASN1_STRING
*a
, const ASN1_STRING
*b
)
142 unsigned char *pa
= NULL
, *pb
= NULL
;
150 /* skip leading spaces */
151 while (la
> 0 && isspace(*pa
))
156 while (lb
> 0 && isspace(*pb
))
162 /* skip trailing spaces */
163 while (la
> 0 && isspace(pa
[la
-1]))
165 while (lb
> 0 && isspace(pb
[lb
-1]))
168 /* compare strings with space normalization */
169 while (la
> 0 && lb
> 0)
173 /* compare character */
182 if (la
<= 0 || lb
<= 0)
185 /* is white space next character ? */
186 if (isspace(*pa
) && isspace(*pb
))
188 /* skip remaining white spaces */
189 while (la
> 0 && isspace(*pa
))
194 while (lb
> 0 && isspace(*pb
))
201 if (la
> 0 || lb
> 0)
207 static int X509_NAME_wildcmp(const X509_NAME
*a
, const X509_NAME
*b
)
210 X509_NAME_ENTRY
*na
,*nb
;
212 if (sk_X509_NAME_ENTRY_num(a
->entries
)
213 != sk_X509_NAME_ENTRY_num(b
->entries
))
214 return sk_X509_NAME_ENTRY_num(a
->entries
)
215 -sk_X509_NAME_ENTRY_num(b
->entries
);
216 for (i
=sk_X509_NAME_ENTRY_num(a
->entries
)-1; i
>=0; i
--)
218 na
=sk_X509_NAME_ENTRY_value(a
->entries
,i
);
219 nb
=sk_X509_NAME_ENTRY_value(b
->entries
,i
);
220 j
=OBJ_cmp(na
->object
,nb
->object
);
222 if ((na
->value
->length
== 1 && na
->value
->data
[0] == '*')
223 || (nb
->value
->length
== 1 && nb
->value
->data
[0] == '*'))
225 j
=na
->value
->type
-nb
->value
->type
;
227 if (na
->value
->type
== V_ASN1_PRINTABLESTRING
)
228 j
=nocase_spacenorm_cmp(na
->value
, nb
->value
);
229 else if (na
->value
->type
== V_ASN1_IA5STRING
230 && OBJ_obj2nid(na
->object
) == NID_pkcs9_emailAddress
)
231 j
=nocase_cmp(na
->value
, nb
->value
);
234 j
=na
->value
->length
-nb
->value
->length
;
236 j
=memcmp(na
->value
->data
,nb
->value
->data
,
248 * compare two subjectNames.
254 eay_cmp_asn1dn(n1
, n2
)
257 X509_NAME
*a
= NULL
, *b
= NULL
;
262 if (!d2i_X509_NAME(&a
, (void *)&p
, n1
->l
))
265 if (!d2i_X509_NAME(&b
, (void *)&p
, n2
->l
))
268 i
= X509_NAME_wildcmp(a
, b
);
279 * Get the common name from a cert
281 #define EAY_MAX_CN_LEN 256
283 eay_get_x509_common_name(cert
)
288 vchar_t
*commonName
= NULL
;
290 commonName
= vmalloc(EAY_MAX_CN_LEN
);
291 if (commonName
== NULL
) {
292 plog(ASL_LEVEL_ERR
, "no memory\n");
296 x509
= mem2x509(cert
);
302 name
= X509_get_subject_name(x509
);
303 X509_NAME_get_text_by_NID(name
, NID_commonName
, commonName
->v
, EAY_MAX_CN_LEN
);
305 commonName
->l
= strlen(commonName
->v
);
313 * get the subjectAltName from X509 certificate.
314 * the name must be terminated by '\0'.
317 eay_get_x509subjectaltname(cert
, altname
, type
, pos
, len
)
326 GENERAL_NAMES
*gens
= NULL
;
331 *type
= GENT_OTHERNAME
;
333 x509
= mem2x509(cert
);
337 gens
= X509_get_ext_d2i(x509
, NID_subject_alt_name
, NULL
, NULL
);
341 for (i
= 0; i
< sk_GENERAL_NAME_num(gens
); i
++) {
347 /* there is no data at "pos" */
348 if (i
== sk_GENERAL_NAME_num(gens
))
351 gen
= sk_GENERAL_NAME_value(gens
, i
);
353 /* make sure the data is terminated by '\0'. */
354 if (gen
->d
.ia5
->data
[gen
->d
.ia5
->length
] != '\0') {
356 "data is not terminated by 0.");
357 hexdump(gen
->d
.ia5
->data
, gen
->d
.ia5
->length
+ 1);
361 /* read DNSName / Email */
362 if (gen
->type
== GEN_DNS
||
363 gen
->type
== GEN_EMAIL
||
364 gen
->type
== GEN_URI
) {
366 *len
= gen
->d
.ia5
->length
+ 1;
367 *altname
= racoon_malloc(*len
);
371 strlcpy(*altname
, (const char *)gen
->d
.ia5
->data
, *len
);
375 } else if (gen
->type
== GEN_IPADD
) {
377 *len
= gen
->d
.ia5
->length
+ 1;
378 *altname
= racoon_malloc(*len
);
382 memcpy(*altname
, (const char *)gen
->d
.ia5
->data
, *len
);
391 racoon_free(*altname
);
395 plog(ASL_LEVEL_ERR
, "%s\n", eay_strerror());
397 printf("%s\n", eay_strerror());
401 /* free the whole stack. */
402 sk_GENERAL_NAME_pop_free(gens
, GENERAL_NAME_free
);
410 /* get X509 structure from buffer. */
421 bp
= (unsigned char *) cert
->v
;
423 x509
= d2i_X509(NULL
, (void *)&bp
, cert
->l
);
430 bio
= BIO_new(BIO_s_mem());
433 len
= BIO_write(bio
, cert
->v
, cert
->l
);
436 x509
= PEM_read_bio_X509(bio
, NULL
, NULL
, NULL
);
445 * MUST load ERR_load_crypto_strings() first.
450 static char ebuf
[512];
454 const char *file
, *data
;
458 es
= CRYPTO_thread_id();
460 while ((l
= ERR_get_error_line_data(&file
, &line
, &data
, &flags
)) != 0){
461 n
= snprintf(ebuf
+ len
, sizeof(ebuf
) - len
,
463 es
, ERR_error_string(l
, buf
), file
, line
,
464 (flags
& ERR_TXT_STRING
) ? data
: "");
465 if (n
< 0 || n
>= sizeof(ebuf
) - len
)
468 if (sizeof(ebuf
) < len
)
475 #endif /* HAVE_OPENSSL */
478 eay_CCCrypt(CCOperation oper
,
487 CCCryptorStatus status
;
489 /* allocate buffer for result */
490 if ((res
= vmalloc(data
->l
)) == NULL
)
493 status
= CCCrypt(oper
,
499 res
->v
, res
->l
, &res_len
);
500 if (status
== kCCSuccess
) {
501 if (res
->l
!= res_len
) {
503 "crypt %d %d length mismatch. expected: %zd. got: %zd.\n",
504 oper
, algo
, res
->l
, res_len
);
509 "crypt %d %d error. status %d.\n",
510 oper
, algo
, (int)status
);
520 eay_des_encrypt(data
, key
, iv
)
521 vchar_t
*data
, *key
, *iv
;
523 return(eay_CCCrypt(kCCEncrypt
, kCCAlgorithmDES
, 0 /* CBC */, data
, key
, iv
));
527 eay_des_decrypt(data
, key
, iv
)
528 vchar_t
*data
, *key
, *iv
;
530 return(eay_CCCrypt(kCCDecrypt
, kCCAlgorithmDES
, 0 /* CBC */, data
, key
, iv
));
538 #ifdef USE_NEW_DES_API
539 return DES_is_weak_key((void *)key
->v
);
541 return des_is_weak_key((void *)key
->v
);
552 /* CommonCrypto return lengths in bytes, ipsec-tools
553 * uses lengths in bits, therefore conversion is required.
555 if (len
!= 0 && len
!= (kCCKeySizeDES
<< 3))
558 return kCCKeySizeDES
<< 3;
565 eay_3des_encrypt(data
, key
, iv
)
566 vchar_t
*data
, *key
, *iv
;
568 return(eay_CCCrypt(kCCEncrypt
, kCCAlgorithm3DES
, 0 /* CBC */, data
, key
, iv
));
572 eay_3des_decrypt(data
, key
, iv
)
573 vchar_t
*data
, *key
, *iv
;
575 return(eay_CCCrypt(kCCDecrypt
, kCCAlgorithm3DES
, 0 /* CBC */, data
, key
, iv
));
579 eay_3des_weakkey(key
)
589 /* CommonCrypto return lengths in bytes, ipsec-tools
590 * uses lengths in bits, therefore conversion is required.
592 if (len
!= 0 && len
!= (kCCKeySize3DES
<< 3))
595 return kCCKeySize3DES
<< 3;
602 eay_aes_encrypt(data
, key
, iv
)
603 vchar_t
*data
, *key
, *iv
;
605 return(eay_CCCrypt(kCCEncrypt
, kCCAlgorithmAES128
/* adapts to AES-192, or AES-256 depending on the key size*/, 0 /* CBC */, data
, key
, iv
));
609 eay_aes_decrypt(data
, key
, iv
)
610 vchar_t
*data
, *key
, *iv
;
612 return(eay_CCCrypt(kCCDecrypt
, kCCAlgorithmAES128
/* adapts to AES-192, or AES-256 depending on the key size*/, 0 /* CBC */, data
, key
, iv
));
619 /* CommonCrypto return lengths in bytes, ipsec-tools
620 * uses lengths in bits, therefore conversion is required.
623 if (len
!= (kCCKeySizeAES128
<< 3) &&
624 len
!= (kCCKeySizeAES192
<< 3) &&
625 len
!= (kCCKeySizeAES256
<< 3))
628 return kCCKeySizeAES128
<< 3;
659 eay_hmac_init(key
, algorithm
)
661 CCHmacAlgorithm algorithm
;
663 CCHmacContext
*c
= racoon_malloc(sizeof(*c
));
665 CCHmacInit(c
, algorithm
, key
->v
, key
->l
);
675 eay_hmacsha2_512_one(key
, data
)
681 ctx
= eay_hmacsha2_512_init(key
);
682 eay_hmacsha2_512_update(ctx
, data
);
683 res
= eay_hmacsha2_512_final(ctx
);
689 eay_hmacsha2_512_init(key
)
692 return eay_hmac_init(key
, kCCHmacAlgSHA512
);
696 eay_hmacsha2_512_update(c
, data
)
700 CCHmacUpdate(ALIGNED_CAST(CCHmacContext
*)c
, data
->v
, data
->l
);
704 eay_hmacsha2_512_final(c
)
709 if ((res
= vmalloc(CC_SHA512_DIGEST_LENGTH
)) == 0)
712 CCHmacFinal(ALIGNED_CAST(CCHmacContext
*)c
, res
->v
);
713 res
->l
= CC_SHA512_DIGEST_LENGTH
;
715 (void)racoon_free(c
);
723 eay_hmacsha2_384_one(key
, data
)
729 ctx
= eay_hmacsha2_384_init(key
);
730 eay_hmacsha2_384_update(ctx
, data
);
731 res
= eay_hmacsha2_384_final(ctx
);
737 eay_hmacsha2_384_init(key
)
740 return eay_hmac_init(key
, kCCHmacAlgSHA384
);
744 eay_hmacsha2_384_update(c
, data
)
748 CCHmacUpdate(ALIGNED_CAST(CCHmacContext
*)c
, data
->v
, data
->l
);
752 eay_hmacsha2_384_final(c
)
757 if ((res
= vmalloc(CC_SHA384_DIGEST_LENGTH
)) == 0)
760 CCHmacFinal(ALIGNED_CAST(CCHmacContext
*)c
, res
->v
);
761 res
->l
= CC_SHA384_DIGEST_LENGTH
;
763 (void)racoon_free(c
);
771 eay_hmacsha2_256_one(key
, data
)
777 ctx
= eay_hmacsha2_256_init(key
);
778 eay_hmacsha2_256_update(ctx
, data
);
779 res
= eay_hmacsha2_256_final(ctx
);
785 eay_hmacsha2_256_init(key
)
788 return eay_hmac_init(key
, kCCHmacAlgSHA256
);
792 eay_hmacsha2_256_update(c
, data
)
796 CCHmacUpdate(ALIGNED_CAST(CCHmacContext
*)c
, data
->v
, data
->l
);
800 eay_hmacsha2_256_final(c
)
805 if ((res
= vmalloc(CC_SHA256_DIGEST_LENGTH
)) == 0)
808 CCHmacFinal(ALIGNED_CAST(CCHmacContext
*)c
, res
->v
);
809 res
->l
= CC_SHA256_DIGEST_LENGTH
;
811 (void)racoon_free(c
);
814 #endif /* WITH_SHA2 */
820 eay_hmacsha1_one(key
, data
)
826 ctx
= eay_hmacsha1_init(key
);
827 eay_hmacsha1_update(ctx
, data
);
828 res
= eay_hmacsha1_final(ctx
);
834 eay_hmacsha1_init(key
)
837 return eay_hmac_init(key
, kCCHmacAlgSHA1
);
841 eay_hmacsha1_update(c
, data
)
845 CCHmacUpdate(ALIGNED_CAST(CCHmacContext
*)c
, data
->v
, data
->l
);
849 eay_hmacsha1_final(c
)
854 if ((res
= vmalloc(CC_SHA1_DIGEST_LENGTH
)) == 0)
857 CCHmacFinal(ALIGNED_CAST(CCHmacContext
*)c
, res
->v
);
858 res
->l
= CC_SHA1_DIGEST_LENGTH
;
860 (void)racoon_free(c
);
868 eay_hmacmd5_one(key
, data
)
874 ctx
= eay_hmacmd5_init(key
);
875 eay_hmacmd5_update(ctx
, data
);
876 res
= eay_hmacmd5_final(ctx
);
882 eay_hmacmd5_init(key
)
885 return eay_hmac_init(key
, kCCHmacAlgMD5
);
889 eay_hmacmd5_update(c
, data
)
893 CCHmacUpdate(ALIGNED_CAST(CCHmacContext
*)c
, data
->v
, data
->l
);
902 if ((res
= vmalloc(CC_MD5_DIGEST_LENGTH
)) == 0)
905 CCHmacFinal(ALIGNED_CAST(CCHmacContext
*)c
, res
->v
);
906 res
->l
= CC_MD5_DIGEST_LENGTH
;
907 (void)racoon_free(c
);
921 SHA512_CTX
*c
= racoon_malloc(sizeof(*c
));
929 eay_sha2_512_update(c
, data
)
933 SHA512_Update(ALIGNED_CAST(SHA512_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
939 eay_sha2_512_final(c
)
944 if ((res
= vmalloc(SHA512_DIGEST_LENGTH
)) == 0)
947 SHA512_Final((unsigned char *) res
->v
, ALIGNED_CAST(SHA512_CTX
*)c
);
948 (void)racoon_free(c
);
954 eay_sha2_512_one(data
)
960 ctx
= eay_sha2_512_init();
961 eay_sha2_512_update(ctx
, data
);
962 res
= eay_sha2_512_final(ctx
);
968 eay_sha2_512_hashlen()
970 return SHA512_DIGEST_LENGTH
<< 3;
979 typedef SHA512_CTX SHA384_CTX
;
984 SHA384_CTX
*c
= racoon_malloc(sizeof(*c
));
992 eay_sha2_384_update(c
, data
)
996 SHA384_Update(ALIGNED_CAST(SHA384_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
1002 eay_sha2_384_final(c
)
1007 if ((res
= vmalloc(SHA384_DIGEST_LENGTH
)) == 0)
1010 SHA384_Final((unsigned char *) res
->v
, ALIGNED_CAST(SHA384_CTX
*)c
);
1011 (void)racoon_free(c
);
1017 eay_sha2_384_one(data
)
1023 ctx
= eay_sha2_384_init();
1024 eay_sha2_384_update(ctx
, data
);
1025 res
= eay_sha2_384_final(ctx
);
1031 eay_sha2_384_hashlen()
1033 return SHA384_DIGEST_LENGTH
<< 3;
1039 * SHA2-256 functions
1044 SHA256_CTX
*c
= racoon_malloc(sizeof(*c
));
1052 eay_sha2_256_update(c
, data
)
1056 SHA256_Update(ALIGNED_CAST(SHA256_CTX
*)c
, (unsigned char *) data
->v
, data
->l
);
1062 eay_sha2_256_final(c
)
1067 if ((res
= vmalloc(SHA256_DIGEST_LENGTH
)) == 0)
1070 SHA256_Final((unsigned char *) res
->v
, ALIGNED_CAST(SHA256_CTX
*)c
);
1071 (void)racoon_free(c
);
1077 eay_sha2_256_one(data
)
1083 ctx
= eay_sha2_256_init();
1084 eay_sha2_256_update(ctx
, data
);
1085 res
= eay_sha2_256_final(ctx
);
1091 eay_sha2_256_hashlen()
1093 return SHA256_DIGEST_LENGTH
<< 3;
1103 SHA_CTX
*c
= racoon_malloc(sizeof(*c
));
1111 eay_sha1_update(c
, data
)
1115 SHA1_Update(ALIGNED_CAST(SHA_CTX
*)c
, data
->v
, data
->l
);
1126 if ((res
= vmalloc(SHA_DIGEST_LENGTH
)) == 0)
1129 SHA1_Final((unsigned char *) res
->v
, ALIGNED_CAST(SHA_CTX
*)c
);
1130 (void)racoon_free(c
);
1142 ctx
= eay_sha1_init();
1143 eay_sha1_update(ctx
, data
);
1144 res
= eay_sha1_final(ctx
);
1152 return SHA_DIGEST_LENGTH
<< 3;
1161 MD5_CTX
*c
= racoon_malloc(sizeof(*c
));
1169 eay_md5_update(c
, data
)
1173 MD5_Update(ALIGNED_CAST(MD5_CTX
*)c
, data
->v
, data
->l
);
1184 if ((res
= vmalloc(MD5_DIGEST_LENGTH
)) == 0)
1187 MD5_Final((unsigned char *) res
->v
, ALIGNED_CAST(MD5_CTX
*)c
);
1188 (void)racoon_free(c
);
1200 ctx
= eay_md5_init();
1201 eay_md5_update(ctx
, data
);
1202 res
= eay_md5_final(ctx
);
1210 return MD5_DIGEST_LENGTH
<< 3;
1217 * size: number of bytes.
1220 eay_set_random(size
)
1226 if ((r
= BN_new()) == NULL
)
1228 BN_rand(r
, size
* 8, 0, 0);
1238 eay_set_random(u_int32_t size
)
1240 vchar_t
*res
= vmalloc(size
);
1245 if (SecRandomCopyBytes(kSecRandomDefault
, size
, (uint8_t*)res
->v
)) {
1257 eay_dh_generate(prime
, g
, publen
, pub
, priv
)
1258 vchar_t
*prime
, **pub
, **priv
;
1267 /* pre-process to generate number */
1268 if (eay_v2bn(&p
, prime
) < 0)
1271 if ((dh
= DH_new()) == NULL
)
1274 p
= NULL
; /* p is now part of dh structure */
1276 if ((dh
->g
= BN_new()) == NULL
)
1278 if (!BN_set_word(dh
->g
, g
))
1282 dh
->length
= publen
;
1284 /* generate public and private number */
1285 if (!DH_generate_key(dh
))
1288 /* copy results to buffers */
1289 if (eay_bn2v(pub
, dh
->pub_key
) < 0)
1291 if (eay_bn2v(priv
, dh
->priv_key
) < 0) {
1307 eay_dh_compute(prime
, g
, pub
, priv
, pub2
, key
)
1308 vchar_t
*prime
, *pub
, *priv
, *pub2
, **key
;
1311 BIGNUM
*dh_pub
= NULL
;
1314 unsigned char *v
= NULL
;
1317 /* make public number to compute */
1318 if (eay_v2bn(&dh_pub
, pub2
) < 0)
1321 /* make DH structure */
1322 if ((dh
= DH_new()) == NULL
)
1324 if (eay_v2bn(&dh
->p
, prime
) < 0)
1326 if (eay_v2bn(&dh
->pub_key
, pub
) < 0)
1328 if (eay_v2bn(&dh
->priv_key
, priv
) < 0)
1330 dh
->length
= pub2
->l
* 8;
1333 if ((dh
->g
= BN_new()) == NULL
)
1335 if (!BN_set_word(dh
->g
, g
))
1338 if ((v
= racoon_calloc(prime
->l
, sizeof(u_char
))) == NULL
)
1340 if ((l
= DH_compute_key(v
, dh_pub
, dh
)) == -1)
1342 memcpy((*key
)->v
+ (prime
->l
- l
), v
, l
);
1357 * convert vchar_t <-> BIGNUM.
1359 * vchar_t: unit is u_char, network endian, most significant byte first.
1360 * BIGNUM: unit is BN_ULONG, each of BN_ULONG is in host endian,
1361 * least significant BN_ULONG must come first.
1363 * hex value of "0x3ffe050104" is represented as follows:
1364 * vchar_t: 3f fe 05 01 04
1365 * BIGNUM (BN_ULONG = u_int8_t): 04 01 05 fe 3f
1366 * BIGNUM (BN_ULONG = u_int16_t): 0x0104 0xfe05 0x003f
1367 * BIGNUM (BN_ULONG = u_int32_t_t): 0xfe050104 0x0000003f
1374 if ((*bn
= BN_bin2bn((unsigned char *) var
->v
, var
->l
, NULL
)) == NULL
)
1385 *var
= vmalloc(bn
->top
* BN_BYTES
);
1389 (*var
)->l
= BN_bn2bin(bn
, (unsigned char *) (*var
)->v
);
1397 OpenSSL_add_all_algorithms();
1398 ERR_load_crypto_strings();
1399 #ifdef HAVE_OPENSSL_ENGINE_H
1400 ENGINE_load_builtin_engines();
1401 ENGINE_register_all_complete();
1404 #endif /* HAVE_OPENSSL */
1412 vrand
= eay_set_random(sizeof(result
));
1413 memcpy(&result
, vrand
->v
, sizeof(result
));
1423 return SSLeay_version(SSLEAY_VERSION
);