[tests] Test loading references from LoadFrom and LoadFile contexts
[mono-project.git] / mono / btls / btls-x509-verify-param.c
blob21a37d91674ff45940f1fcac2445652af10c496b
1 //
2 // btls-x509-verify-param.c
3 // MonoBtls
4 //
5 // Created by Martin Baulig on 3/5/16.
6 // Copyright © 2016 Xamarin. All rights reserved.
7 //
9 #include "btls-x509-verify-param.h"
10 #include "btls-x509-store-ctx.h"
12 struct MonoBtlsX509VerifyParam {
13 int owns;
14 MonoBtlsX509StoreCtx *owner;
15 X509_VERIFY_PARAM *param;
18 MONO_API MonoBtlsX509VerifyParam *
19 mono_btls_x509_verify_param_new (void)
21 MonoBtlsX509VerifyParam *param;
23 param = OPENSSL_malloc (sizeof(MonoBtlsX509VerifyParam));
24 if (!param)
25 return NULL;
26 memset (param, 0, sizeof (MonoBtlsX509VerifyParam));
27 param->param = X509_VERIFY_PARAM_new();
28 param->owns = 1;
29 return param;
32 MONO_API MonoBtlsX509VerifyParam *
33 mono_btls_x509_verify_param_from_store_ctx (MonoBtlsX509StoreCtx *ctx, X509_VERIFY_PARAM *param)
35 MonoBtlsX509VerifyParam *instance;
37 instance = OPENSSL_malloc (sizeof(MonoBtlsX509VerifyParam));
38 if (!instance)
39 return NULL;
40 memset (instance, 0, sizeof (MonoBtlsX509VerifyParam));
41 instance->param = param;
42 instance->owner = mono_btls_x509_store_ctx_up_ref (ctx);
43 return instance;
46 MONO_API MonoBtlsX509VerifyParam *
47 mono_btls_x509_verify_param_copy (const MonoBtlsX509VerifyParam *from)
49 MonoBtlsX509VerifyParam *param;
51 param = mono_btls_x509_verify_param_new ();
52 if (!param)
53 return NULL;
55 X509_VERIFY_PARAM_set1 (param->param, from->param);
56 return param;
59 MONO_API const X509_VERIFY_PARAM *
60 mono_btls_x509_verify_param_peek_param (const MonoBtlsX509VerifyParam *param)
62 return param->param;
65 MONO_API int
66 mono_btls_x509_verify_param_can_modify (MonoBtlsX509VerifyParam *param)
68 return param->owns;
71 MONO_API MonoBtlsX509VerifyParam *
72 mono_btls_x509_verify_param_lookup (const char *name)
74 MonoBtlsX509VerifyParam *param;
75 const X509_VERIFY_PARAM *p;
77 p = X509_VERIFY_PARAM_lookup(name);
78 if (!p)
79 return NULL;
81 param = OPENSSL_malloc (sizeof(MonoBtlsX509VerifyParam));
82 if (!param)
83 return NULL;
84 memset (param, 0, sizeof (MonoBtlsX509VerifyParam));
85 param->param = (X509_VERIFY_PARAM *)p;
86 return param;
89 MONO_API void
90 mono_btls_x509_verify_param_free (MonoBtlsX509VerifyParam *param)
92 if (param->owns) {
93 if (param->param) {
94 X509_VERIFY_PARAM_free (param->param);
95 param->param = NULL;
98 if (param->owner) {
99 mono_btls_x509_store_ctx_free (param->owner);
100 param->owner = NULL;
102 OPENSSL_free (param);
105 MONO_API int
106 mono_btls_x509_verify_param_set_name (MonoBtlsX509VerifyParam *param, const char *name)
108 if (!param->owns)
109 return -1;
110 return X509_VERIFY_PARAM_set1_name (param->param, name);
113 MONO_API int
114 mono_btls_x509_verify_param_set_host (MonoBtlsX509VerifyParam *param, const char *host, int namelen)
116 if (!param->owns)
117 return -1;
118 return X509_VERIFY_PARAM_set1_host (param->param, host, namelen);
121 MONO_API int
122 mono_btls_x509_verify_param_add_host (MonoBtlsX509VerifyParam *param, const char *host, int namelen)
124 if (!param->owns)
125 return -1;
126 return X509_VERIFY_PARAM_set1_host (param->param, host, namelen);
129 MONO_API uint64_t
130 mono_btls_x509_verify_param_get_flags (MonoBtlsX509VerifyParam *param)
132 return X509_VERIFY_PARAM_get_flags (param->param);
135 MONO_API int
136 mono_btls_x509_verify_param_set_flags (MonoBtlsX509VerifyParam *param, uint64_t flags)
138 if (!param->owns)
139 return -1;
140 return X509_VERIFY_PARAM_set_flags (param->param, flags);
143 MONO_API MonoBtlsX509VerifyFlags
144 mono_btls_x509_verify_param_get_mono_flags (MonoBtlsX509VerifyParam *param)
146 MonoBtlsX509VerifyFlags current;
147 uint64_t flags;
149 if (!param->owns)
150 return -1;
152 current = 0;
153 flags = X509_VERIFY_PARAM_get_flags (param->param);
155 if (flags & X509_V_FLAG_CRL_CHECK)
156 current |= MONO_BTLS_X509_VERIFY_FLAGS_CRL_CHECK;
157 if (flags & X509_V_FLAG_CRL_CHECK_ALL)
158 current |= MONO_BTLS_X509_VERIFY_FLAGS_CRL_CHECK_ALL;
159 if (flags & X509_V_FLAG_X509_STRICT)
160 current |= MONO_BTLS_X509_VERIFY_FLAGS_X509_STRICT;
162 return current;
165 MONO_API int
166 mono_btls_x509_verify_param_set_mono_flags (MonoBtlsX509VerifyParam *param, MonoBtlsX509VerifyFlags flags)
168 uint64_t current;
170 if (!param->owns)
171 return -1;
173 current = X509_VERIFY_PARAM_get_flags (param->param);
174 if (flags & MONO_BTLS_X509_VERIFY_FLAGS_CRL_CHECK)
175 current |= X509_V_FLAG_CRL_CHECK;
176 if (flags & MONO_BTLS_X509_VERIFY_FLAGS_CRL_CHECK_ALL)
177 current |= X509_V_FLAG_CRL_CHECK_ALL;
178 if (flags & MONO_BTLS_X509_VERIFY_FLAGS_X509_STRICT)
179 current |= X509_V_FLAG_X509_STRICT;
181 return X509_VERIFY_PARAM_set_flags (param->param, current);
184 MONO_API int
185 mono_btls_x509_verify_param_set_purpose (MonoBtlsX509VerifyParam *param, MonoBtlsX509Purpose purpose)
187 if (!param->owns)
188 return -1;
189 return X509_VERIFY_PARAM_set_purpose (param->param, purpose);
192 MONO_API int
193 mono_btls_x509_verify_param_get_depth (MonoBtlsX509VerifyParam *param)
195 return X509_VERIFY_PARAM_get_depth (param->param);
198 MONO_API int
199 mono_btls_x509_verify_param_set_depth (MonoBtlsX509VerifyParam *param, int depth)
201 if (!param->owns)
202 return -1;
203 X509_VERIFY_PARAM_set_depth (param->param, depth);
204 return 1;
207 MONO_API int
208 mono_btls_x509_verify_param_set_time (MonoBtlsX509VerifyParam *param, int64_t time)
210 if (!param->owns)
211 return -1;
212 X509_VERIFY_PARAM_set_time (param->param, time);
213 return 1;
216 MONO_API char *
217 mono_btls_x509_verify_param_get_peername (MonoBtlsX509VerifyParam *param)
219 char *peer = X509_VERIFY_PARAM_get0_peername (param->param);
220 return peer;