Path: blob/main/contrib/libder/tests/test_privkey.c
39478 views
/*-1* Copyright (c) 2024 Kyle Evans <[email protected]>2*3* SPDX-License-Identifier: BSD-2-Clause4*/56#include <sys/stat.h>78#include <assert.h>9#include <fcntl.h>10#include <inttypes.h>11#include <stdio.h>12#include <stdlib.h>13#include <string.h>14#include <unistd.h>1516#include <libder.h>1718#include "test_common.h"1920/*21* Note that the choice of secp112r1 is completely arbitrary. I was mainly22* shooting for something pretty weak to avoid people trying to "catch me"23* checking in private key material, even though it's very incredibly clearly24* just for a test case.25*/26static const uint8_t oid_secp112r1[] =27{ 0x2b, 0x81, 0x04, 0x00, 0x06 };2829static const uint8_t privdata[] = { 0xa5, 0xf5, 0x2a, 0x56, 0x61, 0xe3, 0x58,300x76, 0x5c, 0x4f, 0xd6, 0x8d, 0x60, 0x54 };3132static const uint8_t pubdata[] = { 0x00, 0x04, 0xae, 0x69, 0x41, 0x0d, 0x9c,330x9b, 0xf2, 0x34, 0xf6, 0x2d, 0x7c, 0x91, 0xe1, 0xc7, 0x7f, 0x23, 0xa0,340x84, 0x34, 0x5c, 0x38, 0x26, 0xd8, 0xcf, 0xbe, 0xf7, 0xdc, 0x8a };3536static void37test_interface(struct libder_object *root)38{39const uint8_t *data;40size_t datasz;41struct libder_object *keystring, *oid;4243/* Grab the oid first. */44oid = libder_obj_child(root, 2);45assert(oid != NULL); /* Actually just the container... */46assert(libder_obj_type_simple(oid) == 0xa0);4748oid = libder_obj_child(oid, 0);49assert(oid != NULL); /* Now *that*'s an OID. */50assert(libder_obj_type_simple(oid) == BT_OID);51data = libder_obj_data(oid, &datasz);52assert(datasz == sizeof(oid_secp112r1));53assert(memcmp(oid_secp112r1, data, datasz) == 0);5455keystring = libder_obj_child(root, 1);56assert(keystring != NULL);57assert(libder_obj_type_simple(keystring) == BT_OCTETSTRING);5859data = libder_obj_data(keystring, &datasz);60assert(datasz == sizeof(privdata));61assert(memcmp(privdata, data, datasz) == 0);62}6364/* buf and bufszs are just our reference */65static void66test_construction(struct libder_ctx *ctx, const uint8_t *buf, size_t bufsz)67{68uint8_t *out;69struct libder_object *obj, *oidp, *pubp, *root;70struct libder_object *keystring;71size_t outsz;72uint8_t data;7374root = libder_obj_alloc_simple(ctx, BT_SEQUENCE, NULL, 0);75assert(root != NULL);7677data = 1;78obj = libder_obj_alloc_simple(ctx, BT_INTEGER, &data, 1);79assert(obj != NULL);80assert(libder_obj_append(root, obj));8182/* Private key material */83obj = libder_obj_alloc_simple(ctx, BT_OCTETSTRING, privdata, sizeof(privdata));84assert(obj != NULL);85assert(libder_obj_append(root, obj));8687/* Now throw in the OID and pubkey containers */88oidp = libder_obj_alloc_simple(ctx,89(BC_CONTEXT << 6) | BER_TYPE_CONSTRUCTED_MASK | 0, NULL, 0);90assert(oidp != NULL);91assert(libder_obj_append(root, oidp));9293pubp = libder_obj_alloc_simple(ctx,94(BC_CONTEXT << 6) | BER_TYPE_CONSTRUCTED_MASK | 1, NULL, 0);95assert(pubp != NULL);96assert(libder_obj_append(root, pubp));9798/* Actually add the OID */99obj = libder_obj_alloc_simple(ctx, BT_OID, oid_secp112r1, sizeof(oid_secp112r1));100assert(obj != NULL);101assert(libder_obj_append(oidp, obj));102103/* Finally, add the pubkey */104obj = libder_obj_alloc_simple(ctx, BT_BITSTRING, pubdata, sizeof(pubdata));105assert(obj != NULL);106assert(libder_obj_append(pubp, obj));107108out = NULL;109outsz = 0;110out = libder_write(ctx, root, out, &outsz);111assert(out != NULL);112assert(outsz == bufsz);113114assert(memcmp(out, buf, bufsz) == 0);115116libder_obj_free(root);117free(out);118}119120int121main(int argc, char *argv[])122{123struct stat sb;124struct libder_ctx *ctx;125struct libder_object *root;126uint8_t *buf, *out;127size_t bufsz, outsz, rootsz;128ssize_t readsz;129int dfd, error, fd;130131dfd = open_progdir(argv[0]);132133fd = openat(dfd, "repo.priv", O_RDONLY);134assert(fd >= 0);135136close(dfd);137dfd = -1;138139error = fstat(fd, &sb);140assert(error == 0);141142bufsz = sb.st_size;143buf = malloc(bufsz);144assert(buf != NULL);145146readsz = read(fd, buf, bufsz);147close(fd);148149assert(readsz == bufsz);150151ctx = libder_open();152rootsz = bufsz;153libder_set_verbose(ctx, 2);154root = libder_read(ctx, buf, &rootsz);155156assert(root != NULL);157assert(rootsz == bufsz);158159test_interface(root);160test_construction(ctx, buf, bufsz);161162outsz = 0;163out = NULL;164out = libder_write(ctx, root, out, &outsz);165assert(out != NULL);166assert(outsz == bufsz);167168assert(memcmp(buf, out, outsz) == 0);169170free(out);171free(buf);172libder_obj_free(root);173libder_close(ctx);174}175176177