2009-02-02 01:03:15 -05:00
|
|
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Dan Williams <dcbw@redhat.com>
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* 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 Public
|
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
|
* Boston, MA 02110-1301 USA.
|
|
|
|
|
*
|
2014-07-04 13:33:18 -04:00
|
|
|
* Copyright 2007 - 2011 Red Hat, Inc.
|
2009-02-02 01:03:15 -05:00
|
|
|
*/
|
|
|
|
|
|
2014-11-13 10:07:02 -05:00
|
|
|
#include "config.h"
|
|
|
|
|
|
2009-02-02 01:03:15 -05:00
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2015-07-17 14:38:54 +02:00
|
|
|
#include "nm-default.h"
|
2009-02-02 01:03:15 -05:00
|
|
|
#include "crypto.h"
|
libnm-util: add nm_utils_rsa_key_encrypt() and fix crypto padding mixups
To be backwards compatible clients need to handle both paths to private
keys and the decrypted private key data, which is what used to get passed
in the private-key and phase2-private-key attributes of the 802.1x setting.
When moving a connection around between system-settings and user-settings,
if the private key is decrypted data, the settings service needs to store
that decrypted data somewhere so that the key can be sent to NM during
the connection process.
But we don't want to store the decrypted private key data, so we have to
re-encrypt it (possibly generating a private key password if one wasn't
sent with the decrypted data) and save it to disk, then send NM a path
to that private key during connection.
To help clients do this, and so that they don't have to carry around
multiple crypto implementations depending on whether they want to use
NSS or gnutls/gcrypt, add a helper to libnm-util.
Furthermore, I misunderstood a bunch of stuff with crypto padding when
writing the encrypt/decrypt functions long ago, so fix that up. Don't
return padding as part of the decrypted data, and make sure to verify
the padding's expected lengths and values when decrypting. Many thanks
to Nalin Dahyabhai for pointing me in the right direction.
2009-09-15 16:01:50 -07:00
|
|
|
#include "nm-utils.h"
|
2009-02-02 01:03:15 -05:00
|
|
|
|
2014-04-22 16:55:28 +02:00
|
|
|
#include "nm-test-utils.h"
|
|
|
|
|
|
2009-02-02 01:03:15 -05:00
|
|
|
#if 0
|
|
|
|
|
static const char *pem_rsa_key_begin = "-----BEGIN RSA PRIVATE KEY-----";
|
|
|
|
|
static const char *pem_rsa_key_end = "-----END RSA PRIVATE KEY-----";
|
|
|
|
|
|
|
|
|
|
static const char *pem_dsa_key_begin = "-----BEGIN DSA PRIVATE KEY-----";
|
|
|
|
|
static const char *pem_dsa_key_end = "-----END DSA PRIVATE KEY-----";
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
dump_key_to_pem (const char *key, gsize key_len, int key_type)
|
|
|
|
|
{
|
|
|
|
|
char *b64 = NULL;
|
|
|
|
|
GString *str = NULL;
|
|
|
|
|
const char *start_tag;
|
|
|
|
|
const char *end_tag;
|
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
|
|
switch (key_type) {
|
|
|
|
|
case NM_CRYPTO_KEY_TYPE_RSA:
|
|
|
|
|
start_tag = pem_rsa_key_begin;
|
|
|
|
|
end_tag = pem_rsa_key_end;
|
|
|
|
|
break;
|
|
|
|
|
case NM_CRYPTO_KEY_TYPE_DSA:
|
|
|
|
|
start_tag = pem_dsa_key_begin;
|
|
|
|
|
end_tag = pem_dsa_key_end;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
g_warning ("Unknown key type %d", key_type);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b64 = g_base64_encode ((const unsigned char *) key, key_len);
|
|
|
|
|
if (!b64) {
|
|
|
|
|
g_warning ("Couldn't base64 encode the key.");
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
str = g_string_new (NULL);
|
|
|
|
|
|
|
|
|
|
g_string_append (str, start_tag);
|
|
|
|
|
g_string_append_c (str, '\n');
|
|
|
|
|
|
|
|
|
|
for (p = b64; p < (b64 + strlen (b64)); p += 64) {
|
|
|
|
|
g_string_append_len (str, p, strnlen (p, 64));
|
|
|
|
|
g_string_append_c (str, '\n');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_string_append (str, end_tag);
|
|
|
|
|
g_string_append_c (str, '\n');
|
|
|
|
|
|
|
|
|
|
g_message ("Decrypted private key:\n\n%s", str->str);
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
g_free (b64);
|
|
|
|
|
if (str)
|
|
|
|
|
g_string_free (str, TRUE);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
static void
|
2014-08-06 20:19:07 -04:00
|
|
|
test_cert (gconstpointer test_data)
|
2009-02-02 01:03:15 -05:00
|
|
|
{
|
2015-02-09 15:17:53 +01:00
|
|
|
gs_free char *path = NULL;
|
2009-02-02 01:03:15 -05:00
|
|
|
GByteArray *array;
|
|
|
|
|
NMCryptoFileFormat format = NM_CRYPTO_FILE_FORMAT_UNKNOWN;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
2014-08-06 20:19:07 -04:00
|
|
|
path = g_build_filename (TEST_CERT_DIR, (const char *) test_data, NULL);
|
|
|
|
|
|
2009-02-02 01:03:15 -05:00
|
|
|
array = crypto_load_and_verify_certificate (path, &format, &error);
|
2014-08-06 20:19:07 -04:00
|
|
|
ASSERT (array != NULL, "cert",
|
2009-02-02 01:03:15 -05:00
|
|
|
"couldn't read certificate file '%s': %d %s",
|
|
|
|
|
path, error->code, error->message);
|
|
|
|
|
|
2014-08-06 20:19:07 -04:00
|
|
|
ASSERT (format == NM_CRYPTO_FILE_FORMAT_X509, "cert",
|
2009-02-02 01:03:15 -05:00
|
|
|
"%s: unexpected certificate format (expected %d, got %d)",
|
|
|
|
|
path, NM_CRYPTO_FILE_FORMAT_X509, format);
|
|
|
|
|
|
|
|
|
|
g_byte_array_free (array, TRUE);
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-02 12:00:47 -06:00
|
|
|
static GByteArray *
|
|
|
|
|
file_to_byte_array (const char *filename)
|
|
|
|
|
{
|
|
|
|
|
char *contents;
|
|
|
|
|
GByteArray *array = NULL;
|
|
|
|
|
gsize length = 0;
|
|
|
|
|
|
|
|
|
|
if (g_file_get_contents (filename, &contents, &length, NULL)) {
|
|
|
|
|
array = g_byte_array_sized_new (length);
|
2013-01-31 15:36:12 -05:00
|
|
|
g_byte_array_append (array, (guint8 *) contents, length);
|
|
|
|
|
g_assert (array->len == length);
|
2011-03-02 12:00:47 -06:00
|
|
|
g_free (contents);
|
|
|
|
|
}
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-02 01:03:15 -05:00
|
|
|
static void
|
|
|
|
|
test_load_private_key (const char *path,
|
|
|
|
|
const char *password,
|
2011-03-02 12:00:47 -06:00
|
|
|
const char *decrypted_path,
|
2009-02-02 01:03:15 -05:00
|
|
|
gboolean expect_fail,
|
|
|
|
|
const char *desc)
|
|
|
|
|
{
|
|
|
|
|
NMCryptoKeyType key_type = NM_CRYPTO_KEY_TYPE_UNKNOWN;
|
2011-03-02 12:00:47 -06:00
|
|
|
GByteArray *array, *decrypted;
|
2009-02-02 01:03:15 -05:00
|
|
|
GError *error = NULL;
|
|
|
|
|
|
2011-03-02 12:00:47 -06:00
|
|
|
array = crypto_decrypt_private_key (path, password, &key_type, &error);
|
2009-02-02 01:03:15 -05:00
|
|
|
if (expect_fail) {
|
|
|
|
|
ASSERT (array == NULL, desc,
|
|
|
|
|
"unexpected success reading private key file '%s' with "
|
|
|
|
|
"invalid password",
|
|
|
|
|
path);
|
|
|
|
|
|
2011-03-02 12:00:47 -06:00
|
|
|
ASSERT (key_type != NM_CRYPTO_KEY_TYPE_UNKNOWN, desc,
|
|
|
|
|
"unexpected failure determining private key file '%s' "
|
|
|
|
|
"type with invalid password (expected %d, got %d)",
|
|
|
|
|
path, NM_CRYPTO_KEY_TYPE_UNKNOWN, key_type);
|
2015-02-06 13:31:06 +01:00
|
|
|
g_clear_error (&error);
|
2009-02-02 01:03:15 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ASSERT (array != NULL, desc,
|
|
|
|
|
"couldn't read private key file '%s': %d %s",
|
|
|
|
|
path, error->code, error->message);
|
|
|
|
|
|
|
|
|
|
ASSERT (key_type == NM_CRYPTO_KEY_TYPE_RSA, desc,
|
|
|
|
|
"%s: unexpected private key type (expected %d, got %d)",
|
2011-03-02 12:00:47 -06:00
|
|
|
path, NM_CRYPTO_KEY_TYPE_RSA, key_type);
|
|
|
|
|
|
|
|
|
|
if (decrypted_path) {
|
|
|
|
|
/* Compare the crypto decrypted key against a known-good decryption */
|
|
|
|
|
decrypted = file_to_byte_array (decrypted_path);
|
|
|
|
|
ASSERT (decrypted != NULL, desc,
|
|
|
|
|
"couldn't read decrypted private key file '%s': %d %s",
|
|
|
|
|
decrypted_path, error->code, error->message);
|
|
|
|
|
|
|
|
|
|
ASSERT (decrypted->len > 0, desc, "decrypted key file invalid (size 0)");
|
|
|
|
|
|
|
|
|
|
ASSERT (decrypted->len == array->len,
|
|
|
|
|
desc, "decrypted key file (%d) and decrypted key data (%d) lengths don't match",
|
|
|
|
|
decrypted->len, array->len);
|
|
|
|
|
|
|
|
|
|
ASSERT (memcmp (decrypted->data, array->data, array->len) == 0,
|
|
|
|
|
desc, "decrypted key file and decrypted key data don't match");
|
|
|
|
|
|
|
|
|
|
g_byte_array_free (decrypted, TRUE);
|
|
|
|
|
}
|
2009-02-02 01:03:15 -05:00
|
|
|
|
2015-02-06 13:31:06 +01:00
|
|
|
g_clear_error (&error);
|
2009-02-02 01:03:15 -05:00
|
|
|
g_byte_array_free (array, TRUE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
test_load_pkcs12 (const char *path,
|
|
|
|
|
const char *password,
|
|
|
|
|
gboolean expect_fail,
|
|
|
|
|
const char *desc)
|
|
|
|
|
{
|
|
|
|
|
NMCryptoFileFormat format = NM_CRYPTO_FILE_FORMAT_UNKNOWN;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
2011-03-02 12:00:47 -06:00
|
|
|
format = crypto_verify_private_key (path, password, &error);
|
2009-02-02 01:03:15 -05:00
|
|
|
if (expect_fail) {
|
2011-03-02 12:00:47 -06:00
|
|
|
ASSERT (format == NM_CRYPTO_FILE_FORMAT_UNKNOWN, desc,
|
2009-02-02 01:03:15 -05:00
|
|
|
"unexpected success reading PKCS#12 private key file "
|
|
|
|
|
"'%s' with invalid password",
|
|
|
|
|
path);
|
2011-03-02 12:00:47 -06:00
|
|
|
} else {
|
|
|
|
|
ASSERT (format == NM_CRYPTO_FILE_FORMAT_PKCS12, desc,
|
|
|
|
|
"%s: unexpected PKCS#12 private key file format (expected %d, got "
|
|
|
|
|
"%d): %d %s",
|
|
|
|
|
path, NM_CRYPTO_FILE_FORMAT_PKCS12, format, error->code, error->message);
|
2009-02-02 01:03:15 -05:00
|
|
|
}
|
2015-02-06 13:31:06 +01:00
|
|
|
g_clear_error (&error);
|
2011-03-02 12:00:47 -06:00
|
|
|
}
|
2009-02-02 01:03:15 -05:00
|
|
|
|
2011-03-02 12:00:47 -06:00
|
|
|
static void
|
|
|
|
|
test_load_pkcs12_no_password (const char *path, const char *desc)
|
|
|
|
|
{
|
|
|
|
|
NMCryptoFileFormat format = NM_CRYPTO_FILE_FORMAT_UNKNOWN;
|
|
|
|
|
GError *error = NULL;
|
2009-02-02 01:03:15 -05:00
|
|
|
|
2011-03-02 12:00:47 -06:00
|
|
|
/* We should still get a valid returned crypto file format */
|
|
|
|
|
format = crypto_verify_private_key (path, NULL, &error);
|
2009-02-02 01:03:15 -05:00
|
|
|
ASSERT (format == NM_CRYPTO_FILE_FORMAT_PKCS12, desc,
|
2011-03-02 12:00:47 -06:00
|
|
|
"%s: unexpected PKCS#12 private key file format (expected %d, got "
|
|
|
|
|
"%d): %d %s",
|
|
|
|
|
path, NM_CRYPTO_FILE_FORMAT_PKCS12, format, error->code, error->message);
|
2009-02-02 01:03:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
test_is_pkcs12 (const char *path, gboolean expect_fail, const char *desc)
|
|
|
|
|
{
|
|
|
|
|
gboolean is_pkcs12;
|
|
|
|
|
|
2009-09-04 09:07:00 -05:00
|
|
|
is_pkcs12 = crypto_is_pkcs12_file (path, NULL);
|
2009-02-02 01:03:15 -05:00
|
|
|
if (expect_fail) {
|
|
|
|
|
ASSERT (is_pkcs12 == FALSE, desc,
|
|
|
|
|
"unexpected success reading non-PKCS#12 file '%s'",
|
|
|
|
|
path);
|
2011-03-02 12:00:47 -06:00
|
|
|
} else {
|
|
|
|
|
ASSERT (is_pkcs12 == TRUE, desc, "couldn't read PKCS#12 file '%s'", path);
|
2009-02-02 01:03:15 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-12 10:09:18 -05:00
|
|
|
static void
|
|
|
|
|
test_load_pkcs8 (const char *path,
|
|
|
|
|
const char *password,
|
|
|
|
|
gboolean expect_fail,
|
|
|
|
|
const char *desc)
|
|
|
|
|
{
|
|
|
|
|
NMCryptoFileFormat format = NM_CRYPTO_FILE_FORMAT_UNKNOWN;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
|
|
format = crypto_verify_private_key (path, password, &error);
|
|
|
|
|
if (expect_fail) {
|
|
|
|
|
ASSERT (format == NM_CRYPTO_FILE_FORMAT_UNKNOWN, desc,
|
|
|
|
|
"unexpected success reading PKCS#8 private key file "
|
|
|
|
|
"'%s' with invalid password",
|
|
|
|
|
path);
|
|
|
|
|
} else {
|
|
|
|
|
ASSERT (format == NM_CRYPTO_FILE_FORMAT_RAW_KEY, desc,
|
|
|
|
|
"%s: unexpected PKCS#8 private key file format (expected %d, got "
|
|
|
|
|
"%d): %d %s",
|
|
|
|
|
path, NM_CRYPTO_FILE_FORMAT_RAW_KEY, format, error->code, error->message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-09 13:48:30 +02:00
|
|
|
static gboolean
|
|
|
|
|
is_cipher_aes (const char *path)
|
|
|
|
|
{
|
|
|
|
|
char *contents;
|
|
|
|
|
gsize length = 0;
|
|
|
|
|
const char *cipher;
|
|
|
|
|
gboolean is_aes = FALSE;
|
|
|
|
|
|
|
|
|
|
if (!g_file_get_contents (path, &contents, &length, NULL))
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
cipher = strstr (contents, "DEK-Info: ");
|
|
|
|
|
if (cipher) {
|
|
|
|
|
cipher += strlen ("DEK-Info: ");
|
|
|
|
|
if (g_str_has_prefix (cipher, "AES-128-CBC"))
|
|
|
|
|
is_aes = TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_free (contents);
|
2014-05-09 14:45:59 -04:00
|
|
|
return is_aes;
|
2014-05-09 13:48:30 +02:00
|
|
|
}
|
|
|
|
|
|
libnm-util: add nm_utils_rsa_key_encrypt() and fix crypto padding mixups
To be backwards compatible clients need to handle both paths to private
keys and the decrypted private key data, which is what used to get passed
in the private-key and phase2-private-key attributes of the 802.1x setting.
When moving a connection around between system-settings and user-settings,
if the private key is decrypted data, the settings service needs to store
that decrypted data somewhere so that the key can be sent to NM during
the connection process.
But we don't want to store the decrypted private key data, so we have to
re-encrypt it (possibly generating a private key password if one wasn't
sent with the decrypted data) and save it to disk, then send NM a path
to that private key during connection.
To help clients do this, and so that they don't have to carry around
multiple crypto implementations depending on whether they want to use
NSS or gnutls/gcrypt, add a helper to libnm-util.
Furthermore, I misunderstood a bunch of stuff with crypto padding when
writing the encrypt/decrypt functions long ago, so fix that up. Don't
return padding as part of the decrypted data, and make sure to verify
the padding's expected lengths and values when decrypting. Many thanks
to Nalin Dahyabhai for pointing me in the right direction.
2009-09-15 16:01:50 -07:00
|
|
|
static void
|
|
|
|
|
test_encrypt_private_key (const char *path,
|
|
|
|
|
const char *password,
|
|
|
|
|
const char *desc)
|
|
|
|
|
{
|
|
|
|
|
NMCryptoKeyType key_type = NM_CRYPTO_KEY_TYPE_UNKNOWN;
|
|
|
|
|
GByteArray *array, *encrypted, *re_decrypted;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
2011-03-02 12:00:47 -06:00
|
|
|
array = crypto_decrypt_private_key (path, password, &key_type, &error);
|
libnm-util: add nm_utils_rsa_key_encrypt() and fix crypto padding mixups
To be backwards compatible clients need to handle both paths to private
keys and the decrypted private key data, which is what used to get passed
in the private-key and phase2-private-key attributes of the 802.1x setting.
When moving a connection around between system-settings and user-settings,
if the private key is decrypted data, the settings service needs to store
that decrypted data somewhere so that the key can be sent to NM during
the connection process.
But we don't want to store the decrypted private key data, so we have to
re-encrypt it (possibly generating a private key password if one wasn't
sent with the decrypted data) and save it to disk, then send NM a path
to that private key during connection.
To help clients do this, and so that they don't have to carry around
multiple crypto implementations depending on whether they want to use
NSS or gnutls/gcrypt, add a helper to libnm-util.
Furthermore, I misunderstood a bunch of stuff with crypto padding when
writing the encrypt/decrypt functions long ago, so fix that up. Don't
return padding as part of the decrypted data, and make sure to verify
the padding's expected lengths and values when decrypting. Many thanks
to Nalin Dahyabhai for pointing me in the right direction.
2009-09-15 16:01:50 -07:00
|
|
|
ASSERT (array != NULL, desc,
|
|
|
|
|
"couldn't read private key file '%s': %d %s",
|
|
|
|
|
path, error->code, error->message);
|
|
|
|
|
|
|
|
|
|
ASSERT (key_type == NM_CRYPTO_KEY_TYPE_RSA, desc,
|
|
|
|
|
"%s: unexpected private key type (expected %d, got %d)",
|
2011-03-02 12:00:47 -06:00
|
|
|
path, NM_CRYPTO_KEY_TYPE_RSA, key_type);
|
libnm-util: add nm_utils_rsa_key_encrypt() and fix crypto padding mixups
To be backwards compatible clients need to handle both paths to private
keys and the decrypted private key data, which is what used to get passed
in the private-key and phase2-private-key attributes of the 802.1x setting.
When moving a connection around between system-settings and user-settings,
if the private key is decrypted data, the settings service needs to store
that decrypted data somewhere so that the key can be sent to NM during
the connection process.
But we don't want to store the decrypted private key data, so we have to
re-encrypt it (possibly generating a private key password if one wasn't
sent with the decrypted data) and save it to disk, then send NM a path
to that private key during connection.
To help clients do this, and so that they don't have to carry around
multiple crypto implementations depending on whether they want to use
NSS or gnutls/gcrypt, add a helper to libnm-util.
Furthermore, I misunderstood a bunch of stuff with crypto padding when
writing the encrypt/decrypt functions long ago, so fix that up. Don't
return padding as part of the decrypted data, and make sure to verify
the padding's expected lengths and values when decrypting. Many thanks
to Nalin Dahyabhai for pointing me in the right direction.
2009-09-15 16:01:50 -07:00
|
|
|
|
|
|
|
|
/* Now re-encrypt the private key */
|
2014-05-09 13:48:30 +02:00
|
|
|
if (is_cipher_aes (path))
|
|
|
|
|
encrypted = nm_utils_rsa_key_encrypt_aes (array, password, NULL, &error);
|
|
|
|
|
else
|
|
|
|
|
encrypted = nm_utils_rsa_key_encrypt (array, password, NULL, &error);
|
libnm-util: add nm_utils_rsa_key_encrypt() and fix crypto padding mixups
To be backwards compatible clients need to handle both paths to private
keys and the decrypted private key data, which is what used to get passed
in the private-key and phase2-private-key attributes of the 802.1x setting.
When moving a connection around between system-settings and user-settings,
if the private key is decrypted data, the settings service needs to store
that decrypted data somewhere so that the key can be sent to NM during
the connection process.
But we don't want to store the decrypted private key data, so we have to
re-encrypt it (possibly generating a private key password if one wasn't
sent with the decrypted data) and save it to disk, then send NM a path
to that private key during connection.
To help clients do this, and so that they don't have to carry around
multiple crypto implementations depending on whether they want to use
NSS or gnutls/gcrypt, add a helper to libnm-util.
Furthermore, I misunderstood a bunch of stuff with crypto padding when
writing the encrypt/decrypt functions long ago, so fix that up. Don't
return padding as part of the decrypted data, and make sure to verify
the padding's expected lengths and values when decrypting. Many thanks
to Nalin Dahyabhai for pointing me in the right direction.
2009-09-15 16:01:50 -07:00
|
|
|
ASSERT (encrypted != NULL, desc,
|
|
|
|
|
"couldn't re-encrypt private key file '%s': %d %s",
|
|
|
|
|
path, error->code, error->message);
|
|
|
|
|
|
|
|
|
|
/* Then re-decrypt the private key */
|
|
|
|
|
key_type = NM_CRYPTO_KEY_TYPE_UNKNOWN;
|
2011-03-02 12:00:47 -06:00
|
|
|
re_decrypted = crypto_decrypt_private_key_data (encrypted, password, &key_type, &error);
|
libnm-util: add nm_utils_rsa_key_encrypt() and fix crypto padding mixups
To be backwards compatible clients need to handle both paths to private
keys and the decrypted private key data, which is what used to get passed
in the private-key and phase2-private-key attributes of the 802.1x setting.
When moving a connection around between system-settings and user-settings,
if the private key is decrypted data, the settings service needs to store
that decrypted data somewhere so that the key can be sent to NM during
the connection process.
But we don't want to store the decrypted private key data, so we have to
re-encrypt it (possibly generating a private key password if one wasn't
sent with the decrypted data) and save it to disk, then send NM a path
to that private key during connection.
To help clients do this, and so that they don't have to carry around
multiple crypto implementations depending on whether they want to use
NSS or gnutls/gcrypt, add a helper to libnm-util.
Furthermore, I misunderstood a bunch of stuff with crypto padding when
writing the encrypt/decrypt functions long ago, so fix that up. Don't
return padding as part of the decrypted data, and make sure to verify
the padding's expected lengths and values when decrypting. Many thanks
to Nalin Dahyabhai for pointing me in the right direction.
2009-09-15 16:01:50 -07:00
|
|
|
ASSERT (re_decrypted != NULL, desc,
|
|
|
|
|
"couldn't read private key file '%s': %d %s",
|
|
|
|
|
path, error->code, error->message);
|
|
|
|
|
|
|
|
|
|
ASSERT (key_type == NM_CRYPTO_KEY_TYPE_RSA, desc,
|
|
|
|
|
"%s: unexpected private key type (expected %d, got %d)",
|
2011-03-02 12:00:47 -06:00
|
|
|
path, NM_CRYPTO_KEY_TYPE_RSA, key_type);
|
libnm-util: add nm_utils_rsa_key_encrypt() and fix crypto padding mixups
To be backwards compatible clients need to handle both paths to private
keys and the decrypted private key data, which is what used to get passed
in the private-key and phase2-private-key attributes of the 802.1x setting.
When moving a connection around between system-settings and user-settings,
if the private key is decrypted data, the settings service needs to store
that decrypted data somewhere so that the key can be sent to NM during
the connection process.
But we don't want to store the decrypted private key data, so we have to
re-encrypt it (possibly generating a private key password if one wasn't
sent with the decrypted data) and save it to disk, then send NM a path
to that private key during connection.
To help clients do this, and so that they don't have to carry around
multiple crypto implementations depending on whether they want to use
NSS or gnutls/gcrypt, add a helper to libnm-util.
Furthermore, I misunderstood a bunch of stuff with crypto padding when
writing the encrypt/decrypt functions long ago, so fix that up. Don't
return padding as part of the decrypted data, and make sure to verify
the padding's expected lengths and values when decrypting. Many thanks
to Nalin Dahyabhai for pointing me in the right direction.
2009-09-15 16:01:50 -07:00
|
|
|
|
|
|
|
|
/* Compare the original decrypted key with the re-decrypted key */
|
|
|
|
|
ASSERT (array->len == re_decrypted->len, desc,
|
|
|
|
|
"%s: unexpected re-decrypted private key length (expected %d, got %d)",
|
|
|
|
|
path, array->len, re_decrypted->len);
|
|
|
|
|
|
|
|
|
|
ASSERT (!memcmp (array->data, re_decrypted->data, array->len), desc,
|
|
|
|
|
"%s: unexpected private key data",
|
|
|
|
|
path);
|
|
|
|
|
|
|
|
|
|
g_byte_array_free (re_decrypted, TRUE);
|
|
|
|
|
g_byte_array_free (encrypted, TRUE);
|
|
|
|
|
g_byte_array_free (array, TRUE);
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-06 20:19:07 -04:00
|
|
|
static void
|
|
|
|
|
test_key (gconstpointer test_data)
|
|
|
|
|
{
|
|
|
|
|
char **parts, *path, *password, *decrypted_path;
|
|
|
|
|
int len;
|
|
|
|
|
|
|
|
|
|
parts = g_strsplit ((const char *) test_data, ", ", -1);
|
|
|
|
|
len = g_strv_length (parts);
|
|
|
|
|
ASSERT (len == 2 || len == 3, "test-crypto",
|
|
|
|
|
"wrong number of arguments (<key file>, <password>, [<decrypted key file>])");
|
|
|
|
|
|
|
|
|
|
path = g_build_filename (TEST_CERT_DIR, parts[0], NULL);
|
|
|
|
|
password = parts[1];
|
|
|
|
|
decrypted_path = parts[2] ? g_build_filename (TEST_CERT_DIR, parts[2], NULL) : NULL;
|
|
|
|
|
|
|
|
|
|
test_is_pkcs12 (path, TRUE, "not-pkcs12");
|
|
|
|
|
test_load_private_key (path, password, decrypted_path, FALSE, "private-key");
|
|
|
|
|
test_load_private_key (path, "blahblahblah", NULL, TRUE, "private-key-bad-password");
|
|
|
|
|
test_load_private_key (path, NULL, NULL, TRUE, "private-key-no-password");
|
|
|
|
|
test_encrypt_private_key (path, password, "private-key-rencrypt");
|
|
|
|
|
|
|
|
|
|
g_free (path);
|
|
|
|
|
g_free (decrypted_path);
|
|
|
|
|
g_strfreev (parts);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
test_pkcs12 (gconstpointer test_data)
|
|
|
|
|
{
|
|
|
|
|
char **parts, *path, *password;
|
|
|
|
|
|
|
|
|
|
parts = g_strsplit ((const char *) test_data, ", ", -1);
|
|
|
|
|
ASSERT (g_strv_length (parts) == 2, "test-crypto",
|
|
|
|
|
"wrong number of arguments (<file>, <password>)");
|
|
|
|
|
|
|
|
|
|
path = g_build_filename (TEST_CERT_DIR, parts[0], NULL);
|
|
|
|
|
password = parts[1];
|
|
|
|
|
|
|
|
|
|
test_is_pkcs12 (path, FALSE, "is-pkcs12");
|
|
|
|
|
test_load_pkcs12 (path, password, FALSE, "pkcs12-private-key");
|
|
|
|
|
test_load_pkcs12 (path, "blahblahblah", TRUE, "pkcs12-private-key-bad-password");
|
|
|
|
|
test_load_pkcs12_no_password (path, "pkcs12-private-key-no-password");
|
|
|
|
|
|
|
|
|
|
g_free (path);
|
|
|
|
|
g_strfreev (parts);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
test_pkcs8 (gconstpointer test_data)
|
|
|
|
|
{
|
|
|
|
|
char **parts, *path, *password;
|
|
|
|
|
|
|
|
|
|
parts = g_strsplit ((const char *) test_data, ", ", -1);
|
|
|
|
|
ASSERT (g_strv_length (parts) == 2, "test-crypto",
|
|
|
|
|
"wrong number of arguments (<file>, <password>)");
|
|
|
|
|
|
|
|
|
|
path = g_build_filename (TEST_CERT_DIR, parts[0], NULL);
|
|
|
|
|
password = parts[1];
|
|
|
|
|
|
|
|
|
|
test_is_pkcs12 (path, TRUE, "not-pkcs12");
|
|
|
|
|
test_load_pkcs8 (path, password, FALSE, "pkcs8-private-key");
|
|
|
|
|
/* Until gnutls and NSS grow support for all the ciphers that openssl
|
|
|
|
|
* can use with PKCS#8, we can't actually verify the password. So we
|
|
|
|
|
* expect a bad password to work for the time being.
|
|
|
|
|
*/
|
|
|
|
|
test_load_pkcs8 (path, "blahblahblah", FALSE, "pkcs8-private-key-bad-password");
|
|
|
|
|
|
|
|
|
|
g_free (path);
|
|
|
|
|
g_strfreev (parts);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NMTST_DEFINE ();
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
main (int argc, char **argv)
|
2009-02-02 01:03:15 -05:00
|
|
|
{
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
2014-08-06 20:19:07 -04:00
|
|
|
nmtst_init (&argc, &argv, TRUE);
|
2009-02-02 01:03:15 -05:00
|
|
|
|
|
|
|
|
if (!crypto_init (&error))
|
|
|
|
|
FAIL ("crypto-init", "failed to initialize crypto: %s", error->message);
|
|
|
|
|
|
2014-08-06 20:19:07 -04:00
|
|
|
g_test_add_data_func ("/libnm/crypto/cert/pem",
|
|
|
|
|
"test_ca_cert.pem",
|
|
|
|
|
test_cert);
|
|
|
|
|
g_test_add_data_func ("/libnm/crypto/cert/pem-2",
|
|
|
|
|
"test2_ca_cert.pem",
|
|
|
|
|
test_cert);
|
|
|
|
|
g_test_add_data_func ("/libnm/crypto/cert/der",
|
|
|
|
|
"test_ca_cert.der",
|
|
|
|
|
test_cert);
|
|
|
|
|
g_test_add_data_func ("/libnm/crypto/cert/pem-no-ending-newline",
|
|
|
|
|
"ca-no-ending-newline.pem",
|
|
|
|
|
test_cert);
|
|
|
|
|
g_test_add_data_func ("/libnm/crypto/cert/pem-combined",
|
|
|
|
|
"test_key_and_cert.pem",
|
|
|
|
|
test_cert);
|
|
|
|
|
g_test_add_data_func ("/libnm/crypto/cert/pem-combined-2",
|
|
|
|
|
"test2_key_and_cert.pem",
|
|
|
|
|
test_cert);
|
|
|
|
|
|
|
|
|
|
g_test_add_data_func ("/libnm/crypto/key/padding-6",
|
|
|
|
|
"test_key_and_cert.pem, test, test-key-only-decrypted.der",
|
|
|
|
|
test_key);
|
|
|
|
|
g_test_add_data_func ("/libnm/crypto/key/key-only",
|
|
|
|
|
"test-key-only.pem, test, test-key-only-decrypted.der",
|
|
|
|
|
test_key);
|
|
|
|
|
g_test_add_data_func ("/libnm/crypto/key/padding-8",
|
|
|
|
|
"test2_key_and_cert.pem, 12345testing",
|
|
|
|
|
test_key);
|
|
|
|
|
g_test_add_data_func ("/libnm/crypto/key/aes",
|
|
|
|
|
"test-aes-key.pem, test-aes-password",
|
|
|
|
|
test_key);
|
|
|
|
|
|
|
|
|
|
g_test_add_data_func ("/libnm/crypto/PKCS#12/1",
|
|
|
|
|
"test-cert.p12, test",
|
|
|
|
|
test_pkcs12);
|
|
|
|
|
g_test_add_data_func ("/libnm/crypto/PKCS#12/2",
|
|
|
|
|
"test2-cert.p12, 12345testing",
|
|
|
|
|
test_pkcs12);
|
|
|
|
|
|
|
|
|
|
g_test_add_data_func ("/libnm/crypto/PKCS#8",
|
|
|
|
|
"pkcs8-enc-key.pem, 1234567890",
|
|
|
|
|
test_pkcs8);
|
|
|
|
|
|
2014-12-02 09:26:39 -05:00
|
|
|
return g_test_run ();
|
2009-02-02 01:03:15 -05:00
|
|
|
}
|
|
|
|
|
|