2 * Copyright (C) 2008-2012 Free Software Foundation, Inc.
4 * Author: Simon Josefsson
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
30 #include <gnutls/gnutls.h>
31 #include <gnutls/x509.h>
36 tls_log_func (int level
, const char *str
)
38 fprintf (stderr
, "%s |<%d>| %s", "crq_key_id", level
, str
);
41 static unsigned char key_pem
[] =
42 "-----BEGIN RSA PRIVATE KEY-----\n"
43 "MIICXAIBAAKBgQC7ZkP18sXXtozMxd/1iDuxyUtqDqGtIFBACIChT1yj0Phsz+Y8\n"
44 "9+wEdhMXi2SJIlvA3VN8O+18BLuAuSi+jpvGjqClEsv1Vx6i57u3M0mf47tKrmpN\n"
45 "aP/JEeIyjc49gAuNde/YAIGPKAQDoCKNYQQH+rY3fSEHSdIJYWmYkKNYqQIDAQAB\n"
46 "AoGADpmARG5CQxS+AesNkGmpauepiCz1JBF/JwnyiX6vEzUh0Ypd39SZztwrDxvF\n"
47 "PJjQaKVljml1zkJpIDVsqvHdyVdse8M+Qn6hw4x2p5rogdvhhIL1mdWo7jWeVJTF\n"
48 "RKB7zLdMPs3ySdtcIQaF9nUAQ2KJEvldkO3m/bRJFEp54k0CQQDYy+RlTmwRD6hy\n"
49 "7UtMjR0H3CSZJeQ8svMCxHLmOluG9H1UKk55ZBYfRTsXniqUkJBZ5wuV1L+pR9EK\n"
50 "ca89a+1VAkEA3UmBelwEv2u9cAU1QjKjmwju1JgXbrjEohK+3B5y0ESEXPAwNQT9\n"
51 "TrDM1m9AyxYTWLxX93dI5QwNFJtmbtjeBQJARSCWXhsoaDRG8QZrCSjBxfzTCqZD\n"
52 "ZXtl807ymCipgJm60LiAt0JLr4LiucAsMZz6+j+quQbSakbFCACB8SLV1QJBAKZQ\n"
53 "YKf+EPNtnmta/rRKKvySsi3GQZZN+Dt3q0r094XgeTsAqrqujVNfPhTMeP4qEVBX\n"
54 "/iVX2cmMTSh3w3z8MaECQEp0XJWDVKOwcTW6Ajp9SowtmiZ3YDYo1LF9igb4iaLv\n"
55 "sWZGfbnU3ryjvkb6YuFjgtzbZDZHWQCo8/cOtOBmPdk=\n"
56 "-----END RSA PRIVATE KEY-----\n";
57 const gnutls_datum_t key
= { key_pem
, sizeof (key_pem
) };
62 gnutls_x509_privkey_t pkey
;
63 gnutls_x509_crt_t crt
;
64 gnutls_x509_crq_t crq
;
74 ret
= gnutls_global_init ();
76 fail ("gnutls_global_init\n");
78 gnutls_global_set_log_function (tls_log_func
);
80 gnutls_global_set_log_level (4711);
82 ret
= gnutls_x509_crq_init (&crq
);
84 fail ("gnutls_x509_crq_init\n");
86 ret
= gnutls_x509_privkey_init (&pkey
);
88 fail ("gnutls_x509_privkey_init\n");
90 ret
= gnutls_x509_crt_init (&crt
);
92 fail ("gnutls_x509_crt_init\n");
94 ret
= gnutls_x509_privkey_import (pkey
, &key
, GNUTLS_X509_FMT_PEM
);
96 fail ("gnutls_x509_privkey_import\n");
98 ret
= gnutls_x509_crq_set_version (crq
, 0);
100 fail ("gnutls_x509_crq_set_version\n");
102 ret
= gnutls_x509_crq_set_key (crq
, pkey
);
104 fail ("gnutls_x509_crq_set_key\n");
107 ret
= gnutls_x509_crq_get_extension_info (crq
, 0, NULL
, &s
, NULL
);
108 if (ret
!= GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
)
109 fail ("gnutls_x509_crq_get_extension_info\n");
111 ret
= gnutls_x509_crq_set_basic_constraints (crq
, 0, 0);
113 fail ("gnutls_x509_crq_set_basic_constraints %d\n", ret
);
115 ret
= gnutls_x509_crq_set_key_usage (crq
, 0);
117 fail ("gnutls_x509_crq_set_key_usage %d\n", ret
);
119 ret
= gnutls_x509_crq_get_challenge_password (crq
, NULL
, &s
);
120 if (ret
!= GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
)
121 fail ("gnutls_x509_crq_get_challenge_password %d\n", ret
);
123 ret
= gnutls_x509_crq_set_challenge_password (crq
, "foo");
125 fail ("gnutls_x509_crq_set_challenge_password %d\n", ret
);
128 ret
= gnutls_x509_crq_get_challenge_password (crq
, NULL
, &s
);
129 if (ret
!= 0 || s
!= 3)
130 fail ("gnutls_x509_crq_get_challenge_password2 %d/%d\n", ret
, (int) s
);
133 ret
= gnutls_x509_crq_get_challenge_password (crq
, smallbuf
, &s
);
134 if (ret
!= 0 || s
!= 3 || strcmp (smallbuf
, "foo") != 0)
135 fail ("gnutls_x509_crq_get_challenge_password3 %d/%d/%s\n",
136 ret
, (int) s
, smallbuf
);
139 ret
= gnutls_x509_crq_get_extension_info (crq
, 0, NULL
, &s
, NULL
);
141 fail ("gnutls_x509_crq_get_extension_info2\n");
144 ret
= gnutls_x509_crq_get_extension_data (crq
, 0, NULL
, &s
);
146 fail ("gnutls_x509_crq_get_extension_data\n");
148 ret
= gnutls_x509_crq_set_subject_alt_name (crq
, GNUTLS_SAN_DNSNAME
,
151 fail ("gnutls_x509_crq_set_subject_alt_name\n");
153 ret
= gnutls_x509_crq_set_subject_alt_name (crq
, GNUTLS_SAN_DNSNAME
,
156 fail ("gnutls_x509_crq_set_subject_alt_name\n");
158 ret
= gnutls_x509_crq_set_subject_alt_name (crq
, GNUTLS_SAN_DNSNAME
,
161 fail ("gnutls_x509_crq_set_subject_alt_name\n");
163 ret
= gnutls_x509_crq_set_subject_alt_name (crq
, GNUTLS_SAN_DNSNAME
,
166 fail ("gnutls_x509_crq_set_subject_alt_name\n");
169 ret
= gnutls_x509_crq_get_key_purpose_oid (crq
, 0, NULL
, &s
, NULL
);
170 if (ret
!= GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
)
171 fail ("gnutls_x509_crq_get_key_purpose_oid %d\n", ret
);
175 gnutls_x509_crq_set_key_purpose_oid (crq
, GNUTLS_KP_TLS_WWW_SERVER
, 0);
177 fail ("gnutls_x509_crq_set_key_purpose_oid %d\n", ret
);
180 ret
= gnutls_x509_crq_get_key_purpose_oid (crq
, 0, NULL
, &s
, NULL
);
181 if (ret
!= GNUTLS_E_SHORT_MEMORY_BUFFER
)
182 fail ("gnutls_x509_crq_get_key_purpose_oid %d\n", ret
);
186 gnutls_x509_crq_set_key_purpose_oid (crq
, GNUTLS_KP_TLS_WWW_CLIENT
, 1);
188 fail ("gnutls_x509_crq_set_key_purpose_oid2 %d\n", ret
);
190 ret
= gnutls_x509_crq_print (crq
, GNUTLS_CRT_PRINT_FULL
, &out
);
192 fail ("gnutls_x509_crq_print\n");
194 printf ("crq: %.*s\n", out
.size
, out
.data
);
195 gnutls_free (out
.data
);
197 ret
= gnutls_x509_crt_set_version (crt
, 3);
199 fail ("gnutls_x509_crt_set_version\n");
201 ret
= gnutls_x509_crt_set_crq_extensions (crt
, crq
);
203 fail ("gnutls_x509_crt_set_crq_extensions\n");
205 ret
= gnutls_x509_crt_print (crt
, GNUTLS_CRT_PRINT_FULL
, &out
);
207 fail ("gnutls_x509_crt_print\n");
209 printf ("crt: %.*s\n", out
.size
, out
.data
);
210 gnutls_free (out
.data
);
212 gnutls_x509_crq_deinit (crq
);
213 gnutls_x509_crt_deinit (crt
);
214 gnutls_x509_privkey_deinit (pkey
);
216 gnutls_global_deinit ();