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 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 /** 67 * Sets our main struct and passes it to the parent class. 68 */ 69 public this (GTlsCertificate* gTlsCertificate, bool ownedRef = false) 70 { 71 this.gTlsCertificate = gTlsCertificate; 72 super(cast(GObject*)gTlsCertificate, ownedRef); 73 } 74 75 /** 76 * Creates a #GTlsCertificate from a PKCS \#11 URI. 77 * 78 * An example @pkcs11_uri would be `pkcs11:model=Model;manufacturer=Manufacture;serial=1;token=My%20Client%20Certificate;id=%01` 79 * 80 * Where the token’s layout is: 81 * 82 * ``` 83 * Object 0: 84 * URL: pkcs11:model=Model;manufacturer=Manufacture;serial=1;token=My%20Client%20Certificate;id=%01;object=private%20key;type=private 85 * Type: Private key (RSA-2048) 86 * ID: 01 87 * 88 * Object 1: 89 * URL: pkcs11:model=Model;manufacturer=Manufacture;serial=1;token=My%20Client%20Certificate;id=%01;object=Certificate%20for%20Authentication;type=cert 90 * Type: X.509 Certificate (RSA-2048) 91 * ID: 01 92 * ``` 93 * 94 * In this case the certificate and private key would both be detected and used as expected. 95 * @pkcs_uri may also just reference an X.509 certificate object and then optionally 96 * @private_key_pkcs11_uri allows using a private key exposed under a different URI. 97 * 98 * Note that the private key is not accessed until usage and may fail or require a PIN later. 99 * 100 * Params: 101 * pkcs11Uri = A PKCS \#11 URI 102 * privateKeyPkcs11Uri = A PKCS \#11 URI 103 * 104 * Returns: the new certificate, or %NULL on error 105 * 106 * Since: 2.68 107 * 108 * Throws: GException on failure. 109 * Throws: ConstructionException GTK+ fails to create the object. 110 */ 111 public static TlsCertificate newFromPkcs11Uris(string pkcs11Uri, string privateKeyPkcs11Uri) 112 { 113 GError* err = null; 114 115 auto __p = g_tls_certificate_new_from_pkcs11_uris(Str.toStringz(pkcs11Uri), Str.toStringz(privateKeyPkcs11Uri), &err); 116 117 if (err !is null) 118 { 119 throw new GException( new ErrorG(err) ); 120 } 121 122 if(__p is null) 123 { 124 throw new ConstructionException("null returned by new_from_pkcs11_uris"); 125 } 126 127 return new TlsCertificate(cast(GTlsCertificate*) __p, true); 128 } 129 130 /** 131 */ 132 133 /** */ 134 public static GType getType() 135 { 136 return g_tls_certificate_get_type(); 137 } 138 139 /** 140 * Creates a #GTlsCertificate from the PEM-encoded data in @file. The 141 * returned certificate will be the first certificate found in @file. As 142 * of GLib 2.44, if @file contains more certificates it will try to load 143 * a certificate chain. All certificates will be verified in the order 144 * found (top-level certificate should be the last one in the file) and 145 * the #GTlsCertificate:issuer property of each certificate will be set 146 * accordingly if the verification succeeds. If any certificate in the 147 * chain cannot be verified, the first certificate in the file will 148 * still be returned. 149 * 150 * If @file cannot be read or parsed, the function will return %NULL and 151 * set @error. Otherwise, this behaves like 152 * g_tls_certificate_new_from_pem(). 153 * 154 * Params: 155 * file = file containing a PEM-encoded certificate to import 156 * 157 * Returns: the new certificate, or %NULL on error 158 * 159 * Since: 2.28 160 * 161 * Throws: GException on failure. 162 * Throws: ConstructionException GTK+ fails to create the object. 163 */ 164 public this(string file) 165 { 166 GError* err = null; 167 168 auto __p = g_tls_certificate_new_from_file(Str.toStringz(file), &err); 169 170 if (err !is null) 171 { 172 throw new GException( new ErrorG(err) ); 173 } 174 175 if(__p is null) 176 { 177 throw new ConstructionException("null returned by new_from_file"); 178 } 179 180 this(cast(GTlsCertificate*) __p, true); 181 } 182 183 /** 184 * Creates a #GTlsCertificate from the PEM-encoded data in @cert_file 185 * and @key_file. The returned certificate will be the first certificate 186 * found in @cert_file. As of GLib 2.44, if @cert_file contains more 187 * certificates it will try to load a certificate chain. All 188 * certificates will be verified in the order found (top-level 189 * certificate should be the last one in the file) and the 190 * #GTlsCertificate:issuer property of each certificate will be set 191 * accordingly if the verification succeeds. If any certificate in the 192 * chain cannot be verified, the first certificate in the file will 193 * still be returned. 194 * 195 * If either file cannot be read or parsed, the function will return 196 * %NULL and set @error. Otherwise, this behaves like 197 * g_tls_certificate_new_from_pem(). 198 * 199 * Params: 200 * certFile = file containing one or more PEM-encoded 201 * certificates to import 202 * keyFile = file containing a PEM-encoded private key 203 * to import 204 * 205 * Returns: the new certificate, or %NULL on error 206 * 207 * Since: 2.28 208 * 209 * Throws: GException on failure. 210 * Throws: ConstructionException GTK+ fails to create the object. 211 */ 212 public this(string certFile, string keyFile) 213 { 214 GError* err = null; 215 216 auto __p = g_tls_certificate_new_from_files(Str.toStringz(certFile), Str.toStringz(keyFile), &err); 217 218 if (err !is null) 219 { 220 throw new GException( new ErrorG(err) ); 221 } 222 223 if(__p is null) 224 { 225 throw new ConstructionException("null returned by new_from_files"); 226 } 227 228 this(cast(GTlsCertificate*) __p, true); 229 } 230 231 /** 232 * Creates a #GTlsCertificate from the PEM-encoded data in @data. If 233 * @data includes both a certificate and a private key, then the 234 * returned certificate will include the private key data as well. (See 235 * the #GTlsCertificate:private-key-pem property for information about 236 * supported formats.) 237 * 238 * The returned certificate will be the first certificate found in 239 * @data. As of GLib 2.44, if @data contains more certificates it will 240 * try to load a certificate chain. All certificates will be verified in 241 * the order found (top-level certificate should be the last one in the 242 * file) and the #GTlsCertificate:issuer property of each certificate 243 * will be set accordingly if the verification succeeds. If any 244 * certificate in the chain cannot be verified, the first certificate in 245 * the file will still be returned. 246 * 247 * Params: 248 * data = PEM-encoded certificate data 249 * length = the length of @data, or -1 if it's 0-terminated. 250 * 251 * Returns: the new certificate, or %NULL if @data is invalid 252 * 253 * Since: 2.28 254 * 255 * Throws: GException on failure. 256 * Throws: ConstructionException GTK+ fails to create the object. 257 */ 258 public this(string data, ptrdiff_t length) 259 { 260 GError* err = null; 261 262 auto __p = g_tls_certificate_new_from_pem(Str.toStringz(data), length, &err); 263 264 if (err !is null) 265 { 266 throw new GException( new ErrorG(err) ); 267 } 268 269 if(__p is null) 270 { 271 throw new ConstructionException("null returned by new_from_pem"); 272 } 273 274 this(cast(GTlsCertificate*) __p, true); 275 } 276 277 /** 278 * Creates one or more #GTlsCertificates from the PEM-encoded 279 * data in @file. If @file cannot be read or parsed, the function will 280 * return %NULL and set @error. If @file does not contain any 281 * PEM-encoded certificates, this will return an empty list and not 282 * set @error. 283 * 284 * Params: 285 * file = file containing PEM-encoded certificates to import 286 * 287 * Returns: a 288 * #GList containing #GTlsCertificate objects. You must free the list 289 * and its contents when you are done with it. 290 * 291 * Since: 2.28 292 * 293 * Throws: GException on failure. 294 */ 295 public static ListG listNewFromFile(string file) 296 { 297 GError* err = null; 298 299 auto __p = g_tls_certificate_list_new_from_file(Str.toStringz(file), &err); 300 301 if (err !is null) 302 { 303 throw new GException( new ErrorG(err) ); 304 } 305 306 if(__p is null) 307 { 308 return null; 309 } 310 311 return new ListG(cast(GList*) __p, true); 312 } 313 314 /** 315 * Gets the #GTlsCertificate representing @cert's issuer, if known 316 * 317 * Returns: The certificate of @cert's issuer, 318 * or %NULL if @cert is self-signed or signed with an unknown 319 * certificate. 320 * 321 * Since: 2.28 322 */ 323 public TlsCertificate getIssuer() 324 { 325 auto __p = g_tls_certificate_get_issuer(gTlsCertificate); 326 327 if(__p is null) 328 { 329 return null; 330 } 331 332 return ObjectG.getDObject!(TlsCertificate)(cast(GTlsCertificate*) __p); 333 } 334 335 /** 336 * Check if two #GTlsCertificate objects represent the same certificate. 337 * The raw DER byte data of the two certificates are checked for equality. 338 * This has the effect that two certificates may compare equal even if 339 * their #GTlsCertificate:issuer, #GTlsCertificate:private-key, or 340 * #GTlsCertificate:private-key-pem properties differ. 341 * 342 * Params: 343 * certTwo = second certificate to compare 344 * 345 * Returns: whether the same or not 346 * 347 * Since: 2.34 348 */ 349 public bool isSame(TlsCertificate certTwo) 350 { 351 return g_tls_certificate_is_same(gTlsCertificate, (certTwo is null) ? null : certTwo.getTlsCertificateStruct()) != 0; 352 } 353 354 /** 355 * This verifies @cert and returns a set of #GTlsCertificateFlags 356 * indicating any problems found with it. This can be used to verify a 357 * certificate outside the context of making a connection, or to 358 * check a certificate against a CA that is not part of the system 359 * CA database. 360 * 361 * If @identity is not %NULL, @cert's name(s) will be compared against 362 * it, and %G_TLS_CERTIFICATE_BAD_IDENTITY will be set in the return 363 * value if it does not match. If @identity is %NULL, that bit will 364 * never be set in the return value. 365 * 366 * If @trusted_ca is not %NULL, then @cert (or one of the certificates 367 * in its chain) must be signed by it, or else 368 * %G_TLS_CERTIFICATE_UNKNOWN_CA will be set in the return value. If 369 * @trusted_ca is %NULL, that bit will never be set in the return 370 * value. 371 * 372 * (All other #GTlsCertificateFlags values will always be set or unset 373 * as appropriate.) 374 * 375 * Params: 376 * identity = the expected peer identity 377 * trustedCa = the certificate of a trusted authority 378 * 379 * Returns: the appropriate #GTlsCertificateFlags 380 * 381 * Since: 2.28 382 */ 383 public GTlsCertificateFlags verify(SocketConnectableIF identity, TlsCertificate trustedCa) 384 { 385 return g_tls_certificate_verify(gTlsCertificate, (identity is null) ? null : identity.getSocketConnectableStruct(), (trustedCa is null) ? null : trustedCa.getTlsCertificateStruct()); 386 } 387 }