/src/opensc/openpace/src/ssl_compat.c
Line | Count | Source |
1 | | #ifdef HAVE_CONFIG_H |
2 | | #include "config.h" |
3 | | #endif |
4 | | |
5 | | #include "eac_dh.h" |
6 | | #include <openssl/crypto.h> |
7 | | #include <openssl/dh.h> |
8 | | #include <openssl/ec.h> |
9 | | #include <openssl/ecdsa.h> |
10 | | #include <openssl/evp.h> |
11 | | #include <openssl/rsa.h> |
12 | | #include <string.h> |
13 | | |
14 | | #ifndef HAVE_DH_SET0_KEY |
15 | | int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key) |
16 | | { |
17 | | /* If the field pub_key in dh is NULL, the corresponding input |
18 | | * parameters MUST be non-NULL. The priv_key field may |
19 | | * be left NULL. |
20 | | */ |
21 | | if (dh->pub_key == NULL && pub_key == NULL) |
22 | | return 0; |
23 | | |
24 | | if (pub_key != NULL) { |
25 | | BN_free(dh->pub_key); |
26 | | dh->pub_key = pub_key; |
27 | | } |
28 | | if (priv_key != NULL) { |
29 | | BN_free(dh->priv_key); |
30 | | dh->priv_key = priv_key; |
31 | | } |
32 | | |
33 | | return 1; |
34 | | } |
35 | | #endif |
36 | | |
37 | | #ifndef HAVE_DH_GET0_KEY |
38 | | void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key) |
39 | | { |
40 | | if (pub_key != NULL) |
41 | | *pub_key = dh->pub_key; |
42 | | if (priv_key != NULL) |
43 | | *priv_key = dh->priv_key; |
44 | | } |
45 | | #endif |
46 | | |
47 | | #ifndef HAVE_DH_GET0_PQG |
48 | | void DH_get0_pqg(const DH *dh, |
49 | | const BIGNUM **p, const BIGNUM **q, const BIGNUM **g) |
50 | | { |
51 | | if (p != NULL) |
52 | | *p = dh->p; |
53 | | if (q != NULL) |
54 | | *q = dh->q; |
55 | | if (g != NULL) |
56 | | *g = dh->g; |
57 | | } |
58 | | #endif |
59 | | |
60 | | #ifndef HAVE_DH_SET0_PQG |
61 | | int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g) |
62 | | { |
63 | | /* If the fields p and g in d are NULL, the corresponding input |
64 | | * parameters MUST be non-NULL. q may remain NULL. |
65 | | */ |
66 | | if ((dh->p == NULL && p == NULL) |
67 | | || (dh->g == NULL && g == NULL)) |
68 | | return 0; |
69 | | |
70 | | if (p != NULL) { |
71 | | BN_free(dh->p); |
72 | | dh->p = p; |
73 | | } |
74 | | if (q != NULL) { |
75 | | BN_free(dh->q); |
76 | | dh->q = q; |
77 | | } |
78 | | if (g != NULL) { |
79 | | BN_free(dh->g); |
80 | | dh->g = g; |
81 | | } |
82 | | |
83 | | if (q != NULL) { |
84 | | dh->length = BN_num_bits(q); |
85 | | } |
86 | | |
87 | | return 1; |
88 | | } |
89 | | #endif |
90 | | |
91 | | #ifndef HAVE_RSA_SET0_KEY |
92 | | int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d) |
93 | | { |
94 | | /* If the fields n and e in r are NULL, the corresponding input |
95 | | * parameters MUST be non-NULL for n and e. d may be |
96 | | * left NULL (in case only the public key is used). |
97 | | */ |
98 | | if ((r->n == NULL && n == NULL) |
99 | | || (r->e == NULL && e == NULL)) |
100 | | return 0; |
101 | | |
102 | | if (n != NULL) { |
103 | | BN_free(r->n); |
104 | | r->n = n; |
105 | | } |
106 | | if (e != NULL) { |
107 | | BN_free(r->e); |
108 | | r->e = e; |
109 | | } |
110 | | if (d != NULL) { |
111 | | BN_free(r->d); |
112 | | r->d = d; |
113 | | } |
114 | | |
115 | | return 1; |
116 | | } |
117 | | #endif |
118 | | |
119 | | #ifndef HAVE_RSA_GET0_KEY |
120 | | void RSA_get0_key(const RSA *r, |
121 | | const BIGNUM **n, const BIGNUM **e, const BIGNUM **d) |
122 | | { |
123 | | if (n != NULL) |
124 | | *n = r->n; |
125 | | if (e != NULL) |
126 | | *e = r->e; |
127 | | if (d != NULL) |
128 | | *d = r->d; |
129 | | } |
130 | | #endif |
131 | | |
132 | | #ifndef HAVE_ECDSA_SIG_SET0 |
133 | | int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s) |
134 | | { |
135 | | if (r == NULL || s == NULL) |
136 | | return 0; |
137 | | BN_clear_free(sig->r); |
138 | | BN_clear_free(sig->s); |
139 | | sig->r = r; |
140 | | sig->s = s; |
141 | | return 1; |
142 | | } |
143 | | #endif |
144 | | |
145 | | #ifndef HAVE_ECDSA_SIG_GET0 |
146 | | void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps) |
147 | | { |
148 | | if (pr != NULL) |
149 | | *pr = sig->r; |
150 | | if (ps != NULL) |
151 | | *ps = sig->s; |
152 | | } |
153 | | #endif |
154 | | |
155 | | #ifndef HAVE_ASN1_STRING_GET0_DATA |
156 | | const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *x) |
157 | | { |
158 | | return x->data; |
159 | | } |
160 | | #endif |
161 | | |
162 | | #if !defined(HAVE_DECL_OPENSSL_ZALLOC) || HAVE_DECL_OPENSSL_ZALLOC == 0 |
163 | | void *OPENSSL_zalloc(size_t num) |
164 | | { |
165 | | void *out = OPENSSL_malloc(num); |
166 | | |
167 | | if (out != NULL) |
168 | | memset(out, 0, num); |
169 | | |
170 | | return out; |
171 | | } |
172 | | #endif |
173 | | |
174 | | #ifndef HAVE_EC_POINT_GET_AFFINE_COORDINATES |
175 | | int EC_POINT_get_affine_coordinates(const EC_GROUP *group, const EC_POINT *p, BIGNUM *x, BIGNUM *y, BN_CTX *ctx) |
176 | | { |
177 | | return EC_POINT_get_affine_coordinates_GF2m(group, p, x, y, ctx); |
178 | | } |
179 | | #endif |
180 | | |
181 | | #ifndef HAVE_EC_POINT_SET_AFFINE_COORDINATES |
182 | | int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *p, const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx) |
183 | | { |
184 | | return EC_POINT_set_affine_coordinates_GFp(group, p, x, y, ctx); |
185 | | } |
186 | | #endif |
187 | | |
188 | | #ifndef HAVE_EVP_PKEY_DUP |
189 | | EVP_PKEY * |
190 | | EVP_PKEY_dup(EVP_PKEY *key) |
191 | 0 | { |
192 | 0 | EVP_PKEY *out = NULL; |
193 | 0 | DH *dh_in = NULL, *dh_out = NULL; |
194 | 0 | EC_KEY *ec_in = NULL, *ec_out = NULL; |
195 | 0 | RSA *rsa_in = NULL, *rsa_out = NULL; |
196 | |
|
197 | 0 | out = EVP_PKEY_new(); |
198 | |
|
199 | 0 | if (!key || !out) |
200 | 0 | goto err; |
201 | | |
202 | 0 | switch (EVP_PKEY_base_id(key)) { |
203 | 0 | case EVP_PKEY_DH: |
204 | 0 | case EVP_PKEY_DHX: |
205 | 0 | dh_in = EVP_PKEY_get1_DH(key); |
206 | 0 | if (!dh_in) |
207 | 0 | goto err; |
208 | | |
209 | 0 | dh_out = DHparams_dup_with_q(dh_in); |
210 | 0 | if (!dh_out) |
211 | 0 | goto err; |
212 | | |
213 | 0 | EVP_PKEY_set1_DH(out, dh_out); |
214 | 0 | DH_free(dh_out); |
215 | 0 | DH_free(dh_in); |
216 | 0 | break; |
217 | | |
218 | 0 | case EVP_PKEY_EC: |
219 | 0 | ec_in = EVP_PKEY_get1_EC_KEY(key); |
220 | 0 | if (!ec_in) |
221 | 0 | goto err; |
222 | | |
223 | 0 | ec_out = EC_KEY_dup(ec_in); |
224 | 0 | if (!ec_out) |
225 | 0 | goto err; |
226 | | |
227 | 0 | EVP_PKEY_set1_EC_KEY(out, ec_out); |
228 | 0 | EC_KEY_free(ec_out); |
229 | 0 | EC_KEY_free(ec_in); |
230 | 0 | break; |
231 | | |
232 | 0 | case EVP_PKEY_RSA: |
233 | 0 | rsa_in = EVP_PKEY_get1_RSA(key); |
234 | 0 | if (!rsa_in) |
235 | 0 | goto err; |
236 | | |
237 | 0 | rsa_out = RSAPrivateKey_dup(rsa_in); |
238 | 0 | if (!rsa_out) |
239 | 0 | goto err; |
240 | | |
241 | 0 | EVP_PKEY_set1_RSA(out, rsa_out); |
242 | 0 | RSA_free(rsa_out); |
243 | 0 | RSA_free(rsa_in); |
244 | 0 | break; |
245 | | |
246 | 0 | default: |
247 | 0 | goto err; |
248 | 0 | } |
249 | | |
250 | 0 | return out; |
251 | | |
252 | 0 | err: |
253 | 0 | if (dh_in) |
254 | 0 | DH_free(dh_in); |
255 | 0 | if (ec_in) |
256 | 0 | EC_KEY_free(ec_in); |
257 | 0 | if (rsa_in) |
258 | 0 | RSA_free(rsa_in); |
259 | 0 | if (out) |
260 | 0 | EVP_PKEY_free(out); |
261 | |
|
262 | | return NULL; |
263 | 0 | } |
264 | | #endif |