dst_api.c revision 1.3 1 1.2 christos /* $NetBSD: dst_api.c,v 1.3 2019/01/09 16:55:11 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Portions Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 1.1 christos *
6 1.1 christos * This Source Code Form is subject to the terms of the Mozilla Public
7 1.1 christos * License, v. 2.0. If a copy of the MPL was not distributed with this
8 1.1 christos * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 1.1 christos *
10 1.1 christos * See the COPYRIGHT file distributed with this work for additional
11 1.1 christos * information regarding copyright ownership.
12 1.1 christos *
13 1.1 christos * Portions Copyright (C) Network Associates, Inc.
14 1.1 christos *
15 1.1 christos * Permission to use, copy, modify, and/or distribute this software for any
16 1.1 christos * purpose with or without fee is hereby granted, provided that the above
17 1.1 christos * copyright notice and this permission notice appear in all copies.
18 1.1 christos *
19 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
20 1.1 christos * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
21 1.1 christos * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
22 1.1 christos * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
23 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
24 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
25 1.1 christos * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 1.1 christos */
27 1.1 christos
28 1.1 christos /*! \file */
29 1.1 christos
30 1.1 christos #include <config.h>
31 1.1 christos
32 1.3 christos #include <inttypes.h>
33 1.3 christos #include <stdbool.h>
34 1.1 christos #include <stdlib.h>
35 1.1 christos #include <time.h>
36 1.1 christos
37 1.1 christos #include <isc/buffer.h>
38 1.1 christos #include <isc/dir.h>
39 1.1 christos #include <isc/fsaccess.h>
40 1.1 christos #include <isc/lex.h>
41 1.1 christos #include <isc/mem.h>
42 1.1 christos #include <isc/once.h>
43 1.1 christos #include <isc/platform.h>
44 1.1 christos #include <isc/print.h>
45 1.1 christos #include <isc/refcount.h>
46 1.1 christos #include <isc/random.h>
47 1.1 christos #include <isc/safe.h>
48 1.1 christos #include <isc/string.h>
49 1.1 christos #include <isc/time.h>
50 1.1 christos #include <isc/util.h>
51 1.1 christos #include <isc/file.h>
52 1.1 christos
53 1.1 christos #include <pk11/site.h>
54 1.1 christos
55 1.1 christos #define DST_KEY_INTERNAL
56 1.1 christos
57 1.1 christos #include <dns/fixedname.h>
58 1.1 christos #include <dns/keyvalues.h>
59 1.1 christos #include <dns/name.h>
60 1.1 christos #include <dns/rdata.h>
61 1.1 christos #include <dns/rdataclass.h>
62 1.1 christos #include <dns/ttl.h>
63 1.1 christos #include <dns/types.h>
64 1.1 christos
65 1.1 christos #include <dst/result.h>
66 1.1 christos
67 1.1 christos #include "dst_internal.h"
68 1.1 christos
69 1.1 christos #define DST_AS_STR(t) ((t).value.as_textregion.base)
70 1.1 christos
71 1.1 christos static dst_func_t *dst_t_func[DST_MAX_ALGS];
72 1.1 christos
73 1.3 christos static bool dst_initialized = false;
74 1.1 christos
75 1.1 christos void gss_log(int level, const char *fmt, ...) ISC_FORMAT_PRINTF(2, 3);
76 1.1 christos
77 1.1 christos /*
78 1.1 christos * Static functions.
79 1.1 christos */
80 1.1 christos static dst_key_t * get_key_struct(const dns_name_t *name,
81 1.1 christos unsigned int alg,
82 1.1 christos unsigned int flags,
83 1.1 christos unsigned int protocol,
84 1.1 christos unsigned int bits,
85 1.1 christos dns_rdataclass_t rdclass,
86 1.1 christos dns_ttl_t ttl,
87 1.1 christos isc_mem_t *mctx);
88 1.1 christos static isc_result_t write_public_key(const dst_key_t *key, int type,
89 1.1 christos const char *directory);
90 1.1 christos static isc_result_t buildfilename(dns_name_t *name,
91 1.1 christos dns_keytag_t id,
92 1.1 christos unsigned int alg,
93 1.1 christos unsigned int type,
94 1.1 christos const char *directory,
95 1.1 christos isc_buffer_t *out);
96 1.1 christos static isc_result_t computeid(dst_key_t *key);
97 1.1 christos static isc_result_t frombuffer(const dns_name_t *name,
98 1.1 christos unsigned int alg,
99 1.1 christos unsigned int flags,
100 1.1 christos unsigned int protocol,
101 1.1 christos dns_rdataclass_t rdclass,
102 1.1 christos isc_buffer_t *source,
103 1.1 christos isc_mem_t *mctx,
104 1.1 christos dst_key_t **keyp);
105 1.1 christos
106 1.1 christos static isc_result_t algorithm_status(unsigned int alg);
107 1.1 christos
108 1.1 christos static isc_result_t addsuffix(char *filename, int len,
109 1.1 christos const char *dirname, const char *ofilename,
110 1.1 christos const char *suffix);
111 1.1 christos
112 1.1 christos #define RETERR(x) \
113 1.1 christos do { \
114 1.1 christos result = (x); \
115 1.1 christos if (result != ISC_R_SUCCESS) \
116 1.1 christos goto out; \
117 1.2 christos } while (/*CONSTCOND*/0)
118 1.1 christos
119 1.1 christos #define CHECKALG(alg) \
120 1.1 christos do { \
121 1.1 christos isc_result_t _r; \
122 1.1 christos _r = algorithm_status(alg); \
123 1.1 christos if (_r != ISC_R_SUCCESS) \
124 1.1 christos return (_r); \
125 1.2 christos } while (/*CONSTCOND*/0); \
126 1.1 christos
127 1.1 christos isc_result_t
128 1.3 christos dst_lib_init(isc_mem_t *mctx, const char *engine) {
129 1.1 christos isc_result_t result;
130 1.1 christos
131 1.1 christos REQUIRE(mctx != NULL);
132 1.3 christos REQUIRE(dst_initialized == false);
133 1.1 christos
134 1.1 christos UNUSED(engine);
135 1.1 christos
136 1.1 christos dst_result_register();
137 1.1 christos
138 1.1 christos memset(dst_t_func, 0, sizeof(dst_t_func));
139 1.1 christos RETERR(dst__hmacmd5_init(&dst_t_func[DST_ALG_HMACMD5]));
140 1.1 christos RETERR(dst__hmacsha1_init(&dst_t_func[DST_ALG_HMACSHA1]));
141 1.1 christos RETERR(dst__hmacsha224_init(&dst_t_func[DST_ALG_HMACSHA224]));
142 1.1 christos RETERR(dst__hmacsha256_init(&dst_t_func[DST_ALG_HMACSHA256]));
143 1.1 christos RETERR(dst__hmacsha384_init(&dst_t_func[DST_ALG_HMACSHA384]));
144 1.1 christos RETERR(dst__hmacsha512_init(&dst_t_func[DST_ALG_HMACSHA512]));
145 1.3 christos RETERR(dst__openssl_init(mctx, engine));
146 1.3 christos RETERR(dst__openssldh_init(&dst_t_func[DST_ALG_DH]));
147 1.3 christos #if USE_OPENSSL
148 1.1 christos RETERR(dst__opensslrsa_init(&dst_t_func[DST_ALG_RSAMD5],
149 1.1 christos DST_ALG_RSAMD5));
150 1.1 christos RETERR(dst__opensslrsa_init(&dst_t_func[DST_ALG_RSASHA1],
151 1.1 christos DST_ALG_RSASHA1));
152 1.1 christos RETERR(dst__opensslrsa_init(&dst_t_func[DST_ALG_NSEC3RSASHA1],
153 1.1 christos DST_ALG_NSEC3RSASHA1));
154 1.1 christos RETERR(dst__opensslrsa_init(&dst_t_func[DST_ALG_RSASHA256],
155 1.1 christos DST_ALG_RSASHA256));
156 1.1 christos RETERR(dst__opensslrsa_init(&dst_t_func[DST_ALG_RSASHA512],
157 1.1 christos DST_ALG_RSASHA512));
158 1.1 christos RETERR(dst__opensslecdsa_init(&dst_t_func[DST_ALG_ECDSA256]));
159 1.1 christos RETERR(dst__opensslecdsa_init(&dst_t_func[DST_ALG_ECDSA384]));
160 1.1 christos #ifdef HAVE_OPENSSL_ED25519
161 1.1 christos RETERR(dst__openssleddsa_init(&dst_t_func[DST_ALG_ED25519]));
162 1.1 christos #endif
163 1.1 christos #ifdef HAVE_OPENSSL_ED448
164 1.1 christos RETERR(dst__openssleddsa_init(&dst_t_func[DST_ALG_ED448]));
165 1.1 christos #endif
166 1.3 christos #endif /* USE_OPENSSL */
167 1.3 christos
168 1.3 christos #if USE_PKCS11
169 1.1 christos RETERR(dst__pkcs11_init(mctx, engine));
170 1.1 christos RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_RSAMD5]));
171 1.1 christos RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_RSASHA1]));
172 1.1 christos RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_NSEC3RSASHA1]));
173 1.1 christos RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_RSASHA256]));
174 1.1 christos RETERR(dst__pkcs11rsa_init(&dst_t_func[DST_ALG_RSASHA512]));
175 1.1 christos RETERR(dst__pkcs11ecdsa_init(&dst_t_func[DST_ALG_ECDSA256]));
176 1.1 christos RETERR(dst__pkcs11ecdsa_init(&dst_t_func[DST_ALG_ECDSA384]));
177 1.1 christos #ifdef HAVE_PKCS11_ED25519
178 1.1 christos RETERR(dst__pkcs11eddsa_init(&dst_t_func[DST_ALG_ED25519]));
179 1.1 christos #endif
180 1.1 christos #ifdef HAVE_PKCS11_ED448
181 1.1 christos RETERR(dst__pkcs11eddsa_init(&dst_t_func[DST_ALG_ED448]));
182 1.1 christos #endif
183 1.3 christos #endif /* USE_PKCS11 */
184 1.1 christos #ifdef GSSAPI
185 1.1 christos RETERR(dst__gssapi_init(&dst_t_func[DST_ALG_GSSAPI]));
186 1.1 christos #endif
187 1.3 christos
188 1.3 christos dst_initialized = true;
189 1.1 christos return (ISC_R_SUCCESS);
190 1.1 christos
191 1.1 christos out:
192 1.1 christos /* avoid immediate crash! */
193 1.3 christos dst_initialized = true;
194 1.1 christos dst_lib_destroy();
195 1.1 christos return (result);
196 1.1 christos }
197 1.1 christos
198 1.1 christos void
199 1.1 christos dst_lib_destroy(void) {
200 1.1 christos int i;
201 1.3 christos RUNTIME_CHECK(dst_initialized == true);
202 1.3 christos dst_initialized = false;
203 1.1 christos
204 1.1 christos for (i = 0; i < DST_MAX_ALGS; i++)
205 1.1 christos if (dst_t_func[i] != NULL && dst_t_func[i]->cleanup != NULL)
206 1.1 christos dst_t_func[i]->cleanup();
207 1.1 christos dst__openssl_destroy();
208 1.3 christos #if USE_PKCS11
209 1.1 christos (void) dst__pkcs11_destroy();
210 1.3 christos #endif /* USE_PKCS11 */
211 1.1 christos }
212 1.1 christos
213 1.3 christos bool
214 1.1 christos dst_algorithm_supported(unsigned int alg) {
215 1.3 christos REQUIRE(dst_initialized == true);
216 1.1 christos
217 1.1 christos if (alg >= DST_MAX_ALGS || dst_t_func[alg] == NULL)
218 1.3 christos return (false);
219 1.3 christos return (true);
220 1.1 christos }
221 1.1 christos
222 1.3 christos bool
223 1.1 christos dst_ds_digest_supported(unsigned int digest_type) {
224 1.3 christos return (digest_type == DNS_DSDIGEST_SHA1 ||
225 1.3 christos digest_type == DNS_DSDIGEST_SHA256 ||
226 1.3 christos digest_type == DNS_DSDIGEST_SHA384);
227 1.1 christos }
228 1.1 christos
229 1.1 christos isc_result_t
230 1.3 christos dst_context_create(dst_key_t *key, isc_mem_t *mctx,
231 1.3 christos isc_logcategory_t *category, bool useforsigning,
232 1.3 christos int maxbits, dst_context_t **dctxp)
233 1.1 christos {
234 1.1 christos dst_context_t *dctx;
235 1.1 christos isc_result_t result;
236 1.1 christos
237 1.3 christos REQUIRE(dst_initialized == true);
238 1.1 christos REQUIRE(VALID_KEY(key));
239 1.1 christos REQUIRE(mctx != NULL);
240 1.1 christos REQUIRE(dctxp != NULL && *dctxp == NULL);
241 1.1 christos
242 1.1 christos if (key->func->createctx == NULL &&
243 1.1 christos key->func->createctx2 == NULL)
244 1.1 christos return (DST_R_UNSUPPORTEDALG);
245 1.1 christos if (key->keydata.generic == NULL)
246 1.1 christos return (DST_R_NULLKEY);
247 1.1 christos
248 1.1 christos dctx = isc_mem_get(mctx, sizeof(dst_context_t));
249 1.1 christos if (dctx == NULL)
250 1.1 christos return (ISC_R_NOMEMORY);
251 1.1 christos memset(dctx, 0, sizeof(*dctx));
252 1.1 christos dst_key_attach(key, &dctx->key);
253 1.1 christos isc_mem_attach(mctx, &dctx->mctx);
254 1.1 christos dctx->category = category;
255 1.1 christos if (useforsigning)
256 1.1 christos dctx->use = DO_SIGN;
257 1.1 christos else
258 1.1 christos dctx->use = DO_VERIFY;
259 1.1 christos if (key->func->createctx2 != NULL)
260 1.1 christos result = key->func->createctx2(key, maxbits, dctx);
261 1.1 christos else
262 1.1 christos result = key->func->createctx(key, dctx);
263 1.1 christos if (result != ISC_R_SUCCESS) {
264 1.1 christos if (dctx->key != NULL)
265 1.1 christos dst_key_free(&dctx->key);
266 1.1 christos isc_mem_putanddetach(&dctx->mctx, dctx, sizeof(dst_context_t));
267 1.1 christos return (result);
268 1.1 christos }
269 1.1 christos dctx->magic = CTX_MAGIC;
270 1.1 christos *dctxp = dctx;
271 1.1 christos return (ISC_R_SUCCESS);
272 1.1 christos }
273 1.1 christos
274 1.1 christos void
275 1.1 christos dst_context_destroy(dst_context_t **dctxp) {
276 1.1 christos dst_context_t *dctx;
277 1.1 christos
278 1.1 christos REQUIRE(dctxp != NULL && VALID_CTX(*dctxp));
279 1.1 christos
280 1.1 christos dctx = *dctxp;
281 1.1 christos INSIST(dctx->key->func->destroyctx != NULL);
282 1.1 christos dctx->key->func->destroyctx(dctx);
283 1.1 christos if (dctx->key != NULL)
284 1.1 christos dst_key_free(&dctx->key);
285 1.1 christos dctx->magic = 0;
286 1.1 christos isc_mem_putanddetach(&dctx->mctx, dctx, sizeof(dst_context_t));
287 1.1 christos *dctxp = NULL;
288 1.1 christos }
289 1.1 christos
290 1.1 christos isc_result_t
291 1.1 christos dst_context_adddata(dst_context_t *dctx, const isc_region_t *data) {
292 1.1 christos REQUIRE(VALID_CTX(dctx));
293 1.1 christos REQUIRE(data != NULL);
294 1.1 christos INSIST(dctx->key->func->adddata != NULL);
295 1.1 christos
296 1.1 christos return (dctx->key->func->adddata(dctx, data));
297 1.1 christos }
298 1.1 christos
299 1.1 christos isc_result_t
300 1.1 christos dst_context_sign(dst_context_t *dctx, isc_buffer_t *sig) {
301 1.1 christos dst_key_t *key;
302 1.1 christos
303 1.1 christos REQUIRE(VALID_CTX(dctx));
304 1.1 christos REQUIRE(sig != NULL);
305 1.1 christos
306 1.1 christos key = dctx->key;
307 1.1 christos CHECKALG(key->key_alg);
308 1.1 christos if (key->keydata.generic == NULL)
309 1.1 christos return (DST_R_NULLKEY);
310 1.1 christos
311 1.1 christos if (key->func->sign == NULL)
312 1.1 christos return (DST_R_NOTPRIVATEKEY);
313 1.1 christos if (key->func->isprivate == NULL ||
314 1.3 christos key->func->isprivate(key) == false)
315 1.1 christos return (DST_R_NOTPRIVATEKEY);
316 1.1 christos
317 1.1 christos return (key->func->sign(dctx, sig));
318 1.1 christos }
319 1.1 christos
320 1.1 christos isc_result_t
321 1.1 christos dst_context_verify(dst_context_t *dctx, isc_region_t *sig) {
322 1.1 christos REQUIRE(VALID_CTX(dctx));
323 1.1 christos REQUIRE(sig != NULL);
324 1.1 christos
325 1.1 christos CHECKALG(dctx->key->key_alg);
326 1.1 christos if (dctx->key->keydata.generic == NULL)
327 1.1 christos return (DST_R_NULLKEY);
328 1.1 christos if (dctx->key->func->verify == NULL)
329 1.1 christos return (DST_R_NOTPUBLICKEY);
330 1.1 christos
331 1.1 christos return (dctx->key->func->verify(dctx, sig));
332 1.1 christos }
333 1.1 christos
334 1.1 christos isc_result_t
335 1.1 christos dst_context_verify2(dst_context_t *dctx, unsigned int maxbits,
336 1.1 christos isc_region_t *sig)
337 1.1 christos {
338 1.1 christos REQUIRE(VALID_CTX(dctx));
339 1.1 christos REQUIRE(sig != NULL);
340 1.1 christos
341 1.1 christos CHECKALG(dctx->key->key_alg);
342 1.1 christos if (dctx->key->keydata.generic == NULL)
343 1.1 christos return (DST_R_NULLKEY);
344 1.1 christos if (dctx->key->func->verify == NULL &&
345 1.1 christos dctx->key->func->verify2 == NULL)
346 1.1 christos return (DST_R_NOTPUBLICKEY);
347 1.1 christos
348 1.1 christos return (dctx->key->func->verify2 != NULL ?
349 1.1 christos dctx->key->func->verify2(dctx, maxbits, sig) :
350 1.1 christos dctx->key->func->verify(dctx, sig));
351 1.1 christos }
352 1.1 christos
353 1.1 christos isc_result_t
354 1.1 christos dst_key_computesecret(const dst_key_t *pub, const dst_key_t *priv,
355 1.1 christos isc_buffer_t *secret)
356 1.1 christos {
357 1.3 christos REQUIRE(dst_initialized == true);
358 1.1 christos REQUIRE(VALID_KEY(pub) && VALID_KEY(priv));
359 1.1 christos REQUIRE(secret != NULL);
360 1.1 christos
361 1.1 christos CHECKALG(pub->key_alg);
362 1.1 christos CHECKALG(priv->key_alg);
363 1.1 christos
364 1.1 christos if (pub->keydata.generic == NULL || priv->keydata.generic == NULL)
365 1.1 christos return (DST_R_NULLKEY);
366 1.1 christos
367 1.1 christos if (pub->key_alg != priv->key_alg ||
368 1.1 christos pub->func->computesecret == NULL ||
369 1.1 christos priv->func->computesecret == NULL)
370 1.1 christos return (DST_R_KEYCANNOTCOMPUTESECRET);
371 1.1 christos
372 1.3 christos if (dst_key_isprivate(priv) == false)
373 1.1 christos return (DST_R_NOTPRIVATEKEY);
374 1.1 christos
375 1.1 christos return (pub->func->computesecret(pub, priv, secret));
376 1.1 christos }
377 1.1 christos
378 1.1 christos isc_result_t
379 1.1 christos dst_key_tofile(const dst_key_t *key, int type, const char *directory) {
380 1.1 christos isc_result_t ret = ISC_R_SUCCESS;
381 1.1 christos
382 1.3 christos REQUIRE(dst_initialized == true);
383 1.1 christos REQUIRE(VALID_KEY(key));
384 1.1 christos REQUIRE((type & (DST_TYPE_PRIVATE | DST_TYPE_PUBLIC)) != 0);
385 1.1 christos
386 1.1 christos CHECKALG(key->key_alg);
387 1.1 christos
388 1.1 christos if (key->func->tofile == NULL)
389 1.1 christos return (DST_R_UNSUPPORTEDALG);
390 1.1 christos
391 1.3 christos if ((type & DST_TYPE_PUBLIC) != 0) {
392 1.1 christos ret = write_public_key(key, type, directory);
393 1.1 christos if (ret != ISC_R_SUCCESS)
394 1.1 christos return (ret);
395 1.1 christos }
396 1.1 christos
397 1.3 christos if (((type & DST_TYPE_PRIVATE) != 0) &&
398 1.1 christos (key->key_flags & DNS_KEYFLAG_TYPEMASK) != DNS_KEYTYPE_NOKEY)
399 1.1 christos return (key->func->tofile(key, directory));
400 1.1 christos else
401 1.1 christos return (ISC_R_SUCCESS);
402 1.1 christos }
403 1.1 christos
404 1.1 christos void
405 1.3 christos dst_key_setexternal(dst_key_t *key, bool value) {
406 1.1 christos key->external = value;
407 1.1 christos }
408 1.1 christos
409 1.3 christos bool
410 1.1 christos dst_key_isexternal(dst_key_t *key) {
411 1.1 christos return (key->external);
412 1.1 christos }
413 1.1 christos
414 1.1 christos isc_result_t
415 1.1 christos dst_key_getfilename(dns_name_t *name, dns_keytag_t id,
416 1.1 christos unsigned int alg, int type, const char *directory,
417 1.1 christos isc_mem_t *mctx, isc_buffer_t *buf)
418 1.1 christos {
419 1.1 christos isc_result_t result;
420 1.1 christos
421 1.3 christos REQUIRE(dst_initialized == true);
422 1.1 christos REQUIRE(dns_name_isabsolute(name));
423 1.1 christos REQUIRE((type & (DST_TYPE_PRIVATE | DST_TYPE_PUBLIC)) != 0);
424 1.1 christos REQUIRE(mctx != NULL);
425 1.1 christos REQUIRE(buf != NULL);
426 1.1 christos
427 1.1 christos CHECKALG(alg);
428 1.1 christos
429 1.1 christos result = buildfilename(name, id, alg, type, directory, buf);
430 1.1 christos if (result == ISC_R_SUCCESS) {
431 1.1 christos if (isc_buffer_availablelength(buf) > 0)
432 1.1 christos isc_buffer_putuint8(buf, 0);
433 1.1 christos else
434 1.1 christos result = ISC_R_NOSPACE;
435 1.1 christos }
436 1.1 christos
437 1.1 christos return (result);
438 1.1 christos }
439 1.1 christos
440 1.1 christos isc_result_t
441 1.1 christos dst_key_fromfile(dns_name_t *name, dns_keytag_t id,
442 1.1 christos unsigned int alg, int type, const char *directory,
443 1.1 christos isc_mem_t *mctx, dst_key_t **keyp)
444 1.1 christos {
445 1.1 christos isc_result_t result;
446 1.3 christos char filename[NAME_MAX];
447 1.1 christos isc_buffer_t buf;
448 1.1 christos dst_key_t *key;
449 1.1 christos
450 1.3 christos REQUIRE(dst_initialized == true);
451 1.1 christos REQUIRE(dns_name_isabsolute(name));
452 1.1 christos REQUIRE((type & (DST_TYPE_PRIVATE | DST_TYPE_PUBLIC)) != 0);
453 1.1 christos REQUIRE(mctx != NULL);
454 1.1 christos REQUIRE(keyp != NULL && *keyp == NULL);
455 1.1 christos
456 1.1 christos CHECKALG(alg);
457 1.1 christos
458 1.1 christos key = NULL;
459 1.1 christos
460 1.3 christos isc_buffer_init(&buf, filename, NAME_MAX);
461 1.1 christos result = dst_key_getfilename(name, id, alg, type, NULL, mctx, &buf);
462 1.1 christos if (result != ISC_R_SUCCESS)
463 1.1 christos goto out;
464 1.1 christos
465 1.1 christos result = dst_key_fromnamedfile(filename, directory, type, mctx, &key);
466 1.1 christos if (result != ISC_R_SUCCESS)
467 1.1 christos goto out;
468 1.1 christos
469 1.1 christos result = computeid(key);
470 1.1 christos if (result != ISC_R_SUCCESS)
471 1.1 christos goto out;
472 1.1 christos
473 1.1 christos if (!dns_name_equal(name, key->key_name) || id != key->key_id ||
474 1.1 christos alg != key->key_alg) {
475 1.1 christos result = DST_R_INVALIDPRIVATEKEY;
476 1.1 christos goto out;
477 1.1 christos }
478 1.1 christos
479 1.1 christos *keyp = key;
480 1.1 christos result = ISC_R_SUCCESS;
481 1.1 christos
482 1.1 christos out:
483 1.1 christos if ((key != NULL) && (result != ISC_R_SUCCESS))
484 1.1 christos dst_key_free(&key);
485 1.1 christos
486 1.1 christos return (result);
487 1.1 christos }
488 1.1 christos
489 1.1 christos isc_result_t
490 1.1 christos dst_key_fromnamedfile(const char *filename, const char *dirname,
491 1.1 christos int type, isc_mem_t *mctx, dst_key_t **keyp)
492 1.1 christos {
493 1.1 christos isc_result_t result;
494 1.1 christos dst_key_t *pubkey = NULL, *key = NULL;
495 1.1 christos char *newfilename = NULL;
496 1.1 christos int newfilenamelen = 0;
497 1.1 christos isc_lex_t *lex = NULL;
498 1.1 christos
499 1.3 christos REQUIRE(dst_initialized == true);
500 1.1 christos REQUIRE(filename != NULL);
501 1.1 christos REQUIRE((type & (DST_TYPE_PRIVATE | DST_TYPE_PUBLIC)) != 0);
502 1.1 christos REQUIRE(mctx != NULL);
503 1.1 christos REQUIRE(keyp != NULL && *keyp == NULL);
504 1.1 christos
505 1.1 christos /* If an absolute path is specified, don't use the key directory */
506 1.1 christos #ifndef WIN32
507 1.1 christos if (filename[0] == '/')
508 1.1 christos dirname = NULL;
509 1.1 christos #else /* WIN32 */
510 1.1 christos if (filename[0] == '/' || filename[0] == '\\')
511 1.1 christos dirname = NULL;
512 1.1 christos #endif
513 1.1 christos
514 1.1 christos newfilenamelen = strlen(filename) + 5;
515 1.1 christos if (dirname != NULL)
516 1.1 christos newfilenamelen += strlen(dirname) + 1;
517 1.1 christos newfilename = isc_mem_get(mctx, newfilenamelen);
518 1.1 christos if (newfilename == NULL)
519 1.1 christos return (ISC_R_NOMEMORY);
520 1.1 christos result = addsuffix(newfilename, newfilenamelen,
521 1.1 christos dirname, filename, ".key");
522 1.1 christos INSIST(result == ISC_R_SUCCESS);
523 1.1 christos
524 1.1 christos result = dst_key_read_public(newfilename, type, mctx, &pubkey);
525 1.1 christos isc_mem_put(mctx, newfilename, newfilenamelen);
526 1.1 christos newfilename = NULL;
527 1.1 christos RETERR(result);
528 1.1 christos
529 1.1 christos if ((type & (DST_TYPE_PRIVATE | DST_TYPE_PUBLIC)) == DST_TYPE_PUBLIC ||
530 1.1 christos (pubkey->key_flags & DNS_KEYFLAG_TYPEMASK) == DNS_KEYTYPE_NOKEY) {
531 1.1 christos result = computeid(pubkey);
532 1.1 christos if (result != ISC_R_SUCCESS) {
533 1.1 christos dst_key_free(&pubkey);
534 1.1 christos return (result);
535 1.1 christos }
536 1.1 christos
537 1.1 christos *keyp = pubkey;
538 1.1 christos return (ISC_R_SUCCESS);
539 1.1 christos }
540 1.1 christos
541 1.1 christos result = algorithm_status(pubkey->key_alg);
542 1.1 christos if (result != ISC_R_SUCCESS) {
543 1.1 christos dst_key_free(&pubkey);
544 1.1 christos return (result);
545 1.1 christos }
546 1.1 christos
547 1.1 christos key = get_key_struct(pubkey->key_name, pubkey->key_alg,
548 1.1 christos pubkey->key_flags, pubkey->key_proto, 0,
549 1.1 christos pubkey->key_class, pubkey->key_ttl, mctx);
550 1.1 christos if (key == NULL) {
551 1.1 christos dst_key_free(&pubkey);
552 1.1 christos return (ISC_R_NOMEMORY);
553 1.1 christos }
554 1.1 christos
555 1.1 christos if (key->func->parse == NULL)
556 1.1 christos RETERR(DST_R_UNSUPPORTEDALG);
557 1.1 christos
558 1.1 christos newfilenamelen = strlen(filename) + 9;
559 1.1 christos if (dirname != NULL)
560 1.1 christos newfilenamelen += strlen(dirname) + 1;
561 1.1 christos newfilename = isc_mem_get(mctx, newfilenamelen);
562 1.1 christos if (newfilename == NULL)
563 1.1 christos RETERR(ISC_R_NOMEMORY);
564 1.1 christos result = addsuffix(newfilename, newfilenamelen,
565 1.1 christos dirname, filename, ".private");
566 1.1 christos INSIST(result == ISC_R_SUCCESS);
567 1.1 christos
568 1.1 christos RETERR(isc_lex_create(mctx, 1500, &lex));
569 1.1 christos RETERR(isc_lex_openfile(lex, newfilename));
570 1.1 christos isc_mem_put(mctx, newfilename, newfilenamelen);
571 1.1 christos
572 1.1 christos RETERR(key->func->parse(key, lex, pubkey));
573 1.1 christos isc_lex_destroy(&lex);
574 1.1 christos
575 1.1 christos RETERR(computeid(key));
576 1.1 christos
577 1.1 christos if (pubkey->key_id != key->key_id)
578 1.1 christos RETERR(DST_R_INVALIDPRIVATEKEY);
579 1.1 christos dst_key_free(&pubkey);
580 1.1 christos
581 1.1 christos *keyp = key;
582 1.1 christos return (ISC_R_SUCCESS);
583 1.1 christos
584 1.1 christos out:
585 1.1 christos if (pubkey != NULL)
586 1.1 christos dst_key_free(&pubkey);
587 1.1 christos if (newfilename != NULL)
588 1.1 christos isc_mem_put(mctx, newfilename, newfilenamelen);
589 1.1 christos if (lex != NULL)
590 1.1 christos isc_lex_destroy(&lex);
591 1.1 christos if (key != NULL)
592 1.1 christos dst_key_free(&key);
593 1.1 christos return (result);
594 1.1 christos }
595 1.1 christos
596 1.1 christos isc_result_t
597 1.1 christos dst_key_todns(const dst_key_t *key, isc_buffer_t *target) {
598 1.3 christos REQUIRE(dst_initialized == true);
599 1.1 christos REQUIRE(VALID_KEY(key));
600 1.1 christos REQUIRE(target != NULL);
601 1.1 christos
602 1.1 christos CHECKALG(key->key_alg);
603 1.1 christos
604 1.1 christos if (key->func->todns == NULL)
605 1.1 christos return (DST_R_UNSUPPORTEDALG);
606 1.1 christos
607 1.1 christos if (isc_buffer_availablelength(target) < 4)
608 1.1 christos return (ISC_R_NOSPACE);
609 1.3 christos isc_buffer_putuint16(target, (uint16_t)(key->key_flags & 0xffff));
610 1.3 christos isc_buffer_putuint8(target, (uint8_t)key->key_proto);
611 1.3 christos isc_buffer_putuint8(target, (uint8_t)key->key_alg);
612 1.1 christos
613 1.3 christos if ((key->key_flags & DNS_KEYFLAG_EXTENDED) != 0) {
614 1.1 christos if (isc_buffer_availablelength(target) < 2)
615 1.1 christos return (ISC_R_NOSPACE);
616 1.1 christos isc_buffer_putuint16(target,
617 1.3 christos (uint16_t)((key->key_flags >> 16)
618 1.1 christos & 0xffff));
619 1.1 christos }
620 1.1 christos
621 1.1 christos if (key->keydata.generic == NULL) /*%< NULL KEY */
622 1.1 christos return (ISC_R_SUCCESS);
623 1.1 christos
624 1.1 christos return (key->func->todns(key, target));
625 1.1 christos }
626 1.1 christos
627 1.1 christos isc_result_t
628 1.1 christos dst_key_fromdns(const dns_name_t *name, dns_rdataclass_t rdclass,
629 1.1 christos isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp)
630 1.1 christos {
631 1.3 christos uint8_t alg, proto;
632 1.3 christos uint32_t flags, extflags;
633 1.1 christos dst_key_t *key = NULL;
634 1.1 christos dns_keytag_t id, rid;
635 1.1 christos isc_region_t r;
636 1.1 christos isc_result_t result;
637 1.1 christos
638 1.1 christos REQUIRE(dst_initialized);
639 1.1 christos
640 1.1 christos isc_buffer_remainingregion(source, &r);
641 1.1 christos
642 1.1 christos if (isc_buffer_remaininglength(source) < 4)
643 1.1 christos return (DST_R_INVALIDPUBLICKEY);
644 1.1 christos flags = isc_buffer_getuint16(source);
645 1.1 christos proto = isc_buffer_getuint8(source);
646 1.1 christos alg = isc_buffer_getuint8(source);
647 1.1 christos
648 1.1 christos id = dst_region_computeid(&r, alg);
649 1.1 christos rid = dst_region_computerid(&r, alg);
650 1.1 christos
651 1.3 christos if ((flags & DNS_KEYFLAG_EXTENDED) != 0) {
652 1.1 christos if (isc_buffer_remaininglength(source) < 2)
653 1.1 christos return (DST_R_INVALIDPUBLICKEY);
654 1.1 christos extflags = isc_buffer_getuint16(source);
655 1.1 christos flags |= (extflags << 16);
656 1.1 christos }
657 1.1 christos
658 1.1 christos result = frombuffer(name, alg, flags, proto, rdclass, source,
659 1.1 christos mctx, &key);
660 1.1 christos if (result != ISC_R_SUCCESS)
661 1.1 christos return (result);
662 1.1 christos key->key_id = id;
663 1.1 christos key->key_rid = rid;
664 1.1 christos
665 1.1 christos *keyp = key;
666 1.1 christos return (ISC_R_SUCCESS);
667 1.1 christos }
668 1.1 christos
669 1.1 christos isc_result_t
670 1.1 christos dst_key_frombuffer(const dns_name_t *name, unsigned int alg,
671 1.1 christos unsigned int flags, unsigned int protocol,
672 1.1 christos dns_rdataclass_t rdclass,
673 1.1 christos isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp)
674 1.1 christos {
675 1.1 christos dst_key_t *key = NULL;
676 1.1 christos isc_result_t result;
677 1.1 christos
678 1.1 christos REQUIRE(dst_initialized);
679 1.1 christos
680 1.1 christos result = frombuffer(name, alg, flags, protocol, rdclass, source,
681 1.1 christos mctx, &key);
682 1.1 christos if (result != ISC_R_SUCCESS)
683 1.1 christos return (result);
684 1.1 christos
685 1.1 christos result = computeid(key);
686 1.1 christos if (result != ISC_R_SUCCESS) {
687 1.1 christos dst_key_free(&key);
688 1.1 christos return (result);
689 1.1 christos }
690 1.1 christos
691 1.1 christos *keyp = key;
692 1.1 christos return (ISC_R_SUCCESS);
693 1.1 christos }
694 1.1 christos
695 1.1 christos isc_result_t
696 1.1 christos dst_key_tobuffer(const dst_key_t *key, isc_buffer_t *target) {
697 1.3 christos REQUIRE(dst_initialized == true);
698 1.1 christos REQUIRE(VALID_KEY(key));
699 1.1 christos REQUIRE(target != NULL);
700 1.1 christos
701 1.1 christos CHECKALG(key->key_alg);
702 1.1 christos
703 1.1 christos if (key->func->todns == NULL)
704 1.1 christos return (DST_R_UNSUPPORTEDALG);
705 1.1 christos
706 1.1 christos return (key->func->todns(key, target));
707 1.1 christos }
708 1.1 christos
709 1.1 christos isc_result_t
710 1.1 christos dst_key_privatefrombuffer(dst_key_t *key, isc_buffer_t *buffer) {
711 1.1 christos isc_lex_t *lex = NULL;
712 1.1 christos isc_result_t result = ISC_R_SUCCESS;
713 1.1 christos
714 1.3 christos REQUIRE(dst_initialized == true);
715 1.1 christos REQUIRE(VALID_KEY(key));
716 1.1 christos REQUIRE(!dst_key_isprivate(key));
717 1.1 christos REQUIRE(buffer != NULL);
718 1.1 christos
719 1.1 christos if (key->func->parse == NULL)
720 1.1 christos RETERR(DST_R_UNSUPPORTEDALG);
721 1.1 christos
722 1.1 christos RETERR(isc_lex_create(key->mctx, 1500, &lex));
723 1.1 christos RETERR(isc_lex_openbuffer(lex, buffer));
724 1.1 christos RETERR(key->func->parse(key, lex, NULL));
725 1.1 christos out:
726 1.1 christos if (lex != NULL)
727 1.1 christos isc_lex_destroy(&lex);
728 1.1 christos return (result);
729 1.1 christos }
730 1.1 christos
731 1.1 christos gss_ctx_id_t
732 1.1 christos dst_key_getgssctx(const dst_key_t *key)
733 1.1 christos {
734 1.1 christos REQUIRE(key != NULL);
735 1.1 christos
736 1.1 christos return (key->keydata.gssctx);
737 1.1 christos }
738 1.1 christos
739 1.1 christos isc_result_t
740 1.1 christos dst_key_fromgssapi(const dns_name_t *name, gss_ctx_id_t gssctx,
741 1.1 christos isc_mem_t *mctx, dst_key_t **keyp, isc_region_t *intoken)
742 1.1 christos {
743 1.1 christos dst_key_t *key;
744 1.1 christos isc_result_t result;
745 1.1 christos
746 1.1 christos REQUIRE(gssctx != NULL);
747 1.1 christos REQUIRE(keyp != NULL && *keyp == NULL);
748 1.1 christos
749 1.1 christos key = get_key_struct(name, DST_ALG_GSSAPI, 0, DNS_KEYPROTO_DNSSEC,
750 1.1 christos 0, dns_rdataclass_in, 0, mctx);
751 1.1 christos if (key == NULL)
752 1.1 christos return (ISC_R_NOMEMORY);
753 1.1 christos
754 1.1 christos if (intoken != NULL) {
755 1.1 christos /*
756 1.1 christos * Keep the token for use by external ssu rules. They may need
757 1.1 christos * to examine the PAC in the kerberos ticket.
758 1.1 christos */
759 1.1 christos RETERR(isc_buffer_allocate(key->mctx, &key->key_tkeytoken,
760 1.1 christos intoken->length));
761 1.1 christos RETERR(isc_buffer_copyregion(key->key_tkeytoken, intoken));
762 1.1 christos }
763 1.1 christos
764 1.1 christos key->keydata.gssctx = gssctx;
765 1.1 christos *keyp = key;
766 1.1 christos result = ISC_R_SUCCESS;
767 1.1 christos out:
768 1.3 christos if (result != ISC_R_SUCCESS) {
769 1.3 christos dst_key_free(&key);
770 1.3 christos }
771 1.1 christos return result;
772 1.1 christos }
773 1.1 christos
774 1.1 christos isc_result_t
775 1.1 christos dst_key_buildinternal(const dns_name_t *name, unsigned int alg,
776 1.1 christos unsigned int bits, unsigned int flags,
777 1.1 christos unsigned int protocol, dns_rdataclass_t rdclass,
778 1.1 christos void *data, isc_mem_t *mctx, dst_key_t **keyp)
779 1.1 christos {
780 1.1 christos dst_key_t *key;
781 1.1 christos isc_result_t result;
782 1.1 christos
783 1.3 christos REQUIRE(dst_initialized == true);
784 1.1 christos REQUIRE(dns_name_isabsolute(name));
785 1.1 christos REQUIRE(mctx != NULL);
786 1.1 christos REQUIRE(keyp != NULL && *keyp == NULL);
787 1.1 christos REQUIRE(data != NULL);
788 1.1 christos
789 1.1 christos CHECKALG(alg);
790 1.1 christos
791 1.1 christos key = get_key_struct(name, alg, flags, protocol, bits, rdclass,
792 1.1 christos 0, mctx);
793 1.1 christos if (key == NULL)
794 1.1 christos return (ISC_R_NOMEMORY);
795 1.1 christos
796 1.1 christos key->keydata.generic = data;
797 1.1 christos
798 1.1 christos result = computeid(key);
799 1.1 christos if (result != ISC_R_SUCCESS) {
800 1.1 christos dst_key_free(&key);
801 1.1 christos return (result);
802 1.1 christos }
803 1.1 christos
804 1.1 christos *keyp = key;
805 1.1 christos return (ISC_R_SUCCESS);
806 1.1 christos }
807 1.1 christos
808 1.1 christos isc_result_t
809 1.1 christos dst_key_fromlabel(const dns_name_t *name, int alg, unsigned int flags,
810 1.1 christos unsigned int protocol, dns_rdataclass_t rdclass,
811 1.1 christos const char *engine, const char *label, const char *pin,
812 1.1 christos isc_mem_t *mctx, dst_key_t **keyp)
813 1.1 christos {
814 1.1 christos dst_key_t *key;
815 1.1 christos isc_result_t result;
816 1.1 christos
817 1.3 christos REQUIRE(dst_initialized == true);
818 1.1 christos REQUIRE(dns_name_isabsolute(name));
819 1.1 christos REQUIRE(mctx != NULL);
820 1.1 christos REQUIRE(keyp != NULL && *keyp == NULL);
821 1.1 christos REQUIRE(label != NULL);
822 1.1 christos
823 1.1 christos CHECKALG(alg);
824 1.1 christos
825 1.1 christos key = get_key_struct(name, alg, flags, protocol, 0, rdclass, 0, mctx);
826 1.1 christos if (key == NULL)
827 1.1 christos return (ISC_R_NOMEMORY);
828 1.1 christos
829 1.1 christos if (key->func->fromlabel == NULL) {
830 1.1 christos dst_key_free(&key);
831 1.1 christos return (DST_R_UNSUPPORTEDALG);
832 1.1 christos }
833 1.1 christos
834 1.1 christos result = key->func->fromlabel(key, engine, label, pin);
835 1.1 christos if (result != ISC_R_SUCCESS) {
836 1.1 christos dst_key_free(&key);
837 1.1 christos return (result);
838 1.1 christos }
839 1.1 christos
840 1.1 christos result = computeid(key);
841 1.1 christos if (result != ISC_R_SUCCESS) {
842 1.1 christos dst_key_free(&key);
843 1.1 christos return (result);
844 1.1 christos }
845 1.1 christos
846 1.1 christos *keyp = key;
847 1.1 christos return (ISC_R_SUCCESS);
848 1.1 christos }
849 1.1 christos
850 1.1 christos isc_result_t
851 1.1 christos dst_key_generate(const dns_name_t *name, unsigned int alg,
852 1.1 christos unsigned int bits, unsigned int param,
853 1.1 christos unsigned int flags, unsigned int protocol,
854 1.1 christos dns_rdataclass_t rdclass,
855 1.3 christos isc_mem_t *mctx, dst_key_t **keyp,
856 1.3 christos void (*callback)(int))
857 1.1 christos {
858 1.1 christos dst_key_t *key;
859 1.1 christos isc_result_t ret;
860 1.1 christos
861 1.3 christos REQUIRE(dst_initialized == true);
862 1.1 christos REQUIRE(dns_name_isabsolute(name));
863 1.1 christos REQUIRE(mctx != NULL);
864 1.1 christos REQUIRE(keyp != NULL && *keyp == NULL);
865 1.1 christos
866 1.1 christos CHECKALG(alg);
867 1.1 christos
868 1.1 christos key = get_key_struct(name, alg, flags, protocol, bits,
869 1.1 christos rdclass, 0, mctx);
870 1.1 christos if (key == NULL)
871 1.1 christos return (ISC_R_NOMEMORY);
872 1.1 christos
873 1.1 christos if (bits == 0) { /*%< NULL KEY */
874 1.1 christos key->key_flags |= DNS_KEYTYPE_NOKEY;
875 1.1 christos *keyp = key;
876 1.1 christos return (ISC_R_SUCCESS);
877 1.1 christos }
878 1.1 christos
879 1.1 christos if (key->func->generate == NULL) {
880 1.1 christos dst_key_free(&key);
881 1.1 christos return (DST_R_UNSUPPORTEDALG);
882 1.1 christos }
883 1.1 christos
884 1.1 christos ret = key->func->generate(key, param, callback);
885 1.1 christos if (ret != ISC_R_SUCCESS) {
886 1.1 christos dst_key_free(&key);
887 1.1 christos return (ret);
888 1.1 christos }
889 1.1 christos
890 1.1 christos ret = computeid(key);
891 1.1 christos if (ret != ISC_R_SUCCESS) {
892 1.1 christos dst_key_free(&key);
893 1.1 christos return (ret);
894 1.1 christos }
895 1.1 christos
896 1.1 christos *keyp = key;
897 1.1 christos return (ISC_R_SUCCESS);
898 1.1 christos }
899 1.1 christos
900 1.1 christos isc_result_t
901 1.3 christos dst_key_getnum(const dst_key_t *key, int type, uint32_t *valuep)
902 1.1 christos {
903 1.1 christos REQUIRE(VALID_KEY(key));
904 1.1 christos REQUIRE(valuep != NULL);
905 1.1 christos REQUIRE(type <= DST_MAX_NUMERIC);
906 1.1 christos if (!key->numset[type])
907 1.1 christos return (ISC_R_NOTFOUND);
908 1.1 christos *valuep = key->nums[type];
909 1.1 christos return (ISC_R_SUCCESS);
910 1.1 christos }
911 1.1 christos
912 1.1 christos void
913 1.3 christos dst_key_setnum(dst_key_t *key, int type, uint32_t value)
914 1.1 christos {
915 1.1 christos REQUIRE(VALID_KEY(key));
916 1.1 christos REQUIRE(type <= DST_MAX_NUMERIC);
917 1.1 christos key->nums[type] = value;
918 1.3 christos key->numset[type] = true;
919 1.1 christos }
920 1.1 christos
921 1.1 christos void
922 1.1 christos dst_key_unsetnum(dst_key_t *key, int type)
923 1.1 christos {
924 1.1 christos REQUIRE(VALID_KEY(key));
925 1.1 christos REQUIRE(type <= DST_MAX_NUMERIC);
926 1.3 christos key->numset[type] = false;
927 1.1 christos }
928 1.1 christos
929 1.1 christos isc_result_t
930 1.1 christos dst_key_gettime(const dst_key_t *key, int type, isc_stdtime_t *timep) {
931 1.1 christos REQUIRE(VALID_KEY(key));
932 1.1 christos REQUIRE(timep != NULL);
933 1.1 christos REQUIRE(type <= DST_MAX_TIMES);
934 1.1 christos if (!key->timeset[type])
935 1.1 christos return (ISC_R_NOTFOUND);
936 1.1 christos *timep = key->times[type];
937 1.1 christos return (ISC_R_SUCCESS);
938 1.1 christos }
939 1.1 christos
940 1.1 christos void
941 1.1 christos dst_key_settime(dst_key_t *key, int type, isc_stdtime_t when) {
942 1.1 christos REQUIRE(VALID_KEY(key));
943 1.1 christos REQUIRE(type <= DST_MAX_TIMES);
944 1.1 christos key->times[type] = when;
945 1.3 christos key->timeset[type] = true;
946 1.1 christos }
947 1.1 christos
948 1.1 christos void
949 1.1 christos dst_key_unsettime(dst_key_t *key, int type) {
950 1.1 christos REQUIRE(VALID_KEY(key));
951 1.1 christos REQUIRE(type <= DST_MAX_TIMES);
952 1.3 christos key->timeset[type] = false;
953 1.1 christos }
954 1.1 christos
955 1.1 christos isc_result_t
956 1.1 christos dst_key_getprivateformat(const dst_key_t *key, int *majorp, int *minorp) {
957 1.1 christos REQUIRE(VALID_KEY(key));
958 1.1 christos REQUIRE(majorp != NULL);
959 1.1 christos REQUIRE(minorp != NULL);
960 1.1 christos *majorp = key->fmt_major;
961 1.1 christos *minorp = key->fmt_minor;
962 1.1 christos return (ISC_R_SUCCESS);
963 1.1 christos }
964 1.1 christos
965 1.1 christos void
966 1.1 christos dst_key_setprivateformat(dst_key_t *key, int major, int minor) {
967 1.1 christos REQUIRE(VALID_KEY(key));
968 1.1 christos key->fmt_major = major;
969 1.1 christos key->fmt_minor = minor;
970 1.1 christos }
971 1.1 christos
972 1.3 christos static bool
973 1.1 christos comparekeys(const dst_key_t *key1, const dst_key_t *key2,
974 1.3 christos bool match_revoked_key,
975 1.3 christos bool (*compare)(const dst_key_t *key1,
976 1.1 christos const dst_key_t *key2))
977 1.1 christos {
978 1.3 christos REQUIRE(dst_initialized == true);
979 1.1 christos REQUIRE(VALID_KEY(key1));
980 1.1 christos REQUIRE(VALID_KEY(key2));
981 1.1 christos
982 1.1 christos if (key1 == key2)
983 1.3 christos return (true);
984 1.1 christos
985 1.1 christos if (key1->key_alg != key2->key_alg)
986 1.3 christos return (false);
987 1.1 christos
988 1.1 christos if (key1->key_id != key2->key_id) {
989 1.1 christos if (!match_revoked_key)
990 1.3 christos return (false);
991 1.1 christos if (key1->key_alg == DST_ALG_RSAMD5)
992 1.3 christos return (false);
993 1.1 christos if ((key1->key_flags & DNS_KEYFLAG_REVOKE) ==
994 1.1 christos (key2->key_flags & DNS_KEYFLAG_REVOKE))
995 1.3 christos return (false);
996 1.1 christos if (key1->key_id != key2->key_rid &&
997 1.1 christos key1->key_rid != key2->key_id)
998 1.3 christos return (false);
999 1.1 christos }
1000 1.1 christos
1001 1.1 christos if (compare != NULL)
1002 1.1 christos return (compare(key1, key2));
1003 1.1 christos else
1004 1.3 christos return (false);
1005 1.1 christos }
1006 1.1 christos
1007 1.1 christos
1008 1.1 christos /*
1009 1.1 christos * Compares only the public portion of two keys, by converting them
1010 1.1 christos * both to wire format and comparing the results.
1011 1.1 christos */
1012 1.3 christos static bool
1013 1.1 christos pub_compare(const dst_key_t *key1, const dst_key_t *key2) {
1014 1.1 christos isc_result_t result;
1015 1.1 christos unsigned char buf1[DST_KEY_MAXSIZE], buf2[DST_KEY_MAXSIZE];
1016 1.1 christos isc_buffer_t b1, b2;
1017 1.1 christos isc_region_t r1, r2;
1018 1.1 christos
1019 1.1 christos isc_buffer_init(&b1, buf1, sizeof(buf1));
1020 1.1 christos result = dst_key_todns(key1, &b1);
1021 1.1 christos if (result != ISC_R_SUCCESS)
1022 1.3 christos return (false);
1023 1.1 christos /* Zero out flags. */
1024 1.1 christos buf1[0] = buf1[1] = 0;
1025 1.1 christos if ((key1->key_flags & DNS_KEYFLAG_EXTENDED) != 0)
1026 1.1 christos isc_buffer_subtract(&b1, 2);
1027 1.1 christos
1028 1.1 christos isc_buffer_init(&b2, buf2, sizeof(buf2));
1029 1.1 christos result = dst_key_todns(key2, &b2);
1030 1.1 christos if (result != ISC_R_SUCCESS)
1031 1.3 christos return (false);
1032 1.1 christos /* Zero out flags. */
1033 1.1 christos buf2[0] = buf2[1] = 0;
1034 1.1 christos if ((key2->key_flags & DNS_KEYFLAG_EXTENDED) != 0)
1035 1.1 christos isc_buffer_subtract(&b2, 2);
1036 1.1 christos
1037 1.1 christos isc_buffer_usedregion(&b1, &r1);
1038 1.1 christos /* Remove extended flags. */
1039 1.1 christos if ((key1->key_flags & DNS_KEYFLAG_EXTENDED) != 0) {
1040 1.1 christos memmove(&buf1[4], &buf1[6], r1.length - 6);
1041 1.1 christos r1.length -= 2;
1042 1.1 christos }
1043 1.1 christos
1044 1.1 christos isc_buffer_usedregion(&b2, &r2);
1045 1.1 christos /* Remove extended flags. */
1046 1.1 christos if ((key2->key_flags & DNS_KEYFLAG_EXTENDED) != 0) {
1047 1.1 christos memmove(&buf2[4], &buf2[6], r2.length - 6);
1048 1.1 christos r2.length -= 2;
1049 1.1 christos }
1050 1.3 christos return (isc_region_compare(&r1, &r2) == 0);
1051 1.1 christos }
1052 1.1 christos
1053 1.3 christos bool
1054 1.1 christos dst_key_compare(const dst_key_t *key1, const dst_key_t *key2) {
1055 1.3 christos return (comparekeys(key1, key2, false, key1->func->compare));
1056 1.1 christos }
1057 1.1 christos
1058 1.3 christos bool
1059 1.1 christos dst_key_pubcompare(const dst_key_t *key1, const dst_key_t *key2,
1060 1.3 christos bool match_revoked_key)
1061 1.1 christos {
1062 1.1 christos return (comparekeys(key1, key2, match_revoked_key, pub_compare));
1063 1.1 christos }
1064 1.1 christos
1065 1.1 christos
1066 1.3 christos bool
1067 1.1 christos dst_key_paramcompare(const dst_key_t *key1, const dst_key_t *key2) {
1068 1.3 christos REQUIRE(dst_initialized == true);
1069 1.1 christos REQUIRE(VALID_KEY(key1));
1070 1.1 christos REQUIRE(VALID_KEY(key2));
1071 1.1 christos
1072 1.1 christos if (key1 == key2)
1073 1.3 christos return (true);
1074 1.1 christos if (key1->key_alg == key2->key_alg &&
1075 1.1 christos key1->func->paramcompare != NULL &&
1076 1.3 christos key1->func->paramcompare(key1, key2) == true)
1077 1.3 christos return (true);
1078 1.1 christos else
1079 1.3 christos return (false);
1080 1.1 christos }
1081 1.1 christos
1082 1.1 christos void
1083 1.1 christos dst_key_attach(dst_key_t *source, dst_key_t **target) {
1084 1.1 christos
1085 1.3 christos REQUIRE(dst_initialized == true);
1086 1.1 christos REQUIRE(target != NULL && *target == NULL);
1087 1.1 christos REQUIRE(VALID_KEY(source));
1088 1.1 christos
1089 1.3 christos isc_refcount_increment(&source->refs);
1090 1.1 christos *target = source;
1091 1.1 christos }
1092 1.1 christos
1093 1.1 christos void
1094 1.1 christos dst_key_free(dst_key_t **keyp) {
1095 1.3 christos REQUIRE(dst_initialized == true);
1096 1.1 christos REQUIRE(keyp != NULL && VALID_KEY(*keyp));
1097 1.3 christos dst_key_t *key = *keyp;
1098 1.3 christos *keyp = NULL;
1099 1.1 christos
1100 1.3 christos if (isc_refcount_decrement(&key->refs) == 1) {
1101 1.3 christos isc_refcount_destroy(&key->refs);
1102 1.3 christos isc_mem_t *mctx = key->mctx;
1103 1.3 christos if (key->keydata.generic != NULL) {
1104 1.3 christos INSIST(key->func->destroy != NULL);
1105 1.3 christos key->func->destroy(key);
1106 1.3 christos }
1107 1.3 christos if (key->engine != NULL)
1108 1.3 christos isc_mem_free(mctx, key->engine);
1109 1.3 christos if (key->label != NULL)
1110 1.3 christos isc_mem_free(mctx, key->label);
1111 1.3 christos dns_name_free(key->key_name, mctx);
1112 1.3 christos isc_mem_put(mctx, key->key_name, sizeof(dns_name_t));
1113 1.3 christos if (key->key_tkeytoken) {
1114 1.3 christos isc_buffer_free(&key->key_tkeytoken);
1115 1.3 christos }
1116 1.3 christos isc_safe_memwipe(key, sizeof(*key));
1117 1.3 christos isc_mem_putanddetach(&mctx, key, sizeof(*key));
1118 1.1 christos }
1119 1.1 christos }
1120 1.1 christos
1121 1.3 christos bool
1122 1.1 christos dst_key_isprivate(const dst_key_t *key) {
1123 1.1 christos REQUIRE(VALID_KEY(key));
1124 1.1 christos INSIST(key->func->isprivate != NULL);
1125 1.1 christos return (key->func->isprivate(key));
1126 1.1 christos }
1127 1.1 christos
1128 1.1 christos isc_result_t
1129 1.1 christos dst_key_buildfilename(const dst_key_t *key, int type,
1130 1.1 christos const char *directory, isc_buffer_t *out) {
1131 1.1 christos
1132 1.1 christos REQUIRE(VALID_KEY(key));
1133 1.1 christos REQUIRE(type == DST_TYPE_PRIVATE || type == DST_TYPE_PUBLIC ||
1134 1.1 christos type == 0);
1135 1.1 christos
1136 1.1 christos return (buildfilename(key->key_name, key->key_id, key->key_alg,
1137 1.1 christos type, directory, out));
1138 1.1 christos }
1139 1.1 christos
1140 1.1 christos isc_result_t
1141 1.1 christos dst_key_sigsize(const dst_key_t *key, unsigned int *n) {
1142 1.3 christos REQUIRE(dst_initialized == true);
1143 1.1 christos REQUIRE(VALID_KEY(key));
1144 1.1 christos REQUIRE(n != NULL);
1145 1.1 christos
1146 1.1 christos /* XXXVIX this switch statement is too sparse to gen a jump table. */
1147 1.1 christos switch (key->key_alg) {
1148 1.1 christos case DST_ALG_RSAMD5:
1149 1.1 christos case DST_ALG_RSASHA1:
1150 1.1 christos case DST_ALG_NSEC3RSASHA1:
1151 1.1 christos case DST_ALG_RSASHA256:
1152 1.1 christos case DST_ALG_RSASHA512:
1153 1.1 christos *n = (key->key_size + 7) / 8;
1154 1.1 christos break;
1155 1.1 christos case DST_ALG_ECDSA256:
1156 1.1 christos *n = DNS_SIG_ECDSA256SIZE;
1157 1.1 christos break;
1158 1.1 christos case DST_ALG_ECDSA384:
1159 1.1 christos *n = DNS_SIG_ECDSA384SIZE;
1160 1.1 christos break;
1161 1.1 christos case DST_ALG_ED25519:
1162 1.1 christos *n = DNS_SIG_ED25519SIZE;
1163 1.1 christos break;
1164 1.1 christos case DST_ALG_ED448:
1165 1.1 christos *n = DNS_SIG_ED448SIZE;
1166 1.1 christos break;
1167 1.1 christos case DST_ALG_HMACMD5:
1168 1.3 christos *n = isc_md_type_get_size(ISC_MD_MD5);
1169 1.1 christos break;
1170 1.1 christos case DST_ALG_HMACSHA1:
1171 1.3 christos *n = isc_md_type_get_size(ISC_MD_SHA1);
1172 1.1 christos break;
1173 1.1 christos case DST_ALG_HMACSHA224:
1174 1.3 christos *n = isc_md_type_get_size(ISC_MD_SHA224);
1175 1.1 christos break;
1176 1.1 christos case DST_ALG_HMACSHA256:
1177 1.3 christos *n = isc_md_type_get_size(ISC_MD_SHA256);
1178 1.1 christos break;
1179 1.1 christos case DST_ALG_HMACSHA384:
1180 1.3 christos *n = isc_md_type_get_size(ISC_MD_SHA384);
1181 1.1 christos break;
1182 1.1 christos case DST_ALG_HMACSHA512:
1183 1.3 christos *n = isc_md_type_get_size(ISC_MD_SHA512);
1184 1.1 christos break;
1185 1.1 christos case DST_ALG_GSSAPI:
1186 1.1 christos *n = 128; /*%< XXX */
1187 1.1 christos break;
1188 1.1 christos case DST_ALG_DH:
1189 1.1 christos default:
1190 1.1 christos return (DST_R_UNSUPPORTEDALG);
1191 1.1 christos }
1192 1.1 christos return (ISC_R_SUCCESS);
1193 1.1 christos }
1194 1.1 christos
1195 1.1 christos isc_result_t
1196 1.1 christos dst_key_secretsize(const dst_key_t *key, unsigned int *n) {
1197 1.3 christos REQUIRE(dst_initialized == true);
1198 1.1 christos REQUIRE(VALID_KEY(key));
1199 1.1 christos REQUIRE(n != NULL);
1200 1.1 christos
1201 1.3 christos if (key->key_alg == DST_ALG_DH) {
1202 1.1 christos *n = (key->key_size + 7) / 8;
1203 1.3 christos return (ISC_R_SUCCESS);
1204 1.3 christos }
1205 1.3 christos return (DST_R_UNSUPPORTEDALG);
1206 1.1 christos }
1207 1.1 christos
1208 1.1 christos /*%
1209 1.1 christos * Set the flags on a key, then recompute the key ID
1210 1.1 christos */
1211 1.1 christos isc_result_t
1212 1.3 christos dst_key_setflags(dst_key_t *key, uint32_t flags) {
1213 1.1 christos REQUIRE(VALID_KEY(key));
1214 1.1 christos key->key_flags = flags;
1215 1.1 christos return (computeid(key));
1216 1.1 christos }
1217 1.1 christos
1218 1.1 christos void
1219 1.1 christos dst_key_format(const dst_key_t *key, char *cp, unsigned int size) {
1220 1.1 christos char namestr[DNS_NAME_FORMATSIZE];
1221 1.1 christos char algstr[DNS_NAME_FORMATSIZE];
1222 1.1 christos
1223 1.1 christos dns_name_format(dst_key_name(key), namestr, sizeof(namestr));
1224 1.1 christos dns_secalg_format((dns_secalg_t) dst_key_alg(key), algstr,
1225 1.1 christos sizeof(algstr));
1226 1.1 christos snprintf(cp, size, "%s/%s/%d", namestr, algstr, dst_key_id(key));
1227 1.1 christos }
1228 1.1 christos
1229 1.1 christos isc_result_t
1230 1.1 christos dst_key_dump(dst_key_t *key, isc_mem_t *mctx, char **buffer, int *length) {
1231 1.1 christos
1232 1.1 christos REQUIRE(buffer != NULL && *buffer == NULL);
1233 1.1 christos REQUIRE(length != NULL && *length == 0);
1234 1.1 christos REQUIRE(VALID_KEY(key));
1235 1.1 christos
1236 1.1 christos if (key->func->dump == NULL)
1237 1.1 christos return (ISC_R_NOTIMPLEMENTED);
1238 1.1 christos return (key->func->dump(key, mctx, buffer, length));
1239 1.1 christos }
1240 1.1 christos
1241 1.1 christos isc_result_t
1242 1.1 christos dst_key_restore(dns_name_t *name, unsigned int alg, unsigned int flags,
1243 1.1 christos unsigned int protocol, dns_rdataclass_t rdclass,
1244 1.1 christos isc_mem_t *mctx, const char *keystr, dst_key_t **keyp)
1245 1.1 christos {
1246 1.1 christos isc_result_t result;
1247 1.1 christos dst_key_t *key;
1248 1.1 christos
1249 1.3 christos REQUIRE(dst_initialized == true);
1250 1.1 christos REQUIRE(keyp != NULL && *keyp == NULL);
1251 1.1 christos
1252 1.1 christos if (alg >= DST_MAX_ALGS || dst_t_func[alg] == NULL)
1253 1.1 christos return (DST_R_UNSUPPORTEDALG);
1254 1.1 christos
1255 1.1 christos if (dst_t_func[alg]->restore == NULL)
1256 1.1 christos return (ISC_R_NOTIMPLEMENTED);
1257 1.1 christos
1258 1.1 christos key = get_key_struct(name, alg, flags, protocol, 0, rdclass, 0, mctx);
1259 1.1 christos if (key == NULL)
1260 1.1 christos return (ISC_R_NOMEMORY);
1261 1.1 christos
1262 1.1 christos result = (dst_t_func[alg]->restore)(key, keystr);
1263 1.1 christos if (result == ISC_R_SUCCESS)
1264 1.1 christos *keyp = key;
1265 1.1 christos else
1266 1.1 christos dst_key_free(&key);
1267 1.1 christos
1268 1.1 christos return (result);
1269 1.1 christos }
1270 1.1 christos
1271 1.1 christos /***
1272 1.1 christos *** Static methods
1273 1.1 christos ***/
1274 1.1 christos
1275 1.1 christos /*%
1276 1.1 christos * Allocates a key structure and fills in some of the fields.
1277 1.1 christos */
1278 1.1 christos static dst_key_t *
1279 1.1 christos get_key_struct(const dns_name_t *name, unsigned int alg,
1280 1.1 christos unsigned int flags, unsigned int protocol,
1281 1.1 christos unsigned int bits, dns_rdataclass_t rdclass,
1282 1.1 christos dns_ttl_t ttl, isc_mem_t *mctx)
1283 1.1 christos {
1284 1.1 christos dst_key_t *key;
1285 1.1 christos isc_result_t result;
1286 1.1 christos int i;
1287 1.1 christos
1288 1.1 christos key = (dst_key_t *) isc_mem_get(mctx, sizeof(dst_key_t));
1289 1.1 christos if (key == NULL)
1290 1.1 christos return (NULL);
1291 1.1 christos
1292 1.1 christos memset(key, 0, sizeof(dst_key_t));
1293 1.1 christos
1294 1.1 christos key->key_name = isc_mem_get(mctx, sizeof(dns_name_t));
1295 1.1 christos if (key->key_name == NULL) {
1296 1.1 christos isc_mem_put(mctx, key, sizeof(dst_key_t));
1297 1.1 christos return (NULL);
1298 1.1 christos }
1299 1.1 christos
1300 1.1 christos dns_name_init(key->key_name, NULL);
1301 1.1 christos result = dns_name_dup(name, mctx, key->key_name);
1302 1.1 christos if (result != ISC_R_SUCCESS) {
1303 1.1 christos isc_mem_put(mctx, key->key_name, sizeof(dns_name_t));
1304 1.1 christos isc_mem_put(mctx, key, sizeof(dst_key_t));
1305 1.1 christos return (NULL);
1306 1.1 christos }
1307 1.1 christos
1308 1.3 christos isc_refcount_init(&key->refs, 1);
1309 1.1 christos isc_mem_attach(mctx, &key->mctx);
1310 1.1 christos key->key_alg = alg;
1311 1.1 christos key->key_flags = flags;
1312 1.1 christos key->key_proto = protocol;
1313 1.1 christos key->keydata.generic = NULL;
1314 1.1 christos key->key_size = bits;
1315 1.1 christos key->key_class = rdclass;
1316 1.1 christos key->key_ttl = ttl;
1317 1.1 christos key->func = dst_t_func[alg];
1318 1.1 christos key->fmt_major = 0;
1319 1.1 christos key->fmt_minor = 0;
1320 1.1 christos for (i = 0; i < (DST_MAX_TIMES + 1); i++) {
1321 1.1 christos key->times[i] = 0;
1322 1.3 christos key->timeset[i] = false;
1323 1.1 christos }
1324 1.3 christos key->inactive = false;
1325 1.1 christos key->magic = KEY_MAGIC;
1326 1.1 christos return (key);
1327 1.1 christos }
1328 1.1 christos
1329 1.3 christos bool
1330 1.1 christos dst_key_inactive(const dst_key_t *key) {
1331 1.1 christos
1332 1.1 christos REQUIRE(VALID_KEY(key));
1333 1.1 christos
1334 1.1 christos return (key->inactive);
1335 1.1 christos }
1336 1.1 christos
1337 1.1 christos void
1338 1.3 christos dst_key_setinactive(dst_key_t *key, bool inactive) {
1339 1.1 christos
1340 1.1 christos REQUIRE(VALID_KEY(key));
1341 1.1 christos
1342 1.1 christos key->inactive = inactive;
1343 1.1 christos }
1344 1.1 christos
1345 1.1 christos /*%
1346 1.1 christos * Reads a public key from disk
1347 1.1 christos */
1348 1.1 christos isc_result_t
1349 1.1 christos dst_key_read_public(const char *filename, int type,
1350 1.1 christos isc_mem_t *mctx, dst_key_t **keyp)
1351 1.1 christos {
1352 1.1 christos u_char rdatabuf[DST_KEY_MAXSIZE];
1353 1.1 christos isc_buffer_t b;
1354 1.1 christos dns_fixedname_t name;
1355 1.1 christos isc_lex_t *lex = NULL;
1356 1.1 christos isc_token_t token;
1357 1.1 christos isc_result_t ret;
1358 1.1 christos dns_rdata_t rdata = DNS_RDATA_INIT;
1359 1.1 christos unsigned int opt = ISC_LEXOPT_DNSMULTILINE;
1360 1.1 christos dns_rdataclass_t rdclass = dns_rdataclass_in;
1361 1.1 christos isc_lexspecials_t specials;
1362 1.3 christos uint32_t ttl = 0;
1363 1.1 christos isc_result_t result;
1364 1.1 christos dns_rdatatype_t keytype;
1365 1.1 christos
1366 1.1 christos /*
1367 1.1 christos * Open the file and read its formatted contents
1368 1.1 christos * File format:
1369 1.1 christos * domain.name [ttl] [class] [KEY|DNSKEY] <flags> <protocol> <algorithm> <key>
1370 1.1 christos */
1371 1.1 christos
1372 1.1 christos /* 1500 should be large enough for any key */
1373 1.1 christos ret = isc_lex_create(mctx, 1500, &lex);
1374 1.1 christos if (ret != ISC_R_SUCCESS)
1375 1.1 christos goto cleanup;
1376 1.1 christos
1377 1.1 christos memset(specials, 0, sizeof(specials));
1378 1.1 christos specials['('] = 1;
1379 1.1 christos specials[')'] = 1;
1380 1.1 christos specials['"'] = 1;
1381 1.1 christos isc_lex_setspecials(lex, specials);
1382 1.1 christos isc_lex_setcomments(lex, ISC_LEXCOMMENT_DNSMASTERFILE);
1383 1.1 christos
1384 1.1 christos ret = isc_lex_openfile(lex, filename);
1385 1.1 christos if (ret != ISC_R_SUCCESS)
1386 1.1 christos goto cleanup;
1387 1.1 christos
1388 1.1 christos #define NEXTTOKEN(lex, opt, token) { \
1389 1.1 christos ret = isc_lex_gettoken(lex, opt, token); \
1390 1.1 christos if (ret != ISC_R_SUCCESS) \
1391 1.1 christos goto cleanup; \
1392 1.1 christos }
1393 1.1 christos
1394 1.1 christos #define BADTOKEN() { \
1395 1.1 christos ret = ISC_R_UNEXPECTEDTOKEN; \
1396 1.1 christos goto cleanup; \
1397 1.1 christos }
1398 1.1 christos
1399 1.1 christos /* Read the domain name */
1400 1.1 christos NEXTTOKEN(lex, opt, &token);
1401 1.1 christos if (token.type != isc_tokentype_string)
1402 1.1 christos BADTOKEN();
1403 1.1 christos
1404 1.1 christos /*
1405 1.1 christos * We don't support "@" in .key files.
1406 1.1 christos */
1407 1.1 christos if (!strcmp(DST_AS_STR(token), "@"))
1408 1.1 christos BADTOKEN();
1409 1.1 christos
1410 1.1 christos dns_fixedname_init(&name);
1411 1.1 christos isc_buffer_init(&b, DST_AS_STR(token), strlen(DST_AS_STR(token)));
1412 1.1 christos isc_buffer_add(&b, strlen(DST_AS_STR(token)));
1413 1.1 christos ret = dns_name_fromtext(dns_fixedname_name(&name), &b, dns_rootname,
1414 1.1 christos 0, NULL);
1415 1.1 christos if (ret != ISC_R_SUCCESS)
1416 1.1 christos goto cleanup;
1417 1.1 christos
1418 1.1 christos /* Read the next word: either TTL, class, or 'KEY' */
1419 1.1 christos NEXTTOKEN(lex, opt, &token);
1420 1.1 christos
1421 1.1 christos if (token.type != isc_tokentype_string)
1422 1.1 christos BADTOKEN();
1423 1.1 christos
1424 1.1 christos /* If it's a TTL, read the next one */
1425 1.1 christos result = dns_ttl_fromtext(&token.value.as_textregion, &ttl);
1426 1.1 christos if (result == ISC_R_SUCCESS)
1427 1.1 christos NEXTTOKEN(lex, opt, &token);
1428 1.1 christos
1429 1.1 christos if (token.type != isc_tokentype_string)
1430 1.1 christos BADTOKEN();
1431 1.1 christos
1432 1.1 christos ret = dns_rdataclass_fromtext(&rdclass, &token.value.as_textregion);
1433 1.1 christos if (ret == ISC_R_SUCCESS)
1434 1.1 christos NEXTTOKEN(lex, opt, &token);
1435 1.1 christos
1436 1.1 christos if (token.type != isc_tokentype_string)
1437 1.1 christos BADTOKEN();
1438 1.1 christos
1439 1.1 christos if (strcasecmp(DST_AS_STR(token), "DNSKEY") == 0)
1440 1.1 christos keytype = dns_rdatatype_dnskey;
1441 1.1 christos else if (strcasecmp(DST_AS_STR(token), "KEY") == 0)
1442 1.1 christos keytype = dns_rdatatype_key; /*%< SIG(0), TKEY */
1443 1.1 christos else
1444 1.1 christos BADTOKEN();
1445 1.1 christos
1446 1.1 christos if (((type & DST_TYPE_KEY) != 0 && keytype != dns_rdatatype_key) ||
1447 1.1 christos ((type & DST_TYPE_KEY) == 0 && keytype != dns_rdatatype_dnskey)) {
1448 1.1 christos ret = DST_R_BADKEYTYPE;
1449 1.1 christos goto cleanup;
1450 1.1 christos }
1451 1.1 christos
1452 1.1 christos isc_buffer_init(&b, rdatabuf, sizeof(rdatabuf));
1453 1.1 christos ret = dns_rdata_fromtext(&rdata, rdclass, keytype, lex, NULL,
1454 1.3 christos false, mctx, &b, NULL);
1455 1.1 christos if (ret != ISC_R_SUCCESS)
1456 1.1 christos goto cleanup;
1457 1.1 christos
1458 1.1 christos ret = dst_key_fromdns(dns_fixedname_name(&name), rdclass, &b, mctx,
1459 1.1 christos keyp);
1460 1.1 christos if (ret != ISC_R_SUCCESS)
1461 1.1 christos goto cleanup;
1462 1.1 christos
1463 1.1 christos dst_key_setttl(*keyp, ttl);
1464 1.1 christos
1465 1.1 christos cleanup:
1466 1.1 christos if (lex != NULL)
1467 1.1 christos isc_lex_destroy(&lex);
1468 1.1 christos return (ret);
1469 1.1 christos }
1470 1.1 christos
1471 1.3 christos static bool
1472 1.1 christos issymmetric(const dst_key_t *key) {
1473 1.3 christos REQUIRE(dst_initialized == true);
1474 1.1 christos REQUIRE(VALID_KEY(key));
1475 1.1 christos
1476 1.1 christos /* XXXVIX this switch statement is too sparse to gen a jump table. */
1477 1.1 christos switch (key->key_alg) {
1478 1.1 christos case DST_ALG_RSAMD5:
1479 1.1 christos case DST_ALG_RSASHA1:
1480 1.1 christos case DST_ALG_NSEC3RSASHA1:
1481 1.1 christos case DST_ALG_RSASHA256:
1482 1.1 christos case DST_ALG_RSASHA512:
1483 1.1 christos case DST_ALG_DH:
1484 1.1 christos case DST_ALG_ECDSA256:
1485 1.1 christos case DST_ALG_ECDSA384:
1486 1.1 christos case DST_ALG_ED25519:
1487 1.1 christos case DST_ALG_ED448:
1488 1.3 christos return (false);
1489 1.1 christos case DST_ALG_HMACMD5:
1490 1.1 christos case DST_ALG_HMACSHA1:
1491 1.1 christos case DST_ALG_HMACSHA224:
1492 1.1 christos case DST_ALG_HMACSHA256:
1493 1.1 christos case DST_ALG_HMACSHA384:
1494 1.1 christos case DST_ALG_HMACSHA512:
1495 1.1 christos case DST_ALG_GSSAPI:
1496 1.3 christos return (true);
1497 1.1 christos default:
1498 1.3 christos return (false);
1499 1.1 christos }
1500 1.1 christos }
1501 1.1 christos
1502 1.1 christos /*%
1503 1.1 christos * Write key timing metadata to a file pointer, preceded by 'tag'
1504 1.1 christos */
1505 1.1 christos static void
1506 1.1 christos printtime(const dst_key_t *key, int type, const char *tag, FILE *stream) {
1507 1.1 christos isc_result_t result;
1508 1.1 christos char output[26]; /* Minimum buffer as per ctime_r() specification. */
1509 1.1 christos isc_stdtime_t when;
1510 1.1 christos time_t t;
1511 1.1 christos char utc[sizeof("YYYYMMDDHHSSMM")];
1512 1.1 christos isc_buffer_t b;
1513 1.1 christos isc_region_t r;
1514 1.1 christos
1515 1.1 christos result = dst_key_gettime(key, type, &when);
1516 1.1 christos if (result == ISC_R_NOTFOUND)
1517 1.1 christos return;
1518 1.1 christos
1519 1.1 christos /* time_t and isc_stdtime_t might be different sizes */
1520 1.1 christos t = when;
1521 1.1 christos #ifdef WIN32
1522 1.1 christos if (ctime_s(output, sizeof(output), &t) != 0)
1523 1.1 christos goto error;
1524 1.1 christos #else
1525 1.1 christos if (ctime_r(&t, output) == NULL)
1526 1.1 christos goto error;
1527 1.1 christos #endif
1528 1.1 christos
1529 1.1 christos isc_buffer_init(&b, utc, sizeof(utc));
1530 1.1 christos result = dns_time32_totext(when, &b);
1531 1.1 christos if (result != ISC_R_SUCCESS)
1532 1.1 christos goto error;
1533 1.1 christos
1534 1.1 christos isc_buffer_usedregion(&b, &r);
1535 1.1 christos fprintf(stream, "%s: %.*s (%.*s)\n", tag, (int)r.length, r.base,
1536 1.1 christos (int)strlen(output) - 1, output);
1537 1.1 christos return;
1538 1.1 christos
1539 1.1 christos error:
1540 1.1 christos fprintf(stream, "%s: (set, unable to display)\n", tag);
1541 1.1 christos }
1542 1.1 christos
1543 1.1 christos /*%
1544 1.1 christos * Writes a public key to disk in DNS format.
1545 1.1 christos */
1546 1.1 christos static isc_result_t
1547 1.1 christos write_public_key(const dst_key_t *key, int type, const char *directory) {
1548 1.1 christos FILE *fp;
1549 1.1 christos isc_buffer_t keyb, textb, fileb, classb;
1550 1.1 christos isc_region_t r;
1551 1.3 christos char filename[NAME_MAX];
1552 1.1 christos unsigned char key_array[DST_KEY_MAXSIZE];
1553 1.1 christos char text_array[DST_KEY_MAXTEXTSIZE];
1554 1.1 christos char class_array[10];
1555 1.1 christos isc_result_t ret;
1556 1.1 christos dns_rdata_t rdata = DNS_RDATA_INIT;
1557 1.1 christos isc_fsaccess_t access;
1558 1.1 christos
1559 1.1 christos REQUIRE(VALID_KEY(key));
1560 1.1 christos
1561 1.1 christos isc_buffer_init(&keyb, key_array, sizeof(key_array));
1562 1.1 christos isc_buffer_init(&textb, text_array, sizeof(text_array));
1563 1.1 christos isc_buffer_init(&classb, class_array, sizeof(class_array));
1564 1.1 christos
1565 1.1 christos ret = dst_key_todns(key, &keyb);
1566 1.1 christos if (ret != ISC_R_SUCCESS)
1567 1.1 christos return (ret);
1568 1.1 christos
1569 1.1 christos isc_buffer_usedregion(&keyb, &r);
1570 1.1 christos dns_rdata_fromregion(&rdata, key->key_class, dns_rdatatype_dnskey, &r);
1571 1.1 christos
1572 1.1 christos ret = dns_rdata_totext(&rdata, (dns_name_t *) NULL, &textb);
1573 1.1 christos if (ret != ISC_R_SUCCESS)
1574 1.1 christos return (DST_R_INVALIDPUBLICKEY);
1575 1.1 christos
1576 1.1 christos ret = dns_rdataclass_totext(key->key_class, &classb);
1577 1.1 christos if (ret != ISC_R_SUCCESS)
1578 1.1 christos return (DST_R_INVALIDPUBLICKEY);
1579 1.1 christos
1580 1.1 christos /*
1581 1.1 christos * Make the filename.
1582 1.1 christos */
1583 1.1 christos isc_buffer_init(&fileb, filename, sizeof(filename));
1584 1.1 christos ret = dst_key_buildfilename(key, DST_TYPE_PUBLIC, directory, &fileb);
1585 1.1 christos if (ret != ISC_R_SUCCESS)
1586 1.1 christos return (ret);
1587 1.1 christos
1588 1.1 christos /*
1589 1.1 christos * Create public key file.
1590 1.1 christos */
1591 1.1 christos if ((fp = fopen(filename, "w")) == NULL)
1592 1.1 christos return (DST_R_WRITEERROR);
1593 1.1 christos
1594 1.1 christos if (issymmetric(key)) {
1595 1.1 christos access = 0;
1596 1.1 christos isc_fsaccess_add(ISC_FSACCESS_OWNER,
1597 1.1 christos ISC_FSACCESS_READ | ISC_FSACCESS_WRITE,
1598 1.1 christos &access);
1599 1.1 christos (void)isc_fsaccess_set(filename, access);
1600 1.1 christos }
1601 1.1 christos
1602 1.1 christos /* Write key information in comments */
1603 1.1 christos if ((type & DST_TYPE_KEY) == 0) {
1604 1.1 christos fprintf(fp, "; This is a %s%s-signing key, keyid %d, for ",
1605 1.1 christos (key->key_flags & DNS_KEYFLAG_REVOKE) != 0 ?
1606 1.1 christos "revoked " :
1607 1.1 christos "",
1608 1.1 christos (key->key_flags & DNS_KEYFLAG_KSK) != 0 ?
1609 1.1 christos "key" :
1610 1.1 christos "zone",
1611 1.1 christos key->key_id);
1612 1.1 christos ret = dns_name_print(key->key_name, fp);
1613 1.1 christos if (ret != ISC_R_SUCCESS) {
1614 1.1 christos fclose(fp);
1615 1.1 christos return (ret);
1616 1.1 christos }
1617 1.1 christos fputc('\n', fp);
1618 1.1 christos
1619 1.1 christos printtime(key, DST_TIME_CREATED, "; Created", fp);
1620 1.1 christos printtime(key, DST_TIME_PUBLISH, "; Publish", fp);
1621 1.1 christos printtime(key, DST_TIME_ACTIVATE, "; Activate", fp);
1622 1.1 christos printtime(key, DST_TIME_REVOKE, "; Revoke", fp);
1623 1.1 christos printtime(key, DST_TIME_INACTIVE, "; Inactive", fp);
1624 1.1 christos printtime(key, DST_TIME_DELETE, "; Delete", fp);
1625 1.1 christos printtime(key, DST_TIME_SYNCPUBLISH , "; SyncPublish", fp);
1626 1.1 christos printtime(key, DST_TIME_SYNCDELETE , "; SyncDelete", fp);
1627 1.1 christos }
1628 1.1 christos
1629 1.1 christos /* Now print the actual key */
1630 1.1 christos ret = dns_name_print(key->key_name, fp);
1631 1.1 christos fprintf(fp, " ");
1632 1.1 christos
1633 1.1 christos if (key->key_ttl != 0)
1634 1.1 christos fprintf(fp, "%u ", key->key_ttl);
1635 1.1 christos
1636 1.1 christos isc_buffer_usedregion(&classb, &r);
1637 1.1 christos if ((unsigned) fwrite(r.base, 1, r.length, fp) != r.length)
1638 1.1 christos ret = DST_R_WRITEERROR;
1639 1.1 christos
1640 1.1 christos if ((type & DST_TYPE_KEY) != 0)
1641 1.1 christos fprintf(fp, " KEY ");
1642 1.1 christos else
1643 1.1 christos fprintf(fp, " DNSKEY ");
1644 1.1 christos
1645 1.1 christos isc_buffer_usedregion(&textb, &r);
1646 1.1 christos if ((unsigned) fwrite(r.base, 1, r.length, fp) != r.length)
1647 1.1 christos ret = DST_R_WRITEERROR;
1648 1.1 christos
1649 1.1 christos fputc('\n', fp);
1650 1.1 christos fflush(fp);
1651 1.1 christos if (ferror(fp))
1652 1.1 christos ret = DST_R_WRITEERROR;
1653 1.1 christos fclose(fp);
1654 1.1 christos
1655 1.1 christos return (ret);
1656 1.1 christos }
1657 1.1 christos
1658 1.1 christos static isc_result_t
1659 1.1 christos buildfilename(dns_name_t *name, dns_keytag_t id,
1660 1.1 christos unsigned int alg, unsigned int type,
1661 1.1 christos const char *directory, isc_buffer_t *out)
1662 1.1 christos {
1663 1.1 christos const char *suffix = "";
1664 1.1 christos isc_result_t result;
1665 1.1 christos
1666 1.1 christos REQUIRE(out != NULL);
1667 1.1 christos if ((type & DST_TYPE_PRIVATE) != 0)
1668 1.1 christos suffix = ".private";
1669 1.1 christos else if (type == DST_TYPE_PUBLIC)
1670 1.1 christos suffix = ".key";
1671 1.1 christos if (directory != NULL) {
1672 1.1 christos if (isc_buffer_availablelength(out) < strlen(directory))
1673 1.1 christos return (ISC_R_NOSPACE);
1674 1.1 christos isc_buffer_putstr(out, directory);
1675 1.1 christos if (strlen(directory) > 0U &&
1676 1.1 christos directory[strlen(directory) - 1] != '/')
1677 1.1 christos isc_buffer_putstr(out, "/");
1678 1.1 christos }
1679 1.1 christos if (isc_buffer_availablelength(out) < 1)
1680 1.1 christos return (ISC_R_NOSPACE);
1681 1.1 christos isc_buffer_putstr(out, "K");
1682 1.3 christos result = dns_name_tofilenametext(name, false, out);
1683 1.1 christos if (result != ISC_R_SUCCESS)
1684 1.1 christos return (result);
1685 1.1 christos
1686 1.1 christos return (isc_buffer_printf(out, "+%03d+%05d%s", alg, id, suffix));
1687 1.1 christos }
1688 1.1 christos
1689 1.1 christos static isc_result_t
1690 1.1 christos computeid(dst_key_t *key) {
1691 1.1 christos isc_buffer_t dnsbuf;
1692 1.1 christos unsigned char dns_array[DST_KEY_MAXSIZE];
1693 1.1 christos isc_region_t r;
1694 1.1 christos isc_result_t ret;
1695 1.1 christos
1696 1.1 christos isc_buffer_init(&dnsbuf, dns_array, sizeof(dns_array));
1697 1.1 christos ret = dst_key_todns(key, &dnsbuf);
1698 1.1 christos if (ret != ISC_R_SUCCESS)
1699 1.1 christos return (ret);
1700 1.1 christos
1701 1.1 christos isc_buffer_usedregion(&dnsbuf, &r);
1702 1.1 christos key->key_id = dst_region_computeid(&r, key->key_alg);
1703 1.1 christos key->key_rid = dst_region_computerid(&r, key->key_alg);
1704 1.1 christos return (ISC_R_SUCCESS);
1705 1.1 christos }
1706 1.1 christos
1707 1.1 christos static isc_result_t
1708 1.1 christos frombuffer(const dns_name_t *name, unsigned int alg, unsigned int flags,
1709 1.1 christos unsigned int protocol, dns_rdataclass_t rdclass,
1710 1.1 christos isc_buffer_t *source, isc_mem_t *mctx, dst_key_t **keyp)
1711 1.1 christos {
1712 1.1 christos dst_key_t *key;
1713 1.1 christos isc_result_t ret;
1714 1.1 christos
1715 1.1 christos REQUIRE(dns_name_isabsolute(name));
1716 1.1 christos REQUIRE(source != NULL);
1717 1.1 christos REQUIRE(mctx != NULL);
1718 1.1 christos REQUIRE(keyp != NULL && *keyp == NULL);
1719 1.1 christos
1720 1.1 christos key = get_key_struct(name, alg, flags, protocol, 0, rdclass, 0, mctx);
1721 1.1 christos if (key == NULL)
1722 1.1 christos return (ISC_R_NOMEMORY);
1723 1.1 christos
1724 1.1 christos if (isc_buffer_remaininglength(source) > 0) {
1725 1.1 christos ret = algorithm_status(alg);
1726 1.1 christos if (ret != ISC_R_SUCCESS) {
1727 1.1 christos dst_key_free(&key);
1728 1.1 christos return (ret);
1729 1.1 christos }
1730 1.1 christos if (key->func->fromdns == NULL) {
1731 1.1 christos dst_key_free(&key);
1732 1.1 christos return (DST_R_UNSUPPORTEDALG);
1733 1.1 christos }
1734 1.1 christos
1735 1.1 christos ret = key->func->fromdns(key, source);
1736 1.1 christos if (ret != ISC_R_SUCCESS) {
1737 1.1 christos dst_key_free(&key);
1738 1.1 christos return (ret);
1739 1.1 christos }
1740 1.1 christos }
1741 1.1 christos
1742 1.1 christos *keyp = key;
1743 1.1 christos return (ISC_R_SUCCESS);
1744 1.1 christos }
1745 1.1 christos
1746 1.1 christos static isc_result_t
1747 1.1 christos algorithm_status(unsigned int alg) {
1748 1.3 christos REQUIRE(dst_initialized == true);
1749 1.1 christos
1750 1.3 christos if (dst_algorithm_supported(alg)) {
1751 1.1 christos return (ISC_R_SUCCESS);
1752 1.3 christos }
1753 1.1 christos return (DST_R_UNSUPPORTEDALG);
1754 1.1 christos }
1755 1.1 christos
1756 1.1 christos static isc_result_t
1757 1.1 christos addsuffix(char *filename, int len, const char *odirname,
1758 1.1 christos const char *ofilename, const char *suffix)
1759 1.1 christos {
1760 1.1 christos int olen = strlen(ofilename);
1761 1.1 christos int n;
1762 1.1 christos
1763 1.1 christos if (olen > 1 && ofilename[olen - 1] == '.')
1764 1.1 christos olen -= 1;
1765 1.1 christos else if (olen > 8 && strcmp(ofilename + olen - 8, ".private") == 0)
1766 1.1 christos olen -= 8;
1767 1.1 christos else if (olen > 4 && strcmp(ofilename + olen - 4, ".key") == 0)
1768 1.1 christos olen -= 4;
1769 1.1 christos
1770 1.1 christos if (odirname == NULL)
1771 1.1 christos n = snprintf(filename, len, "%.*s%s", olen, ofilename, suffix);
1772 1.1 christos else
1773 1.1 christos n = snprintf(filename, len, "%s/%.*s%s",
1774 1.1 christos odirname, olen, ofilename, suffix);
1775 1.1 christos if (n < 0)
1776 1.1 christos return (ISC_R_FAILURE);
1777 1.1 christos if (n >= len)
1778 1.1 christos return (ISC_R_NOSPACE);
1779 1.1 christos return (ISC_R_SUCCESS);
1780 1.1 christos }
1781 1.1 christos
1782 1.1 christos isc_buffer_t *
1783 1.1 christos dst_key_tkeytoken(const dst_key_t *key) {
1784 1.1 christos REQUIRE(VALID_KEY(key));
1785 1.1 christos return (key->key_tkeytoken);
1786 1.1 christos }
1787