Import PKCS #12 keys
[gnutls.git] / tests / key-openssl.c
blob02d174ed160d4ba6f1628321ac4f7c75efc5b00f
1 /*
2 * Copyright (C) 2008-2012 Free Software Foundation, Inc.
4 * Author: David Marín Carreño
6 * This file is part of GnuTLS.
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.
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.
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
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <gnutls/gnutls.h>
31 #include <gnutls/x509.h>
32 #include <gnutls/abstract.h>
34 #include "utils.h"
36 static void
37 tls_log_func (int level, const char *str)
39 fprintf (stderr, "%s |<%d>| %s", "crq_key_id", level, str);
42 const char key1[] =
43 "-----BEGIN RSA PRIVATE KEY-----\n"
44 "Proc-Type: 4,ENCRYPTED\n"
45 "DEK-Info: DES-EDE3-CBC,82B2F7684A1713F8\n"
46 "\n"
47 "1zzOuu89dfFc2UkFCtSJBsBeEFxV8wE84OSxoWu4aYkPhl1LR08BchaTbjeLTP0b\n"
48 "t961vVpva0ekJkwGDEgmqlGjmhJq9y2sJfq7IeYa8OdTilfGrG1xeJ1QGBi6SCfR\n"
49 "s/PhkMxwGBtrZ2Z7bEcLT5dQKmKRqsthnClQggmngvk7zX7bPk0hKQKvf+FDxt6x\n"
50 "hzEaF3k9juU6vAVVSakrZ4QDqk9MUuTGHx0ksTDcC4EESS0l3Ybuum/rAzR4lQKR\n"
51 "4OLmAeYBDl+l/PSMllfd5x/z1YXYoiAbkpT4ix0lyZJgHrvrYIeUtJk2ODiMHezL\n"
52 "9BbK7EobtOGmrDLUNVX5BpdaExkWMGkioqzs2QqD/VkKu8RcNSsHVGqkdWKuhzXo\n"
53 "wcczQ+RiHckN2uy/zApubEWZNLPeDQ499kaF+QdZ+h4RM6E1r1Gu+A==\n"
54 "-----END RSA PRIVATE KEY-----\n";
56 const char key2[] =
57 "-----BEGIN RSA PRIVATE KEY-----\n"
58 "Proc-Type: 4,ENCRYPTED\n"
59 "DEK-Info: AES-128-CBC,2A57FF97B701B3F760145D7446929481\n"
60 "\n"
61 "mGAPhSw48wZBnkHOhfMDg8yL2IBgMuTmeKE4xoHi7T6isHBNfkqMd0iJ+DJP/OKb\n"
62 "t+7lkKjj/xQ7w/bOBvBxlfRe4MW6+ejCdAFD9XSolW6WN6CEJPMI4UtmOK5inqcC\n"
63 "8l2l54f/VGrVN9uavU3KlXCjrd3Jp9B0Mu4Zh/UU4+EWs9rJAZfLIn+vHZ3OHetx\n"
64 "g74LdV7nC7lt/fjxc1caNIfgHs40dUt9FVrnJvAtkcNMtcjX/D+L8ZrLgQzIWFcs\n"
65 "WAbUZj7Me22mCli3RPET7Je37K59IzfWgbWFCGaNu3X02g5xtCfdcn/Uqy9eofH0\n"
66 "YjKRhpgXPeGJCkoRqDeUHQNPpVP5HrzDZMVK3E4DC03C8qvgsYvuwYt3KkbG2fuA\n"
67 "F3bDyqlxSOm7uxF/K3YzI44v8/D8GGnLBTpN+ANBdiY=\n"
68 "-----END RSA PRIVATE KEY-----\n";
70 void
71 doit (void)
73 gnutls_x509_privkey_t pkey;
74 int ret;
75 gnutls_datum_t key;
77 ret = gnutls_global_init ();
78 if (ret < 0)
79 fail ("gnutls_global_init: %d\n", ret);
81 gnutls_global_set_log_function (tls_log_func);
82 if (debug)
83 gnutls_global_set_log_level (4711);
85 ret = gnutls_x509_privkey_init (&pkey);
86 if (ret < 0)
87 fail ("gnutls_x509_privkey_init: %d\n", ret);
89 key.data = (void*)key1;
90 key.size = sizeof(key1);
91 ret = gnutls_x509_privkey_import_openssl (pkey, &key, "123456");
92 if (ret < 0)
94 fail ("gnutls_x509_privkey_import_openssl (key1): %s\n", gnutls_strerror(ret)) ;
96 gnutls_x509_privkey_deinit (pkey);
98 ret = gnutls_x509_privkey_init (&pkey);
99 if (ret < 0)
100 fail ("gnutls_x509_privkey_init: %d\n", ret);
102 key.data = (void*)key2;
103 key.size = sizeof(key2);
104 ret = gnutls_x509_privkey_import_openssl (pkey, &key, "a123456");
105 if (ret < 0)
107 fail ("gnutls_x509_privkey_import_openssl (key2): %s\n", gnutls_strerror(ret)) ;
110 gnutls_x509_privkey_deinit (pkey);
112 ret = gnutls_x509_privkey_init (&pkey);
113 if (ret < 0)
114 fail ("gnutls_x509_privkey_init: %d\n", ret);
116 key.data = (void*)key1;
117 key.size = sizeof(key1);
118 ret = gnutls_x509_privkey_import2 (pkey, &key, GNUTLS_X509_FMT_PEM, "123456", 0);
119 if (ret < 0)
121 fail ("gnutls_x509_privkey_import2: %s\n", gnutls_strerror(ret)) ;
123 gnutls_x509_privkey_deinit (pkey);
125 gnutls_global_deinit ();