2f981370174b8523a50b73630b9c96e3abea003b
2 * Copyright (C) 2009 Martin Willi
3 * Copyright (C) 2008 Tobias Brunner
4 * Hochschule fuer Technik Rapperswil
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 #include "openssl_util.h"
19 #include <utils/debug.h>
21 #include <openssl/bn.h>
22 #include <openssl/evp.h>
23 #include <openssl/x509.h>
26 * Described in header.
28 bool openssl_hash_chunk(int hash_type
, chunk_t data
, chunk_t
*hash
)
32 const EVP_MD
*hasher
= EVP_get_digestbynid(hash_type
);
38 ctx
= EVP_MD_CTX_create();
44 if (!EVP_DigestInit_ex(ctx
, hasher
, NULL
))
49 if (!EVP_DigestUpdate(ctx
, data
.ptr
, data
.len
))
54 *hash
= chunk_alloc(hasher
->md_size
);
55 if (!EVP_DigestFinal_ex(ctx
, hash
->ptr
, NULL
))
65 EVP_MD_CTX_destroy(ctx
);
71 * Described in header.
73 bool openssl_bn_cat(int len
, BIGNUM
*a
, BIGNUM
*b
, chunk_t
*chunk
)
77 chunk
->len
= len
+ (b ? len
: 0);
78 chunk
->ptr
= malloc(chunk
->len
);
79 memset(chunk
->ptr
, 0, chunk
->len
);
82 offset
= len
- BN_num_bytes(a
);
83 if (!BN_bn2bin(a
, chunk
->ptr
+ offset
))
88 /* optionally convert and concatenate b */
91 offset
= len
- BN_num_bytes(b
);
92 if (!BN_bn2bin(b
, chunk
->ptr
+ len
+ offset
))
105 * Described in header.
107 bool openssl_bn_split(chunk_t chunk
, BIGNUM
*a
, BIGNUM
*b
)
111 if ((chunk
.len
% 2) != 0)
118 if (!BN_bin2bn(chunk
.ptr
, len
, a
) ||
119 !BN_bin2bn(chunk
.ptr
+ len
, len
, b
))
128 * Described in header.
130 bool openssl_bn2chunk(BIGNUM
*bn
, chunk_t
*chunk
)
132 *chunk
= chunk_alloc(BN_num_bytes(bn
));
133 if (BN_bn2bin(bn
, chunk
->ptr
) == chunk
->len
)
135 if (chunk
->len
&& chunk
->ptr
[0] & 0x80)
136 { /* if MSB is set, prepend a zero to make it non-negative */
137 *chunk
= chunk_cat("cm", chunk_from_chars(0x00), *chunk
);
146 * Described in header.
148 chunk_t
openssl_asn1_obj2chunk(ASN1_OBJECT
*asn1
)
152 return chunk_create((u_char
*)asn1
->data
, asn1
->length
);
158 * Described in header.
160 chunk_t
openssl_asn1_str2chunk(ASN1_STRING
*asn1
)
164 return chunk_create(ASN1_STRING_data(asn1
), ASN1_STRING_length(asn1
));
170 * Convert a X509 name to a ID_DER_ASN1_DN identification_t
172 identification_t
*openssl_x509_name2id(X509_NAME
*name
)
176 identification_t
*id
;
179 chunk
= openssl_i2chunk(X509_NAME
, name
);
182 id
= identification_create_from_encoding(ID_DER_ASN1_DN
, chunk
);
191 * We can't include <asn1/asn1.h>, as the ASN1_ definitions would clash
192 * with OpenSSL. Redeclare what we need.
194 int asn1_known_oid(chunk_t
);
195 time_t asn1_to_time(chunk_t
*,int);
198 * Described in header.
200 int openssl_asn1_known_oid(ASN1_OBJECT
*obj
)
202 return asn1_known_oid(openssl_asn1_obj2chunk(obj
));
206 * Described in header.
208 time_t openssl_asn1_to_time(ASN1_TIME
*time
)
214 chunk
= openssl_asn1_str2chunk(time
);
218 case V_ASN1_GENERALIZEDTIME
:
219 return asn1_to_time(&chunk
, time
->type
);
224 DBG1(DBG_LIB
, "invalid ASN1 time");