Corrected bugs in record parsing.
[gnutls.git] / tests / crq_apis.c
blob83ed6d962b966d7b8ecb829ab7258638e7c0639b
1 /*
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
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>
33 #include "utils.h"
35 static void
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) };
59 void
60 doit (void)
62 gnutls_x509_privkey_t pkey;
63 gnutls_x509_crt_t crt;
64 gnutls_x509_crq_t crq;
66 gnutls_datum_t out;
68 size_t s = 0;
70 char smallbuf[10];
72 int ret;
74 ret = gnutls_global_init ();
75 if (ret < 0)
76 fail ("gnutls_global_init\n");
78 gnutls_global_set_log_function (tls_log_func);
79 if (debug)
80 gnutls_global_set_log_level (4711);
82 ret = gnutls_x509_crq_init (&crq);
83 if (ret != 0)
84 fail ("gnutls_x509_crq_init\n");
86 ret = gnutls_x509_privkey_init (&pkey);
87 if (ret != 0)
88 fail ("gnutls_x509_privkey_init\n");
90 ret = gnutls_x509_crt_init (&crt);
91 if (ret != 0)
92 fail ("gnutls_x509_crt_init\n");
94 ret = gnutls_x509_privkey_import (pkey, &key, GNUTLS_X509_FMT_PEM);
95 if (ret != 0)
96 fail ("gnutls_x509_privkey_import\n");
98 ret = gnutls_x509_crq_set_version (crq, 0);
99 if (ret != 0)
100 fail ("gnutls_x509_crq_set_version\n");
102 ret = gnutls_x509_crq_set_key (crq, pkey);
103 if (ret != 0)
104 fail ("gnutls_x509_crq_set_key\n");
106 s = 0;
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);
112 if (ret != 0)
113 fail ("gnutls_x509_crq_set_basic_constraints %d\n", ret);
115 ret = gnutls_x509_crq_set_key_usage (crq, 0);
116 if (ret != 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 ("%d: gnutls_x509_crq_get_challenge_password %d: %s\n", __LINE__, ret, gnutls_strerror(ret));
123 ret = gnutls_x509_crq_set_challenge_password (crq, "foo");
124 if (ret != 0)
125 fail ("gnutls_x509_crq_set_challenge_password %d\n", ret);
127 s = 0;
128 ret = gnutls_x509_crq_get_challenge_password (crq, NULL, &s);
129 if (ret != GNUTLS_E_SHORT_MEMORY_BUFFER || s != 4)
130 fail ("%d: gnutls_x509_crq_get_challenge_password %d: %s (passlen: %d)\n", __LINE__, ret, gnutls_strerror(ret), (int)s);
132 s = 10;
133 ret = gnutls_x509_crq_get_challenge_password (crq, smallbuf, &s);
134 if (ret != 0 || s != 3 || strcmp (smallbuf, "foo") != 0)
135 fail ("%d: gnutls_x509_crq_get_challenge_password3 %d/%d/%s\n",
136 __LINE__, ret, (int) s, smallbuf);
138 s = 0;
139 ret = gnutls_x509_crq_get_extension_info (crq, 0, NULL, &s, NULL);
140 if (ret != 0)
141 fail ("gnutls_x509_crq_get_extension_info2\n");
143 s = 0;
144 ret = gnutls_x509_crq_get_extension_data (crq, 0, NULL, &s);
145 if (ret != 0)
146 fail ("gnutls_x509_crq_get_extension_data\n");
148 ret = gnutls_x509_crq_set_subject_alt_name (crq, GNUTLS_SAN_DNSNAME,
149 "foo", 3, 1);
150 if (ret != 0)
151 fail ("gnutls_x509_crq_set_subject_alt_name\n");
153 ret = gnutls_x509_crq_set_subject_alt_name (crq, GNUTLS_SAN_DNSNAME,
154 "bar", 3, 1);
155 if (ret != 0)
156 fail ("gnutls_x509_crq_set_subject_alt_name\n");
158 ret = gnutls_x509_crq_set_subject_alt_name (crq, GNUTLS_SAN_DNSNAME,
159 "apa", 3, 0);
160 if (ret != 0)
161 fail ("gnutls_x509_crq_set_subject_alt_name\n");
163 ret = gnutls_x509_crq_set_subject_alt_name (crq, GNUTLS_SAN_DNSNAME,
164 "foo", 3, 1);
165 if (ret != 0)
166 fail ("gnutls_x509_crq_set_subject_alt_name\n");
168 s = 0;
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);
173 s = 0;
174 ret =
175 gnutls_x509_crq_set_key_purpose_oid (crq, GNUTLS_KP_TLS_WWW_SERVER, 0);
176 if (ret != 0)
177 fail ("gnutls_x509_crq_set_key_purpose_oid %d\n", ret);
179 s = 0;
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);
184 s = 0;
185 ret =
186 gnutls_x509_crq_set_key_purpose_oid (crq, GNUTLS_KP_TLS_WWW_CLIENT, 1);
187 if (ret != 0)
188 fail ("gnutls_x509_crq_set_key_purpose_oid2 %d\n", ret);
190 ret = gnutls_x509_crq_print (crq, GNUTLS_CRT_PRINT_FULL, &out);
191 if (ret != 0)
192 fail ("gnutls_x509_crq_print\n");
193 if (debug)
194 printf ("crq: %.*s\n", out.size, out.data);
195 gnutls_free (out.data);
197 ret = gnutls_x509_crt_set_version (crt, 3);
198 if (ret != 0)
199 fail ("gnutls_x509_crt_set_version\n");
201 ret = gnutls_x509_crt_set_crq_extensions (crt, crq);
202 if (ret != 0)
203 fail ("gnutls_x509_crt_set_crq_extensions\n");
205 ret = gnutls_x509_crt_print (crt, GNUTLS_CRT_PRINT_FULL, &out);
206 if (ret != 0)
207 fail ("gnutls_x509_crt_print\n");
208 if (debug)
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 ();