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_init (MonoBtlsX509StoreCtx
*ctx
,
129 MonoBtlsX509Store
*store
, MonoBtlsX509Chain
*chain
)
131 STACK_OF(X509
) *certs
;
138 certs
= mono_btls_x509_chain_peek_certs (chain
);
139 if (!certs
|| !sk_X509_num (certs
))
142 ctx
->store
= mono_btls_x509_store_up_ref(store
);
143 ctx
->chain
= mono_btls_x509_chain_up_ref(chain
);
145 leaf
= sk_X509_value (certs
, 0);
146 ret
= X509_STORE_CTX_init (ctx
->ctx
, mono_btls_x509_store_peek_store (store
), leaf
, certs
);
150 X509_STORE_CTX_set_app_data (ctx
->ctx
, ctx
);
155 mono_btls_x509_store_ctx_set_param (MonoBtlsX509StoreCtx
*ctx
, MonoBtlsX509VerifyParam
*param
)
157 return X509_VERIFY_PARAM_set1 (X509_STORE_CTX_get0_param (ctx
->ctx
), mono_btls_x509_verify_param_peek_param (param
));
161 mono_btls_x509_store_ctx_verify_cert (MonoBtlsX509StoreCtx
*ctx
)
163 return X509_verify_cert (ctx
->ctx
);
167 mono_btls_x509_store_ctx_get_by_subject (MonoBtlsX509StoreCtx
*ctx
, MonoBtlsX509Name
*name
)
173 ret
= X509_STORE_get_by_subject (ctx
->ctx
, X509_LU_X509
, mono_btls_x509_name_peek_name (name
), &obj
);
174 if (ret
!= X509_LU_X509
) {
175 X509_OBJECT_free_contents (&obj
);
179 x509
= X509_up_ref (obj
.data
.x509
);
184 mono_btls_x509_store_ctx_get_current_cert (MonoBtlsX509StoreCtx
*ctx
)
186 X509
*x509
= X509_STORE_CTX_get_current_cert (ctx
->ctx
);
189 return X509_up_ref (x509
);
193 mono_btls_x509_store_ctx_get_current_issuer (MonoBtlsX509StoreCtx
*ctx
)
195 X509
*x509
= X509_STORE_CTX_get0_current_issuer (ctx
->ctx
);
198 return X509_up_ref (x509
);
201 MonoBtlsX509VerifyParam
*
202 mono_btls_x509_store_ctx_get_verify_param (MonoBtlsX509StoreCtx
*ctx
)
204 X509_VERIFY_PARAM
*param
;
206 param
= X509_STORE_CTX_get0_param (ctx
->ctx
);
210 return mono_btls_x509_verify_param_from_store_ctx (ctx
, param
);
214 mono_btls_x509_store_ctx_get_foo (MonoBtlsX509StoreCtx
*ctx
)