[BTLS]: Add the native BTLS sources.
[mono.git] / mono / btls / btls-x509.c
1 //
2 //  btls-x509.c
3 //  MonoBtls
4 //
5 //  Created by Martin Baulig on 14/11/15.
6 //  Copyright (c) 2015 Xamarin. All rights reserved.
7 //
8
9 #include <btls-x509.h>
10 #include <openssl/x509v3.h>
11 #include <openssl/pkcs12.h>
12
13 X509 *
14 mono_btls_x509_from_data (const void *buf, int len, MonoBtlsX509Format format)
15 {
16         BIO *bio;
17         X509 *cert = NULL;
18
19         bio = BIO_new_mem_buf ((void *)buf, len);
20         switch (format) {
21                 case MONO_BTLS_X509_FORMAT_DER:
22                         cert = d2i_X509_bio (bio, NULL);
23                         break;
24                 case MONO_BTLS_X509_FORMAT_PEM:
25                         cert = PEM_read_bio_X509 (bio, NULL, NULL, NULL);
26                         break;
27         }
28         BIO_free (bio);
29         return cert;
30 }
31
32 X509 *
33 mono_btls_x509_up_ref (X509 *x509)
34 {
35         X509_up_ref (x509);
36         return x509;
37 }
38
39 void
40 mono_btls_x509_free (X509 *x509)
41 {
42         X509_free (x509);
43 }
44
45 X509 *
46 mono_btls_x509_dup (X509 *x509)
47 {
48         return X509_dup (x509);
49 }
50
51 MonoBtlsX509Name *
52 mono_btls_x509_get_subject_name (X509 *x509)
53 {
54         return mono_btls_x509_name_copy (X509_get_subject_name (x509));
55 }
56
57 MonoBtlsX509Name *
58 mono_btls_x509_get_issuer_name (X509 *x509)
59 {
60         return mono_btls_x509_name_copy (X509_get_issuer_name (x509));
61 }
62
63 int
64 mono_btls_x509_get_subject_name_string (X509 *name, char *buffer, int size)
65 {
66         *buffer = 0;
67         return X509_NAME_oneline (X509_get_subject_name (name), buffer, size) != NULL;
68 }
69
70 int
71 mono_btls_x509_get_issuer_name_string (X509 *name, char *buffer, int size)
72 {
73         *buffer = 0;
74         return X509_NAME_oneline (X509_get_issuer_name (name), buffer, size) != NULL;
75 }
76
77 int
78 mono_btls_x509_get_raw_data (X509 *x509, BIO *bio, MonoBtlsX509Format format)
79 {
80         switch (format) {
81                 case MONO_BTLS_X509_FORMAT_DER:
82                         return i2d_X509_bio (bio, x509);
83                 case MONO_BTLS_X509_FORMAT_PEM:
84                         return PEM_write_bio_X509 (bio, x509);
85                 default:
86                         return 0;
87         }
88 }
89
90 int
91 mono_btls_x509_cmp (const X509 *a, const X509 *b)
92 {
93         return X509_cmp (a, b);
94 }
95
96 int
97 mono_btls_x509_get_hash (X509 *x509, const void **data)
98 {
99         X509_check_purpose (x509, -1, 0);
100         *data = x509->sha1_hash;
101         return SHA_DIGEST_LENGTH;
102 }
103
104 long
105 mono_btls_x509_get_not_before (X509 *x509)
106 {
107         return mono_btls_util_asn1_time_to_ticks (X509_get_notBefore (x509));
108 }
109
110 long
111 mono_btls_x509_get_not_after (X509 *x509)
112 {
113         return mono_btls_util_asn1_time_to_ticks (X509_get_notAfter (x509));
114 }
115
116 int
117 mono_btls_x509_get_public_key (X509 *x509, BIO *bio)
118 {
119         EVP_PKEY *pkey;
120         uint8_t *data = NULL;
121         int ret;
122
123         pkey = X509_get_pubkey (x509);
124         if (!pkey)
125                 return -1;
126
127         ret = i2d_PublicKey (pkey, &data);
128
129         if (ret > 0 && data) {
130                 ret = BIO_write (bio, data, ret);
131                 OPENSSL_free (data);
132         }
133
134         EVP_PKEY_free (pkey);
135         return ret;
136 }
137
138 int
139 mono_btls_x509_get_serial_number (X509 *x509, char *buffer, int size, int mono_style)
140 {
141         ASN1_INTEGER *serial;
142         char *pos;
143         int len, idx;
144
145         serial = X509_get_serialNumber (x509);
146         if (serial->length == 0 || serial->length+1 > size)
147                 return 0;
148
149         if (!mono_style) {
150                 memcpy (buffer, serial->data, serial->length);
151                 return serial->length;
152         }
153
154         pos = buffer;
155         len = 0;
156
157         for (idx = serial->length - 1; idx >= 0; idx--) {
158                 *pos++ = serial->data [idx];
159                 len++;
160         }
161
162         if (serial->data [0] >= 0x80) {
163                 *pos++ = 0;
164                 len++;
165         }
166
167         return len;
168 }
169
170 int
171 mono_btls_x509_get_public_key_algorithm (X509 *x509, char *buffer, int size)
172 {
173         X509_PUBKEY *pkey;
174         ASN1_OBJECT *ppkalg;
175         int ret;
176
177         *buffer = 0;
178         pkey = X509_get_X509_PUBKEY (x509);
179         if (!pkey)
180                 return 0;
181
182         ret = X509_PUBKEY_get0_param (&ppkalg, NULL, NULL, NULL, pkey);
183         if (!ret || !ppkalg)
184                 return ret;
185
186         return OBJ_obj2txt (buffer, size, ppkalg, 1);
187 }
188
189 int
190 mono_btls_x509_get_version (X509 *x509)
191 {
192         return (int)X509_get_version (x509) + 1;
193 }
194
195 int
196 mono_btls_x509_get_signature_algorithm (X509 *x509, char *buffer, int size)
197 {
198         const ASN1_OBJECT *obj;
199         int nid;
200
201         *buffer = 0;
202
203         nid = X509_get_signature_nid (x509);
204
205         obj = OBJ_nid2obj (nid);
206         if (!obj)
207                 return 0;
208
209         return OBJ_obj2txt (buffer, size, obj, 1);
210 }
211
212 int
213 mono_btls_x509_get_public_key_asn1 (X509 *x509, char *out_oid, int oid_len, uint8_t **buffer, int *size)
214 {
215         X509_PUBKEY *pkey;
216         ASN1_OBJECT *ppkalg;
217         const unsigned char *pk;
218         int pk_len;
219         int ret;
220
221         if (out_oid)
222                 *out_oid = 0;
223
224         pkey = X509_get_X509_PUBKEY (x509);
225         if (!pkey || !pkey->public_key)
226                 return 0;
227
228         ret = X509_PUBKEY_get0_param (&ppkalg, &pk, &pk_len, NULL, pkey);
229         if (ret != 1 || !ppkalg || !pk)
230                 return 0;
231
232         if (out_oid) {
233                 OBJ_obj2txt (out_oid, oid_len, ppkalg, 1);
234         }
235
236         if (buffer) {
237                 *size = pk_len;
238                 *buffer = OPENSSL_malloc (pk_len);
239                 if (!*buffer)
240                         return 0;
241
242                 memcpy (*buffer, pk, pk_len);
243         }
244
245         return 1;
246
247 }
248
249 int
250 mono_btls_x509_get_public_key_parameters (X509 *x509, char *out_oid, int oid_len, uint8_t **buffer, int *size)
251 {
252         X509_PUBKEY *pkey;
253         X509_ALGOR *algor;
254         ASN1_OBJECT *paobj;
255         int ptype;
256         void *pval;
257         int ret;
258
259         if (out_oid)
260                 *out_oid = 0;
261
262         pkey = X509_get_X509_PUBKEY (x509);
263
264         ret = X509_PUBKEY_get0_param (NULL, NULL, NULL, &algor, pkey);
265         if (ret != 1 || !algor)
266                 return 0;
267
268         X509_ALGOR_get0 (&paobj, &ptype, &pval, algor);
269
270         if (ptype != V_ASN1_NULL && ptype != V_ASN1_SEQUENCE)
271                 return 0;
272
273         if (ptype == V_ASN1_NULL) {
274                 uint8_t *ptr;
275
276                 *size = 2;
277                 *buffer = OPENSSL_malloc (2);
278                 if (!*buffer)
279                         return 0;
280
281                 ptr = *buffer;
282                 *ptr++ = 0x05;
283                 *ptr++ = 0x00;
284
285                 if (out_oid)
286                         OBJ_obj2txt (out_oid, oid_len, paobj, 1);
287
288                 return 1;
289         } else if (ptype == V_ASN1_SEQUENCE) {
290                 ASN1_STRING *pstr = pval;
291
292                 *size = pstr->length;
293                 *buffer = OPENSSL_malloc (pstr->length);
294                 if (!*buffer)
295                         return 0;
296
297                 memcpy (*buffer, pstr->data, pstr->length);
298
299                 if (out_oid)
300                         OBJ_obj2txt (out_oid, oid_len, paobj, 1);
301
302                 return 1;
303         } else {
304                 return 0;
305         }
306 }
307
308 EVP_PKEY *
309 mono_btls_x509_get_pubkey (X509 *x509)
310 {
311         return X509_get_pubkey (x509);
312 }
313
314 int
315 mono_btls_x509_get_subject_key_identifier (X509 *x509, uint8_t **buffer, int *size)
316 {
317         ASN1_OCTET_STRING *skid;
318
319         *size = 0;
320         *buffer = NULL;
321
322         if (X509_get_version (x509) != 2)
323                 return 0;
324
325         skid = X509_get_ext_d2i (x509, NID_subject_key_identifier, NULL, NULL);
326         if (!skid)
327                 return 0;
328
329         *size = skid->length;
330         *buffer = OPENSSL_malloc (*size);
331         if (!*buffer)
332                 return 0;
333
334         memcpy (*buffer, skid->data, *size);
335         return 1;
336 }
337
338 int
339 mono_btls_x509_print (X509 *x509, BIO *bio)
340 {
341         return X509_print_ex (bio, x509, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
342 }
343
344 static int
345 get_trust_nid (MonoBtlsX509Purpose purpose)
346 {
347         switch (purpose) {
348                 case MONO_BTLS_X509_PURPOSE_SSL_CLIENT:
349                         return NID_client_auth;
350                 case MONO_BTLS_X509_PURPOSE_SSL_SERVER:
351                         return NID_server_auth;
352                 default:
353                         return 0;
354         }
355 }
356
357 int
358 mono_btls_x509_add_trust_object (X509 *x509, MonoBtlsX509Purpose purpose)
359 {
360         ASN1_OBJECT *trust;
361         int nid;
362
363         nid = get_trust_nid (purpose);
364         if (!nid)
365                 return 0;
366
367         trust = ASN1_OBJECT_new ();
368         if (!trust)
369                 return 0;
370
371         trust->nid = nid;
372         return X509_add1_trust_object (x509, trust);
373 }
374
375 int
376 mono_btls_x509_add_reject_object (X509 *x509, MonoBtlsX509Purpose purpose)
377 {
378         ASN1_OBJECT *reject;
379         int nid;
380
381         nid = get_trust_nid (purpose);
382         if (!nid)
383                 return 0;
384
385         reject = ASN1_OBJECT_new ();
386         if (!reject)
387                 return 0;
388
389         reject->nid = nid;
390         return X509_add1_reject_object (x509, reject);
391 }
392
393 int
394 mono_btls_x509_add_explicit_trust (X509 *x509, MonoBtlsX509TrustKind kind)
395 {
396         int ret = 0;
397
398         if ((kind & MONO_BTLS_X509_TRUST_KIND_REJECT_ALL) != 0)
399                 kind |= MONO_BTLS_X509_TRUST_KIND_REJECT_CLIENT | MONO_BTLS_X509_TRUST_KIND_REJECT_SERVER;
400
401         if ((kind & MONO_BTLS_X509_TRUST_KIND_TRUST_ALL) != 0)
402                 kind |= MONO_BTLS_X509_TRUST_KIND_TRUST_CLIENT | MONO_BTLS_X509_TRUST_KIND_TRUST_SERVER;
403
404
405         if ((kind & MONO_BTLS_X509_TRUST_KIND_REJECT_CLIENT) != 0) {
406                 ret = mono_btls_x509_add_reject_object (x509, MONO_BTLS_X509_PURPOSE_SSL_CLIENT);
407                 if (!ret)
408                         return ret;
409         }
410
411         if ((kind & MONO_BTLS_X509_TRUST_KIND_REJECT_SERVER) != 0) {
412                 ret = mono_btls_x509_add_reject_object (x509, MONO_BTLS_X509_PURPOSE_SSL_SERVER);
413                 if (!ret)
414                         return ret;
415         }
416
417         if (ret) {
418                 // Ignore any MONO_BTLS_X509_TRUST_KIND_TRUST_* settings if we added
419                 // any kind of MONO_BTLS_X509_TRUST_KIND_REJECT_* before.
420                 return ret;
421         }
422
423         if ((kind & MONO_BTLS_X509_TRUST_KIND_TRUST_CLIENT) != 0) {
424                 ret = mono_btls_x509_add_trust_object (x509, MONO_BTLS_X509_PURPOSE_SSL_CLIENT);
425                 if (!ret)
426                         return ret;
427         }
428
429         if ((kind & MONO_BTLS_X509_TRUST_KIND_TRUST_SERVER) != 0) {
430                 ret = mono_btls_x509_add_trust_object (x509, MONO_BTLS_X509_PURPOSE_SSL_SERVER);
431                 if (!ret)
432                         return ret;
433         }
434
435         return ret;
436 }