remove p11-kit in gnutls.spec file
[platform/upstream/gnutls.git] / tests / infoaccess.c
1 /*
2  * Copyright (C) 2011-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 char cert_with_aia_data[] =
36     "-----BEGIN CERTIFICATE-----\n"
37     "MIIF0DCCBLigAwIBAgIEOrZQizANBgkqhkiG9w0BAQUFADB/MQswCQYDVQQGEwJC\n"
38     "TTEZMBcGA1UEChMQUXVvVmFkaXMgTGltaXRlZDElMCMGA1UECxMcUm9vdCBDZXJ0\n"
39     "aWZpY2F0aW9uIEF1dGhvcml0eTEuMCwGA1UEAxMlUXVvVmFkaXMgUm9vdCBDZXJ0\n"
40     "aWZpY2F0aW9uIEF1dGhvcml0eTAeFw0wMTAzMTkxODMzMzNaFw0yMTAzMTcxODMz\n"
41     "MzNaMH8xCzAJBgNVBAYTAkJNMRkwFwYDVQQKExBRdW9WYWRpcyBMaW1pdGVkMSUw\n"
42     "IwYDVQQLExxSb290IENlcnRpZmljYXRpb24gQXV0aG9yaXR5MS4wLAYDVQQDEyVR\n"
43     "dW9WYWRpcyBSb290IENlcnRpZmljYXRpb24gQXV0aG9yaXR5MIIBIjANBgkqhkiG\n"
44     "9w0BAQEFAAOCAQ8AMIIBCgKCAQEAv2G1lVO6V/z68mcLOhrfEYBklbTRvM16z/Yp\n"
45     "li4kVEAkOPcahdxYTMukJ0KX0J+DisPkBgNbAKVRHnAEdOLB1Dqr1607BxgFjv2D\n"
46     "rOpm2RgbaIr1VxqYuvXtdj182d6UajtLF8HVj71lODqV0D1VNk7feVcxKh7YWWVJ\n"
47     "WCCYfqtffp/p1k3sg3Spx2zY7ilKhSoGFPlU5tPaZQeLYzcS19Dsw3sgQUSj7cug\n"
48     "F+FxZc4dZjH3dgEZyH0DWLaVSR2mEiboxgx24ONmy+pdpibu5cxfvWenAScOospU\n"
49     "xbF6lR1xHkopigPcakXBpBlebzbNw6Kwt/5cOOJSvPhEQ+aQuwIDAQABo4ICUjCC\n"
50     "Ak4wPQYIKwYBBQUHAQEEMTAvMC0GCCsGAQUFBzABhiFodHRwczovL29jc3AucXVv\n"
51     "dmFkaXNvZmZzaG9yZS5jb20wDwYDVR0TAQH/BAUwAwEB/zCCARoGA1UdIASCAREw\n"
52     "ggENMIIBCQYJKwYBBAG+WAABMIH7MIHUBggrBgEFBQcCAjCBxxqBxFJlbGlhbmNl\n"
53     "IG9uIHRoZSBRdW9WYWRpcyBSb290IENlcnRpZmljYXRlIGJ5IGFueSBwYXJ0eSBh\n"
54     "c3N1bWVzIGFjY2VwdGFuY2Ugb2YgdGhlIHRoZW4gYXBwbGljYWJsZSBzdGFuZGFy\n"
55     "ZCB0ZXJtcyBhbmQgY29uZGl0aW9ucyBvZiB1c2UsIGNlcnRpZmljYXRpb24gcHJh\n"
56     "Y3RpY2VzLCBhbmQgdGhlIFF1b1ZhZGlzIENlcnRpZmljYXRlIFBvbGljeS4wIgYI\n"
57     "KwYBBQUHAgEWFmh0dHA6Ly93d3cucXVvdmFkaXMuYm0wHQYDVR0OBBYEFItLbe3T\n"
58     "KbkGGew5Oanwl4Rqy+/fMIGuBgNVHSMEgaYwgaOAFItLbe3TKbkGGew5Oanwl4Rq\n"
59     "y+/foYGEpIGBMH8xCzAJBgNVBAYTAkJNMRkwFwYDVQQKExBRdW9WYWRpcyBMaW1p\n"
60     "dGVkMSUwIwYDVQQLExxSb290IENlcnRpZmljYXRpb24gQXV0aG9yaXR5MS4wLAYD\n"
61     "VQQDEyVRdW9WYWRpcyBSb290IENlcnRpZmljYXRpb24gQXV0aG9yaXR5ggQ6tlCL\n"
62     "MA4GA1UdDwEB/wQEAwIBBjANBgkqhkiG9w0BAQUFAAOCAQEAitQUtf70mpKnGdSk\n"
63     "fnIYj9lofFIk3WdvOXrEql494liwTXCYhGHoG+NpGA7O+0dQoE7/8CQfvbLO9Sf8\n"
64     "7C9TqnN7Az10buYWnuulLsS/VidQK2K6vkscPFVcQR0kvoIgR13VRH56FmjffU1R\n"
65     "cHhXHTMe/QKZnAzNCgVPx7uOpHX6Sm2xgI4JVrmcGmD+XcHXetwReNDWXcG31a0y\n"
66     "mQM6isxUJTkxgXsTIlG6Rmyhu576BGxJJnSP0nPrzDCi5upZIof4l/UO/erMkqQW\n"
67     "xFIY6iHOsfHmhIHluqmGKPJDWl0Snawe2ajlCmqnf6CHKc/yiU3U7MXi5nrQNiOK\n"
68     "SnQ2+Q==\n" "-----END CERTIFICATE-----\n";
69
70 const gnutls_datum_t cert_with_aia = {
71         (void *) cert_with_aia_data, sizeof(cert_with_aia_data)
72 };
73
74 void doit(void)
75 {
76         gnutls_x509_crt_t crt;
77         int ret;
78         gnutls_datum_t data;
79         unsigned int critical;
80
81         ret = global_init();
82         if (ret < 0) {
83                 fail("global_init\n");
84                 exit(1);
85         }
86
87         ret = gnutls_x509_crt_init(&crt);
88         if (ret != 0) {
89                 fail("gnutls_x509_crt_init\n");
90                 exit(1);
91         }
92
93         ret =
94             gnutls_x509_crt_import(crt, &cert_with_aia,
95                                    GNUTLS_X509_FMT_PEM);
96         if (ret < 0) {
97                 fail("gnutls_x509_crt_import\n");
98                 exit(1);
99         }
100
101         /* test null input */
102         ret =
103             gnutls_x509_crt_get_authority_info_access(NULL, 0, 0, NULL,
104                                                       NULL);
105         if (ret != GNUTLS_E_INVALID_REQUEST) {
106                 fail("gnutls_x509_crt_get_authority_info_access null input\n");
107                 exit(1);
108         }
109
110         /* test unused enum */
111         ret =
112             gnutls_x509_crt_get_authority_info_access(crt, 0, 44, NULL,
113                                                       NULL);
114         if (ret != GNUTLS_E_INVALID_REQUEST) {
115                 fail("gnutls_x509_crt_get_authority_info_access insane input\n");
116                 exit(1);
117         }
118
119         /* test basic query with null output */
120         ret = gnutls_x509_crt_get_authority_info_access
121             (crt, 0, GNUTLS_IA_ACCESSMETHOD_OID, NULL, NULL);
122         if (ret < 0) {
123                 fail("gnutls_x509_crt_get_authority_info_access "
124                      "GNUTLS_IA_ACCESSMETHOD_OID null output critical\n");
125                 exit(1);
126         }
127
128         /* test same as previous but also check that critical flag is
129            correct */
130         ret = gnutls_x509_crt_get_authority_info_access
131             (crt, 0, GNUTLS_IA_ACCESSMETHOD_OID, NULL, &critical);
132         if (ret < 0) {
133                 fail("gnutls_x509_crt_get_authority_info_access "
134                      "GNUTLS_IA_ACCESSMETHOD_OID null output\n");
135                 exit(1);
136         }
137
138         if (critical != 0) {
139                 fail("gnutls_x509_crt_get_authority_info_access "
140                      "critical failed: %d\n", critical);
141                 exit(1);
142         }
143
144         /* basic query of another type */
145         ret = gnutls_x509_crt_get_authority_info_access
146             (crt, 0, GNUTLS_IA_ACCESSLOCATION_GENERALNAME_TYPE, NULL,
147              NULL);
148         if (ret < 0) {
149                 fail("gnutls_x509_crt_get_authority_info_access "
150                      "GNUTLS_IA_ACCESSLOCATION_GENERALNAME_TYPE null output\n");
151                 exit(1);
152         }
153
154         /* basic query of another type, with out-of-bound sequence */
155         ret = gnutls_x509_crt_get_authority_info_access
156             (crt, 1, GNUTLS_IA_ACCESSLOCATION_GENERALNAME_TYPE, NULL,
157              NULL);
158         if (ret != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) {
159                 fail("gnutls_x509_crt_get_authority_info_access "
160                      "GNUTLS_IA_ACCESSLOCATION_GENERALNAME_TYPE out-of-bounds\n");
161                 exit(1);
162         }
163
164         /* basic query and check output value */
165         ret = gnutls_x509_crt_get_authority_info_access
166             (crt, 0, GNUTLS_IA_ACCESSMETHOD_OID, &data, NULL);
167         if (ret < 0) {
168                 fail("gnutls_x509_crt_get_authority_info_access "
169                      "GNUTLS_IA_ACCESSMETHOD_OID\n");
170                 exit(1);
171         }
172
173         if (memcmp("1.3.6.1.5.5.7.48.1", data.data, data.size) != 0) {
174                 fail("memcmp OCSP OID failed\n");
175                 exit(1);
176         }
177         gnutls_free(data.data);
178
179         /* basic query of another type and check output value */
180         ret = gnutls_x509_crt_get_authority_info_access
181             (crt, 0, GNUTLS_IA_ACCESSLOCATION_GENERALNAME_TYPE, &data,
182              NULL);
183         if (ret < 0) {
184                 fail("gnutls_x509_crt_get_authority_info_access "
185                      "GNUTLS_IA_ACCESSLOCATION_GENERALNAME_TYPE\n");
186                 exit(1);
187         }
188
189         if (memcmp("uniformResourceIdentifier", data.data, data.size) != 0) {
190                 fail("memcmp URI failed\n");
191                 exit(1);
192         }
193         gnutls_free(data.data);
194
195         /* specific query */
196         ret = gnutls_x509_crt_get_authority_info_access
197             (crt, 0, GNUTLS_IA_URI, &data, NULL);
198         if (ret < 0) {
199                 fail("gnutls_x509_crt_get_authority_info_access GNUTLS_IA_URI\n");
200                 exit(1);
201         }
202
203         if (memcmp
204             ("https://ocsp.quovadisoffshore.com", data.data,
205              data.size) != 0) {
206                 fail("memcmp URI value failed\n");
207                 exit(1);
208         }
209         gnutls_free(data.data);
210
211         /* even more specific query */
212         ret = gnutls_x509_crt_get_authority_info_access
213             (crt, 0, GNUTLS_IA_OCSP_URI, &data, NULL);
214         if (ret < 0) {
215                 fail("gnutls_x509_crt_get_authority_info_access GNUTLS_IA_OCSP_URI\n");
216                 exit(1);
217         }
218
219         if (memcmp
220             ("https://ocsp.quovadisoffshore.com", data.data,
221              data.size) != 0) {
222                 fail("memcmp URI value failed\n");
223                 exit(1);
224         }
225         gnutls_free(data.data);
226
227         gnutls_x509_crt_deinit(crt);
228
229         gnutls_global_deinit();
230
231 }