Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / btls / btls-x509-store.c
1 //
2 //  btls-x509-store.c
3 //  MonoBtls
4 //
5 //  Created by Martin Baulig on 3/3/16.
6 //  Copyright © 2016 Xamarin. All rights reserved.
7 //
8
9 #include <btls-x509-store.h>
10
11 struct MonoBtlsX509Store {
12         X509_STORE *store;
13         CRYPTO_refcount_t references;
14 };
15
16 MONO_API MonoBtlsX509Store *
17 mono_btls_x509_store_from_store (X509_STORE *ctx)
18 {
19         MonoBtlsX509Store *store;
20
21         store = OPENSSL_malloc (sizeof(MonoBtlsX509Store));
22         if (!store)
23                 return NULL;
24
25         memset (store, 0, sizeof(MonoBtlsX509Store));
26         store->store = ctx;
27         CRYPTO_refcount_inc (&store->store->references);
28         store->references = 1;
29         return store;
30 }
31
32 MONO_API MonoBtlsX509Store *
33 mono_btls_x509_store_from_ctx (X509_STORE_CTX *ctx)
34 {
35         return mono_btls_x509_store_from_store (ctx->ctx);
36 }
37
38 MONO_API MonoBtlsX509Store *
39 mono_btls_x509_store_new (void)
40 {
41         MonoBtlsX509Store *store;
42
43         store = OPENSSL_malloc (sizeof(MonoBtlsX509Store));
44         if (!store)
45                 return NULL;
46
47         memset (store, 0, sizeof(MonoBtlsX509Store));
48         store->store = X509_STORE_new ();
49         store->references = 1;
50         return store;
51 }
52
53 MONO_API X509_STORE *
54 mono_btls_x509_store_peek_store (MonoBtlsX509Store *store)
55 {
56         return store->store;
57 }
58
59 MONO_API MonoBtlsX509Store *
60 mono_btls_x509_store_from_ssl_ctx (MonoBtlsSslCtx *ctx)
61 {
62         X509_STORE *store = mono_btls_ssl_ctx_peek_store (ctx);
63         return mono_btls_x509_store_from_store (store);
64 }
65
66 MONO_API int
67 mono_btls_x509_store_free (MonoBtlsX509Store *store)
68 {
69         if (!CRYPTO_refcount_dec_and_test_zero(&store->references))
70                 return 0;
71
72         if (store->store) {
73                 X509_STORE_free (store->store);
74                 store->store = NULL;
75         }
76         OPENSSL_free (store);
77         return 1;
78 }
79
80 MONO_API MonoBtlsX509Store *
81 mono_btls_x509_store_up_ref (MonoBtlsX509Store *store)
82 {
83         CRYPTO_refcount_inc (&store->references);
84         return store;
85 }
86
87 MONO_API int
88 mono_btls_x509_store_add_cert (MonoBtlsX509Store *store, X509 *cert)
89 {
90         return X509_STORE_add_cert (store->store, cert);
91 }
92
93 MONO_API int
94 mono_btls_x509_store_load_locations (MonoBtlsX509Store *store, const char *file, const char *path)
95 {
96         return X509_STORE_load_locations (store->store, file, path);
97 }
98
99 MONO_API int
100 mono_btls_x509_store_set_default_paths (MonoBtlsX509Store *store)
101 {
102         return X509_STORE_set_default_paths (store->store);
103 }
104
105 MONO_API int
106 mono_btls_x509_store_get_count (MonoBtlsX509Store *store)
107 {
108         return (int)sk_X509_OBJECT_num (store->store->objs);
109 }
110