From 800fee3a642ab01f127660ef79a4120bd0e0caad Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Tue, 13 Dec 2016 11:41:12 +0100 Subject: tests: added test for PKCS#8 encrypted key decoding This also verifies that the return value when attempting to decrypt without a password is GNUTLS_E_DECRYPTION_FAILED. --- tests/Makefile.am | 3 +- tests/pkcs8-key-decode-encrypted.c | 75 +++++++++++++++++++++++++++++++++++ tests/pkcs8-key-decode.c | 80 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 tests/pkcs8-key-decode-encrypted.c create mode 100644 tests/pkcs8-key-decode.c diff --git a/tests/Makefile.am b/tests/Makefile.am index 61107891ce..5b60899abf 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -87,7 +87,8 @@ ctests = mini-record-2 simple gc set_pkcs12_cred certder certuniqueid \ fips-test mini-global-load name-constraints x509-extensions \ long-session-id mini-x509-callbacks-intr \ crlverify init_fds mini-rehandshake-2 sign-md5-rep global-init-override \ - version-checks mini-server-name naked-alerts multi-alerts + version-checks mini-server-name naked-alerts multi-alerts \ + pkcs8-key-decode-encrypted pkcs8-key-decode if ENABLE_PKCS11 if !HAVE_BUGGY_P11_KIT diff --git a/tests/pkcs8-key-decode-encrypted.c b/tests/pkcs8-key-decode-encrypted.c new file mode 100644 index 0000000000..48ab9b64dc --- /dev/null +++ b/tests/pkcs8-key-decode-encrypted.c @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2015 Red Hat, Inc. + * + * Author: Daniel Berrange + * + * This file is part of GnuTLS. + * + * GnuTLS is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GnuTLS 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 + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GnuTLS; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include +#include +#include +#include +#include + +#include "utils.h" + +#define PRIVATE_KEY \ + "-----BEGIN ENCRYPTED PRIVATE KEY-----\n" \ + "MIHeMEkGCSqGSIb3DQEFDTA8MBsGCSqGSIb3DQEFDDAOBAiebBrnqPv4owICCAAw\n" \ + "HQYJYIZIAWUDBAEqBBBykFR6i1My/DYFBYrz1lmABIGQ3XGpp3+v/ENC1S+X7Ay6\n" \ + "JoquYKuMw6yUmWoGFvPIPA9UWqMve2Uj4l2l96Sywd6iNFP63ow6pIq4wUP6REuY\n" \ + "ZhCgoAOQomeFqhAhkw6QJCygp5vw2rh9OZ5tiP/Ko6IDTA2rSas91nepHpQOb247\n" \ + "zta5XzXb5TRkBsVU8tAPADP+wS/vBCS05ne1wmhdD6c6\n" \ + "-----END ENCRYPTED PRIVATE KEY-----\n" + + +static int test_decode(void) +{ + gnutls_x509_privkey_t key; + const gnutls_datum_t data = { + (unsigned char *)PRIVATE_KEY, + strlen(PRIVATE_KEY) + }; + int err; + + if ((err = gnutls_x509_privkey_init(&key)) < 0) { + fail("Failed to init key %s\n", gnutls_strerror(err)); + } + + err = gnutls_x509_privkey_import_pkcs8(key, &data, + GNUTLS_X509_FMT_PEM, "", 0); + if (err != GNUTLS_E_DECRYPTION_FAILED) { + fail("Unexpected error code: %s/%d\n", gnutls_strerror(err), err); + } + + err = gnutls_x509_privkey_import_pkcs8(key, &data, + GNUTLS_X509_FMT_PEM, "password", 0); + if (err != 0) { + fail("Unexpected error code: %s\n", gnutls_strerror(err)); + } + + success("Loaded key\n%s", PRIVATE_KEY); + + gnutls_x509_privkey_deinit(key); + return 0; +} + +void doit(void) +{ + test_decode(); +} diff --git a/tests/pkcs8-key-decode.c b/tests/pkcs8-key-decode.c new file mode 100644 index 0000000000..1d3af96a7b --- /dev/null +++ b/tests/pkcs8-key-decode.c @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2015 Red Hat, Inc. + * + * Author: Daniel Berrange + * + * This file is part of GnuTLS. + * + * GnuTLS is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GnuTLS 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 + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GnuTLS; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include +#include +#include +#include +#include + +#include "utils.h" + +# define PRIVATE_KEY \ + "-----BEGIN PRIVATE KEY-----\n" \ + "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBALVcr\n" \ + "BL40Tm6yq88FBhJNw1aaoCjmtg0l4dWQZ/e9Fimx4ARxFpT+ji4FE\n" \ + "Cgl9s/SGqC+1nvlkm9ViSo0j7MKDbnDB+VRHDvMAzQhA2X7e8M0n9\n" \ + "rPolUY2lIVC83q0BBaOBkCj2RSmT2xTEbbC2xLukSrg2WP/ihVOxc\n" \ + "kXRuyFtzAgMBAAECgYB7slBexDwXrtItAMIH6m/U+LUpNe0Xx48OL\n" \ + "IOn4a4whNgO/o84uIwygUK27ZGFZT0kAGAk8CdF9hA6ArcbQ62s1H\n" \ + "myxrUbF9/mrLsQw1NEqpuUk9Ay2Tx5U/wPx35S3W/X2AvR/ZpTnCn\n" \ + "2q/7ym9fyiSoj86drD7BTvmKXlOnOwQJBAPOFMp4mMa9NGpGuEssO\n" \ + "m3Uwbp6lhcP0cA9MK+iOmeANpoKWfBdk5O34VbmeXnGYWEkrnX+9J\n" \ + "bM4wVhnnBWtgBMCQQC+qAEmvwcfhauERKYznMVUVksyeuhxhCe7EK\n" \ + "mPh+U2+g0WwdKvGDgO0PPt1gq0ILEjspMDeMHVdTwkaVBo/uMhAkA\n" \ + "Z5SsZyCP2aTOPFDypXRdI4eqRcjaEPOUBq27r3uYb/jeboVb2weLa\n" \ + "L1MmVuHiIHoa5clswPdWVI2y0em2IGoDAkBPSp/v9VKJEZabk9Frd\n" \ + "a+7u4fanrM9QrEjY3KhduslSilXZZSxrWjjAJPyPiqFb3M8XXA26W\n" \ + "nz1KYGnqYKhLcBAkB7dt57n9xfrhDpuyVEv+Uv1D3VVAhZlsaZ5Pp\n" \ + "dcrhrkJn2sa/+O8OKvdrPSeeu/N5WwYhJf61+CPoenMp7IFci\n" \ + "-----END PRIVATE KEY-----\n" + +static int test_load(void) +{ + gnutls_x509_privkey_t key; + const gnutls_datum_t data = { + (unsigned char *)PRIVATE_KEY, + strlen(PRIVATE_KEY) + }; + int err; + + if ((err = gnutls_x509_privkey_init(&key)) < 0) { + fail("Failed to init key %s\n", gnutls_strerror(err)); + exit(1); + } + + if ((err = gnutls_x509_privkey_import(key, &data, + GNUTLS_X509_FMT_PEM)) < 0) { + fail("Failed to import key %s\n", gnutls_strerror(err)); + exit(1); + } + + success("Loaded key\n%s", PRIVATE_KEY); + + gnutls_x509_privkey_deinit(key); + return 0; +} + +void doit(void) +{ + test_load(); +} -- cgit v1.2.1