Fix potential crash for Encoder.Convert (#20522)
[mono-project.git] / mono / btls / btls-x509-store.c
blobefa3db3424b954e3f740d139da42a0852b3ff777
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 //
9 #include "btls-x509-store.h"
11 struct MonoBtlsX509Store {
12 X509_STORE *store;
13 CRYPTO_refcount_t references;
16 MonoBtlsX509Store *
17 mono_btls_x509_store_from_store (X509_STORE *ctx)
19 MonoBtlsX509Store *store;
21 store = OPENSSL_malloc (sizeof(MonoBtlsX509Store));
22 if (!store)
23 return NULL;
25 memset (store, 0, sizeof(MonoBtlsX509Store));
26 store->store = ctx;
27 CRYPTO_refcount_inc (&store->store->references);
28 store->references = 1;
29 return store;
32 MonoBtlsX509Store *
33 mono_btls_x509_store_from_ctx (X509_STORE_CTX *ctx)
35 return mono_btls_x509_store_from_store (ctx->ctx);
38 MonoBtlsX509Store *
39 mono_btls_x509_store_new (void)
41 MonoBtlsX509Store *store;
43 store = OPENSSL_malloc (sizeof(MonoBtlsX509Store));
44 if (!store)
45 return NULL;
47 memset (store, 0, sizeof(MonoBtlsX509Store));
48 store->store = X509_STORE_new ();
49 store->references = 1;
50 return store;
53 X509_STORE *
54 mono_btls_x509_store_peek_store (MonoBtlsX509Store *store)
56 return store->store;
59 MonoBtlsX509Store *
60 mono_btls_x509_store_from_ssl_ctx (MonoBtlsSslCtx *ctx)
62 X509_STORE *store = mono_btls_ssl_ctx_peek_store (ctx);
63 return mono_btls_x509_store_from_store (store);
66 int
67 mono_btls_x509_store_free (MonoBtlsX509Store *store)
69 if (!CRYPTO_refcount_dec_and_test_zero(&store->references))
70 return 0;
72 if (store->store) {
73 X509_STORE_free (store->store);
74 store->store = NULL;
76 OPENSSL_free (store);
77 return 1;
80 MonoBtlsX509Store *
81 mono_btls_x509_store_up_ref (MonoBtlsX509Store *store)
83 CRYPTO_refcount_inc (&store->references);
84 return store;
87 int
88 mono_btls_x509_store_add_cert (MonoBtlsX509Store *store, X509 *cert)
90 return X509_STORE_add_cert (store->store, cert);
93 int
94 mono_btls_x509_store_load_locations (MonoBtlsX509Store *store, const char *file, const char *path)
96 return X509_STORE_load_locations (store->store, file, path);
99 int
100 mono_btls_x509_store_set_default_paths (MonoBtlsX509Store *store)
102 return X509_STORE_set_default_paths (store->store);
106 mono_btls_x509_store_get_count (MonoBtlsX509Store *store)
108 return (int)sk_X509_OBJECT_num (store->store->objs);