71b827824d9ebf1d9fa180368a7dab4ac3f74e14
[platform/upstream/p11-kit.git] / common / tests / test-oid.c
1 /*
2  * Copyright (c) 2012 Red Hat Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  *     * Redistributions of source code must retain the above
9  *       copyright notice, this list of conditions and the
10  *       following disclaimer.
11  *     * Redistributions in binary form must reproduce the
12  *       above copyright notice, this list of conditions and
13  *       the following disclaimer in the documentation and/or
14  *       other materials provided with the distribution.
15  *     * The names of contributors to this software may not be
16  *       used to endorse or promote products derived from this
17  *       software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
30  * DAMAGE.
31  *
32  * Author: Stef Walter <stefw@gnome.org>
33  */
34
35 #include "config.h"
36 #include "CuTest.h"
37
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41
42 #include "debug.h"
43 #include "oid.h"
44
45 #include <libtasn1.h>
46
47 #include "pkix.asn.h"
48
49 static void
50 test_known_oids (CuTest *cu)
51 {
52         char buffer[128];
53         node_asn *definitions = NULL;
54         node_asn *node;
55         int ret;
56         int len;
57         int i;
58
59         struct {
60                 const unsigned char *oid;
61                 size_t length;
62                 const char *string;
63         } known_oids[] = {
64                 { P11_OID_SUBJECT_KEY_IDENTIFIER, sizeof (P11_OID_SUBJECT_KEY_IDENTIFIER), "2.5.29.14", },
65                 { P11_OID_KEY_USAGE, sizeof (P11_OID_KEY_USAGE), "2.5.29.15", },
66                 { P11_OID_BASIC_CONSTRAINTS, sizeof (P11_OID_BASIC_CONSTRAINTS), "2.5.29.19" },
67                 { P11_OID_EXTENDED_KEY_USAGE, sizeof (P11_OID_EXTENDED_KEY_USAGE), "2.5.29.37" },
68                 { P11_OID_OPENSSL_REJECT, sizeof (P11_OID_OPENSSL_REJECT), "1.3.6.1.4.1.3319.6.10.1" },
69                 { P11_OID_SERVER_AUTH, sizeof (P11_OID_SERVER_AUTH), P11_OID_SERVER_AUTH_STR },
70                 { P11_OID_CLIENT_AUTH, sizeof (P11_OID_CLIENT_AUTH), P11_OID_CLIENT_AUTH_STR },
71                 { P11_OID_CODE_SIGNING, sizeof (P11_OID_CODE_SIGNING), P11_OID_CODE_SIGNING_STR },
72                 { P11_OID_EMAIL_PROTECTION, sizeof (P11_OID_EMAIL_PROTECTION), P11_OID_EMAIL_PROTECTION_STR },
73                 { P11_OID_IPSEC_END_SYSTEM, sizeof (P11_OID_IPSEC_END_SYSTEM), P11_OID_IPSEC_END_SYSTEM_STR },
74                 { P11_OID_IPSEC_TUNNEL, sizeof (P11_OID_IPSEC_TUNNEL), P11_OID_IPSEC_TUNNEL_STR },
75                 { P11_OID_IPSEC_USER, sizeof (P11_OID_IPSEC_USER), P11_OID_IPSEC_USER_STR },
76                 { P11_OID_TIME_STAMPING, sizeof (P11_OID_TIME_STAMPING), P11_OID_TIME_STAMPING_STR },
77                 { P11_OID_RESERVED_PURPOSE, sizeof (P11_OID_RESERVED_PURPOSE), P11_OID_RESERVED_PURPOSE_STR },
78                 { NULL },
79         };
80
81         ret = asn1_array2tree (pkix_asn1_tab, &definitions, NULL);
82         CuAssertTrue (cu, ret == ASN1_SUCCESS);
83
84         for (i = 0; known_oids[i].oid != NULL; i++) {
85
86                 CuAssertTrue (cu, p11_oid_simple (known_oids[i].oid, known_oids[i].length));
87                 CuAssertIntEquals (cu, known_oids[i].length, p11_oid_length (known_oids[i].oid));
88                 CuAssertTrue (cu, p11_oid_equal (known_oids[i].oid, known_oids[i].oid));
89
90                 if (i > 0)
91                         CuAssertTrue (cu, !p11_oid_equal (known_oids[i].oid, known_oids[i - 1].oid));
92
93                 /* AttributeType is a OBJECT IDENTIFIER */
94                 ret = asn1_create_element (definitions, "PKIX1.AttributeType", &node);
95                 CuAssertTrue (cu, ret == ASN1_SUCCESS);
96
97                 ret = asn1_der_decoding (&node, known_oids[i].oid, known_oids[i].length, NULL);
98                 CuAssertTrue (cu, ret == ASN1_SUCCESS);
99
100                 len = sizeof (buffer);
101                 ret = asn1_read_value (node, "", buffer, &len);
102                 CuAssertTrue (cu, ret == ASN1_SUCCESS);
103
104                 CuAssertStrEquals (cu, known_oids[i].string, buffer);
105
106                 asn1_delete_structure (&node);
107         }
108
109         asn1_delete_structure (&definitions);
110 }
111
112 int
113 main (void)
114 {
115         CuString *output = CuStringNew ();
116         CuSuite* suite = CuSuiteNew ();
117         int ret;
118
119         putenv ("P11_KIT_STRICT=1");
120         p11_debug_init ();
121
122         SUITE_ADD_TEST (suite, test_known_oids);
123
124         CuSuiteRun (suite);
125         CuSuiteSummary (suite, output);
126         CuSuiteDetails (suite, output);
127         printf ("%s\n", output->buffer);
128         ret = suite->failCount;
129         CuSuiteDelete (suite);
130         CuStringDelete (output);
131
132         return ret;
133 }