1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gio.TlsCertificate;
26 
27 private import gio.SocketConnectableIF;
28 private import glib.ConstructionException;
29 private import glib.ErrorG;
30 private import glib.GException;
31 private import glib.ListG;
32 private import glib.Str;
33 private import gobject.ObjectG;
34 private import gtkc.gio;
35 public  import gtkc.giotypes;
36 
37 
38 /**
39  * A certificate used for TLS authentication and encryption.
40  * This can represent either a certificate only (eg, the certificate
41  * received by a client from a server), or the combination of
42  * a certificate and a private key (which is needed when acting as a
43  * #GTlsServerConnection).
44  *
45  * Since: 2.28
46  */
47 public class TlsCertificate : ObjectG
48 {
49 	/** the main Gtk struct */
50 	protected GTlsCertificate* gTlsCertificate;
51 
52 	/** Get the main Gtk struct */
53 	public GTlsCertificate* getTlsCertificateStruct(bool transferOwnership = false)
54 	{
55 		if (transferOwnership)
56 			ownedRef = false;
57 		return gTlsCertificate;
58 	}
59 
60 	/** the main Gtk struct as a void* */
61 	protected override void* getStruct()
62 	{
63 		return cast(void*)gTlsCertificate;
64 	}
65 
66 	protected override void setStruct(GObject* obj)
67 	{
68 		gTlsCertificate = cast(GTlsCertificate*)obj;
69 		super.setStruct(obj);
70 	}
71 
72 	/**
73 	 * Sets our main struct and passes it to the parent class.
74 	 */
75 	public this (GTlsCertificate* gTlsCertificate, bool ownedRef = false)
76 	{
77 		this.gTlsCertificate = gTlsCertificate;
78 		super(cast(GObject*)gTlsCertificate, ownedRef);
79 	}
80 
81 
82 	/** */
83 	public static GType getType()
84 	{
85 		return g_tls_certificate_get_type();
86 	}
87 
88 	/**
89 	 * Creates a #GTlsCertificate from the PEM-encoded data in @file. The
90 	 * returned certificate will be the first certificate found in @file. As
91 	 * of GLib 2.44, if @file contains more certificates it will try to load
92 	 * a certificate chain. All certificates will be verified in the order
93 	 * found (top-level certificate should be the last one in the file) and
94 	 * the #GTlsCertificate:issuer property of each certificate will be set
95 	 * accordingly if the verification succeeds. If any certificate in the
96 	 * chain cannot be verified, the first certificate in the file will
97 	 * still be returned.
98 	 *
99 	 * If @file cannot be read or parsed, the function will return %NULL and
100 	 * set @error. Otherwise, this behaves like
101 	 * g_tls_certificate_new_from_pem().
102 	 *
103 	 * Params:
104 	 *     file = file containing a PEM-encoded certificate to import
105 	 *
106 	 * Returns: the new certificate, or %NULL on error
107 	 *
108 	 * Since: 2.28
109 	 *
110 	 * Throws: GException on failure.
111 	 * Throws: ConstructionException GTK+ fails to create the object.
112 	 */
113 	public this(string file)
114 	{
115 		GError* err = null;
116 		
117 		auto p = g_tls_certificate_new_from_file(Str.toStringz(file), &err);
118 		
119 		if (err !is null)
120 		{
121 			throw new GException( new ErrorG(err) );
122 		}
123 		
124 		if(p is null)
125 		{
126 			throw new ConstructionException("null returned by new_from_file");
127 		}
128 		
129 		this(cast(GTlsCertificate*) p, true);
130 	}
131 
132 	/**
133 	 * Creates a #GTlsCertificate from the PEM-encoded data in @cert_file
134 	 * and @key_file. The returned certificate will be the first certificate
135 	 * found in @cert_file. As of GLib 2.44, if @cert_file contains more
136 	 * certificates it will try to load a certificate chain. All
137 	 * certificates will be verified in the order found (top-level
138 	 * certificate should be the last one in the file) and the
139 	 * #GTlsCertificate:issuer property of each certificate will be set
140 	 * accordingly if the verification succeeds. If any certificate in the
141 	 * chain cannot be verified, the first certificate in the file will
142 	 * still be returned.
143 	 *
144 	 * If either file cannot be read or parsed, the function will return
145 	 * %NULL and set @error. Otherwise, this behaves like
146 	 * g_tls_certificate_new_from_pem().
147 	 *
148 	 * Params:
149 	 *     certFile = file containing one or more PEM-encoded
150 	 *         certificates to import
151 	 *     keyFile = file containing a PEM-encoded private key
152 	 *         to import
153 	 *
154 	 * Returns: the new certificate, or %NULL on error
155 	 *
156 	 * Since: 2.28
157 	 *
158 	 * Throws: GException on failure.
159 	 * Throws: ConstructionException GTK+ fails to create the object.
160 	 */
161 	public this(string certFile, string keyFile)
162 	{
163 		GError* err = null;
164 		
165 		auto p = g_tls_certificate_new_from_files(Str.toStringz(certFile), Str.toStringz(keyFile), &err);
166 		
167 		if (err !is null)
168 		{
169 			throw new GException( new ErrorG(err) );
170 		}
171 		
172 		if(p is null)
173 		{
174 			throw new ConstructionException("null returned by new_from_files");
175 		}
176 		
177 		this(cast(GTlsCertificate*) p, true);
178 	}
179 
180 	/**
181 	 * Creates a #GTlsCertificate from the PEM-encoded data in @data. If
182 	 * @data includes both a certificate and a private key, then the
183 	 * returned certificate will include the private key data as well. (See
184 	 * the #GTlsCertificate:private-key-pem property for information about
185 	 * supported formats.)
186 	 *
187 	 * The returned certificate will be the first certificate found in
188 	 * @data. As of GLib 2.44, if @data contains more certificates it will
189 	 * try to load a certificate chain. All certificates will be verified in
190 	 * the order found (top-level certificate should be the last one in the
191 	 * file) and the #GTlsCertificate:issuer property of each certificate
192 	 * will be set accordingly if the verification succeeds. If any
193 	 * certificate in the chain cannot be verified, the first certificate in
194 	 * the file will still be returned.
195 	 *
196 	 * Params:
197 	 *     data = PEM-encoded certificate data
198 	 *     length = the length of @data, or -1 if it's 0-terminated.
199 	 *
200 	 * Returns: the new certificate, or %NULL if @data is invalid
201 	 *
202 	 * Since: 2.28
203 	 *
204 	 * Throws: GException on failure.
205 	 * Throws: ConstructionException GTK+ fails to create the object.
206 	 */
207 	public this(string data, ptrdiff_t length)
208 	{
209 		GError* err = null;
210 		
211 		auto p = g_tls_certificate_new_from_pem(Str.toStringz(data), length, &err);
212 		
213 		if (err !is null)
214 		{
215 			throw new GException( new ErrorG(err) );
216 		}
217 		
218 		if(p is null)
219 		{
220 			throw new ConstructionException("null returned by new_from_pem");
221 		}
222 		
223 		this(cast(GTlsCertificate*) p, true);
224 	}
225 
226 	/**
227 	 * Creates one or more #GTlsCertificates from the PEM-encoded
228 	 * data in @file. If @file cannot be read or parsed, the function will
229 	 * return %NULL and set @error. If @file does not contain any
230 	 * PEM-encoded certificates, this will return an empty list and not
231 	 * set @error.
232 	 *
233 	 * Params:
234 	 *     file = file containing PEM-encoded certificates to import
235 	 *
236 	 * Returns: a
237 	 *     #GList containing #GTlsCertificate objects. You must free the list
238 	 *     and its contents when you are done with it.
239 	 *
240 	 * Since: 2.28
241 	 *
242 	 * Throws: GException on failure.
243 	 */
244 	public static ListG listNewFromFile(string file)
245 	{
246 		GError* err = null;
247 		
248 		auto p = g_tls_certificate_list_new_from_file(Str.toStringz(file), &err);
249 		
250 		if (err !is null)
251 		{
252 			throw new GException( new ErrorG(err) );
253 		}
254 		
255 		if(p is null)
256 		{
257 			return null;
258 		}
259 		
260 		return new ListG(cast(GList*) p, true);
261 	}
262 
263 	/**
264 	 * Gets the #GTlsCertificate representing @cert's issuer, if known
265 	 *
266 	 * Returns: The certificate of @cert's issuer,
267 	 *     or %NULL if @cert is self-signed or signed with an unknown
268 	 *     certificate.
269 	 *
270 	 * Since: 2.28
271 	 */
272 	public TlsCertificate getIssuer()
273 	{
274 		auto p = g_tls_certificate_get_issuer(gTlsCertificate);
275 		
276 		if(p is null)
277 		{
278 			return null;
279 		}
280 		
281 		return ObjectG.getDObject!(TlsCertificate)(cast(GTlsCertificate*) p);
282 	}
283 
284 	/**
285 	 * Check if two #GTlsCertificate objects represent the same certificate.
286 	 * The raw DER byte data of the two certificates are checked for equality.
287 	 * This has the effect that two certificates may compare equal even if
288 	 * their #GTlsCertificate:issuer, #GTlsCertificate:private-key, or
289 	 * #GTlsCertificate:private-key-pem properties differ.
290 	 *
291 	 * Params:
292 	 *     certTwo = second certificate to compare
293 	 *
294 	 * Returns: whether the same or not
295 	 *
296 	 * Since: 2.34
297 	 */
298 	public bool isSame(TlsCertificate certTwo)
299 	{
300 		return g_tls_certificate_is_same(gTlsCertificate, (certTwo is null) ? null : certTwo.getTlsCertificateStruct()) != 0;
301 	}
302 
303 	/**
304 	 * This verifies @cert and returns a set of #GTlsCertificateFlags
305 	 * indicating any problems found with it. This can be used to verify a
306 	 * certificate outside the context of making a connection, or to
307 	 * check a certificate against a CA that is not part of the system
308 	 * CA database.
309 	 *
310 	 * If @identity is not %NULL, @cert's name(s) will be compared against
311 	 * it, and %G_TLS_CERTIFICATE_BAD_IDENTITY will be set in the return
312 	 * value if it does not match. If @identity is %NULL, that bit will
313 	 * never be set in the return value.
314 	 *
315 	 * If @trusted_ca is not %NULL, then @cert (or one of the certificates
316 	 * in its chain) must be signed by it, or else
317 	 * %G_TLS_CERTIFICATE_UNKNOWN_CA will be set in the return value. If
318 	 * @trusted_ca is %NULL, that bit will never be set in the return
319 	 * value.
320 	 *
321 	 * (All other #GTlsCertificateFlags values will always be set or unset
322 	 * as appropriate.)
323 	 *
324 	 * Params:
325 	 *     identity = the expected peer identity
326 	 *     trustedCa = the certificate of a trusted authority
327 	 *
328 	 * Returns: the appropriate #GTlsCertificateFlags
329 	 *
330 	 * Since: 2.28
331 	 */
332 	public GTlsCertificateFlags verify(SocketConnectableIF identity, TlsCertificate trustedCa)
333 	{
334 		return g_tls_certificate_verify(gTlsCertificate, (identity is null) ? null : identity.getSocketConnectableStruct(), (trustedCa is null) ? null : trustedCa.getTlsCertificateStruct());
335 	}
336 }