1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
/* t-dnparser.c - basic test for the DN parser
* Copyright (C) 2002, 2006 g10 Code GmbH
*
* This file is part of KSBA.
*
* KSBA is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* KSBA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <errno.h>
#include "../src/ksba.h"
#include "t-common.h"
static void
test_0 (void)
{
static char *good_strings[] = {
"C=de,O=g10 Code,OU=qa,CN=Pépé le Moko",
"C= de, O=g10 Code , OU=qa ,CN=Pépé le Moko",
"CN=www.gnupg.org",
" CN=www.gnupg.org ",
"C=fr,L=Paris,CN=Julien Duvivier,EMAIL=julien@example.org",
NULL
};
gpg_error_t err;
int i;
unsigned char *buf;
size_t off, len;
for (i=0; good_strings[i]; i++)
{
err = ksba_dn_str2der (good_strings[i], &buf, &len);
if (err)
{
fprintf (stderr, "%s:%d: ksba_dn_str2der failed for `%s': %s\n",
__FILE__,__LINE__, good_strings[i], gpg_strerror (err));
exit (1);
}
err = ksba_dn_teststr (good_strings[i], 0, &off, &len);
if (err)
{
fprintf (stderr, "%s:%d: ksba_dn_teststr failed for `%s': %s\n",
__FILE__,__LINE__, good_strings[i], gpg_strerror (err));
exit (1);
}
xfree (buf);
}
}
static void
test_1 (void)
{
static char *empty_elements[] = {
"C=de,O=foo,OU=,CN=joe",
"C=de,O=foo,OU= ,CN=joe",
"C=de,O=foo,OU=\"\" ,CN=joe",
"C=de,O=foo,OU=",
"C=de,O=foo,OU= ",
"C=,O=foo,OU=bar ",
"C = ,O=foo,OU=bar ",
"C=",
NULL
};
gpg_error_t err;
int i;
unsigned char *buf;
size_t off, len;
for (i=0; empty_elements[i]; i++)
{
err = ksba_dn_str2der (empty_elements[i], &buf, &len);
if (gpg_err_code (err) != GPG_ERR_SYNTAX)
fail ("empty element not detected");
err = ksba_dn_teststr (empty_elements[i], 0, &off, &len);
if (!err)
fail ("ksba_dn_teststr returned no error");
printf ("string ->%s<- error at %lu.%lu (%.*s)\n",
empty_elements[i], (unsigned long)off, (unsigned long)len,
(int)len, empty_elements[i]+off);
xfree (buf);
}
}
static void
test_2 (void)
{
static char *invalid_labels[] = {
"C=de,FOO=something,O=bar",
"Y=foo, C=baz",
NULL
};
gpg_error_t err;
int i;
unsigned char *buf;
size_t off, len;
for (i=0; invalid_labels[i]; i++)
{
err = ksba_dn_str2der (invalid_labels[i], &buf, &len);
if (gpg_err_code (err) != GPG_ERR_UNKNOWN_NAME)
fail ("invalid label not detected");
err = ksba_dn_teststr (invalid_labels[i], 0, &off, &len);
if (!err)
fail ("ksba_dn_test_str returned no error");
printf ("string ->%s<- error at %lu.%lu (%.*s)\n",
invalid_labels[i], (unsigned long)off, (unsigned long)len,
(int)len, invalid_labels[i]+off);
xfree (buf);
}
}
int
main (int argc, char **argv)
{
char inputbuf[4096];
int inputlen;
unsigned char *buf;
size_t len;
gpg_error_t err;
if (argc == 2 && !strcmp (argv[1], "--to-str") )
{ /* Read the DER encoded DN from stdin write the string to stdout */
inputlen = fread (inputbuf, 1, sizeof inputbuf, stdin);
if (!feof (stdin))
fail ("read error or input too large");
fail ("no yet implemented");
}
else if (argc == 2 && !strcmp (argv[1], "--to-der") )
{ /* Read the String from stdin write the DER encoding to stdout */
inputlen = fread (inputbuf, 1, sizeof inputbuf, stdin);
if (!feof (stdin))
fail ("read error or input too large");
err = ksba_dn_str2der (inputbuf, &buf, &len);
fail_if_err (err);
fwrite (buf, len, 1, stdout);
}
else if (argc == 1)
{
test_0 ();
test_1 ();
test_2 ();
}
else
{
fprintf (stderr, "usage: t-dnparser [--to-str|--to-der]\n");
return 1;
}
return 0;
}
|