/src/bind9/lib/dns/hmac_link.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
3 | | * |
4 | | * SPDX-License-Identifier: MPL-2.0 AND ISC |
5 | | * |
6 | | * This Source Code Form is subject to the terms of the Mozilla Public |
7 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
8 | | * file, you can obtain one at https://mozilla.org/MPL/2.0/. |
9 | | * |
10 | | * See the COPYRIGHT file distributed with this work for additional |
11 | | * information regarding copyright ownership. |
12 | | */ |
13 | | |
14 | | /* |
15 | | * Copyright (C) Network Associates, Inc. |
16 | | * |
17 | | * Permission to use, copy, modify, and/or distribute this software for any |
18 | | * purpose with or without fee is hereby granted, provided that the above |
19 | | * copyright notice and this permission notice appear in all copies. |
20 | | * |
21 | | * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS |
22 | | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED |
23 | | * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE |
24 | | * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
25 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
26 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR |
27 | | * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
28 | | */ |
29 | | |
30 | | #include <arpa/inet.h> |
31 | | #include <stdbool.h> |
32 | | |
33 | | #include <isc/buffer.h> |
34 | | #include <isc/hmac.h> |
35 | | #include <isc/lex.h> |
36 | | #include <isc/md.h> |
37 | | #include <isc/mem.h> |
38 | | #include <isc/nonce.h> |
39 | | #include <isc/random.h> |
40 | | #include <isc/result.h> |
41 | | #include <isc/safe.h> |
42 | | #include <isc/string.h> |
43 | | #include <isc/util.h> |
44 | | |
45 | | #include "dst_internal.h" |
46 | | #include "dst_parse.h" |
47 | | |
48 | 22 | #define ISC_MD_md5 ISC_MD_MD5 |
49 | 22 | #define ISC_MD_sha1 ISC_MD_SHA1 |
50 | 22 | #define ISC_MD_sha224 ISC_MD_SHA224 |
51 | 24 | #define ISC_MD_sha256 ISC_MD_SHA256 |
52 | 22 | #define ISC_MD_sha384 ISC_MD_SHA384 |
53 | 22 | #define ISC_MD_sha512 ISC_MD_SHA512 |
54 | | |
55 | | #define hmac_register_algorithm(alg) \ |
56 | | static isc_result_t hmac##alg##_createctx(dst_key_t *key, \ |
57 | 46 | dst_context_t *dctx) { \ |
58 | 46 | return (hmac_createctx(key, dctx)); \ |
59 | 46 | } \ Unexecuted instantiation: hmac_link.c:hmacmd5_createctx Unexecuted instantiation: hmac_link.c:hmacsha1_createctx Unexecuted instantiation: hmac_link.c:hmacsha224_createctx hmac_link.c:hmacsha256_createctx Line | Count | Source | 57 | 46 | dst_context_t *dctx) { \ | 58 | 46 | return (hmac_createctx(key, dctx)); \ | 59 | 46 | } \ |
Unexecuted instantiation: hmac_link.c:hmacsha384_createctx Unexecuted instantiation: hmac_link.c:hmacsha512_createctx |
60 | 46 | static void hmac##alg##_destroyctx(dst_context_t *dctx) { \ |
61 | 46 | hmac_destroyctx(dctx); \ |
62 | 46 | } \ Unexecuted instantiation: hmac_link.c:hmacmd5_destroyctx Unexecuted instantiation: hmac_link.c:hmacsha1_destroyctx Unexecuted instantiation: hmac_link.c:hmacsha224_destroyctx hmac_link.c:hmacsha256_destroyctx Line | Count | Source | 60 | 46 | static void hmac##alg##_destroyctx(dst_context_t *dctx) { \ | 61 | 46 | hmac_destroyctx(dctx); \ | 62 | 46 | } \ |
Unexecuted instantiation: hmac_link.c:hmacsha384_destroyctx Unexecuted instantiation: hmac_link.c:hmacsha512_destroyctx |
63 | | static isc_result_t hmac##alg##_adddata(dst_context_t *dctx, \ |
64 | 311 | const isc_region_t *data) { \ |
65 | 311 | return (hmac_adddata(dctx, data)); \ |
66 | 311 | } \ Unexecuted instantiation: hmac_link.c:hmacmd5_adddata Unexecuted instantiation: hmac_link.c:hmacsha1_adddata Unexecuted instantiation: hmac_link.c:hmacsha224_adddata hmac_link.c:hmacsha256_adddata Line | Count | Source | 64 | 311 | const isc_region_t *data) { \ | 65 | 311 | return (hmac_adddata(dctx, data)); \ | 66 | 311 | } \ |
Unexecuted instantiation: hmac_link.c:hmacsha384_adddata Unexecuted instantiation: hmac_link.c:hmacsha512_adddata |
67 | | static isc_result_t hmac##alg##_sign(dst_context_t *dctx, \ |
68 | 0 | isc_buffer_t *sig) { \ |
69 | 0 | return (hmac_sign(dctx, sig)); \ |
70 | 0 | } \ Unexecuted instantiation: hmac_link.c:hmacmd5_sign Unexecuted instantiation: hmac_link.c:hmacsha1_sign Unexecuted instantiation: hmac_link.c:hmacsha224_sign Unexecuted instantiation: hmac_link.c:hmacsha256_sign Unexecuted instantiation: hmac_link.c:hmacsha384_sign Unexecuted instantiation: hmac_link.c:hmacsha512_sign |
71 | | static isc_result_t hmac##alg##_verify(dst_context_t *dctx, \ |
72 | 46 | const isc_region_t *sig) { \ |
73 | 46 | return (hmac_verify(dctx, sig)); \ |
74 | 46 | } \ Unexecuted instantiation: hmac_link.c:hmacmd5_verify Unexecuted instantiation: hmac_link.c:hmacsha1_verify Unexecuted instantiation: hmac_link.c:hmacsha224_verify hmac_link.c:hmacsha256_verify Line | Count | Source | 72 | 46 | const isc_region_t *sig) { \ | 73 | 46 | return (hmac_verify(dctx, sig)); \ | 74 | 46 | } \ |
Unexecuted instantiation: hmac_link.c:hmacsha384_verify Unexecuted instantiation: hmac_link.c:hmacsha512_verify |
75 | | static bool hmac##alg##_compare(const dst_key_t *key1, \ |
76 | 0 | const dst_key_t *key2) { \ |
77 | 0 | return (hmac_compare(key1, key2)); \ |
78 | 0 | } \ Unexecuted instantiation: hmac_link.c:hmacmd5_compare Unexecuted instantiation: hmac_link.c:hmacsha1_compare Unexecuted instantiation: hmac_link.c:hmacsha224_compare Unexecuted instantiation: hmac_link.c:hmacsha256_compare Unexecuted instantiation: hmac_link.c:hmacsha384_compare Unexecuted instantiation: hmac_link.c:hmacsha512_compare |
79 | | static isc_result_t hmac##alg##_generate( \ |
80 | 0 | dst_key_t *key, int pseudorandom_ok, void (*callback)(int)) { \ |
81 | 0 | UNUSED(pseudorandom_ok); \ |
82 | 0 | UNUSED(callback); \ |
83 | 0 | return (hmac_generate(ISC_MD_##alg, key)); \ |
84 | 0 | } \ Unexecuted instantiation: hmac_link.c:hmacmd5_generate Unexecuted instantiation: hmac_link.c:hmacsha1_generate Unexecuted instantiation: hmac_link.c:hmacsha224_generate Unexecuted instantiation: hmac_link.c:hmacsha256_generate Unexecuted instantiation: hmac_link.c:hmacsha384_generate Unexecuted instantiation: hmac_link.c:hmacsha512_generate |
85 | 0 | static bool hmac##alg##_isprivate(const dst_key_t *key) { \ |
86 | 0 | return (hmac_isprivate(key)); \ |
87 | 0 | } \ Unexecuted instantiation: hmac_link.c:hmacmd5_isprivate Unexecuted instantiation: hmac_link.c:hmacsha1_isprivate Unexecuted instantiation: hmac_link.c:hmacsha224_isprivate Unexecuted instantiation: hmac_link.c:hmacsha256_isprivate Unexecuted instantiation: hmac_link.c:hmacsha384_isprivate Unexecuted instantiation: hmac_link.c:hmacsha512_isprivate |
88 | 0 | static void hmac##alg##_destroy(dst_key_t *key) { hmac_destroy(key); } \Unexecuted instantiation: hmac_link.c:hmacmd5_destroy Unexecuted instantiation: hmac_link.c:hmacsha1_destroy Unexecuted instantiation: hmac_link.c:hmacsha224_destroy Unexecuted instantiation: hmac_link.c:hmacsha256_destroy Unexecuted instantiation: hmac_link.c:hmacsha384_destroy Unexecuted instantiation: hmac_link.c:hmacsha512_destroy |
89 | | static isc_result_t hmac##alg##_todns(const dst_key_t *key, \ |
90 | 2 | isc_buffer_t *data) { \ |
91 | 2 | return (hmac_todns(key, data)); \ |
92 | 2 | } \ Unexecuted instantiation: hmac_link.c:hmacmd5_todns Unexecuted instantiation: hmac_link.c:hmacsha1_todns Unexecuted instantiation: hmac_link.c:hmacsha224_todns hmac_link.c:hmacsha256_todns Line | Count | Source | 90 | 2 | isc_buffer_t *data) { \ | 91 | 2 | return (hmac_todns(key, data)); \ | 92 | 2 | } \ |
Unexecuted instantiation: hmac_link.c:hmacsha384_todns Unexecuted instantiation: hmac_link.c:hmacsha512_todns |
93 | | static isc_result_t hmac##alg##_fromdns(dst_key_t *key, \ |
94 | 2 | isc_buffer_t *data) { \ |
95 | 2 | return (hmac_fromdns(ISC_MD_##alg, key, data)); \ |
96 | 2 | } \ Unexecuted instantiation: hmac_link.c:hmacmd5_fromdns Unexecuted instantiation: hmac_link.c:hmacsha1_fromdns Unexecuted instantiation: hmac_link.c:hmacsha224_fromdns hmac_link.c:hmacsha256_fromdns Line | Count | Source | 94 | 2 | isc_buffer_t *data) { \ | 95 | 2 | return (hmac_fromdns(ISC_MD_##alg, key, data)); \ | 96 | 2 | } \ |
Unexecuted instantiation: hmac_link.c:hmacsha384_fromdns Unexecuted instantiation: hmac_link.c:hmacsha512_fromdns |
97 | | static isc_result_t hmac##alg##_tofile(const dst_key_t *key, \ |
98 | 0 | const char *directory) { \ |
99 | 0 | return (hmac_tofile(ISC_MD_##alg, key, directory)); \ |
100 | 0 | } \ Unexecuted instantiation: hmac_link.c:hmacmd5_tofile Unexecuted instantiation: hmac_link.c:hmacsha1_tofile Unexecuted instantiation: hmac_link.c:hmacsha224_tofile Unexecuted instantiation: hmac_link.c:hmacsha256_tofile Unexecuted instantiation: hmac_link.c:hmacsha384_tofile Unexecuted instantiation: hmac_link.c:hmacsha512_tofile |
101 | | static isc_result_t hmac##alg##_parse( \ |
102 | 0 | dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub) { \ |
103 | 0 | const char *file = isc_lex_getsourcename(lexer); \ |
104 | 0 | isc_result_t result; \ |
105 | 0 | result = hmac_parse(ISC_MD_##alg, key, lexer, pub); \ |
106 | 0 | if (result == ISC_R_SUCCESS && file != NULL) { \ |
107 | 0 | isc_log_write(DNS_LOGCATEGORY_GENERAL, \ |
108 | 0 | DNS_LOGMODULE_CRYPTO, ISC_LOG_WARNING, \ |
109 | 0 | "%s: Use of K* file pairs for HMAC is " \ |
110 | 0 | "deprecated\n", \ |
111 | 0 | file); \ |
112 | 0 | } \ |
113 | 0 | return (result); \ |
114 | 0 | } \ |
115 | | static dst_func_t hmac##alg##_functions = { \ |
116 | | .createctx = hmac##alg##_createctx, \ |
117 | | .destroyctx = hmac##alg##_destroyctx, \ |
118 | | .adddata = hmac##alg##_adddata, \ |
119 | | .sign = hmac##alg##_sign, \ |
120 | | .verify = hmac##alg##_verify, \ |
121 | | .compare = hmac##alg##_compare, \ |
122 | | .generate = hmac##alg##_generate, \ |
123 | | .isprivate = hmac##alg##_isprivate, \ |
124 | | .destroy = hmac##alg##_destroy, \ |
125 | | .todns = hmac##alg##_todns, \ |
126 | | .fromdns = hmac##alg##_fromdns, \ |
127 | | .tofile = hmac##alg##_tofile, \ |
128 | | .parse = hmac##alg##_parse, \ |
129 | | }; \ |
130 | 132 | void dst__hmac##alg##_init(dst_func_t **funcp) { \ |
131 | 132 | REQUIRE(funcp != NULL); \ |
132 | 132 | if (*funcp == NULL) { \ |
133 | 132 | uint8_t data[] = "data"; \ |
134 | 132 | uint8_t mac_buffer[ISC_MAX_MD_SIZE]; \ |
135 | 132 | unsigned int mac_len = sizeof(mac_buffer); \ |
136 | 132 | if (isc_hmac(ISC_MD_##alg, "test", 4, data, 4, \ |
137 | 132 | mac_buffer, &mac_len) == ISC_R_SUCCESS) \ |
138 | 132 | { \ |
139 | 132 | *funcp = &hmac##alg##_functions; \ |
140 | 132 | } \ |
141 | 132 | } \ |
142 | 132 | } |
143 | | |
144 | | static isc_result_t |
145 | | hmac_fromdns(isc_md_type_t type, dst_key_t *key, isc_buffer_t *data); |
146 | | |
147 | | static isc_result_t |
148 | 0 | getkeybits(dst_key_t *key, struct dst_private_element *element) { |
149 | 0 | uint16_t *bits = (uint16_t *)element->data; |
150 | |
|
151 | 0 | if (element->length != 2) { |
152 | 0 | return DST_R_INVALIDPRIVATEKEY; |
153 | 0 | } |
154 | | |
155 | 0 | key->key_bits = ntohs(*bits); |
156 | |
|
157 | 0 | return ISC_R_SUCCESS; |
158 | 0 | } |
159 | | |
160 | | static isc_result_t |
161 | 46 | hmac_createctx(const dst_key_t *key, dst_context_t *dctx) { |
162 | 46 | isc_result_t result; |
163 | 46 | isc_hmac_t *ctx = isc_hmac_new(); /* Either returns or abort()s */ |
164 | | |
165 | 46 | result = isc_hmac_init(ctx, key->keydata.hmac_key); |
166 | 46 | if (result != ISC_R_SUCCESS) { |
167 | 0 | isc_hmac_free(ctx); |
168 | 0 | return DST_R_UNSUPPORTEDALG; |
169 | 0 | } |
170 | | |
171 | 46 | dctx->ctxdata.hmac_ctx = ctx; |
172 | 46 | return ISC_R_SUCCESS; |
173 | 46 | } |
174 | | |
175 | | static void |
176 | 46 | hmac_destroyctx(dst_context_t *dctx) { |
177 | 46 | isc_hmac_t *ctx = dctx->ctxdata.hmac_ctx; |
178 | 46 | REQUIRE(ctx != NULL); |
179 | | |
180 | 46 | isc_hmac_free(ctx); |
181 | 46 | dctx->ctxdata.hmac_ctx = NULL; |
182 | 46 | } |
183 | | |
184 | | static isc_result_t |
185 | 311 | hmac_adddata(const dst_context_t *dctx, const isc_region_t *data) { |
186 | 311 | isc_result_t result; |
187 | 311 | isc_hmac_t *ctx = dctx->ctxdata.hmac_ctx; |
188 | | |
189 | 311 | REQUIRE(ctx != NULL); |
190 | | |
191 | 311 | result = isc_hmac_update(ctx, data->base, data->length); |
192 | 311 | if (result != ISC_R_SUCCESS) { |
193 | 0 | return DST_R_OPENSSLFAILURE; |
194 | 0 | } |
195 | | |
196 | 311 | return ISC_R_SUCCESS; |
197 | 311 | } |
198 | | |
199 | | static isc_result_t |
200 | 0 | hmac_sign(const dst_context_t *dctx, isc_buffer_t *sig) { |
201 | 0 | isc_hmac_t *ctx = dctx->ctxdata.hmac_ctx; |
202 | |
|
203 | 0 | REQUIRE(ctx != NULL); |
204 | |
|
205 | 0 | return isc_hmac_final(ctx, sig); |
206 | 0 | } |
207 | | |
208 | | static isc_result_t |
209 | 46 | hmac_verify(const dst_context_t *dctx, const isc_region_t *sig) { |
210 | 46 | isc_hmac_t *ctx = dctx->ctxdata.hmac_ctx; |
211 | 46 | unsigned char digest[ISC_MAX_MD_SIZE]; |
212 | 46 | isc_buffer_t hmac; |
213 | | |
214 | 46 | REQUIRE(ctx != NULL); |
215 | | |
216 | 46 | isc_buffer_init(&hmac, digest, sizeof(digest)); |
217 | | |
218 | 46 | if (isc_hmac_final(ctx, &hmac) != ISC_R_SUCCESS) { |
219 | 0 | return DST_R_OPENSSLFAILURE; |
220 | 0 | } |
221 | | |
222 | 46 | if (sig->length > isc_buffer_usedlength(&hmac)) { |
223 | 0 | return DST_R_VERIFYFAILURE; |
224 | 0 | } |
225 | | |
226 | 46 | return isc_safe_memequal(digest, sig->base, sig->length) |
227 | 46 | ? ISC_R_SUCCESS |
228 | 46 | : DST_R_VERIFYFAILURE; |
229 | 46 | } |
230 | | |
231 | | static bool |
232 | 0 | hmac_compare(const dst_key_t *key1, const dst_key_t *key2) { |
233 | 0 | isc_hmac_key_t *hkey1, *hkey2; |
234 | |
|
235 | 0 | hkey1 = key1->keydata.hmac_key; |
236 | 0 | hkey2 = key2->keydata.hmac_key; |
237 | |
|
238 | 0 | if (hkey1 == NULL && hkey2 == NULL) { |
239 | 0 | return true; |
240 | 0 | } else if (hkey1 == NULL || hkey2 == NULL) { |
241 | 0 | return false; |
242 | 0 | } |
243 | | |
244 | 0 | return isc_hmac_key_equal(hkey1, hkey2); |
245 | 0 | } |
246 | | |
247 | | static isc_result_t |
248 | 0 | hmac_generate(isc_md_type_t type, dst_key_t *key) { |
249 | 0 | isc_buffer_t b; |
250 | 0 | isc_result_t result; |
251 | 0 | unsigned int bytes, len; |
252 | 0 | unsigned char data[ISC_MAX_BLOCK_SIZE] = { 0 }; |
253 | |
|
254 | 0 | len = isc_md_type_get_block_size(type); |
255 | |
|
256 | 0 | bytes = (key->key_size + 7) / 8; |
257 | |
|
258 | 0 | if (bytes > len) { |
259 | 0 | bytes = len; |
260 | 0 | key->key_size = len * 8; |
261 | 0 | } |
262 | |
|
263 | 0 | isc_nonce_buf(data, bytes); |
264 | |
|
265 | 0 | isc_buffer_init(&b, data, bytes); |
266 | 0 | isc_buffer_add(&b, bytes); |
267 | |
|
268 | 0 | result = hmac_fromdns(type, key, &b); |
269 | |
|
270 | 0 | isc_safe_memwipe(data, sizeof(data)); |
271 | |
|
272 | 0 | return result; |
273 | 0 | } |
274 | | |
275 | | static bool |
276 | 0 | hmac_isprivate(const dst_key_t *key) { |
277 | 0 | UNUSED(key); |
278 | 0 | return true; |
279 | 0 | } |
280 | | |
281 | | static void |
282 | 0 | hmac_destroy(dst_key_t *key) { |
283 | 0 | isc_hmac_key_destroy(&key->keydata.hmac_key); |
284 | 0 | } |
285 | | |
286 | | static isc_result_t |
287 | 2 | hmac_todns(const dst_key_t *key, isc_buffer_t *data) { |
288 | 2 | isc_region_t raw_key; |
289 | | |
290 | 2 | REQUIRE(key != NULL && key->keydata.hmac_key != NULL); |
291 | | |
292 | 2 | raw_key = isc_hmac_key_expose(key->keydata.hmac_key); |
293 | | |
294 | 2 | if (isc_buffer_availablelength(data) < raw_key.length) { |
295 | 0 | return ISC_R_NOSPACE; |
296 | 0 | } |
297 | | |
298 | 2 | return isc_buffer_copyregion(data, &raw_key); |
299 | 2 | } |
300 | | |
301 | | static isc_result_t |
302 | 2 | hmac_fromdns(isc_md_type_t type, dst_key_t *key, isc_buffer_t *data) { |
303 | 2 | isc_hmac_key_t *hkey = NULL; |
304 | 2 | isc_result_t result; |
305 | 2 | isc_region_t r; |
306 | | |
307 | 2 | isc_buffer_remainingregion(data, &r); |
308 | 2 | if (r.length == 0) { |
309 | 0 | return ISC_R_SUCCESS; |
310 | 0 | } |
311 | | |
312 | 2 | result = isc_hmac_key_create(type, r.base, r.length, key->mctx, &hkey); |
313 | 2 | if (result != ISC_R_SUCCESS) { |
314 | 0 | return DST_R_OPENSSLFAILURE; |
315 | 0 | } |
316 | | |
317 | 2 | key->key_size = isc_hmac_key_expose(hkey).length * 8; |
318 | 2 | key->keydata.hmac_key = hkey; |
319 | | |
320 | 2 | isc_buffer_forward(data, r.length); |
321 | | |
322 | 2 | return ISC_R_SUCCESS; |
323 | 2 | } |
324 | | |
325 | | static int |
326 | 0 | hmac__get_tag_key(isc_md_type_t type) { |
327 | 0 | switch (type) { |
328 | 0 | case ISC_MD_MD5: |
329 | 0 | return TAG_HMACMD5_KEY; |
330 | 0 | case ISC_MD_SHA1: |
331 | 0 | return TAG_HMACSHA1_KEY; |
332 | 0 | case ISC_MD_SHA224: |
333 | 0 | return TAG_HMACSHA224_KEY; |
334 | 0 | case ISC_MD_SHA256: |
335 | 0 | return TAG_HMACSHA256_KEY; |
336 | 0 | case ISC_MD_SHA384: |
337 | 0 | return TAG_HMACSHA384_KEY; |
338 | 0 | case ISC_MD_SHA512: |
339 | 0 | return TAG_HMACSHA512_KEY; |
340 | 0 | default: |
341 | 0 | UNREACHABLE(); |
342 | 0 | } |
343 | 0 | } |
344 | | |
345 | | static int |
346 | 0 | hmac__get_tag_bits(isc_md_type_t type) { |
347 | 0 | switch (type) { |
348 | 0 | case ISC_MD_MD5: |
349 | 0 | return TAG_HMACMD5_BITS; |
350 | 0 | case ISC_MD_SHA1: |
351 | 0 | return TAG_HMACSHA1_BITS; |
352 | 0 | case ISC_MD_SHA224: |
353 | 0 | return TAG_HMACSHA224_BITS; |
354 | 0 | case ISC_MD_SHA256: |
355 | 0 | return TAG_HMACSHA256_BITS; |
356 | 0 | case ISC_MD_SHA384: |
357 | 0 | return TAG_HMACSHA384_BITS; |
358 | 0 | case ISC_MD_SHA512: |
359 | 0 | return TAG_HMACSHA512_BITS; |
360 | 0 | default: |
361 | 0 | UNREACHABLE(); |
362 | 0 | } |
363 | 0 | } |
364 | | |
365 | | static isc_result_t |
366 | 0 | hmac_tofile(isc_md_type_t type, const dst_key_t *key, const char *directory) { |
367 | 0 | isc_region_t raw_key; |
368 | 0 | dst_private_t priv; |
369 | 0 | uint16_t bits; |
370 | |
|
371 | 0 | if (key->keydata.hmac_key == NULL) { |
372 | 0 | return DST_R_NULLKEY; |
373 | 0 | } |
374 | | |
375 | 0 | if (key->external) { |
376 | 0 | return DST_R_EXTERNALKEY; |
377 | 0 | } |
378 | | |
379 | 0 | raw_key = isc_hmac_key_expose(key->keydata.hmac_key); |
380 | |
|
381 | 0 | priv.elements[0].tag = hmac__get_tag_key(type); |
382 | 0 | priv.elements[0].length = raw_key.length; |
383 | 0 | priv.elements[0].data = raw_key.base; |
384 | |
|
385 | 0 | bits = htons(key->key_bits); |
386 | |
|
387 | 0 | priv.elements[1].tag = hmac__get_tag_bits(type); |
388 | 0 | priv.elements[1].length = sizeof(bits); |
389 | 0 | priv.elements[1].data = (uint8_t *)&bits; |
390 | |
|
391 | 0 | priv.nelements = 2; |
392 | |
|
393 | 0 | return dst__privstruct_writefile(key, &priv, directory); |
394 | 0 | } |
395 | | |
396 | | static int |
397 | 0 | hmac__to_dst_alg(isc_md_type_t type) { |
398 | 0 | if (type == ISC_MD_MD5) { |
399 | 0 | return DST_ALG_HMACMD5; |
400 | 0 | } else if (type == ISC_MD_SHA1) { |
401 | 0 | return DST_ALG_HMACSHA1; |
402 | 0 | } else if (type == ISC_MD_SHA224) { |
403 | 0 | return DST_ALG_HMACSHA224; |
404 | 0 | } else if (type == ISC_MD_SHA256) { |
405 | 0 | return DST_ALG_HMACSHA256; |
406 | 0 | } else if (type == ISC_MD_SHA384) { |
407 | 0 | return DST_ALG_HMACSHA384; |
408 | 0 | } else if (type == ISC_MD_SHA512) { |
409 | 0 | return DST_ALG_HMACSHA512; |
410 | 0 | } else { |
411 | 0 | UNREACHABLE(); |
412 | 0 | } |
413 | 0 | } |
414 | | |
415 | | static isc_result_t |
416 | | hmac_parse(isc_md_type_t type, dst_key_t *key, isc_lex_t *lexer, |
417 | 0 | dst_key_t *pub) { |
418 | 0 | dst_private_t priv; |
419 | 0 | isc_result_t result = ISC_R_SUCCESS, tresult; |
420 | 0 | isc_buffer_t b; |
421 | 0 | isc_mem_t *mctx = key->mctx; |
422 | 0 | unsigned int i; |
423 | |
|
424 | 0 | UNUSED(pub); |
425 | | /* read private key file */ |
426 | 0 | RETERR(dst__privstruct_parse(key, hmac__to_dst_alg(type), lexer, mctx, |
427 | 0 | &priv)); |
428 | |
|
429 | 0 | if (key->external) { |
430 | 0 | result = DST_R_EXTERNALKEY; |
431 | 0 | } |
432 | |
|
433 | 0 | key->key_bits = 0; |
434 | 0 | for (i = 0; i < priv.nelements && result == ISC_R_SUCCESS; i++) { |
435 | 0 | switch (priv.elements[i].tag) { |
436 | 0 | case TAG_HMACMD5_KEY: |
437 | 0 | case TAG_HMACSHA1_KEY: |
438 | 0 | case TAG_HMACSHA224_KEY: |
439 | 0 | case TAG_HMACSHA256_KEY: |
440 | 0 | case TAG_HMACSHA384_KEY: |
441 | 0 | case TAG_HMACSHA512_KEY: |
442 | 0 | isc_buffer_init(&b, priv.elements[i].data, |
443 | 0 | priv.elements[i].length); |
444 | 0 | isc_buffer_add(&b, priv.elements[i].length); |
445 | 0 | tresult = hmac_fromdns(type, key, &b); |
446 | 0 | if (tresult != ISC_R_SUCCESS) { |
447 | 0 | result = tresult; |
448 | 0 | } |
449 | 0 | break; |
450 | 0 | case TAG_HMACMD5_BITS: |
451 | 0 | case TAG_HMACSHA1_BITS: |
452 | 0 | case TAG_HMACSHA224_BITS: |
453 | 0 | case TAG_HMACSHA256_BITS: |
454 | 0 | case TAG_HMACSHA384_BITS: |
455 | 0 | case TAG_HMACSHA512_BITS: |
456 | 0 | tresult = getkeybits(key, &priv.elements[i]); |
457 | 0 | if (tresult != ISC_R_SUCCESS) { |
458 | 0 | result = tresult; |
459 | 0 | } |
460 | 0 | break; |
461 | 0 | default: |
462 | 0 | result = DST_R_INVALIDPRIVATEKEY; |
463 | 0 | break; |
464 | 0 | } |
465 | 0 | } |
466 | 0 | dst__privstruct_free(&priv, mctx); |
467 | 0 | isc_safe_memwipe(&priv, sizeof(priv)); |
468 | 0 | return result; |
469 | 0 | } |
470 | | |
471 | 22 | hmac_register_algorithm(md5); Line | Count | Source | 471 | | hmac_register_algorithm(md5); |
Unexecuted instantiation: hmac_link.c:hmacmd5_parse |
472 | 22 | hmac_register_algorithm(sha1); Line | Count | Source | 472 | | hmac_register_algorithm(sha1); |
Unexecuted instantiation: hmac_link.c:hmacsha1_parse |
473 | 22 | hmac_register_algorithm(sha224); Line | Count | Source | 473 | | hmac_register_algorithm(sha224); |
Unexecuted instantiation: hmac_link.c:hmacsha224_parse |
474 | 22 | hmac_register_algorithm(sha256); Line | Count | Source | 474 | | hmac_register_algorithm(sha256); |
Unexecuted instantiation: hmac_link.c:hmacsha256_parse |
475 | 22 | hmac_register_algorithm(sha384); Line | Count | Source | 475 | | hmac_register_algorithm(sha384); |
Unexecuted instantiation: hmac_link.c:hmacsha384_parse |
476 | | hmac_register_algorithm(sha512); Line | Count | Source | 476 | | hmac_register_algorithm(sha512); |
Unexecuted instantiation: hmac_link.c:hmacsha512_parse |
477 | | |
478 | | /*! \file */ |