update readme (#21797)
[mono-project.git] / mono / btls / btls-x509-crl.c
blob90426fa14714bd5802c19694f8811cb53ff9e18f
1 //
2 // btls-x509-crl.c
3 // MonoBtls
4 //
5 // Created by Martin Baulig on 3/23/16.
6 // Copyright © 2016 Xamarin. All rights reserved.
7 //
9 #include "btls-x509-crl.h"
10 #include "btls-x509-revoked.h"
12 struct MonoBtlsX509Crl {
13 X509_CRL *crl;
14 CRYPTO_refcount_t references;
17 MonoBtlsX509Crl *
18 mono_btls_x509_crl_from_data (const void *buf, int len, MonoBtlsX509Format format)
20 MonoBtlsX509Crl *crl;
21 BIO *bio;
23 crl = OPENSSL_malloc (sizeof (MonoBtlsX509Crl));
24 memset (crl, 0, sizeof(MonoBtlsX509Crl));
25 crl->references = 1;
27 bio = BIO_new_mem_buf ((void *)buf, len);
28 switch (format) {
29 case MONO_BTLS_X509_FORMAT_DER:
30 crl->crl = d2i_X509_CRL_bio (bio, NULL);
31 break;
32 case MONO_BTLS_X509_FORMAT_PEM:
33 crl->crl = PEM_read_bio_X509_CRL (bio, NULL, NULL, NULL);
34 break;
36 BIO_free (bio);
38 if (!crl->crl) {
39 OPENSSL_free (crl);
40 return NULL;
43 return crl;
46 MonoBtlsX509Crl *
47 mono_btls_x509_crl_ref (MonoBtlsX509Crl *crl)
49 CRYPTO_refcount_inc (&crl->references);
50 return crl;
53 int
54 mono_btls_x509_crl_free (MonoBtlsX509Crl *crl)
56 if (!CRYPTO_refcount_dec_and_test_zero (&crl->references))
57 return 0;
59 X509_CRL_free (crl->crl);
60 OPENSSL_free (crl);
61 return 1;
64 MonoBtlsX509Revoked *
65 mono_btls_x509_crl_get_by_cert (MonoBtlsX509Crl *crl, X509 *x509)
67 X509_REVOKED *revoked;
68 int ret;
70 revoked = NULL;
71 ret = X509_CRL_get0_by_cert (crl->crl, &revoked, x509);
72 fprintf (stderr, "mono_btls_x509_crl_get_by_cert: %d - %p\n", ret, revoked);
74 if (!ret || !revoked)
75 return NULL;
77 return mono_btls_x509_revoked_new (crl, revoked);
80 MonoBtlsX509Revoked *
81 mono_btls_x509_crl_get_by_serial (MonoBtlsX509Crl *crl, void *serial, int len)
83 ASN1_INTEGER si;
84 X509_REVOKED *revoked;
85 int ret;
87 si.type = V_ASN1_INTEGER;
88 si.length = len;
89 si.data = serial;
91 revoked = NULL;
92 ret = X509_CRL_get0_by_serial (crl->crl, &revoked, &si);
93 fprintf (stderr, "mono_btls_x509_crl_get_by_serial: %d - %p\n", ret, revoked);
95 if (!ret || !revoked)
96 return NULL;
98 return mono_btls_x509_revoked_new (crl, revoked);
102 mono_btls_x509_crl_get_revoked_count (MonoBtlsX509Crl *crl)
104 STACK_OF(X509_REVOKED) *stack;
106 stack = X509_CRL_get_REVOKED (crl->crl);
107 return (int)sk_X509_REVOKED_num (stack);
110 MonoBtlsX509Revoked *
111 mono_btls_x509_crl_get_revoked (MonoBtlsX509Crl *crl, int index)
113 STACK_OF(X509_REVOKED) *stack;
114 X509_REVOKED *revoked;
116 stack = X509_CRL_get_REVOKED (crl->crl);
117 if ((size_t)index >= sk_X509_REVOKED_num (stack))
118 return NULL;
120 revoked = sk_X509_REVOKED_value (stack, index);
121 if (!revoked)
122 return NULL;
124 return mono_btls_x509_revoked_new (crl, revoked);
127 int64_t
128 mono_btls_x509_crl_get_last_update (MonoBtlsX509Crl *crl)
130 return mono_btls_util_asn1_time_to_ticks (X509_CRL_get_lastUpdate (crl->crl));
133 int64_t
134 mono_btls_x509_crl_get_next_update (MonoBtlsX509Crl *crl)
136 return mono_btls_util_asn1_time_to_ticks (X509_CRL_get_nextUpdate (crl->crl));
139 int64_t
140 mono_btls_x509_crl_get_version (MonoBtlsX509Crl *crl)
142 return X509_CRL_get_version (crl->crl);
145 MonoBtlsX509Name *
146 mono_btls_x509_crl_get_issuer (MonoBtlsX509Crl *crl)
148 return mono_btls_x509_name_copy (X509_CRL_get_issuer (crl->crl));