Updates for CODEOWNERS
[mono-project.git] / mono / btls / btls-x509-chain.c
blob5b7082ddb135f645fc74dd3a05197f70c7b1468a
1 //
2 // btls-x509-chain.c
3 // MonoBtls
4 //
5 // Created by Martin Baulig on 3/3/16.
6 // Copyright © 2016 Xamarin. All rights reserved.
7 //
9 #include <btls-x509-chain.h>
11 struct MonoBtlsX509Chain {
12 STACK_OF(X509) *certs;
13 CRYPTO_refcount_t references;
16 MONO_API MonoBtlsX509Chain *
17 mono_btls_x509_chain_new (void)
19 MonoBtlsX509Chain *chain = (MonoBtlsX509Chain *)OPENSSL_malloc (sizeof (MonoBtlsX509Chain));
20 if (chain == NULL)
21 return NULL;
23 memset(chain, 0, sizeof(MonoBtlsX509Chain));
24 chain->certs = sk_X509_new_null ();
25 chain->references = 1;
26 return chain;
29 MONO_API MonoBtlsX509Chain *
30 mono_btls_x509_chain_from_certs (STACK_OF(X509) *certs)
32 MonoBtlsX509Chain *chain = (MonoBtlsX509Chain *)OPENSSL_malloc (sizeof (MonoBtlsX509Chain));
33 if (chain == NULL)
34 return NULL;
36 memset(chain, 0, sizeof(MonoBtlsX509Chain));
37 chain->certs = X509_chain_up_ref(certs);
38 chain->references = 1;
39 return chain;
42 MONO_API STACK_OF(X509) *
43 mono_btls_x509_chain_peek_certs (MonoBtlsX509Chain *chain)
45 return chain->certs;
48 MONO_API int
49 mono_btls_x509_chain_get_count (MonoBtlsX509Chain *chain)
51 return (int)sk_X509_num(chain->certs);
54 MONO_API X509 *
55 mono_btls_x509_chain_get_cert (MonoBtlsX509Chain *chain, int index)
57 X509 *cert;
59 if ((size_t)index >= sk_X509_num(chain->certs))
60 return NULL;
61 cert = sk_X509_value(chain->certs, index);
62 if (cert)
63 X509_up_ref(cert);
64 return cert;
67 MONO_API STACK_OF(X509) *
68 mono_btls_x509_chain_get_certs (MonoBtlsX509Chain *chain)
70 return chain->certs;
73 MONO_API int
74 mono_btls_x509_chain_free (MonoBtlsX509Chain *chain)
76 if (!CRYPTO_refcount_dec_and_test_zero(&chain->references))
77 return 0;
79 sk_X509_pop_free(chain->certs, X509_free);
80 OPENSSL_free (chain);
81 return 1;
84 MONO_API MonoBtlsX509Chain *
85 mono_btls_x509_chain_up_ref (MonoBtlsX509Chain *chain)
87 CRYPTO_refcount_inc(&chain->references);
88 return chain;
91 MONO_API void
92 mono_btls_x509_chain_add_cert (MonoBtlsX509Chain *chain, X509 *x509)
94 X509_up_ref(x509);
95 sk_X509_push(chain->certs, x509);