2 // btls-x509-store-ctx.c
5 // Created by Martin Baulig on 3/5/16.
6 // Copyright © 2016 Xamarin. All rights reserved.
9 #include <btls-x509-store-ctx.h>
11 struct MonoBtlsX509StoreCtx {
14 CRYPTO_refcount_t references;
15 MonoBtlsX509Store *store;
16 MonoBtlsX509Chain *chain;
19 MonoBtlsX509StoreCtx *
20 mono_btls_x509_store_ctx_from_ptr (X509_STORE_CTX *ptr)
22 MonoBtlsX509StoreCtx *ctx;
24 ctx = OPENSSL_malloc (sizeof(MonoBtlsX509StoreCtx));
28 memset (ctx, 0, sizeof (MonoBtlsX509StoreCtx));
34 MonoBtlsX509StoreCtx *
35 mono_btls_x509_store_ctx_new (void)
37 MonoBtlsX509StoreCtx *ctx;
39 ctx = OPENSSL_malloc (sizeof(MonoBtlsX509StoreCtx));
43 memset (ctx, 0, sizeof (MonoBtlsX509StoreCtx));
44 ctx->ctx = X509_STORE_CTX_new ();
50 MonoBtlsX509StoreCtx *
51 mono_btls_x509_store_ctx_up_ref (MonoBtlsX509StoreCtx *ctx)
53 CRYPTO_refcount_inc (&ctx->references);
58 mono_btls_x509_store_ctx_free (MonoBtlsX509StoreCtx *ctx)
60 if (!CRYPTO_refcount_dec_and_test_zero (&ctx->references))
64 X509_STORE_CTX_cleanup (ctx->ctx);
65 X509_STORE_CTX_free (ctx->ctx);
69 mono_btls_x509_store_free (ctx->store);
73 mono_btls_x509_chain_free (ctx->chain);
81 mono_btls_x509_store_ctx_get_error (MonoBtlsX509StoreCtx *ctx, const char **error_string)
85 error = X509_STORE_CTX_get_error (ctx->ctx);
87 *error_string = X509_verify_cert_error_string (error);
92 mono_btls_x509_store_ctx_get_error_depth (MonoBtlsX509StoreCtx *ctx)
94 return X509_STORE_CTX_get_error_depth (ctx->ctx);
98 mono_btls_x509_store_ctx_get_chain (MonoBtlsX509StoreCtx *ctx)
100 STACK_OF(X509) *certs;
102 certs = X509_STORE_CTX_get_chain (ctx->ctx);
106 return mono_btls_x509_chain_from_certs (certs);
110 mono_btls_x509_store_ctx_get_untrusted (MonoBtlsX509StoreCtx *ctx)
112 STACK_OF(X509) *untrusted;
115 * Unfortunately, there is no accessor function for this.
117 * This is the set of certificate that's passed in by
118 * X509_STORE_CTX_init() and X509_STORE_CTX_set_chain().
120 untrusted = ctx->ctx->untrusted;
124 return mono_btls_x509_chain_from_certs (untrusted);
128 mono_btls_x509_store_ctx_test (MonoBtlsX509StoreCtx *ctx)
130 X509_VERIFY_PARAM *param;
133 fprintf (stderr, "TEST: %p!\n", ctx);
134 param = X509_STORE_CTX_get0_param (ctx->ctx);
135 peer = X509_VERIFY_PARAM_get0_peername(param);
136 fprintf (stderr, "TEST #1: %s\n", peer);
140 mono_btls_x509_store_ctx_init (MonoBtlsX509StoreCtx *ctx,
141 MonoBtlsX509Store *store, MonoBtlsX509Chain *chain)
143 STACK_OF(X509) *certs;
150 certs = mono_btls_x509_chain_peek_certs (chain);
151 if (!certs || !sk_X509_num (certs))
154 ctx->store = mono_btls_x509_store_up_ref(store);
155 ctx->chain = mono_btls_x509_chain_up_ref(chain);
157 leaf = sk_X509_value (certs, 0);
158 ret = X509_STORE_CTX_init (ctx->ctx, mono_btls_x509_store_peek_store (store), leaf, certs);
162 X509_STORE_CTX_set_app_data (ctx->ctx, ctx);
167 mono_btls_x509_store_ctx_set_param (MonoBtlsX509StoreCtx *ctx, MonoBtlsX509VerifyParam *param)
169 return X509_VERIFY_PARAM_set1 (X509_STORE_CTX_get0_param (ctx->ctx), mono_btls_x509_verify_param_peek_param (param));
173 mono_btls_x509_store_ctx_verify_cert (MonoBtlsX509StoreCtx *ctx)
175 return X509_verify_cert (ctx->ctx);
179 mono_btls_x509_store_ctx_get_by_subject (MonoBtlsX509StoreCtx *ctx, MonoBtlsX509Name *name)
185 ret = X509_STORE_get_by_subject (ctx->ctx, X509_LU_X509, mono_btls_x509_name_peek_name (name), &obj);
186 if (ret != X509_LU_X509) {
187 X509_OBJECT_free_contents (&obj);
191 x509 = X509_up_ref (obj.data.x509);
196 mono_btls_x509_store_ctx_get_current_cert (MonoBtlsX509StoreCtx *ctx)
198 X509 *x509 = X509_STORE_CTX_get_current_cert (ctx->ctx);
201 return X509_up_ref (x509);
205 mono_btls_x509_store_ctx_get_current_issuer (MonoBtlsX509StoreCtx *ctx)
207 X509 *x509 = X509_STORE_CTX_get0_current_issuer (ctx->ctx);
210 return X509_up_ref (x509);
213 MonoBtlsX509VerifyParam *
214 mono_btls_x509_store_ctx_get_verify_param (MonoBtlsX509StoreCtx *ctx)
216 X509_VERIFY_PARAM *param;
218 param = X509_STORE_CTX_get0_param (ctx->ctx);
222 return mono_btls_x509_verify_param_from_store_ctx (ctx, param);
226 mono_btls_x509_store_ctx_get_foo (MonoBtlsX509StoreCtx *ctx)