Imported Upstream version 3.0.21
[platform/upstream/gnutls.git] / tests / crq_apis.c
1 /*
2  * Copyright (C) 2008-2012 Free Software Foundation, Inc.
3  *
4  * Author: Simon Josefsson
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 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <gnutls/gnutls.h>
31 #include <gnutls/x509.h>
32
33 #include "utils.h"
34
35 static void
36 tls_log_func (int level, const char *str)
37 {
38   fprintf (stderr, "%s |<%d>| %s", "crq_key_id", level, str);
39 }
40
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) };
58
59 void
60 doit (void)
61 {
62   gnutls_x509_privkey_t pkey;
63   gnutls_x509_crt_t crt;
64   gnutls_x509_crq_t crq;
65
66   gnutls_datum_t out;
67
68   size_t s = 0;
69
70   char smallbuf[10];
71
72   int ret;
73
74   ret = gnutls_global_init ();
75   if (ret < 0)
76     fail ("gnutls_global_init\n");
77
78   gnutls_global_set_log_function (tls_log_func);
79   if (debug)
80     gnutls_global_set_log_level (4711);
81
82   ret = gnutls_x509_crq_init (&crq);
83   if (ret != 0)
84     fail ("gnutls_x509_crq_init\n");
85
86   ret = gnutls_x509_privkey_init (&pkey);
87   if (ret != 0)
88     fail ("gnutls_x509_privkey_init\n");
89
90   ret = gnutls_x509_crt_init (&crt);
91   if (ret != 0)
92     fail ("gnutls_x509_crt_init\n");
93
94   ret = gnutls_x509_privkey_import (pkey, &key, GNUTLS_X509_FMT_PEM);
95   if (ret != 0)
96     fail ("gnutls_x509_privkey_import\n");
97
98   ret = gnutls_x509_crq_set_version (crq, 0);
99   if (ret != 0)
100     fail ("gnutls_x509_crq_set_version\n");
101
102   ret = gnutls_x509_crq_set_key (crq, pkey);
103   if (ret != 0)
104     fail ("gnutls_x509_crq_set_key\n");
105
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");
110
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);
114
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);
118
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);
122
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);
126
127   s = 0;
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);
131
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 ("gnutls_x509_crq_get_challenge_password3 %d/%d/%s\n",
136           ret, (int) s, smallbuf);
137
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");
142
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");
147
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");
152
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");
157
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");
162
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");
167
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);
172
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);
178
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);
183
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);
189
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);
196
197   ret = gnutls_x509_crt_set_version (crt, 3);
198   if (ret != 0)
199     fail ("gnutls_x509_crt_set_version\n");
200
201   ret = gnutls_x509_crt_set_crq_extensions (crt, crq);
202   if (ret != 0)
203     fail ("gnutls_x509_crt_set_crq_extensions\n");
204
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);
211
212   gnutls_x509_crq_deinit (crq);
213   gnutls_x509_crt_deinit (crt);
214   gnutls_x509_privkey_deinit (pkey);
215
216   gnutls_global_deinit ();
217 }