chiark / gitweb /
Silence two -Wlogical-op warnings.
[gnupg2.git] / common / t-openpgp-oid.c
1 /* t-openpgp-oid.c - Module test for openpgp-oid.c
2  *      Copyright (C) 2011 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
6  * GnuPG is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GnuPG is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <https://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <assert.h>
24
25 #include "util.h"
26
27 #define pass()  do { ; } while(0)
28 #define fail(a,e)                                                       \
29   do { fprintf (stderr, "%s:%d: test %d failed (%s)\n",                 \
30                 __FILE__,__LINE__, (a), gpg_strerror (e));              \
31     exit (1);                                                           \
32   } while(0)
33
34
35 #define BADOID "1.3.6.1.4.1.11591.2.12242973"
36
37
38 static int verbose;
39
40
41
42 static void
43 test_openpgp_oid_from_str (void)
44 {
45    static char *sample_oids[] =
46     {
47       "0.0",
48       "1.0",
49       "1.2.3",
50       "1.2.840.10045.3.1.7",
51       "1.3.132.0.34",
52       "1.3.132.0.35",
53       NULL
54     };
55   gpg_error_t err;
56   gcry_mpi_t a;
57   int idx;
58   char *string;
59   unsigned char *p;
60   unsigned int nbits;
61   size_t length;
62
63   err = openpgp_oid_from_str ("", &a);
64   if (gpg_err_code (err) != GPG_ERR_INV_VALUE)
65     fail (0, err);
66   gcry_mpi_release (a);
67
68   err = openpgp_oid_from_str (".", &a);
69   if (gpg_err_code (err) != GPG_ERR_INV_OID_STRING)
70     fail (0, err);
71   gcry_mpi_release (a);
72
73   err = openpgp_oid_from_str ("0", &a);
74   if (gpg_err_code (err) != GPG_ERR_INV_OID_STRING)
75     fail (0, err);
76   gcry_mpi_release (a);
77
78   for (idx=0; sample_oids[idx]; idx++)
79     {
80       err = openpgp_oid_from_str (sample_oids[idx], &a);
81       if (err)
82         fail (idx, err);
83
84       string = openpgp_oid_to_str (a);
85       if (!string)
86         fail (idx, gpg_error_from_syserror ());
87       if (strcmp (string, sample_oids[idx]))
88         fail (idx, 0);
89       xfree (string);
90
91       p = gcry_mpi_get_opaque (a, &nbits);
92       length = (nbits+7)/8;
93       if (!p || !length || p[0] != length - 1)
94         fail (idx, 0);
95
96       gcry_mpi_release (a);
97     }
98
99 }
100
101
102 static void
103 test_openpgp_oid_to_str (void)
104 {
105   static struct {
106     const char *string;
107     unsigned char der[10];
108   } samples[] = {
109     { "1.2.840.10045.3.1.7",
110       {8, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07 }},
111
112     { "1.3.132.0.34",
113       {5, 0x2B, 0x81, 0x04, 0x00, 0x22 }},
114
115     { "1.3.132.0.35",
116       { 5, 0x2B, 0x81, 0x04, 0x00, 0x23 }},
117
118     { BADOID,
119       { 9, 0x80, 0x02, 0x70, 0x50, 0x25, 0x46, 0xfd, 0x0c, 0xc0 }},
120
121     { BADOID,
122       { 1, 0x80 }},
123
124     { NULL }};
125   gcry_mpi_t a;
126   int idx;
127   char *string;
128   unsigned char *p;
129
130   for (idx=0; samples[idx].string; idx++)
131     {
132       p = xmalloc (samples[idx].der[0]+1);
133       memcpy (p, samples[idx].der, samples[idx].der[0]+1);
134       a = gcry_mpi_set_opaque (NULL, p, (samples[idx].der[0]+1)*8);
135       if (!a)
136         fail (idx, gpg_error_from_syserror ());
137
138       string = openpgp_oid_to_str (a);
139       if (!string)
140         fail (idx, gpg_error_from_syserror ());
141       if (strcmp (string, samples[idx].string))
142         fail (idx, 0);
143       xfree (string);
144       gcry_mpi_release (a);
145     }
146
147 }
148
149
150 static void
151 test_openpgp_oid_is_ed25519 (void)
152 {
153   static struct
154   {
155     int yes;
156     const char *oidstr;
157   } samples[] = {
158     { 0, "0.0" },
159     { 0, "1.3.132.0.35" },
160     { 0, "1.3.6.1.4.1.3029.1.5.0" },
161     { 0, "1.3.6.1.4.1.3029.1.5.1" }, /* Used during Libgcrypt development. */
162     { 0, "1.3.6.1.4.1.3029.1.5.2" },
163     { 0, "1.3.6.1.4.1.3029.1.5.1.0" },
164     { 0, "1.3.6.1.4.1.3029.1.5" },
165     { 0, "1.3.6.1.4.1.11591.15.0" },
166     { 1, "1.3.6.1.4.1.11591.15.1" }, /* Your the one we want.  */
167     { 0, "1.3.6.1.4.1.11591.15.2" },
168     { 0, "1.3.6.1.4.1.11591.15.1.0" },
169     { 0, "1.3.6.1.4.1.11591.15" },
170     { 0, NULL },
171   };
172   gpg_error_t err;
173   gcry_mpi_t a;
174   int idx;
175
176   for (idx=0; samples[idx].oidstr; idx++)
177     {
178       err = openpgp_oid_from_str (samples[idx].oidstr, &a);
179       if (err)
180         fail (idx, err);
181
182       if (openpgp_oid_is_ed25519 (a) != samples[idx].yes)
183         fail (idx, 0);
184
185       gcry_mpi_release (a);
186     }
187
188 }
189
190
191 static void
192 test_openpgp_enum_curves (void)
193 {
194   int iter = 0;
195   const char *name;
196   int p256 = 0;
197   int p384 = 0;
198   int p521 = 0;
199
200   while ((name = openpgp_enum_curves (&iter)))
201     {
202       if (verbose)
203         printf ("curve: %s\n", name);
204       if (!strcmp (name, "nistp256"))
205         p256++;
206       else if (!strcmp (name, "nistp384"))
207         p384++;
208       else if (!strcmp (name, "nistp521"))
209         p521++;
210     }
211
212   if (p256 != 1 || p384 != 1 || p521 != 1)
213     {
214       /* We can only check the basic RFC-6637 requirements.  */
215       fputs ("standard ECC curve missing\n", stderr);
216       exit (1);
217     }
218 }
219
220
221 int
222 main (int argc, char **argv)
223 {
224   if (argc)
225     { argc--; argv++; }
226   if (argc && !strcmp (argv[0], "--verbose"))
227     {
228       verbose = 1;
229       argc--; argv++;
230     }
231
232   test_openpgp_oid_from_str ();
233   test_openpgp_oid_to_str ();
234   test_openpgp_oid_is_ed25519 ();
235   test_openpgp_enum_curves ();
236
237   return 0;
238 }