Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/external/libder/tests/test_pubkey.c
2066 views
1
/*-
2
* Copyright (c) 2024 Kyle Evans <[email protected]>
3
*
4
* SPDX-License-Identifier: BSD-2-Clause
5
*/
6
7
#include <sys/stat.h>
8
9
#include <assert.h>
10
#include <fcntl.h>
11
#include <inttypes.h>
12
#include <stdio.h>
13
#include <stdlib.h>
14
#include <string.h>
15
#include <unistd.h>
16
17
#include <libder.h>
18
19
#include "test_common.h"
20
21
static const uint8_t oid_ecpubkey[] =
22
{ 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01 };
23
static const uint8_t oid_secp256k1[] =
24
{ 0x2b, 0x81, 0x04, 0x00, 0x0a };
25
26
static const uint8_t pubdata[] = { 0x00, 0x04, 0xd1, 0x76, 0x20, 0x39, 0xe5, 0x3e,
27
0x67, 0x7d, 0x8d, 0xfd, 0xc4, 0x21, 0x20, 0xcd, 0xb0, 0xbf, 0x47, 0x87, 0x6a,
28
0xf8, 0x07, 0x73, 0xbe, 0xbe, 0xd5, 0xbb, 0x3c, 0xbc, 0x32, 0x93, 0xd9, 0xdf,
29
0x96, 0x25, 0xb7, 0x0e, 0x3c, 0x55, 0x12, 0xee, 0x7a, 0x02, 0x39, 0x0f, 0xee,
30
0x7b, 0xfe, 0x1a, 0x93, 0x76, 0xf7, 0xc2, 0xac, 0x05, 0xba, 0x9a, 0x83, 0x37,
31
0xf5, 0xcd, 0x55, 0x57, 0x39, 0x6f };
32
33
static void
34
test_interface(struct libder_object *root)
35
{
36
const uint8_t *data;
37
size_t datasz;
38
struct libder_object *keystring;
39
40
keystring = libder_obj_child(root, 1);
41
assert(keystring != NULL);
42
assert(libder_obj_type_simple(keystring) == BT_BITSTRING);
43
44
data = libder_obj_data(keystring, &datasz);
45
assert(datasz == sizeof(pubdata));
46
assert(memcmp(pubdata, data, datasz) == 0);
47
}
48
49
/* buf and bufszs are just our reference */
50
static void
51
test_construction(struct libder_ctx*ctx, const uint8_t *buf, size_t bufsz)
52
{
53
uint8_t *out;
54
struct libder_object *obj, *params, *root;
55
struct libder_object *keystring;
56
size_t outsz;
57
58
root = libder_obj_alloc_simple(ctx, BT_SEQUENCE, NULL, 0);
59
assert(root != NULL);
60
61
params = libder_obj_alloc_simple(ctx, BT_SEQUENCE, NULL, 0);
62
assert(params != NULL);
63
assert(libder_obj_append(root, params));
64
65
keystring = libder_obj_alloc_simple(ctx, BT_BITSTRING, pubdata, sizeof(pubdata));
66
assert(keystring != NULL);
67
assert(libder_obj_append(root, keystring));
68
69
/* Now go back and build the two params, id and curve */
70
obj = libder_obj_alloc_simple(ctx, BT_OID, oid_ecpubkey, sizeof(oid_ecpubkey));
71
assert(obj != NULL);
72
assert(libder_obj_append(params, obj));
73
74
obj = libder_obj_alloc_simple(ctx, BT_OID, oid_secp256k1, sizeof(oid_secp256k1));
75
assert(obj != NULL);
76
assert(libder_obj_append(params, obj));
77
78
out = NULL;
79
outsz = 0;
80
out = libder_write(ctx, root, out, &outsz);
81
assert(out != NULL);
82
assert(outsz == bufsz);
83
assert(memcmp(out, buf, bufsz) == 0);
84
85
libder_obj_free(root);
86
free(out);
87
}
88
89
int
90
main(int argc, char *argv[])
91
{
92
struct stat sb;
93
struct libder_ctx *ctx;
94
struct libder_object *root;
95
uint8_t *buf, *out;
96
size_t bufsz, outsz, rootsz;
97
ssize_t readsz;
98
int dfd, error, fd;
99
100
dfd = open_progdir(argv[0]);
101
102
fd = openat(dfd, "repo.pub", O_RDONLY);
103
assert(fd >= 0);
104
105
close(dfd);
106
dfd = -1;
107
108
error = fstat(fd, &sb);
109
assert(error == 0);
110
111
bufsz = sb.st_size;
112
buf = malloc(bufsz);
113
assert(buf != NULL);
114
115
readsz = read(fd, buf, bufsz);
116
close(fd);
117
118
assert(readsz == bufsz);
119
120
ctx = libder_open();
121
rootsz = bufsz;
122
libder_set_verbose(ctx, 2);
123
root = libder_read(ctx, buf, &rootsz);
124
125
assert(root != NULL);
126
assert(rootsz == bufsz);
127
128
test_interface(root);
129
test_construction(ctx, buf, bufsz);
130
131
outsz = 0;
132
out = NULL;
133
out = libder_write(ctx, root, out, &outsz);
134
assert(out != NULL);
135
assert(outsz == bufsz);
136
137
assert(memcmp(buf, out, outsz) == 0);
138
139
free(out);
140
free(buf);
141
libder_obj_free(root);
142
libder_close(ctx);
143
}
144
145