1 /* t-openpgp-oid.c - Module test for openpgp-oid.c
2 * Copyright (C) 2011 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
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.
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.
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/>.
27 #define pass() do { ; } while(0)
29 do { fprintf (stderr, "%s:%d: test %d failed (%s)\n", \
30 __FILE__,__LINE__, (a), gpg_strerror (e)); \
35 #define BADOID "1.3.6.1.4.1.11591.2.12242973"
43 test_openpgp_oid_from_str (void)
45 static char *sample_oids[] =
50 "1.2.840.10045.3.1.7",
63 err = openpgp_oid_from_str ("", &a);
64 if (gpg_err_code (err) != GPG_ERR_INV_VALUE)
68 err = openpgp_oid_from_str (".", &a);
69 if (gpg_err_code (err) != GPG_ERR_INV_OID_STRING)
73 err = openpgp_oid_from_str ("0", &a);
74 if (gpg_err_code (err) != GPG_ERR_INV_OID_STRING)
78 for (idx=0; sample_oids[idx]; idx++)
80 err = openpgp_oid_from_str (sample_oids[idx], &a);
84 string = openpgp_oid_to_str (a);
86 fail (idx, gpg_error_from_syserror ());
87 if (strcmp (string, sample_oids[idx]))
91 p = gcry_mpi_get_opaque (a, &nbits);
93 if (!p || !length || p[0] != length - 1)
103 test_openpgp_oid_to_str (void)
107 unsigned char der[10];
109 { "1.2.840.10045.3.1.7",
110 {8, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07 }},
113 {5, 0x2B, 0x81, 0x04, 0x00, 0x22 }},
116 { 5, 0x2B, 0x81, 0x04, 0x00, 0x23 }},
119 { 9, 0x80, 0x02, 0x70, 0x50, 0x25, 0x46, 0xfd, 0x0c, 0xc0 }},
130 for (idx=0; samples[idx].string; idx++)
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);
136 fail (idx, gpg_error_from_syserror ());
138 string = openpgp_oid_to_str (a);
140 fail (idx, gpg_error_from_syserror ());
141 if (strcmp (string, samples[idx].string))
144 gcry_mpi_release (a);
151 test_openpgp_oid_is_ed25519 (void)
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" },
176 for (idx=0; samples[idx].oidstr; idx++)
178 err = openpgp_oid_from_str (samples[idx].oidstr, &a);
182 if (openpgp_oid_is_ed25519 (a) != samples[idx].yes)
185 gcry_mpi_release (a);
192 test_openpgp_enum_curves (void)
200 while ((name = openpgp_enum_curves (&iter)))
203 printf ("curve: %s\n", name);
204 if (!strcmp (name, "nistp256"))
206 else if (!strcmp (name, "nistp384"))
208 else if (!strcmp (name, "nistp521"))
212 if (p256 != 1 || p384 != 1 || p521 != 1)
214 /* We can only check the basic RFC-6637 requirements. */
215 fputs ("standard ECC curve missing\n", stderr);
222 main (int argc, char **argv)
226 if (argc && !strcmp (argv[0], "--verbose"))
232 test_openpgp_oid_from_str ();
233 test_openpgp_oid_to_str ();
234 test_openpgp_oid_is_ed25519 ();
235 test_openpgp_enum_curves ();