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