1 /* GIO - GLib Input, Output and Certificateing Library
3 * Copyright (C) 2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library 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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
23 #include "gtlscertificate.h"
26 #include "ginitable.h"
27 #include "gtlsbackend.h"
28 #include "gtlsconnection.h"
32 * SECTION:gtlscertificate
33 * @title: GTlsCertificate
34 * @short_description: TLS certificate
35 * @see_also: #GTlsConnection
37 * A certificate used for TLS authentication and encryption.
38 * This can represent either a public key only (eg, the certificate
39 * received by a client from a server), or the combination of
40 * a public key and a private key (which is needed when acting as a
41 * #GTlsServerConnection).
49 * Abstract base class for TLS certificate types.
54 G_DEFINE_ABSTRACT_TYPE (GTlsCertificate, g_tls_certificate, G_TYPE_OBJECT);
68 g_tls_certificate_init (GTlsCertificate *cert)
73 g_tls_certificate_get_property (GObject *object,
78 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
82 g_tls_certificate_set_property (GObject *object,
87 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
91 g_tls_certificate_class_init (GTlsCertificateClass *class)
93 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
95 gobject_class->set_property = g_tls_certificate_set_property;
96 gobject_class->get_property = g_tls_certificate_get_property;
99 * GTlsCertificate:certificate:
101 * The DER (binary) encoded representation of the certificate's
102 * public key. This property and the
103 * #GTlsCertificate:certificate-pem property represent the same
104 * data, just in different forms.
108 g_object_class_install_property (gobject_class, PROP_CERTIFICATE,
109 g_param_spec_boxed ("certificate",
111 P_("The DER representation of the certificate"),
114 G_PARAM_CONSTRUCT_ONLY |
115 G_PARAM_STATIC_STRINGS));
117 * GTlsCertificate:certificate-pem:
119 * The PEM (ASCII) encoded representation of the certificate's
120 * public key. This property and the #GTlsCertificate:certificate
121 * property represent the same data, just in different forms.
125 g_object_class_install_property (gobject_class, PROP_CERTIFICATE_PEM,
126 g_param_spec_string ("certificate-pem",
127 P_("Certificate (PEM)"),
128 P_("The PEM representation of the certificate"),
131 G_PARAM_CONSTRUCT_ONLY |
132 G_PARAM_STATIC_STRINGS));
134 * GTlsCertificate:private-key:
136 * The DER (binary) encoded representation of the certificate's
137 * private key. This property (or the
138 * #GTlsCertificate:private-key-pem property) can be set when
139 * constructing a key (eg, from a file), but cannot be read.
143 g_object_class_install_property (gobject_class, PROP_PRIVATE_KEY,
144 g_param_spec_boxed ("private-key",
146 P_("The DER representation of the certificate's private key"),
149 G_PARAM_CONSTRUCT_ONLY |
150 G_PARAM_STATIC_STRINGS));
152 * GTlsCertificate:private-key-pem:
154 * The PEM (ASCII) encoded representation of the certificate's
155 * private key. This property (or the #GTlsCertificate:private-key
156 * property) can be set when constructing a key (eg, from a file),
157 * but cannot be read.
161 g_object_class_install_property (gobject_class, PROP_PRIVATE_KEY_PEM,
162 g_param_spec_string ("private-key-pem",
163 P_("Private key (PEM)"),
164 P_("The PEM representation of the certificate's private key"),
167 G_PARAM_CONSTRUCT_ONLY |
168 G_PARAM_STATIC_STRINGS));
170 * GTlsCertificate:issuer:
172 * A #GTlsCertificate representing the entity that issued this
173 * certificate. If %NULL, this means that the certificate is either
174 * self-signed, or else the certificate of the issuer is not
179 g_object_class_install_property (gobject_class, PROP_ISSUER,
180 g_param_spec_object ("issuer",
182 P_("The certificate for the issuing entity"),
183 G_TYPE_TLS_CERTIFICATE,
185 G_PARAM_CONSTRUCT_ONLY |
186 G_PARAM_STATIC_STRINGS));
189 static GTlsCertificate *
190 g_tls_certificate_new_internal (const gchar *certificate_pem,
191 const gchar *private_key_pem,
195 GTlsBackend *backend;
197 backend = g_tls_backend_get_default ();
199 cert = g_initable_new (g_tls_backend_get_certificate_type (backend),
201 "certificate-pem", certificate_pem,
202 "private-key-pem", private_key_pem,
204 return G_TLS_CERTIFICATE (cert);
207 #define PEM_CERTIFICATE_HEADER "-----BEGIN CERTIFICATE-----"
208 #define PEM_CERTIFICATE_FOOTER "-----END CERTIFICATE-----"
209 #define PEM_PRIVKEY_HEADER "-----BEGIN RSA PRIVATE KEY-----"
210 #define PEM_PRIVKEY_FOOTER "-----END RSA PRIVATE KEY-----"
213 parse_private_key (const gchar *data,
218 const gchar *start, *end;
220 start = g_strstr_len (data, data_len, PEM_PRIVKEY_HEADER);
225 g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
226 _("No PEM-encoded private key found"));
231 end = g_strstr_len (start, data_len - (data - start), PEM_PRIVKEY_FOOTER);
234 g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
235 _("Could not parse PEM-encoded private key"));
238 end += strlen (PEM_PRIVKEY_FOOTER);
239 while (*end == '\r' || *end == '\n')
242 return g_strndup (start, end - start);
247 parse_next_pem_certificate (const gchar **data,
248 const gchar *data_end,
252 const gchar *start, *end;
254 start = g_strstr_len (*data, data_end - *data, PEM_CERTIFICATE_HEADER);
259 g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
260 _("No PEM-encoded certificate found"));
265 end = g_strstr_len (start, data_end - start, PEM_CERTIFICATE_FOOTER);
268 g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
269 _("Could not parse PEM-encoded certificate"));
272 end += strlen (PEM_CERTIFICATE_FOOTER);
273 while (*end == '\r' || *end == '\n')
278 return g_strndup (start, end - start);
282 * g_tls_certificate_new_from_pem:
283 * @data: PEM-encoded certificate data
284 * @length: the length of @data, or -1 if it's 0-terminated.
285 * @error: #GError for error reporting, or %NULL to ignore.
287 * Creates a new #GTlsCertificate from the PEM-encoded data in @data.
288 * If @data includes both a certificate and a private key, then the
289 * returned certificate will include the private key data as well.
291 * If @data includes multiple certificates, only the first one will be
294 * Return value: the new certificate, or %NULL if @data is invalid
299 g_tls_certificate_new_from_pem (const gchar *data,
303 const gchar *data_end;
304 gchar *key_pem, *cert_pem;
305 GTlsCertificate *cert;
307 g_return_val_if_fail (data != NULL, NULL);
310 length = strlen (data);
312 data_end = data + length;
314 key_pem = parse_private_key (data, length, FALSE, error);
318 cert_pem = parse_next_pem_certificate (&data, data_end, TRUE, error);
325 cert = g_tls_certificate_new_internal (cert_pem, key_pem, error);
333 * g_tls_certificate_new_from_file:
334 * @file: file containing a PEM-encoded certificate to import
335 * @error: #GError for error reporting, or %NULL to ignore.
337 * Creates a #GTlsCertificate from the PEM-encoded data in @file. If
338 * @file cannot be read or parsed, the function will return %NULL and
339 * set @error. Otherwise, this behaves like g_tls_certificate_new().
341 * Return value: the new certificate, or %NULL on error
346 g_tls_certificate_new_from_file (const gchar *file,
349 GTlsCertificate *cert;
353 if (!g_file_get_contents (file, &contents, &length, error))
356 cert = g_tls_certificate_new_from_pem (contents, length, error);
362 * g_tls_certificate_new_from_files:
363 * @cert_file: file containing a PEM-encoded certificate to import
364 * @key_file: file containing a PEM-encoded private key to import
365 * @error: #GError for error reporting, or %NULL to ignore.
367 * Creates a #GTlsCertificate from the PEM-encoded data in @cert_file
368 * and @key_file. If either file cannot be read or parsed, the
369 * function will return %NULL and set @error. Otherwise, this behaves
370 * like g_tls_certificate_new().
372 * Return value: the new certificate, or %NULL on error
377 g_tls_certificate_new_from_files (const gchar *cert_file,
378 const gchar *key_file,
381 GTlsCertificate *cert;
382 gchar *cert_data, *key_data;
383 gsize cert_len, key_len;
384 gchar *cert_pem, *key_pem;
387 if (!g_file_get_contents (cert_file, &cert_data, &cert_len, error))
390 cert_pem = parse_next_pem_certificate (&p, p + cert_len, TRUE, error);
395 if (!g_file_get_contents (key_file, &key_data, &key_len, error))
400 key_pem = parse_private_key (key_data, key_len, TRUE, error);
408 cert = g_tls_certificate_new_internal (cert_pem, key_pem, error);
415 * g_tls_certificate_list_new_from_file:
416 * @file: file containing PEM-encoded certificates to import
417 * @error: #GError for error reporting, or %NULL to ignore.
419 * Creates one or more #GTlsCertificate<!-- -->s from the PEM-encoded
420 * data in @file. If @file cannot be read or parsed, the function will
421 * return %NULL and set @error. If @file does not contain any
422 * PEM-encoded certificates, this will return an empty list and not
425 * Return value: (element-type Gio.TlsCertificate) (transfer full): a
426 * #GList containing #GTlsCertificate objects. You must free the list
427 * and its contents when you are done with it.
432 g_tls_certificate_list_new_from_file (const gchar *file,
435 GQueue queue = G_QUEUE_INIT;
436 gchar *contents, *end;
440 if (!g_file_get_contents (file, &contents, &length, error))
443 end = contents + length;
448 GTlsCertificate *cert = NULL;
450 cert_pem = parse_next_pem_certificate (&p, end, FALSE, error);
453 cert = g_tls_certificate_new_internal (cert_pem, NULL, error);
458 g_list_free_full (queue.head, g_object_unref);
462 g_queue_push_tail (&queue, cert);
471 * g_tls_certificate_get_issuer:
472 * @cert: a #GTlsCertificate
474 * Gets the #GTlsCertificate representing @cert's issuer, if known
476 * Return value: (transfer none): The certificate of @cert's issuer,
477 * or %NULL if @cert is self-signed or signed with an unknown
483 g_tls_certificate_get_issuer (GTlsCertificate *cert)
485 GTlsCertificate *issuer;
487 g_object_get (G_OBJECT (cert), "issuer", &issuer, NULL);
489 g_object_unref (issuer);
495 * g_tls_certificate_verify:
496 * @cert: a #GTlsCertificate
497 * @identity: (allow-none): the expected peer identity
498 * @trusted_ca: (allow-none): the certificate of a trusted authority
500 * This verifies @cert and returns a set of #GTlsCertificateFlags
501 * indicating any problems found with it. This can be used to verify a
502 * certificate outside the context of making a connection, or to
503 * check a certificate against a CA that is not part of the system
506 * If @identity is not %NULL, @cert's name(s) will be compared against
507 * it, and %G_TLS_CERTIFICATE_BAD_IDENTITY will be set in the return
508 * value if it does not match. If @identity is %NULL, that bit will
509 * never be set in the return value.
511 * If @trusted_ca is not %NULL, then @cert (or one of the certificates
512 * in its chain) must be signed by it, or else
513 * %G_TLS_CERTIFICATE_UNKNOWN_CA will be set in the return value. If
514 * @trusted_ca is %NULL, that bit will never be set in the return
517 * (All other #GTlsCertificateFlags values will always be set or unset
520 * Return value: the appropriate #GTlsCertificateFlags
525 g_tls_certificate_verify (GTlsCertificate *cert,
526 GSocketConnectable *identity,
527 GTlsCertificate *trusted_ca)
529 return G_TLS_CERTIFICATE_GET_CLASS (cert)->verify (cert, identity, trusted_ca);