2009-12-21 20:50:32 +01:00
|
|
|
|
/* GIO - GLib Input, Output and Certificateing Library
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2010 Red Hat, Inc.
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
2017-05-27 18:21:30 +02:00
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2009-12-21 20:50:32 +01:00
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General
|
2014-01-23 12:58:29 +01:00
|
|
|
|
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2009-12-21 20:50:32 +01:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
|
#include "gtlscertificate.h"
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include "ginitable.h"
|
|
|
|
|
#include "gtlsbackend.h"
|
|
|
|
|
#include "gtlsconnection.h"
|
|
|
|
|
#include "glibintl.h"
|
|
|
|
|
|
|
|
|
|
/**
|
2011-02-01 19:17:23 +01:00
|
|
|
|
* SECTION:gtlscertificate
|
2009-12-21 20:50:32 +01:00
|
|
|
|
* @title: GTlsCertificate
|
2010-11-29 05:55:43 +01:00
|
|
|
|
* @short_description: TLS certificate
|
2014-01-08 04:55:43 +01:00
|
|
|
|
* @include: gio/gio.h
|
2009-12-21 20:50:32 +01:00
|
|
|
|
* @see_also: #GTlsConnection
|
|
|
|
|
*
|
|
|
|
|
* A certificate used for TLS authentication and encryption.
|
2012-08-03 21:51:08 +02:00
|
|
|
|
* This can represent either a certificate only (eg, the certificate
|
2009-12-21 20:50:32 +01:00
|
|
|
|
* received by a client from a server), or the combination of
|
2012-08-03 21:51:08 +02:00
|
|
|
|
* a certificate and a private key (which is needed when acting as a
|
2009-12-21 20:50:32 +01:00
|
|
|
|
* #GTlsServerConnection).
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.28
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* GTlsCertificate:
|
|
|
|
|
*
|
|
|
|
|
* Abstract base class for TLS certificate types.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.28
|
|
|
|
|
*/
|
|
|
|
|
|
2014-10-17 12:54:02 +02:00
|
|
|
|
G_DEFINE_ABSTRACT_TYPE (GTlsCertificate, g_tls_certificate, G_TYPE_OBJECT)
|
2009-12-21 20:50:32 +01:00
|
|
|
|
|
|
|
|
|
enum
|
|
|
|
|
{
|
|
|
|
|
PROP_0,
|
|
|
|
|
|
|
|
|
|
PROP_CERTIFICATE,
|
|
|
|
|
PROP_CERTIFICATE_PEM,
|
|
|
|
|
PROP_PRIVATE_KEY,
|
|
|
|
|
PROP_PRIVATE_KEY_PEM,
|
|
|
|
|
PROP_ISSUER
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
g_tls_certificate_init (GTlsCertificate *cert)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
g_tls_certificate_get_property (GObject *object,
|
|
|
|
|
guint prop_id,
|
|
|
|
|
GValue *value,
|
|
|
|
|
GParamSpec *pspec)
|
|
|
|
|
{
|
2010-12-01 01:57:16 +01:00
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
2009-12-21 20:50:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
g_tls_certificate_set_property (GObject *object,
|
|
|
|
|
guint prop_id,
|
|
|
|
|
const GValue *value,
|
|
|
|
|
GParamSpec *pspec)
|
|
|
|
|
{
|
2010-12-01 01:57:16 +01:00
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
2009-12-21 20:50:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
g_tls_certificate_class_init (GTlsCertificateClass *class)
|
|
|
|
|
{
|
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
|
|
|
|
|
|
|
|
|
|
gobject_class->set_property = g_tls_certificate_set_property;
|
|
|
|
|
gobject_class->get_property = g_tls_certificate_get_property;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* GTlsCertificate:certificate:
|
|
|
|
|
*
|
2012-08-03 21:51:08 +02:00
|
|
|
|
* The DER (binary) encoded representation of the certificate.
|
2012-08-21 13:26:27 +02:00
|
|
|
|
* This property and the #GTlsCertificate:certificate-pem property
|
|
|
|
|
* represent the same data, just in different forms.
|
2009-12-21 20:50:32 +01:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.28
|
|
|
|
|
*/
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_CERTIFICATE,
|
|
|
|
|
g_param_spec_boxed ("certificate",
|
|
|
|
|
P_("Certificate"),
|
|
|
|
|
P_("The DER representation of the certificate"),
|
|
|
|
|
G_TYPE_BYTE_ARRAY,
|
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
|
G_PARAM_CONSTRUCT_ONLY |
|
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
|
|
|
|
/**
|
|
|
|
|
* GTlsCertificate:certificate-pem:
|
|
|
|
|
*
|
2012-08-03 21:51:08 +02:00
|
|
|
|
* The PEM (ASCII) encoded representation of the certificate.
|
2012-08-21 13:26:27 +02:00
|
|
|
|
* This property and the #GTlsCertificate:certificate
|
|
|
|
|
* property represent the same data, just in different forms.
|
2009-12-21 20:50:32 +01:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.28
|
|
|
|
|
*/
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_CERTIFICATE_PEM,
|
|
|
|
|
g_param_spec_string ("certificate-pem",
|
|
|
|
|
P_("Certificate (PEM)"),
|
|
|
|
|
P_("The PEM representation of the certificate"),
|
|
|
|
|
NULL,
|
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
|
G_PARAM_CONSTRUCT_ONLY |
|
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
|
|
|
|
/**
|
|
|
|
|
* GTlsCertificate:private-key:
|
|
|
|
|
*
|
|
|
|
|
* The DER (binary) encoded representation of the certificate's
|
2011-11-18 21:05:34 +01:00
|
|
|
|
* private key, in either PKCS#1 format or unencrypted PKCS#8
|
|
|
|
|
* format. This property (or the #GTlsCertificate:private-key-pem
|
|
|
|
|
* property) can be set when constructing a key (eg, from a file),
|
|
|
|
|
* but cannot be read.
|
|
|
|
|
*
|
|
|
|
|
* PKCS#8 format is supported since 2.32; earlier releases only
|
2014-02-06 14:04:52 +01:00
|
|
|
|
* support PKCS#1. You can use the `openssl rsa`
|
2011-11-18 21:05:34 +01:00
|
|
|
|
* tool to convert PKCS#8 keys to PKCS#1.
|
2009-12-21 20:50:32 +01:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.28
|
|
|
|
|
*/
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_PRIVATE_KEY,
|
|
|
|
|
g_param_spec_boxed ("private-key",
|
|
|
|
|
P_("Private key"),
|
2016-09-30 05:47:15 +02:00
|
|
|
|
P_("The DER representation of the certificate’s private key"),
|
2009-12-21 20:50:32 +01:00
|
|
|
|
G_TYPE_BYTE_ARRAY,
|
|
|
|
|
G_PARAM_WRITABLE |
|
|
|
|
|
G_PARAM_CONSTRUCT_ONLY |
|
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
|
|
|
|
/**
|
|
|
|
|
* GTlsCertificate:private-key-pem:
|
|
|
|
|
*
|
|
|
|
|
* The PEM (ASCII) encoded representation of the certificate's
|
2014-02-06 14:04:52 +01:00
|
|
|
|
* private key in either PKCS#1 format ("`BEGIN RSA PRIVATE
|
|
|
|
|
* KEY`") or unencrypted PKCS#8 format ("`BEGIN
|
|
|
|
|
* PRIVATE KEY`"). This property (or the
|
2011-11-18 21:05:34 +01:00
|
|
|
|
* #GTlsCertificate:private-key property) can be set when
|
|
|
|
|
* constructing a key (eg, from a file), but cannot be read.
|
|
|
|
|
*
|
|
|
|
|
* PKCS#8 format is supported since 2.32; earlier releases only
|
2014-02-06 14:04:52 +01:00
|
|
|
|
* support PKCS#1. You can use the `openssl rsa`
|
2011-11-18 21:05:34 +01:00
|
|
|
|
* tool to convert PKCS#8 keys to PKCS#1.
|
2009-12-21 20:50:32 +01:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.28
|
|
|
|
|
*/
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_PRIVATE_KEY_PEM,
|
|
|
|
|
g_param_spec_string ("private-key-pem",
|
|
|
|
|
P_("Private key (PEM)"),
|
2016-09-30 05:47:15 +02:00
|
|
|
|
P_("The PEM representation of the certificate’s private key"),
|
2009-12-21 20:50:32 +01:00
|
|
|
|
NULL,
|
|
|
|
|
G_PARAM_WRITABLE |
|
|
|
|
|
G_PARAM_CONSTRUCT_ONLY |
|
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
|
|
|
|
/**
|
|
|
|
|
* GTlsCertificate:issuer:
|
|
|
|
|
*
|
|
|
|
|
* A #GTlsCertificate representing the entity that issued this
|
|
|
|
|
* certificate. If %NULL, this means that the certificate is either
|
|
|
|
|
* self-signed, or else the certificate of the issuer is not
|
|
|
|
|
* available.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.28
|
|
|
|
|
*/
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_ISSUER,
|
|
|
|
|
g_param_spec_object ("issuer",
|
|
|
|
|
P_("Issuer"),
|
|
|
|
|
P_("The certificate for the issuing entity"),
|
|
|
|
|
G_TYPE_TLS_CERTIFICATE,
|
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
|
G_PARAM_CONSTRUCT_ONLY |
|
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static GTlsCertificate *
|
2014-10-06 19:19:48 +02:00
|
|
|
|
g_tls_certificate_new_internal (const gchar *certificate_pem,
|
|
|
|
|
const gchar *private_key_pem,
|
|
|
|
|
GTlsCertificate *issuer,
|
|
|
|
|
GError **error)
|
2009-12-21 20:50:32 +01:00
|
|
|
|
{
|
|
|
|
|
GObject *cert;
|
|
|
|
|
GTlsBackend *backend;
|
|
|
|
|
|
|
|
|
|
backend = g_tls_backend_get_default ();
|
|
|
|
|
|
|
|
|
|
cert = g_initable_new (g_tls_backend_get_certificate_type (backend),
|
|
|
|
|
NULL, error,
|
|
|
|
|
"certificate-pem", certificate_pem,
|
|
|
|
|
"private-key-pem", private_key_pem,
|
2014-10-06 19:19:48 +02:00
|
|
|
|
"issuer", issuer,
|
2009-12-21 20:50:32 +01:00
|
|
|
|
NULL);
|
2014-10-06 19:19:48 +02:00
|
|
|
|
|
2009-12-21 20:50:32 +01:00
|
|
|
|
return G_TLS_CERTIFICATE (cert);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-18 21:05:34 +01:00
|
|
|
|
#define PEM_CERTIFICATE_HEADER "-----BEGIN CERTIFICATE-----"
|
|
|
|
|
#define PEM_CERTIFICATE_FOOTER "-----END CERTIFICATE-----"
|
|
|
|
|
#define PEM_PKCS1_PRIVKEY_HEADER "-----BEGIN RSA PRIVATE KEY-----"
|
|
|
|
|
#define PEM_PKCS1_PRIVKEY_FOOTER "-----END RSA PRIVATE KEY-----"
|
|
|
|
|
#define PEM_PKCS8_PRIVKEY_HEADER "-----BEGIN PRIVATE KEY-----"
|
|
|
|
|
#define PEM_PKCS8_PRIVKEY_FOOTER "-----END PRIVATE KEY-----"
|
|
|
|
|
#define PEM_PKCS8_ENCRYPTED_HEADER "-----BEGIN ENCRYPTED PRIVATE KEY-----"
|
|
|
|
|
#define PEM_PKCS8_ENCRYPTED_FOOTER "-----END ENCRYPTED PRIVATE KEY-----"
|
2009-12-21 20:50:32 +01:00
|
|
|
|
|
2011-10-17 03:24:45 +02:00
|
|
|
|
static gchar *
|
2011-06-21 00:49:39 +02:00
|
|
|
|
parse_private_key (const gchar *data,
|
|
|
|
|
gsize data_len,
|
|
|
|
|
gboolean required,
|
|
|
|
|
GError **error)
|
2009-12-21 20:50:32 +01:00
|
|
|
|
{
|
2011-11-18 21:05:34 +01:00
|
|
|
|
const gchar *start, *end, *footer;
|
2009-12-21 20:50:32 +01:00
|
|
|
|
|
2011-11-18 21:05:34 +01:00
|
|
|
|
start = g_strstr_len (data, data_len, PEM_PKCS1_PRIVKEY_HEADER);
|
|
|
|
|
if (start)
|
|
|
|
|
footer = PEM_PKCS1_PRIVKEY_FOOTER;
|
|
|
|
|
else
|
2009-12-21 20:50:32 +01:00
|
|
|
|
{
|
2011-11-18 21:05:34 +01:00
|
|
|
|
start = g_strstr_len (data, data_len, PEM_PKCS8_PRIVKEY_HEADER);
|
|
|
|
|
if (start)
|
|
|
|
|
footer = PEM_PKCS8_PRIVKEY_FOOTER;
|
|
|
|
|
else
|
2009-12-21 20:50:32 +01:00
|
|
|
|
{
|
2011-11-18 21:05:34 +01:00
|
|
|
|
start = g_strstr_len (data, data_len, PEM_PKCS8_ENCRYPTED_HEADER);
|
|
|
|
|
if (start)
|
|
|
|
|
{
|
|
|
|
|
g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
|
|
|
|
|
_("Cannot decrypt PEM-encoded private key"));
|
|
|
|
|
}
|
|
|
|
|
else if (required)
|
|
|
|
|
{
|
|
|
|
|
g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
|
|
|
|
|
_("No PEM-encoded private key found"));
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
2009-12-21 20:50:32 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-18 21:05:34 +01:00
|
|
|
|
end = g_strstr_len (start, data_len - (data - start), footer);
|
2009-12-21 20:50:32 +01:00
|
|
|
|
if (!end)
|
|
|
|
|
{
|
|
|
|
|
g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
|
2011-06-21 00:49:39 +02:00
|
|
|
|
_("Could not parse PEM-encoded private key"));
|
2009-12-21 20:50:32 +01:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2011-11-18 21:05:34 +01:00
|
|
|
|
end += strlen (footer);
|
2009-12-21 20:50:32 +01:00
|
|
|
|
while (*end == '\r' || *end == '\n')
|
|
|
|
|
end++;
|
|
|
|
|
|
2011-06-21 00:49:39 +02:00
|
|
|
|
return g_strndup (start, end - start);
|
|
|
|
|
}
|
2009-12-21 20:50:32 +01:00
|
|
|
|
|
|
|
|
|
|
2011-10-17 03:24:45 +02:00
|
|
|
|
static gchar *
|
2011-06-21 00:49:39 +02:00
|
|
|
|
parse_next_pem_certificate (const gchar **data,
|
|
|
|
|
const gchar *data_end,
|
|
|
|
|
gboolean required,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
|
|
|
|
const gchar *start, *end;
|
2009-12-21 20:50:32 +01:00
|
|
|
|
|
2011-06-21 00:49:39 +02:00
|
|
|
|
start = g_strstr_len (*data, data_end - *data, PEM_CERTIFICATE_HEADER);
|
|
|
|
|
if (!start)
|
2009-12-21 20:50:32 +01:00
|
|
|
|
{
|
2011-06-21 00:49:39 +02:00
|
|
|
|
if (required)
|
2009-12-21 20:50:32 +01:00
|
|
|
|
{
|
|
|
|
|
g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
|
2011-06-21 00:49:39 +02:00
|
|
|
|
_("No PEM-encoded certificate found"));
|
2009-12-21 20:50:32 +01:00
|
|
|
|
}
|
2011-06-21 00:49:39 +02:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2009-12-21 20:50:32 +01:00
|
|
|
|
|
2011-06-21 00:49:39 +02:00
|
|
|
|
end = g_strstr_len (start, data_end - start, PEM_CERTIFICATE_FOOTER);
|
|
|
|
|
if (!end)
|
|
|
|
|
{
|
|
|
|
|
g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_BAD_CERTIFICATE,
|
|
|
|
|
_("Could not parse PEM-encoded certificate"));
|
|
|
|
|
return NULL;
|
2009-12-21 20:50:32 +01:00
|
|
|
|
}
|
2011-06-21 00:49:39 +02:00
|
|
|
|
end += strlen (PEM_CERTIFICATE_FOOTER);
|
|
|
|
|
while (*end == '\r' || *end == '\n')
|
|
|
|
|
end++;
|
2009-12-21 20:50:32 +01:00
|
|
|
|
|
2011-06-21 00:49:39 +02:00
|
|
|
|
*data = end;
|
2009-12-21 20:50:32 +01:00
|
|
|
|
|
2011-06-21 00:49:39 +02:00
|
|
|
|
return g_strndup (start, end - start);
|
2009-12-21 20:50:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-06 19:19:48 +02:00
|
|
|
|
static GSList *
|
|
|
|
|
parse_and_create_certificate_list (const gchar *data,
|
|
|
|
|
gsize data_len,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
|
|
|
|
GSList *first_pem_list = NULL, *pem_list = NULL;
|
|
|
|
|
gchar *first_pem;
|
|
|
|
|
const gchar *p, *end;
|
|
|
|
|
|
|
|
|
|
p = data;
|
|
|
|
|
end = p + data_len;
|
|
|
|
|
|
|
|
|
|
/* Make sure we can load, at least, one certificate. */
|
|
|
|
|
first_pem = parse_next_pem_certificate (&p, end, TRUE, error);
|
|
|
|
|
if (!first_pem)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
/* Create a list with a single element. If we load more certificates
|
|
|
|
|
* below, we will concatenate the two lists at the end. */
|
|
|
|
|
first_pem_list = g_slist_prepend (first_pem_list, first_pem);
|
|
|
|
|
|
|
|
|
|
/* If we read one certificate successfully, let's see if we can read
|
|
|
|
|
* some more. If not, we will simply return a list with the first one.
|
|
|
|
|
*/
|
|
|
|
|
while (p && *p)
|
|
|
|
|
{
|
|
|
|
|
gchar *cert_pem;
|
2015-08-29 02:43:09 +02:00
|
|
|
|
GError *error = NULL;
|
2014-10-06 19:19:48 +02:00
|
|
|
|
|
2015-08-29 02:43:09 +02:00
|
|
|
|
cert_pem = parse_next_pem_certificate (&p, end, FALSE, &error);
|
|
|
|
|
if (error)
|
2014-10-06 19:19:48 +02:00
|
|
|
|
{
|
|
|
|
|
g_slist_free_full (pem_list, g_free);
|
2015-08-29 02:43:09 +02:00
|
|
|
|
g_error_free (error);
|
2014-10-06 19:19:48 +02:00
|
|
|
|
return first_pem_list;
|
|
|
|
|
}
|
2015-08-29 02:43:09 +02:00
|
|
|
|
else if (!cert_pem)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2014-10-06 19:19:48 +02:00
|
|
|
|
|
|
|
|
|
pem_list = g_slist_prepend (pem_list, cert_pem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pem_list = g_slist_concat (pem_list, first_pem_list);
|
|
|
|
|
|
|
|
|
|
return pem_list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static GTlsCertificate *
|
|
|
|
|
create_certificate_chain_from_list (GSList *pem_list,
|
|
|
|
|
const gchar *key_pem)
|
|
|
|
|
{
|
|
|
|
|
GTlsCertificate *cert = NULL, *issuer = NULL, *root = NULL;
|
|
|
|
|
GTlsCertificateFlags flags;
|
|
|
|
|
GSList *pem;
|
|
|
|
|
|
|
|
|
|
pem = pem_list;
|
|
|
|
|
while (pem)
|
|
|
|
|
{
|
|
|
|
|
const gchar *key = NULL;
|
|
|
|
|
|
|
|
|
|
/* Private key belongs only to the first certificate. */
|
|
|
|
|
if (!pem->next)
|
|
|
|
|
key = key_pem;
|
|
|
|
|
|
|
|
|
|
/* We assume that the whole file is a certificate chain, so we use
|
|
|
|
|
* each certificate as the issuer of the next one (list is in
|
|
|
|
|
* reverse order).
|
|
|
|
|
*/
|
|
|
|
|
issuer = cert;
|
|
|
|
|
cert = g_tls_certificate_new_internal (pem->data, key, issuer, NULL);
|
|
|
|
|
if (issuer)
|
|
|
|
|
g_object_unref (issuer);
|
|
|
|
|
|
|
|
|
|
if (!cert)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
/* root will point to the last certificate in the file. */
|
|
|
|
|
if (!root)
|
|
|
|
|
root = cert;
|
|
|
|
|
|
|
|
|
|
pem = g_slist_next (pem);
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-28 20:08:43 +01:00
|
|
|
|
/* Verify that the certificates form a chain. (We don't care at this
|
|
|
|
|
* point if there are other problems with it.)
|
|
|
|
|
*/
|
2014-10-06 19:19:48 +02:00
|
|
|
|
flags = g_tls_certificate_verify (cert, NULL, root);
|
2014-10-28 20:08:43 +01:00
|
|
|
|
if (flags & G_TLS_CERTIFICATE_UNKNOWN_CA)
|
2014-10-06 19:19:48 +02:00
|
|
|
|
{
|
2014-10-28 20:08:43 +01:00
|
|
|
|
/* It wasn't a chain, it's just a bunch of unrelated certs. */
|
|
|
|
|
g_clear_object (&cert);
|
2014-10-06 19:19:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cert;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static GTlsCertificate *
|
|
|
|
|
parse_and_create_certificate (const gchar *data,
|
|
|
|
|
gsize data_len,
|
|
|
|
|
const gchar *key_pem,
|
|
|
|
|
GError **error)
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
GSList *pem_list;
|
|
|
|
|
GTlsCertificate *cert;
|
|
|
|
|
|
|
|
|
|
pem_list = parse_and_create_certificate_list (data, data_len, error);
|
|
|
|
|
if (!pem_list)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
/* We don't pass the error here because, if it fails, we still want to
|
|
|
|
|
* load and return the first certificate.
|
|
|
|
|
*/
|
|
|
|
|
cert = create_certificate_chain_from_list (pem_list, key_pem);
|
|
|
|
|
if (!cert)
|
|
|
|
|
{
|
|
|
|
|
GSList *last = NULL;
|
|
|
|
|
|
|
|
|
|
/* Get the first certificate (which is the last one as the list is
|
|
|
|
|
* in reverse order).
|
|
|
|
|
*/
|
|
|
|
|
last = g_slist_last (pem_list);
|
|
|
|
|
|
|
|
|
|
cert = g_tls_certificate_new_internal (last->data, key_pem, NULL, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_slist_free_full (pem_list, g_free);
|
|
|
|
|
|
|
|
|
|
return cert;
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-21 20:50:32 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_tls_certificate_new_from_pem:
|
|
|
|
|
* @data: PEM-encoded certificate data
|
|
|
|
|
* @length: the length of @data, or -1 if it's 0-terminated.
|
|
|
|
|
* @error: #GError for error reporting, or %NULL to ignore.
|
|
|
|
|
*
|
2014-10-06 19:19:48 +02:00
|
|
|
|
* Creates a #GTlsCertificate from the PEM-encoded data in @data. If
|
|
|
|
|
* @data includes both a certificate and a private key, then the
|
2011-11-18 21:05:34 +01:00
|
|
|
|
* returned certificate will include the private key data as well. (See
|
|
|
|
|
* the #GTlsCertificate:private-key-pem property for information about
|
|
|
|
|
* supported formats.)
|
2009-12-21 20:50:32 +01:00
|
|
|
|
*
|
2014-10-06 19:19:48 +02:00
|
|
|
|
* The returned certificate will be the first certificate found in
|
|
|
|
|
* @data. As of GLib 2.44, if @data contains more certificates it will
|
|
|
|
|
* try to load a certificate chain. All certificates will be verified in
|
|
|
|
|
* the order found (top-level certificate should be the last one in the
|
|
|
|
|
* file) and the #GTlsCertificate:issuer property of each certificate
|
|
|
|
|
* will be set accordingly if the verification succeeds. If any
|
|
|
|
|
* certificate in the chain cannot be verified, the first certificate in
|
|
|
|
|
* the file will still be returned.
|
2009-12-21 20:50:32 +01:00
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
|
* Returns: the new certificate, or %NULL if @data is invalid
|
2009-12-21 20:50:32 +01:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.28
|
|
|
|
|
*/
|
|
|
|
|
GTlsCertificate *
|
|
|
|
|
g_tls_certificate_new_from_pem (const gchar *data,
|
|
|
|
|
gssize length,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
2015-10-03 11:58:18 +02:00
|
|
|
|
GError *child_error = NULL;
|
2014-10-06 19:19:48 +02:00
|
|
|
|
gchar *key_pem;
|
2011-06-21 00:49:39 +02:00
|
|
|
|
GTlsCertificate *cert;
|
2009-12-21 20:50:32 +01:00
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (data != NULL, NULL);
|
2015-10-03 11:58:18 +02:00
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
2009-12-21 20:50:32 +01:00
|
|
|
|
|
|
|
|
|
if (length == -1)
|
2011-06-21 00:49:39 +02:00
|
|
|
|
length = strlen (data);
|
|
|
|
|
|
2015-10-03 11:58:18 +02:00
|
|
|
|
key_pem = parse_private_key (data, length, FALSE, &child_error);
|
|
|
|
|
if (child_error != NULL)
|
|
|
|
|
{
|
|
|
|
|
g_propagate_error (error, child_error);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2011-06-21 00:49:39 +02:00
|
|
|
|
|
2014-10-06 19:19:48 +02:00
|
|
|
|
cert = parse_and_create_certificate (data, length, key_pem, error);
|
2011-06-21 00:49:39 +02:00
|
|
|
|
g_free (key_pem);
|
|
|
|
|
|
|
|
|
|
return cert;
|
2009-12-21 20:50:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_tls_certificate_new_from_file:
|
2016-06-04 17:53:42 +02:00
|
|
|
|
* @file: (type filename): file containing a PEM-encoded certificate to import
|
2009-12-21 20:50:32 +01:00
|
|
|
|
* @error: #GError for error reporting, or %NULL to ignore.
|
|
|
|
|
*
|
2014-10-06 19:19:48 +02:00
|
|
|
|
* Creates a #GTlsCertificate from the PEM-encoded data in @file. The
|
|
|
|
|
* returned certificate will be the first certificate found in @file. As
|
|
|
|
|
* of GLib 2.44, if @file contains more certificates it will try to load
|
|
|
|
|
* a certificate chain. All certificates will be verified in the order
|
|
|
|
|
* found (top-level certificate should be the last one in the file) and
|
|
|
|
|
* the #GTlsCertificate:issuer property of each certificate will be set
|
|
|
|
|
* accordingly if the verification succeeds. If any certificate in the
|
|
|
|
|
* chain cannot be verified, the first certificate in the file will
|
|
|
|
|
* still be returned.
|
|
|
|
|
*
|
|
|
|
|
* If @file cannot be read or parsed, the function will return %NULL and
|
2011-11-18 21:05:34 +01:00
|
|
|
|
* set @error. Otherwise, this behaves like
|
|
|
|
|
* g_tls_certificate_new_from_pem().
|
2009-12-21 20:50:32 +01:00
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
|
* Returns: the new certificate, or %NULL on error
|
2009-12-21 20:50:32 +01:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.28
|
|
|
|
|
*/
|
|
|
|
|
GTlsCertificate *
|
|
|
|
|
g_tls_certificate_new_from_file (const gchar *file,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
|
|
|
|
GTlsCertificate *cert;
|
|
|
|
|
gchar *contents;
|
|
|
|
|
gsize length;
|
|
|
|
|
|
|
|
|
|
if (!g_file_get_contents (file, &contents, &length, error))
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
cert = g_tls_certificate_new_from_pem (contents, length, error);
|
|
|
|
|
g_free (contents);
|
|
|
|
|
return cert;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_tls_certificate_new_from_files:
|
2016-06-04 17:53:42 +02:00
|
|
|
|
* @cert_file: (type filename): file containing one or more PEM-encoded
|
|
|
|
|
* certificates to import
|
|
|
|
|
* @key_file: (type filename): file containing a PEM-encoded private key
|
|
|
|
|
* to import
|
2009-12-21 20:50:32 +01:00
|
|
|
|
* @error: #GError for error reporting, or %NULL to ignore.
|
|
|
|
|
*
|
|
|
|
|
* Creates a #GTlsCertificate from the PEM-encoded data in @cert_file
|
2014-10-06 19:19:48 +02:00
|
|
|
|
* and @key_file. The returned certificate will be the first certificate
|
|
|
|
|
* found in @cert_file. As of GLib 2.44, if @cert_file contains more
|
|
|
|
|
* certificates it will try to load a certificate chain. All
|
|
|
|
|
* certificates will be verified in the order found (top-level
|
|
|
|
|
* certificate should be the last one in the file) and the
|
|
|
|
|
* #GTlsCertificate:issuer property of each certificate will be set
|
|
|
|
|
* accordingly if the verification succeeds. If any certificate in the
|
|
|
|
|
* chain cannot be verified, the first certificate in the file will
|
|
|
|
|
* still be returned.
|
|
|
|
|
*
|
|
|
|
|
* If either file cannot be read or parsed, the function will return
|
|
|
|
|
* %NULL and set @error. Otherwise, this behaves like
|
|
|
|
|
* g_tls_certificate_new_from_pem().
|
2009-12-21 20:50:32 +01:00
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
|
* Returns: the new certificate, or %NULL on error
|
2009-12-21 20:50:32 +01:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.28
|
|
|
|
|
*/
|
|
|
|
|
GTlsCertificate *
|
|
|
|
|
g_tls_certificate_new_from_files (const gchar *cert_file,
|
2014-10-06 19:19:48 +02:00
|
|
|
|
const gchar *key_file,
|
|
|
|
|
GError **error)
|
2009-12-21 20:50:32 +01:00
|
|
|
|
{
|
|
|
|
|
GTlsCertificate *cert;
|
|
|
|
|
gchar *cert_data, *key_data;
|
2011-06-21 00:49:39 +02:00
|
|
|
|
gsize cert_len, key_len;
|
2014-10-06 19:19:48 +02:00
|
|
|
|
gchar *key_pem;
|
2009-12-21 20:50:32 +01:00
|
|
|
|
|
2014-10-06 19:19:48 +02:00
|
|
|
|
if (!g_file_get_contents (key_file, &key_data, &key_len, error))
|
2009-12-21 20:50:32 +01:00
|
|
|
|
return NULL;
|
2011-06-21 00:49:39 +02:00
|
|
|
|
|
|
|
|
|
key_pem = parse_private_key (key_data, key_len, TRUE, error);
|
2009-12-21 20:50:32 +01:00
|
|
|
|
g_free (key_data);
|
2014-10-06 19:19:48 +02:00
|
|
|
|
if (!key_pem)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
if (!g_file_get_contents (cert_file, &cert_data, &cert_len, error))
|
2011-06-21 00:49:39 +02:00
|
|
|
|
{
|
2014-10-06 19:19:48 +02:00
|
|
|
|
g_free (key_pem);
|
2011-06-21 00:49:39 +02:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-06 19:19:48 +02:00
|
|
|
|
cert = parse_and_create_certificate (cert_data, cert_len, key_pem, error);
|
|
|
|
|
g_free (cert_data);
|
2011-06-21 00:49:39 +02:00
|
|
|
|
g_free (key_pem);
|
2009-12-21 20:50:32 +01:00
|
|
|
|
return cert;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_tls_certificate_list_new_from_file:
|
2016-06-04 17:53:42 +02:00
|
|
|
|
* @file: (type filename): file containing PEM-encoded certificates to import
|
2009-12-21 20:50:32 +01:00
|
|
|
|
* @error: #GError for error reporting, or %NULL to ignore.
|
|
|
|
|
*
|
2014-02-01 17:57:13 +01:00
|
|
|
|
* Creates one or more #GTlsCertificates from the PEM-encoded
|
2009-12-21 20:50:32 +01:00
|
|
|
|
* data in @file. If @file cannot be read or parsed, the function will
|
|
|
|
|
* return %NULL and set @error. If @file does not contain any
|
|
|
|
|
* PEM-encoded certificates, this will return an empty list and not
|
|
|
|
|
* set @error.
|
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
|
* Returns: (element-type Gio.TlsCertificate) (transfer full): a
|
2009-12-21 20:50:32 +01:00
|
|
|
|
* #GList containing #GTlsCertificate objects. You must free the list
|
|
|
|
|
* and its contents when you are done with it.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.28
|
|
|
|
|
*/
|
|
|
|
|
GList *
|
|
|
|
|
g_tls_certificate_list_new_from_file (const gchar *file,
|
|
|
|
|
GError **error)
|
|
|
|
|
{
|
2011-06-21 00:49:39 +02:00
|
|
|
|
GQueue queue = G_QUEUE_INIT;
|
2009-12-21 20:50:32 +01:00
|
|
|
|
gchar *contents, *end;
|
|
|
|
|
const gchar *p;
|
|
|
|
|
gsize length;
|
|
|
|
|
|
|
|
|
|
if (!g_file_get_contents (file, &contents, &length, error))
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
end = contents + length;
|
|
|
|
|
p = contents;
|
|
|
|
|
while (p && *p)
|
|
|
|
|
{
|
2011-06-21 00:49:39 +02:00
|
|
|
|
gchar *cert_pem;
|
|
|
|
|
GTlsCertificate *cert = NULL;
|
2014-04-09 16:39:45 +02:00
|
|
|
|
GError *parse_error = NULL;
|
2011-06-21 00:49:39 +02:00
|
|
|
|
|
2014-04-09 16:39:45 +02:00
|
|
|
|
cert_pem = parse_next_pem_certificate (&p, end, FALSE, &parse_error);
|
2011-06-21 00:49:39 +02:00
|
|
|
|
if (cert_pem)
|
2014-04-09 16:39:45 +02:00
|
|
|
|
{
|
2014-10-06 19:19:48 +02:00
|
|
|
|
cert = g_tls_certificate_new_internal (cert_pem, NULL, NULL, &parse_error);
|
2014-04-09 16:39:45 +02:00
|
|
|
|
g_free (cert_pem);
|
|
|
|
|
}
|
2009-12-21 20:50:32 +01:00
|
|
|
|
if (!cert)
|
2014-04-09 16:39:45 +02:00
|
|
|
|
{
|
|
|
|
|
if (parse_error)
|
|
|
|
|
{
|
|
|
|
|
g_propagate_error (error, parse_error);
|
|
|
|
|
g_list_free_full (queue.head, g_object_unref);
|
|
|
|
|
queue.head = NULL;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2011-06-21 00:49:39 +02:00
|
|
|
|
g_queue_push_tail (&queue, cert);
|
2009-12-21 20:50:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2011-04-04 17:10:50 +02:00
|
|
|
|
g_free (contents);
|
2011-06-21 00:49:39 +02:00
|
|
|
|
return queue.head;
|
2009-12-21 20:50:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_tls_certificate_get_issuer:
|
|
|
|
|
* @cert: a #GTlsCertificate
|
|
|
|
|
*
|
|
|
|
|
* Gets the #GTlsCertificate representing @cert's issuer, if known
|
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
|
* Returns: (transfer none): The certificate of @cert's issuer,
|
2009-12-21 20:50:32 +01:00
|
|
|
|
* or %NULL if @cert is self-signed or signed with an unknown
|
|
|
|
|
* certificate.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.28
|
|
|
|
|
*/
|
|
|
|
|
GTlsCertificate *
|
|
|
|
|
g_tls_certificate_get_issuer (GTlsCertificate *cert)
|
|
|
|
|
{
|
2010-12-01 01:57:16 +01:00
|
|
|
|
GTlsCertificate *issuer;
|
|
|
|
|
|
|
|
|
|
g_object_get (G_OBJECT (cert), "issuer", &issuer, NULL);
|
|
|
|
|
if (issuer)
|
|
|
|
|
g_object_unref (issuer);
|
|
|
|
|
|
|
|
|
|
return issuer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_tls_certificate_verify:
|
|
|
|
|
* @cert: a #GTlsCertificate
|
2016-10-29 03:29:02 +02:00
|
|
|
|
* @identity: (nullable): the expected peer identity
|
|
|
|
|
* @trusted_ca: (nullable): the certificate of a trusted authority
|
2010-12-01 01:57:16 +01:00
|
|
|
|
*
|
|
|
|
|
* This verifies @cert and returns a set of #GTlsCertificateFlags
|
|
|
|
|
* indicating any problems found with it. This can be used to verify a
|
|
|
|
|
* certificate outside the context of making a connection, or to
|
|
|
|
|
* check a certificate against a CA that is not part of the system
|
|
|
|
|
* CA database.
|
|
|
|
|
*
|
|
|
|
|
* If @identity is not %NULL, @cert's name(s) will be compared against
|
|
|
|
|
* it, and %G_TLS_CERTIFICATE_BAD_IDENTITY will be set in the return
|
|
|
|
|
* value if it does not match. If @identity is %NULL, that bit will
|
|
|
|
|
* never be set in the return value.
|
|
|
|
|
*
|
|
|
|
|
* If @trusted_ca is not %NULL, then @cert (or one of the certificates
|
|
|
|
|
* in its chain) must be signed by it, or else
|
|
|
|
|
* %G_TLS_CERTIFICATE_UNKNOWN_CA will be set in the return value. If
|
|
|
|
|
* @trusted_ca is %NULL, that bit will never be set in the return
|
|
|
|
|
* value.
|
|
|
|
|
*
|
|
|
|
|
* (All other #GTlsCertificateFlags values will always be set or unset
|
|
|
|
|
* as appropriate.)
|
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
|
* Returns: the appropriate #GTlsCertificateFlags
|
2010-12-01 01:57:16 +01:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.28
|
|
|
|
|
*/
|
|
|
|
|
GTlsCertificateFlags
|
|
|
|
|
g_tls_certificate_verify (GTlsCertificate *cert,
|
|
|
|
|
GSocketConnectable *identity,
|
|
|
|
|
GTlsCertificate *trusted_ca)
|
|
|
|
|
{
|
|
|
|
|
return G_TLS_CERTIFICATE_GET_CLASS (cert)->verify (cert, identity, trusted_ca);
|
2009-12-21 20:50:32 +01:00
|
|
|
|
}
|
2012-08-01 10:41:02 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_tls_certificate_is_same:
|
|
|
|
|
* @cert_one: first certificate to compare
|
|
|
|
|
* @cert_two: second certificate to compare
|
|
|
|
|
*
|
|
|
|
|
* Check if two #GTlsCertificate objects represent the same certificate.
|
|
|
|
|
* The raw DER byte data of the two certificates are checked for equality.
|
|
|
|
|
* This has the effect that two certificates may compare equal even if
|
|
|
|
|
* their #GTlsCertificate:issuer, #GTlsCertificate:private-key, or
|
|
|
|
|
* #GTlsCertificate:private-key-pem properties differ.
|
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
|
* Returns: whether the same or not
|
2012-08-01 10:41:02 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.34
|
|
|
|
|
*/
|
|
|
|
|
gboolean
|
|
|
|
|
g_tls_certificate_is_same (GTlsCertificate *cert_one,
|
|
|
|
|
GTlsCertificate *cert_two)
|
|
|
|
|
{
|
2012-08-21 13:26:27 +02:00
|
|
|
|
GByteArray *b1, *b2;
|
2012-08-01 10:41:02 +02:00
|
|
|
|
gboolean equal;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_TLS_CERTIFICATE (cert_one), FALSE);
|
|
|
|
|
g_return_val_if_fail (G_IS_TLS_CERTIFICATE (cert_two), FALSE);
|
|
|
|
|
|
2012-08-21 13:26:27 +02:00
|
|
|
|
g_object_get (cert_one, "certificate", &b1, NULL);
|
|
|
|
|
g_object_get (cert_two, "certificate", &b2, NULL);
|
2012-08-01 10:41:02 +02:00
|
|
|
|
|
2012-08-21 13:26:27 +02:00
|
|
|
|
equal = (b1->len == b2->len &&
|
|
|
|
|
memcmp (b1->data, b2->data, b1->len) == 0);
|
2012-08-01 10:41:02 +02:00
|
|
|
|
|
2012-08-21 13:26:27 +02:00
|
|
|
|
g_byte_array_unref (b1);
|
|
|
|
|
g_byte_array_unref (b2);
|
2012-08-01 10:41:02 +02:00
|
|
|
|
|
|
|
|
|
return equal;
|
|
|
|
|
}
|