| 1 |
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
| 2 |
/* test-data-asn1.c: Test ASN.1 routines |
| 3 |
|
| 4 |
Copyright (C) 2007 Stefan Walter |
| 5 |
|
| 6 |
The Gnome Keyring Library is free software; you can redistribute it and/or |
| 7 |
modify it under the terms of the GNU Library General Public License as |
| 8 |
published by the Free Software Foundation; either version 2 of the |
| 9 |
License, or (at your option) any later version. |
| 10 |
|
| 11 |
The Gnome Keyring Library 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 GNU |
| 14 |
Library General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU Library General Public |
| 17 |
License along with the Gnome Library; see the file COPYING.LIB. If not, |
| 18 |
<http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
Author: Stef Walter <[email protected]> |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
|
| 25 |
#include "gkm/gkm-data-asn1.h" |
| 26 |
|
| 27 |
#include <glib.h> |
| 28 |
#include <glib-object.h> |
| 29 |
#include <gcrypt.h> |
| 30 |
|
| 31 |
#include <stdlib.h> |
| 32 |
#include <stdio.h> |
| 33 |
#include <string.h> |
| 34 |
|
| 35 |
#include "egg/egg-asn1x.h" |
| 36 |
#include "egg/egg-asn1-defs.h" |
| 37 |
#include "egg/egg-libgcrypt.h" |
| 38 |
#include "egg/egg-secure-memory.h" |
| 39 |
|
| 40 |
typedef struct _EggAsn1xDef ASN1_ARRAY_TYPE; |
| 41 |
typedef struct _EggAsn1xDef asn1_static_node; |
| 42 |
#include "test.asn.h" |
| 43 |
|
| 44 |
#define TEST_STRING "test data to write and read in the ASN1 structures" |
| 45 |
|
| 46 |
static GQuark OID_ANSI_SECP256R1; |
| 47 |
|
| 48 |
EGG_SECURE_DEFINE_GLIB_GLOBALS(); |
| 49 |
|
| 50 |
typedef struct { |
| 51 |
GNode *asn1_cert; |
| 52 |
} Test; |
| 53 |
|
| 54 |
static void |
| 55 |
setup (Test *test, gconstpointer unused) |
| 56 |
{ |
| 57 |
GBytes *data; |
| 58 |
gchar *contents; |
| 59 |
gsize length; |
| 60 |
|
| 61 |
if (!g_file_get_contents (SRCDIR "/pkcs11/gkm/fixtures/test-certificate-1.der", &contents, &length, NULL)) |
| 62 |
g_assert_not_reached (); |
| 63 |
|
| 64 |
data = g_bytes_new_take (contents, length); |
| 65 |
test->asn1_cert = egg_asn1x_create_and_decode (pkix_asn1_tab, "Certificate", data); |
| 66 |
g_assert (test->asn1_cert); |
| 67 |
g_bytes_unref (data); |
| 68 |
} |
| 69 |
|
| 70 |
static void |
| 71 |
teardown (Test *test, gconstpointer unused) |
| 72 |
{ |
| 73 |
egg_asn1x_destroy (test->asn1_cert); |
| 74 |
} |
| 75 |
|
| 76 |
static void |
| 77 |
test_asn1_integers (Test *test, gconstpointer unused) |
| 78 |
{ |
| 79 |
GNode *asn; |
| 80 |
gcry_mpi_t mpi, mpt; |
| 81 |
GBytes *data; |
| 82 |
gboolean ret; |
| 83 |
|
| 84 |
asn = egg_asn1x_create (test_asn1_tab, "TestIntegers"); |
| 85 |
g_assert ("asn test structure is null" && asn != NULL); |
| 86 |
|
| 87 |
/* Make a random number */ |
| 88 |
mpi = gcry_mpi_new (512); |
| 89 |
g_return_if_fail (mpi); |
| 90 |
gcry_mpi_randomize (mpi, 512, GCRY_WEAK_RANDOM); |
| 91 |
|
| 92 |
/* Write the mpi out */ |
| 93 |
ret = gkm_data_asn1_write_mpi (egg_asn1x_node (asn, "mpi", NULL), mpi); |
| 94 |
g_assert ("couldn't write mpi to asn1" && ret); |
| 95 |
|
| 96 |
/* Now encode the whole caboodle */ |
| 97 |
data = egg_asn1x_encode (asn, NULL); |
| 98 |
g_assert ("encoding asn1 didn't work" && data != NULL); |
| 99 |
|
| 100 |
egg_asn1x_destroy (asn); |
| 101 |
|
| 102 |
/* Now decode it all nicely */ |
| 103 |
asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestIntegers", data); |
| 104 |
g_assert (asn != NULL); |
| 105 |
|
| 106 |
ret = gkm_data_asn1_read_mpi (egg_asn1x_node (asn, "mpi", NULL), &mpt); |
| 107 |
egg_asn1x_destroy (asn); |
| 108 |
g_assert ("couldn't read mpi from asn1" && ret); |
| 109 |
g_assert ("mpi returned is null" && mpt != NULL); |
| 110 |
g_assert ("mpi is wrong number" && gcry_mpi_cmp (mpi, mpt) == 0); |
| 111 |
|
| 112 |
g_bytes_unref (data); |
| 113 |
gcry_mpi_release (mpi); |
| 114 |
gcry_mpi_release (mpt); |
| 115 |
} |
| 116 |
|
| 117 |
static void |
| 118 |
test_asn1_string_mpi (Test *test, gconstpointer unused) |
| 119 |
{ |
| 120 |
GNode *asn; |
| 121 |
gcry_mpi_t mpi, mpt; |
| 122 |
GBytes *data; |
| 123 |
gboolean ret; |
| 124 |
|
| 125 |
asn = egg_asn1x_create (test_asn1_tab, "TestStringMpi"); |
| 126 |
g_assert ("asn test structure is null" && asn != NULL); |
| 127 |
|
| 128 |
/* Make a random number */ |
| 129 |
mpi = gcry_mpi_new (512); |
| 130 |
g_return_if_fail (mpi); |
| 131 |
gcry_mpi_randomize (mpi, 512, GCRY_WEAK_RANDOM); |
| 132 |
|
| 133 |
/* Write the mpi out */ |
| 134 |
ret = gkm_data_asn1_write_string_mpi (egg_asn1x_node (asn, "mpi", NULL), mpi); |
| 135 |
g_assert ("couldn't write mpi to bit string in asn1" && ret); |
| 136 |
|
| 137 |
/* Now encode the whole caboodle */ |
| 138 |
data = egg_asn1x_encode (asn, NULL); |
| 139 |
g_assert ("encoding asn1 didn't work" && data != NULL); |
| 140 |
|
| 141 |
egg_asn1x_destroy (asn); |
| 142 |
|
| 143 |
/* Now decode it all nicely */ |
| 144 |
asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestStringMpi", data); |
| 145 |
g_assert (asn != NULL); |
| 146 |
|
| 147 |
ret = gkm_data_asn1_read_string_mpi (egg_asn1x_node (asn, "mpi", NULL), &mpt); |
| 148 |
egg_asn1x_destroy (asn); |
| 149 |
g_assert ("couldn't read mpi from octet string in asn1" && ret); |
| 150 |
g_assert ("mpi returned is null" && mpt != NULL); |
| 151 |
g_assert ("mpi is wrong number" && gcry_mpi_cmp (mpi, mpt) == 0); |
| 152 |
|
| 153 |
g_bytes_unref (data); |
| 154 |
gcry_mpi_release (mpi); |
| 155 |
gcry_mpi_release (mpt); |
| 156 |
} |
| 157 |
|
| 158 |
static void |
| 159 |
test_asn1_bit_string (Test *test, gconstpointer unused) |
| 160 |
{ |
| 161 |
GNode *asn; |
| 162 |
GBytes *data; |
| 163 |
gboolean ret; |
| 164 |
GBytes *source, *target; |
| 165 |
gsize target_bits, source_bits; |
| 166 |
|
| 167 |
asn = egg_asn1x_create (test_asn1_tab, "TestBitString"); |
| 168 |
g_assert ("asn test structure is null" && asn != NULL); |
| 169 |
|
| 170 |
/* Create a string */ |
| 171 |
source = g_bytes_new (TEST_STRING, strlen(TEST_STRING)); |
| 172 |
g_return_if_fail (source); |
| 173 |
source_bits = g_bytes_get_size(source)*8; |
| 174 |
|
| 175 |
/* Write the string out */ |
| 176 |
ret = gkm_data_asn1_write_bit_string (egg_asn1x_node (asn, "data", NULL), |
| 177 |
source, source_bits); |
| 178 |
g_assert ("couldn't write string to asn1" && ret); |
| 179 |
|
| 180 |
/* Now encode the whole caboodle */ |
| 181 |
data = egg_asn1x_encode (asn, NULL); |
| 182 |
g_assert ("encoding asn1 didn't work" && data != NULL); |
| 183 |
|
| 184 |
egg_asn1x_destroy (asn); |
| 185 |
|
| 186 |
/* Now decode it all nicely */ |
| 187 |
asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestBitString", data); |
| 188 |
g_assert (asn != NULL); |
| 189 |
|
| 190 |
ret = gkm_data_asn1_read_bit_string (egg_asn1x_node (asn, "data", NULL), |
| 191 |
&target, &target_bits); |
| 192 |
egg_asn1x_destroy (asn); |
| 193 |
g_assert ("couldn't read bit string from asn1" && ret); |
| 194 |
g_assert ("bit string returned is null" && target != NULL); |
| 195 |
g_assert ("Source and target length differ" && target_bits == source_bits); |
| 196 |
g_assert ("Bit strings differ" && g_bytes_equal (source, target)); |
| 197 |
|
| 198 |
g_bytes_unref (data); |
| 199 |
g_bytes_unref (source); |
| 200 |
g_bytes_unref (target); |
| 201 |
} |
| 202 |
/* XXX test some incomplete octets */ |
| 203 |
|
| 204 |
static void |
| 205 |
test_asn1_string (Test *test, gconstpointer unused) |
| 206 |
{ |
| 207 |
GNode *asn; |
| 208 |
GBytes *data; |
| 209 |
gboolean ret; |
| 210 |
GBytes *source, *target; |
| 211 |
|
| 212 |
asn = egg_asn1x_create (test_asn1_tab, "TestString"); |
| 213 |
g_assert ("asn test structure is null" && asn != NULL); |
| 214 |
|
| 215 |
/* Create a string */ |
| 216 |
source = g_bytes_new (TEST_STRING, strlen(TEST_STRING)); |
| 217 |
g_return_if_fail (source); |
| 218 |
|
| 219 |
/* Write the string out */ |
| 220 |
ret = gkm_data_asn1_write_string (egg_asn1x_node (asn, "data", NULL), |
| 221 |
source); |
| 222 |
g_assert ("couldn't write string to asn1" && ret); |
| 223 |
|
| 224 |
/* Now encode the whole caboodle */ |
| 225 |
data = egg_asn1x_encode (asn, NULL); |
| 226 |
g_assert ("encoding asn1 didn't work" && data != NULL); |
| 227 |
|
| 228 |
egg_asn1x_destroy (asn); |
| 229 |
|
| 230 |
/* Now decode it all nicely */ |
| 231 |
asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestString", data); |
| 232 |
g_assert (asn != NULL); |
| 233 |
|
| 234 |
ret = gkm_data_asn1_read_string (egg_asn1x_node (asn, "data", NULL), |
| 235 |
&target); |
| 236 |
egg_asn1x_destroy (asn); |
| 237 |
g_assert ("couldn't read string from asn1" && ret); |
| 238 |
g_assert ("string returned is null" && target != NULL); |
| 239 |
g_assert ("The strings differ" && g_bytes_equal (source, target)); |
| 240 |
|
| 241 |
g_bytes_unref (data); |
| 242 |
g_bytes_unref (source); |
| 243 |
g_bytes_unref (target); |
| 244 |
} |
| 245 |
|
| 246 |
static void |
| 247 |
test_asn1_oid (Test *test, gconstpointer unused) |
| 248 |
{ |
| 249 |
GNode *asn; |
| 250 |
GBytes *data; |
| 251 |
gboolean ret; |
| 252 |
GQuark source, target; |
| 253 |
|
| 254 |
asn = egg_asn1x_create (test_asn1_tab, "TestOid"); |
| 255 |
g_assert ("asn test structure is null" && asn != NULL); |
| 256 |
|
| 257 |
/* Create a OID Quark */ |
| 258 |
OID_ANSI_SECP256R1 = g_quark_from_static_string("1.2.840.10045.3.1.7"); |
| 259 |
source = OID_ANSI_SECP256R1; |
| 260 |
|
| 261 |
/* Write the OID out */ |
| 262 |
ret = gkm_data_asn1_write_oid (egg_asn1x_node (asn, "oid", NULL), source); |
| 263 |
g_assert ("couldn't write OID to asn1" && ret); |
| 264 |
|
| 265 |
/* Now encode the whole caboodle */ |
| 266 |
data = egg_asn1x_encode (asn, NULL); |
| 267 |
g_assert ("encoding asn1 didn't work" && data != NULL); |
| 268 |
|
| 269 |
egg_asn1x_destroy (asn); |
| 270 |
|
| 271 |
/* Now decode it all nicely */ |
| 272 |
asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestOid", data); |
| 273 |
g_assert (asn != NULL); |
| 274 |
|
| 275 |
ret = gkm_data_asn1_read_oid (egg_asn1x_node (asn, "oid", NULL), &target); |
| 276 |
egg_asn1x_destroy (asn); |
| 277 |
g_assert ("couldn't read oid from asn1" && ret); |
| 278 |
g_assert ("oid returned is 0" && target != 0); |
| 279 |
g_assert ("mpi is wrong number" && source == target); |
| 280 |
|
| 281 |
g_bytes_unref (data); |
| 282 |
} |
| 283 |
|
| 284 |
int |
| 285 |
main (int argc, char **argv) |
| 286 |
{ |
| 287 |
#if !GLIB_CHECK_VERSION(2,35,0) |
| 288 |
g_type_init (); |
| 289 |
#endif |
| 290 |
egg_libgcrypt_initialize(); |
| 291 |
|
| 292 |
g_test_init (&argc, &argv, NULL); |
| 293 |
|
| 294 |
g_test_add ("/gkm/data-asn1/integers", Test, NULL, setup, test_asn1_integers, teardown); |
| 295 |
g_test_add ("/gkm/data-asn1/string_mpi", Test, NULL, setup, test_asn1_string_mpi, teardown); |
| 296 |
g_test_add ("/gkm/data-asn1/bit_string", Test, NULL, setup, test_asn1_bit_string, teardown); |
| 297 |
g_test_add ("/gkm/data-asn1/string", Test, NULL, setup, test_asn1_string, teardown); |
| 298 |
g_test_add ("/gkm/data-asn1/oid", Test, NULL, setup, test_asn1_oid, teardown); |
| 299 |
|
| 300 |
return g_test_run (); |
| 301 |
} |