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