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 //
8 
9 #include <btls-x509-verify-param.h>
10 #include <btls-x509-store-ctx.h>
11 
12 struct MonoBtlsX509VerifyParam {
13 	int owns;
14 	MonoBtlsX509StoreCtx *owner;
15 	X509_VERIFY_PARAM *param;
16 };
17 
18 MONO_API MonoBtlsX509VerifyParam *
mono_btls_x509_verify_param_new(void)19 mono_btls_x509_verify_param_new (void)
20 {
21 	MonoBtlsX509VerifyParam *param;
22 
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;
30 }
31 
32 MONO_API MonoBtlsX509VerifyParam *
mono_btls_x509_verify_param_from_store_ctx(MonoBtlsX509StoreCtx * ctx,X509_VERIFY_PARAM * param)33 mono_btls_x509_verify_param_from_store_ctx (MonoBtlsX509StoreCtx *ctx, X509_VERIFY_PARAM *param)
34 {
35 	MonoBtlsX509VerifyParam *instance;
36 
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;
44 }
45 
46 MONO_API MonoBtlsX509VerifyParam *
mono_btls_x509_verify_param_copy(const MonoBtlsX509VerifyParam * from)47 mono_btls_x509_verify_param_copy (const MonoBtlsX509VerifyParam *from)
48 {
49 	MonoBtlsX509VerifyParam *param;
50 
51 	param = mono_btls_x509_verify_param_new ();
52 	if (!param)
53 		return NULL;
54 
55 	X509_VERIFY_PARAM_set1 (param->param, from->param);
56 	return param;
57 }
58 
59 MONO_API const X509_VERIFY_PARAM *
mono_btls_x509_verify_param_peek_param(const MonoBtlsX509VerifyParam * param)60 mono_btls_x509_verify_param_peek_param (const MonoBtlsX509VerifyParam *param)
61 {
62 	return param->param;
63 }
64 
65 MONO_API int
mono_btls_x509_verify_param_can_modify(MonoBtlsX509VerifyParam * param)66 mono_btls_x509_verify_param_can_modify (MonoBtlsX509VerifyParam *param)
67 {
68 	return param->owns;
69 }
70 
71 MONO_API MonoBtlsX509VerifyParam *
mono_btls_x509_verify_param_lookup(const char * name)72 mono_btls_x509_verify_param_lookup (const char *name)
73 {
74 	MonoBtlsX509VerifyParam *param;
75 	const X509_VERIFY_PARAM *p;
76 
77 	p = X509_VERIFY_PARAM_lookup(name);
78 	if (!p)
79 		return NULL;
80 
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;
87 }
88 
89 MONO_API void
mono_btls_x509_verify_param_free(MonoBtlsX509VerifyParam * param)90 mono_btls_x509_verify_param_free (MonoBtlsX509VerifyParam *param)
91 {
92 	if (param->owns) {
93 		if (param->param) {
94 			X509_VERIFY_PARAM_free (param->param);
95 			param->param = NULL;
96 		}
97 	}
98 	if (param->owner) {
99 		mono_btls_x509_store_ctx_free (param->owner);
100 		param->owner = NULL;
101 	}
102 	OPENSSL_free (param);
103 }
104 
105 MONO_API int
mono_btls_x509_verify_param_set_name(MonoBtlsX509VerifyParam * param,const char * name)106 mono_btls_x509_verify_param_set_name (MonoBtlsX509VerifyParam *param, const char *name)
107 {
108 	if (!param->owns)
109 		return -1;
110 	return X509_VERIFY_PARAM_set1_name (param->param, name);
111 }
112 
113 MONO_API int
mono_btls_x509_verify_param_set_host(MonoBtlsX509VerifyParam * param,const char * host,int namelen)114 mono_btls_x509_verify_param_set_host (MonoBtlsX509VerifyParam *param, const char *host, int namelen)
115 {
116 	if (!param->owns)
117 		return -1;
118 	return X509_VERIFY_PARAM_set1_host (param->param, host, namelen);
119 }
120 
121 MONO_API int
mono_btls_x509_verify_param_add_host(MonoBtlsX509VerifyParam * param,const char * host,int namelen)122 mono_btls_x509_verify_param_add_host (MonoBtlsX509VerifyParam *param, const char *host, int namelen)
123 {
124 	if (!param->owns)
125 		return -1;
126 	return X509_VERIFY_PARAM_set1_host (param->param, host, namelen);
127 }
128 
129 MONO_API uint64_t
mono_btls_x509_verify_param_get_flags(MonoBtlsX509VerifyParam * param)130 mono_btls_x509_verify_param_get_flags (MonoBtlsX509VerifyParam *param)
131 {
132 	return X509_VERIFY_PARAM_get_flags (param->param);
133 }
134 
135 MONO_API int
mono_btls_x509_verify_param_set_flags(MonoBtlsX509VerifyParam * param,uint64_t flags)136 mono_btls_x509_verify_param_set_flags (MonoBtlsX509VerifyParam *param, uint64_t flags)
137 {
138 	if (!param->owns)
139 		return -1;
140 	return X509_VERIFY_PARAM_set_flags (param->param, flags);
141 }
142 
143 MONO_API MonoBtlsX509VerifyFlags
mono_btls_x509_verify_param_get_mono_flags(MonoBtlsX509VerifyParam * param)144 mono_btls_x509_verify_param_get_mono_flags (MonoBtlsX509VerifyParam *param)
145 {
146 	MonoBtlsX509VerifyFlags current;
147 	uint64_t flags;
148 
149 	if (!param->owns)
150 		return -1;
151 
152 	current = 0;
153 	flags = X509_VERIFY_PARAM_get_flags (param->param);
154 
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;
161 
162 	return current;
163 }
164 
165 MONO_API int
mono_btls_x509_verify_param_set_mono_flags(MonoBtlsX509VerifyParam * param,MonoBtlsX509VerifyFlags flags)166 mono_btls_x509_verify_param_set_mono_flags (MonoBtlsX509VerifyParam *param, MonoBtlsX509VerifyFlags flags)
167 {
168 	uint64_t current;
169 
170 	if (!param->owns)
171 		return -1;
172 
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;
180 
181 	return X509_VERIFY_PARAM_set_flags (param->param, current);
182 }
183 
184 MONO_API int
mono_btls_x509_verify_param_set_purpose(MonoBtlsX509VerifyParam * param,MonoBtlsX509Purpose purpose)185 mono_btls_x509_verify_param_set_purpose (MonoBtlsX509VerifyParam *param, MonoBtlsX509Purpose purpose)
186 {
187 	if (!param->owns)
188 		return -1;
189 	return X509_VERIFY_PARAM_set_purpose (param->param, purpose);
190 }
191 
192 MONO_API int
mono_btls_x509_verify_param_get_depth(MonoBtlsX509VerifyParam * param)193 mono_btls_x509_verify_param_get_depth (MonoBtlsX509VerifyParam *param)
194 {
195 	return X509_VERIFY_PARAM_get_depth (param->param);
196 }
197 
198 MONO_API int
mono_btls_x509_verify_param_set_depth(MonoBtlsX509VerifyParam * param,int depth)199 mono_btls_x509_verify_param_set_depth (MonoBtlsX509VerifyParam *param, int depth)
200 {
201 	if (!param->owns)
202 		return -1;
203 	X509_VERIFY_PARAM_set_depth (param->param, depth);
204 	return 1;
205 }
206 
207 MONO_API int
mono_btls_x509_verify_param_set_time(MonoBtlsX509VerifyParam * param,int64_t time)208 mono_btls_x509_verify_param_set_time (MonoBtlsX509VerifyParam *param, int64_t time)
209 {
210 	if (!param->owns)
211 		return -1;
212 	X509_VERIFY_PARAM_set_time (param->param, time);
213 	return 1;
214 }
215 
216 MONO_API char *
mono_btls_x509_verify_param_get_peername(MonoBtlsX509VerifyParam * param)217 mono_btls_x509_verify_param_get_peername (MonoBtlsX509VerifyParam *param)
218 {
219 	char *peer = X509_VERIFY_PARAM_get0_peername (param->param);
220 	return peer;
221 }
222