Add announcement message.
[gnutls.git] / tests / crq_apis.c
blob6d875ef90ef8f6cf29b260b9908233bd80632653
1 /*
2 * Copyright (C) 2008, 2009, 2010 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 gnutls_global_set_log_level (4711);
81 ret = gnutls_x509_crq_init (&crq);
82 if (ret != 0)
83 fail ("gnutls_x509_crq_init\n");
85 ret = gnutls_x509_privkey_init (&pkey);
86 if (ret != 0)
87 fail ("gnutls_x509_privkey_init\n");
89 ret = gnutls_x509_crt_init (&crt);
90 if (ret != 0)
91 fail ("gnutls_x509_crt_init\n");
93 ret = gnutls_x509_privkey_import (pkey, &key, GNUTLS_X509_FMT_PEM);
94 if (ret != 0)
95 fail ("gnutls_x509_privkey_import\n");
97 ret = gnutls_x509_crq_set_version (crq, 0);
98 if (ret != 0)
99 fail ("gnutls_x509_crq_set_version\n");
101 ret = gnutls_x509_crq_set_key (crq, pkey);
102 if (ret != 0)
103 fail ("gnutls_x509_crq_set_key\n");
105 s = 0;
106 ret = gnutls_x509_crq_get_extension_info (crq, 0, NULL, &s, NULL);
107 if (ret != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
108 fail ("gnutls_x509_crq_get_extension_info\n");
110 ret = gnutls_x509_crq_set_basic_constraints (crq, 0, 0);
111 if (ret != 0)
112 fail ("gnutls_x509_crq_set_basic_constraints %d\n", ret);
114 ret = gnutls_x509_crq_set_key_usage (crq, 0);
115 if (ret != 0)
116 fail ("gnutls_x509_crq_set_key_usage %d\n", ret);
118 ret = gnutls_x509_crq_get_challenge_password (crq, NULL, &s);
119 if (ret != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
120 fail ("gnutls_x509_crq_get_challenge_password %d\n", ret);
122 ret = gnutls_x509_crq_set_challenge_password (crq, "foo");
123 if (ret != 0)
124 fail ("gnutls_x509_crq_set_challenge_password %d\n", ret);
126 s = 0;
127 ret = gnutls_x509_crq_get_challenge_password (crq, NULL, &s);
128 if (ret != 0 || s != 3)
129 fail ("gnutls_x509_crq_get_challenge_password2 %d/%d\n", ret, (int)s);
131 s = 10;
132 ret = gnutls_x509_crq_get_challenge_password (crq, smallbuf, &s);
133 if (ret != 0 || s != 3 || strcmp (smallbuf, "foo") != 0)
134 fail ("gnutls_x509_crq_get_challenge_password3 %d/%d/%s\n",
135 ret,(int) s, smallbuf);
137 s = 0;
138 ret = gnutls_x509_crq_get_extension_info (crq, 0, NULL, &s, NULL);
139 if (ret != 0)
140 fail ("gnutls_x509_crq_get_extension_info2\n");
142 s = 0;
143 ret = gnutls_x509_crq_get_extension_data (crq, 0, NULL, &s);
144 if (ret != 0)
145 fail ("gnutls_x509_crq_get_extension_data\n");
147 ret = gnutls_x509_crq_set_subject_alt_name (crq, GNUTLS_SAN_DNSNAME,
148 "foo", 3, 1);
149 if (ret != 0)
150 fail ("gnutls_x509_crq_set_subject_alt_name\n");
152 ret = gnutls_x509_crq_set_subject_alt_name (crq, GNUTLS_SAN_DNSNAME,
153 "bar", 3, 1);
154 if (ret != 0)
155 fail ("gnutls_x509_crq_set_subject_alt_name\n");
157 ret = gnutls_x509_crq_set_subject_alt_name (crq, GNUTLS_SAN_DNSNAME,
158 "apa", 3, 0);
159 if (ret != 0)
160 fail ("gnutls_x509_crq_set_subject_alt_name\n");
162 ret = gnutls_x509_crq_set_subject_alt_name (crq, GNUTLS_SAN_DNSNAME,
163 "foo", 3, 1);
164 if (ret != 0)
165 fail ("gnutls_x509_crq_set_subject_alt_name\n");
167 s = 0;
168 ret = gnutls_x509_crq_get_key_purpose_oid (crq, 0, NULL, &s, NULL);
169 if (ret != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
170 fail ("gnutls_x509_crq_get_key_purpose_oid %d\n", ret);
172 s = 0;
173 ret =
174 gnutls_x509_crq_set_key_purpose_oid (crq, GNUTLS_KP_TLS_WWW_SERVER, 0);
175 if (ret != 0)
176 fail ("gnutls_x509_crq_set_key_purpose_oid %d\n", ret);
178 s = 0;
179 ret = gnutls_x509_crq_get_key_purpose_oid (crq, 0, NULL, &s, NULL);
180 if (ret != GNUTLS_E_SHORT_MEMORY_BUFFER)
181 fail ("gnutls_x509_crq_get_key_purpose_oid %d\n", ret);
183 s = 0;
184 ret =
185 gnutls_x509_crq_set_key_purpose_oid (crq, GNUTLS_KP_TLS_WWW_CLIENT, 1);
186 if (ret != 0)
187 fail ("gnutls_x509_crq_set_key_purpose_oid2 %d\n", ret);
189 ret = gnutls_x509_crq_print (crq, GNUTLS_CRT_PRINT_FULL, &out);
190 if (ret != 0)
191 fail ("gnutls_x509_crq_print\n");
192 printf ("crq: %.*s\n", out.size, out.data);
193 gnutls_free (out.data);
195 ret = gnutls_x509_crt_set_version (crt, 3);
196 if (ret != 0)
197 fail ("gnutls_x509_crt_set_version\n");
199 ret = gnutls_x509_crt_set_crq_extensions (crt, crq);
200 if (ret != 0)
201 fail ("gnutls_x509_crt_set_crq_extensions\n");
203 ret = gnutls_x509_crt_print (crt, GNUTLS_CRT_PRINT_FULL, &out);
204 if (ret != 0)
205 fail ("gnutls_x509_crt_print\n");
206 printf ("crt: %.*s\n", out.size, out.data);
207 gnutls_free (out.data);
209 gnutls_x509_crq_deinit (crq);
210 gnutls_x509_crt_deinit (crt);
211 gnutls_x509_privkey_deinit (pkey);
213 gnutls_global_deinit ();