5 // Created by Martin Baulig on 3/23/16.
6 // Copyright © 2016 Xamarin. All rights reserved.
9 #include "btls-x509-crl.h"
10 #include "btls-x509-revoked.h"
12 struct MonoBtlsX509Crl
{
14 CRYPTO_refcount_t references
;
17 MONO_API MonoBtlsX509Crl
*
18 mono_btls_x509_crl_from_data (const void *buf
, int len
, MonoBtlsX509Format format
)
23 crl
= OPENSSL_malloc (sizeof (MonoBtlsX509Crl
));
24 memset (crl
, 0, sizeof(MonoBtlsX509Crl
));
27 bio
= BIO_new_mem_buf ((void *)buf
, len
);
29 case MONO_BTLS_X509_FORMAT_DER
:
30 crl
->crl
= d2i_X509_CRL_bio (bio
, NULL
);
32 case MONO_BTLS_X509_FORMAT_PEM
:
33 crl
->crl
= PEM_read_bio_X509_CRL (bio
, NULL
, NULL
, NULL
);
46 MONO_API MonoBtlsX509Crl
*
47 mono_btls_x509_crl_ref (MonoBtlsX509Crl
*crl
)
49 CRYPTO_refcount_inc (&crl
->references
);
54 mono_btls_x509_crl_free (MonoBtlsX509Crl
*crl
)
56 if (!CRYPTO_refcount_dec_and_test_zero (&crl
->references
))
59 X509_CRL_free (crl
->crl
);
64 MONO_API MonoBtlsX509Revoked
*
65 mono_btls_x509_crl_get_by_cert (MonoBtlsX509Crl
*crl
, X509
*x509
)
67 X509_REVOKED
*revoked
;
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
);
77 return mono_btls_x509_revoked_new (crl
, revoked
);
80 MONO_API MonoBtlsX509Revoked
*
81 mono_btls_x509_crl_get_by_serial (MonoBtlsX509Crl
*crl
, void *serial
, int len
)
84 X509_REVOKED
*revoked
;
87 si
.type
= V_ASN1_INTEGER
;
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
);
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 MONO_API 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
))
120 revoked
= sk_X509_REVOKED_value (stack
, index
);
124 return mono_btls_x509_revoked_new (crl
, revoked
);
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
));
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
));
140 mono_btls_x509_crl_get_version (MonoBtlsX509Crl
*crl
)
142 return X509_CRL_get_version (crl
->crl
);
145 MONO_API MonoBtlsX509Name
*
146 mono_btls_x509_crl_get_issuer (MonoBtlsX509Crl
*crl
)
148 return mono_btls_x509_name_copy (X509_CRL_get_issuer (crl
->crl
));