Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/libpkg/pkg_key.c
2065 views
1
/*-
2
* Copyright (c) 2021 Kyle Evans <[email protected]>
3
*
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
6
* are met:
7
* 1. Redistributions of source code must retain the above copyright
8
* notice, this list of conditions and the following disclaimer
9
* in this position and unchanged.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
13
*
14
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
*/
25
26
#include <sys/cdefs.h>
27
28
#include <assert.h>
29
#include <errno.h>
30
#include <stdlib.h>
31
#include <string.h>
32
33
#include "pkg.h"
34
#include "xmalloc.h"
35
#include "private/pkg.h"
36
#include "private/pkgsign.h"
37
38
int
39
pkg_key_new(struct pkg_key **key, const char *keytype, const char *keypath,
40
pkg_password_cb *cb)
41
{
42
struct pkg_key *nkey;
43
struct pkgsign_ctx *ctx = NULL;
44
int ret;
45
46
assert(*key == NULL);
47
assert(keytype != NULL); /* XXX for now. */
48
if (*keypath == '\0')
49
return (EPKG_FATAL);
50
51
ret = pkgsign_new_sign(keytype, &ctx);
52
if (ret != 0)
53
return (EPKG_FATAL);
54
55
pkgsign_set(ctx, cb, keypath);
56
57
nkey = xcalloc(1, sizeof(*nkey));
58
nkey->ctx = ctx;
59
60
*key = nkey;
61
return (EPKG_OK);
62
}
63
64
void
65
pkg_key_free(struct pkg_key *key)
66
{
67
68
pkgsign_free(key->ctx);
69
free(key);
70
}
71
72
/*
73
* Key generation callbacks may take any number of options, so we handle those
74
* with an iovec. The pkg_key layer does not discriminate, beyond enforcing
75
* that options come in pairs. The intention is that the first option in every
76
* pair names the option.
77
*/
78
int
79
pkg_key_create(struct pkg_key *key, const struct iovec *iov, int niov)
80
{
81
82
/* Malformed arguments; must come in pairs. */
83
if ((niov % 2) != 0)
84
return (EPKG_FATAL);
85
86
return (pkgsign_generate(key->ctx, iov, niov));
87
}
88
89
int
90
pkg_key_sign_data(struct pkg_key *key, const unsigned char *msg, size_t msgsz,
91
unsigned char **sig, size_t *siglen)
92
{
93
94
return (pkgsign_sign_data(key->ctx, msg, msgsz, sig, siglen));
95
}
96
97
int
98
pkg_key_info(struct pkg_key *key, struct iovec **iov, int *niov)
99
{
100
int rc;
101
struct iovec *kiov;
102
int nkiov;
103
104
kiov = NULL;
105
rc = pkgsign_keyinfo(key->ctx, &kiov, &nkiov);
106
if (rc != EPKG_OK)
107
return (rc);
108
if ((nkiov % 2) != 0) {
109
free(kiov);
110
return (EPKG_FATAL);
111
}
112
113
*iov = kiov;
114
*niov = nkiov;
115
116
return (EPKG_OK);
117
}
118
119
int
120
pkg_key_pubkey(struct pkg_key *key, char **pubkey, size_t *len)
121
{
122
123
return (pkgsign_pubkey(key->ctx, pubkey, len));
124
}
125
126