openssl: Make some utilities take const BIGNUM pointers
[strongswan.git] / src / libstrongswan / plugins / openssl / openssl_util.h
1 /*
2 * Copyright (C) 2008 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 /**
17 * @defgroup openssl_util openssl_util
18 * @{ @ingroup openssl_p
19 */
20
21 #ifndef OPENSSL_UTIL_H_
22 #define OPENSSL_UTIL_H_
23
24 #include <library.h>
25
26 #ifdef X509_NAME
27 /* from <wincrypt.h> */
28 # undef X509_NAME
29 #endif
30
31 #include <openssl/bn.h>
32 #include <openssl/asn1.h>
33
34 /**
35 * Returns the length in bytes of a field element
36 */
37 #define EC_FIELD_ELEMENT_LEN(group) ((EC_GROUP_get_degree(group) + 7) / 8)
38
39 /**
40 * Creates a hash of a given type of a chunk of data.
41 *
42 * Note: this function allocates memory for the hash
43 *
44 * @param hash_type NID of the hash
45 * @param data the chunk of data to hash
46 * @param hash chunk that contains the hash
47 * @return TRUE on success, FALSE otherwise
48 */
49 bool openssl_hash_chunk(int hash_type, chunk_t data, chunk_t *hash);
50
51 /**
52 * Concatenates two bignums into a chunk, thereby enfocing the length of
53 * a single BIGNUM, if necessary, by pre-pending it with zeros.
54 *
55 * Note: this function allocates memory for the chunk
56 *
57 * @param len the length of a single BIGNUM
58 * @param a first BIGNUM
59 * @param b second BIGNUM
60 * @param chunk resulting chunk
61 * @return TRUE on success, FALSE otherwise
62 */
63 bool openssl_bn_cat(const int len, const BIGNUM *a, const BIGNUM *b,
64 chunk_t *chunk);
65
66 /**
67 * Splits a chunk into two bignums of equal binary length.
68 *
69 * @param chunk a chunk that contains the two BIGNUMs
70 * @param a first BIGNUM
71 * @param b second BIGNUM
72 * @return TRUE on success, FALSE otherwise
73 */
74 bool openssl_bn_split(chunk_t chunk, BIGNUM *a, BIGNUM *b);
75
76 /**
77 * Exports the given bignum (assumed to be a positive number) to a chunk in
78 * two's complement format (i.e. a zero byte is added if the MSB is set).
79 *
80 * @param bn the BIGNUM to export
81 * @param chunk the chunk (data gets allocated)
82 * @return TRUE on success, FALSE otherwise
83 */
84 bool openssl_bn2chunk(const BIGNUM *bn, chunk_t *chunk);
85
86 /**
87 * Allocate a chunk using the i2d function of a given object
88 *
89 * @param type type of the object
90 * @param obj object to convert to DER
91 * @returns allocated chunk of the object, or chunk_empty
92 */
93 #define openssl_i2chunk(type, obj) ({ \
94 unsigned char *ptr = NULL; \
95 int len = i2d_##type(obj, &ptr); \
96 len < 0 ? chunk_empty : chunk_create(ptr, len);})
97
98 /**
99 * Convert an OpenSSL ASN1_OBJECT to a chunk.
100 *
101 * @param asn1 asn1 object to convert
102 * @return chunk, pointing into asn1 object
103 */
104 chunk_t openssl_asn1_obj2chunk(ASN1_OBJECT *asn1);
105
106 /**
107 * Convert an OpenSSL ASN1_STRING to a chunk.
108 *
109 * @param asn1 asn1 string to convert
110 * @return chunk, pointing into asn1 string
111 */
112 chunk_t openssl_asn1_str2chunk(ASN1_STRING *asn1);
113
114 /**
115 * Convert an openssl X509_NAME to a identification_t of type ID_DER_ASN1_DN.
116 *
117 * @param name name to convert
118 * @return identification_t, NULL on error
119 */
120 identification_t *openssl_x509_name2id(X509_NAME *name);
121
122 /**
123 * Check if an ASN1 oid is a an OID known by libstrongswan.
124 *
125 * @param obj openssl ASN1 object
126 * @returns OID, as defined in <asn1/oid.h>
127 */
128 int openssl_asn1_known_oid(ASN1_OBJECT *obj);
129
130 /**
131 * Convert an OpenSSL ASN1_TIME to a time_t.
132 *
133 * @param time openssl ASN1_TIME
134 * @returns time_t, 0 on error
135 */
136 time_t openssl_asn1_to_time(ASN1_TIME *time);
137
138 /**
139 * Macros to define fallback getters/setters to access keys (BIGNUM*) for types
140 * that were made opaque with OpenSSL 1.1.0.
141 */
142 #define OPENSSL_KEY_FALLBACK(...) VA_ARGS_DISPATCH(OPENSSL_KEY_FALLBACK, __VA_ARGS__)(__VA_ARGS__)
143 #define OPENSSL_KEY_FALLBACK3(type, k1, k2) \
144 __attribute__((unused)) \
145 static inline void type##_get0(const type *o, const BIGNUM **k1, const BIGNUM **k2) { \
146 if (k1) *k1 = o->k1; \
147 if (k2) *k2 = o->k2; } \
148 __attribute__((unused)) \
149 static inline int type##_set0(type *o, BIGNUM *k1, BIGNUM *k2) { \
150 if (k1) { BN_clear_free(o->k1); o->k1 = k1; } \
151 if (k2) { BN_clear_free(o->k2); o->k2 = k2; } \
152 return 1; }
153 #define OPENSSL_KEY_FALLBACK4(type, name, k1, k2) \
154 __attribute__((unused)) \
155 static inline void type##_get0_##name(const type *o, const BIGNUM **k1, const BIGNUM **k2) { \
156 if (k1) *k1 = o->k1; \
157 if (k2) *k2 = o->k2; } \
158 __attribute__((unused)) \
159 static inline int type##_set0_##name(type *o, BIGNUM *k1, BIGNUM *k2) { \
160 if (k1) { BN_clear_free(o->k1); o->k1 = k1; } \
161 if (k2) { BN_clear_free(o->k2); o->k2 = k2; } \
162 return 1; }
163 #define OPENSSL_KEY_FALLBACK5(type, name, k1, k2, k3) \
164 __attribute__((unused)) \
165 static inline void type##_get0_##name(const type *o, const BIGNUM **k1, const BIGNUM **k2, const BIGNUM **k3) { \
166 if (k1) *k1 = o->k1; \
167 if (k2) *k2 = o->k2; \
168 if (k3) *k3 = o->k3; } \
169 __attribute__((unused)) \
170 static inline int type##_set0_##name(type *o, BIGNUM *k1, BIGNUM *k2, BIGNUM *k3) { \
171 if (k1) { BN_clear_free(o->k1); o->k1 = k1; } \
172 if (k2) { BN_clear_free(o->k2); o->k2 = k2; } \
173 if (k3) { BN_clear_free(o->k3); o->k3 = k3; } \
174 return 1; }
175
176 #endif /** OPENSSL_UTIL_H_ @}*/