/*-1* Copyright (c) 2021 Kyle Evans <[email protected]>2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6* 1. Redistributions of source code must retain the above copyright7* notice, this list of conditions and the following disclaimer8* in this position and unchanged.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR14* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES15* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,17* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT18* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,19* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY20* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT21* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF22* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.23*/2425#include <sys/cdefs.h>2627#include <assert.h>28#include <errno.h>29#include <stdlib.h>30#include <string.h>3132#include "pkg.h"33#include "xmalloc.h"34#include "private/pkg.h"35#include "private/pkgsign.h"3637int38pkg_key_new(struct pkg_key **key, const char *keytype, const char *keypath,39pkg_password_cb *cb)40{41struct pkg_key *nkey;42struct pkgsign_ctx *ctx = NULL;43int ret;4445assert(*key == NULL);46assert(keytype != NULL); /* XXX for now. */47if (*keypath == '\0')48return (EPKG_FATAL);4950ret = pkgsign_new_sign(keytype, &ctx);51if (ret != 0)52return (EPKG_FATAL);5354pkgsign_set(ctx, cb, keypath);5556nkey = xcalloc(1, sizeof(*nkey));57nkey->ctx = ctx;5859*key = nkey;60return (EPKG_OK);61}6263void64pkg_key_free(struct pkg_key *key)65{6667pkgsign_free(key->ctx);68free(key);69}7071/*72* Key generation callbacks may take any number of options, so we handle those73* with an iovec. The pkg_key layer does not discriminate, beyond enforcing74* that options come in pairs. The intention is that the first option in every75* pair names the option.76*/77int78pkg_key_create(struct pkg_key *key, const struct iovec *iov, int niov)79{8081/* Malformed arguments; must come in pairs. */82if ((niov % 2) != 0)83return (EPKG_FATAL);8485return (pkgsign_generate(key->ctx, iov, niov));86}8788int89pkg_key_sign_data(struct pkg_key *key, const unsigned char *msg, size_t msgsz,90unsigned char **sig, size_t *siglen)91{9293return (pkgsign_sign_data(key->ctx, msg, msgsz, sig, siglen));94}9596int97pkg_key_info(struct pkg_key *key, struct iovec **iov, int *niov)98{99int rc;100struct iovec *kiov;101int nkiov;102103kiov = NULL;104rc = pkgsign_keyinfo(key->ctx, &kiov, &nkiov);105if (rc != EPKG_OK)106return (rc);107if ((nkiov % 2) != 0) {108free(kiov);109return (EPKG_FATAL);110}111112*iov = kiov;113*niov = nkiov;114115return (EPKG_OK);116}117118int119pkg_key_pubkey(struct pkg_key *key, char **pubkey, size_t *len)120{121122return (pkgsign_pubkey(key->ctx, pubkey, len));123}124125126