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