/*-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 <assert.h>26#include <errno.h>27#include <stdlib.h>28#include <string.h>2930#include "pkg.h"31#include "xmalloc.h"32#include "private/pkg.h"33#include "private/pkgsign.h"3435int36pkg_key_new(struct pkg_key **key, const char *keytype, const char *keypath,37pkg_password_cb *cb)38{39struct pkg_key *nkey;40struct pkgsign_ctx *ctx = NULL;41int ret;4243assert(*key == NULL);44assert(keytype != NULL); /* XXX for now. */45if (*keypath == '\0')46return (EPKG_FATAL);4748ret = pkgsign_new_sign(keytype, &ctx);49if (ret != 0)50return (EPKG_FATAL);5152pkgsign_set(ctx, cb, keypath);5354nkey = xcalloc(1, sizeof(*nkey));55nkey->ctx = ctx;5657*key = nkey;58return (EPKG_OK);59}6061void62pkg_key_free(struct pkg_key *key)63{6465pkgsign_free(key->ctx);66free(key);67}6869/*70* Key generation callbacks may take any number of options, so we handle those71* with an iovec. The pkg_key layer does not discriminate, beyond enforcing72* that options come in pairs. The intention is that the first option in every73* pair names the option.74*/75int76pkg_key_create(struct pkg_key *key, const struct iovec *iov, int niov)77{7879/* Malformed arguments; must come in pairs. */80if ((niov % 2) != 0)81return (EPKG_FATAL);8283return (pkgsign_generate(key->ctx, iov, niov));84}8586int87pkg_key_sign_data(struct pkg_key *key, const unsigned char *msg, size_t msgsz,88unsigned char **sig, size_t *siglen)89{9091return (pkgsign_sign_data(key->ctx, msg, msgsz, sig, siglen));92}9394int95pkg_key_info(struct pkg_key *key, struct iovec **iov, int *niov)96{97int rc;98struct iovec *kiov;99int nkiov;100101kiov = NULL;102rc = pkgsign_keyinfo(key->ctx, &kiov, &nkiov);103if (rc != EPKG_OK)104return (rc);105if ((nkiov % 2) != 0) {106free(kiov);107return (EPKG_FATAL);108}109110*iov = kiov;111*niov = nkiov;112113return (EPKG_OK);114}115116int117pkg_key_pubkey(struct pkg_key *key, char **pubkey, size_t *len)118{119120return (pkgsign_pubkey(key->ctx, pubkey, len));121}122123124