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