Merge branch 'upstream' into tizen
[platform/upstream/gnutls.git] / tests / pkcs8-key-decode-encrypted.c
1 /*
2  * Copyright (C) 2015 Red Hat, Inc.
3  *
4  * Author: Daniel Berrange
5  *
6  * This file is part of GnuTLS.
7  *
8  * GnuTLS is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * GnuTLS is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with GnuTLS; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include <gnutls/gnutls.h>
24 #include <gnutls/x509.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28
29 #include "utils.h"
30
31 #define PRIVATE_KEY \
32         "-----BEGIN ENCRYPTED PRIVATE KEY-----\n" \
33         "MIHeMEkGCSqGSIb3DQEFDTA8MBsGCSqGSIb3DQEFDDAOBAiebBrnqPv4owICCAAw\n" \
34         "HQYJYIZIAWUDBAEqBBBykFR6i1My/DYFBYrz1lmABIGQ3XGpp3+v/ENC1S+X7Ay6\n" \
35         "JoquYKuMw6yUmWoGFvPIPA9UWqMve2Uj4l2l96Sywd6iNFP63ow6pIq4wUP6REuY\n" \
36         "ZhCgoAOQomeFqhAhkw6QJCygp5vw2rh9OZ5tiP/Ko6IDTA2rSas91nepHpQOb247\n" \
37         "zta5XzXb5TRkBsVU8tAPADP+wS/vBCS05ne1wmhdD6c6\n" \
38         "-----END ENCRYPTED PRIVATE KEY-----\n"
39
40
41 static int test_decode(void)
42 {
43   gnutls_x509_privkey_t key;
44   const gnutls_datum_t data = {
45     (unsigned char *)PRIVATE_KEY,
46     strlen(PRIVATE_KEY)
47   };
48   int err;
49
50   if ((err = gnutls_x509_privkey_init(&key)) < 0) {
51     fail("Failed to init key %s\n", gnutls_strerror(err));
52   }
53
54   err = gnutls_x509_privkey_import_pkcs8(key, &data,
55                                         GNUTLS_X509_FMT_PEM, "", 0);
56   if (err != GNUTLS_E_DECRYPTION_FAILED) {
57     fail("Unexpected error code: %s/%d\n", gnutls_strerror(err), err);
58   }
59
60   err = gnutls_x509_privkey_import_pkcs8(key, &data,
61                                         GNUTLS_X509_FMT_PEM, "password", 0);
62   if (err != 0) {
63     fail("Unexpected error code: %s\n", gnutls_strerror(err));
64   }
65
66   success("Loaded key\n%s", PRIVATE_KEY);
67
68   gnutls_x509_privkey_deinit(key);
69   return 0;
70 }
71
72 void doit(void)
73 {
74         test_decode();
75 }