/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 | 36 | dst_context_t *dctx) { \ |
58 | 36 | return (hmac_createctx(key, dctx)); \ |
59 | 36 | } \ 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 | 36 | dst_context_t *dctx) { \ | 58 | 36 | return (hmac_createctx(key, dctx)); \ | 59 | 36 | } \ |
Unexecuted instantiation: hmac_link.c:hmacsha384_createctx Unexecuted instantiation: hmac_link.c:hmacsha512_createctx |
60 | 36 | static void hmac##alg##_destroyctx(dst_context_t *dctx) { \ |
61 | 36 | hmac_destroyctx(dctx); \ |
62 | 36 | } \ 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 | 36 | static void hmac##alg##_destroyctx(dst_context_t *dctx) { \ | 61 | 36 | hmac_destroyctx(dctx); \ | 62 | 36 | } \ |
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 | 251 | const isc_region_t *data) { \ |
65 | 251 | return (hmac_adddata(dctx, data)); \ |
66 | 251 | } \ 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 | 251 | const isc_region_t *data) { \ | 65 | 251 | return (hmac_adddata(dctx, data)); \ | 66 | 251 | } \ |
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 | 36 | const isc_region_t *sig) { \ |
73 | 36 | return (hmac_verify(dctx, sig)); \ |
74 | 36 | } \ 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 | 36 | const isc_region_t *sig) { \ | 73 | 36 | return (hmac_verify(dctx, sig)); \ | 74 | 36 | } \ |
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", \ |
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 | 36 | hmac_createctx(const dst_key_t *key, dst_context_t *dctx) { |
162 | 36 | isc_result_t result; |
163 | 36 | isc_hmac_t *ctx = isc_hmac_new(); /* Either returns or abort()s */ |
164 | | |
165 | 36 | result = isc_hmac_init(ctx, key->keydata.hmac_key); |
166 | 36 | if (result != ISC_R_SUCCESS) { |
167 | 0 | isc_hmac_free(ctx); |
168 | 0 | return DST_R_UNSUPPORTEDALG; |
169 | 0 | } |
170 | | |
171 | 36 | dctx->ctxdata.hmac_ctx = ctx; |
172 | 36 | return ISC_R_SUCCESS; |
173 | 36 | } |
174 | | |
175 | | static void |
176 | 36 | hmac_destroyctx(dst_context_t *dctx) { |
177 | 36 | isc_hmac_t *ctx = dctx->ctxdata.hmac_ctx; |
178 | 36 | REQUIRE(ctx != NULL); |
179 | | |
180 | 36 | isc_hmac_free(ctx); |
181 | 36 | dctx->ctxdata.hmac_ctx = NULL; |
182 | 36 | } |
183 | | |
184 | | static isc_result_t |
185 | 251 | hmac_adddata(const dst_context_t *dctx, const isc_region_t *data) { |
186 | 251 | isc_result_t result; |
187 | 251 | isc_hmac_t *ctx = dctx->ctxdata.hmac_ctx; |
188 | | |
189 | 251 | REQUIRE(ctx != NULL); |
190 | | |
191 | 251 | result = isc_hmac_update(ctx, data->base, data->length); |
192 | 251 | if (result != ISC_R_SUCCESS) { |
193 | 0 | return DST_R_OPENSSLFAILURE; |
194 | 0 | } |
195 | | |
196 | 251 | return ISC_R_SUCCESS; |
197 | 251 | } |
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 | 36 | hmac_verify(const dst_context_t *dctx, const isc_region_t *sig) { |
210 | 36 | isc_hmac_t *ctx = dctx->ctxdata.hmac_ctx; |
211 | 36 | unsigned char digest[ISC_MAX_MD_SIZE]; |
212 | 36 | isc_buffer_t hmac; |
213 | 36 | unsigned int sig_expected_length = ISC_MAX_MD_SIZE; |
214 | 36 | uint16_t digestbits = dst_key_getbits(dctx->key); |
215 | | |
216 | 36 | REQUIRE(ctx != NULL); |
217 | | |
218 | 36 | isc_buffer_init(&hmac, digest, sizeof(digest)); |
219 | | |
220 | 36 | if (isc_hmac_final(ctx, &hmac) != ISC_R_SUCCESS) { |
221 | 0 | return DST_R_OPENSSLFAILURE; |
222 | 0 | } |
223 | | |
224 | 36 | if (digestbits == 0) { |
225 | | /* |
226 | | * If digestbits is zero then HMAC truncation is not used, just |
227 | | * use the key's actual length. |
228 | | */ |
229 | 36 | isc_result_t result = dst_key_sigsize(dctx->key, |
230 | 36 | &sig_expected_length); |
231 | 36 | if (result != ISC_R_SUCCESS) { |
232 | 0 | return DST_R_VERIFYFAILURE; |
233 | 0 | } |
234 | 36 | } else { |
235 | 0 | sig_expected_length = (digestbits + 7) / 8; |
236 | 0 | } |
237 | | |
238 | 36 | if (sig->length != sig_expected_length || |
239 | 36 | sig->length > isc_buffer_usedlength(&hmac)) |
240 | 0 | { |
241 | 0 | return DST_R_VERIFYFAILURE; |
242 | 0 | } |
243 | | |
244 | 36 | return isc_safe_memequal(digest, sig->base, sig->length) |
245 | 36 | ? ISC_R_SUCCESS |
246 | 36 | : DST_R_VERIFYFAILURE; |
247 | 36 | } |
248 | | |
249 | | static bool |
250 | 0 | hmac_compare(const dst_key_t *key1, const dst_key_t *key2) { |
251 | 0 | isc_hmac_key_t *hkey1, *hkey2; |
252 | |
|
253 | 0 | hkey1 = key1->keydata.hmac_key; |
254 | 0 | hkey2 = key2->keydata.hmac_key; |
255 | |
|
256 | 0 | if (hkey1 == NULL && hkey2 == NULL) { |
257 | 0 | return true; |
258 | 0 | } else if (hkey1 == NULL || hkey2 == NULL) { |
259 | 0 | return false; |
260 | 0 | } |
261 | | |
262 | 0 | return isc_hmac_key_equal(hkey1, hkey2); |
263 | 0 | } |
264 | | |
265 | | static isc_result_t |
266 | 0 | hmac_generate(isc_md_type_t type, dst_key_t *key) { |
267 | 0 | isc_buffer_t b; |
268 | 0 | isc_result_t result; |
269 | 0 | unsigned int bytes, len; |
270 | 0 | unsigned char data[ISC_MAX_BLOCK_SIZE] = { 0 }; |
271 | |
|
272 | 0 | len = isc_md_type_get_block_size(type); |
273 | |
|
274 | 0 | bytes = (key->key_size + 7) / 8; |
275 | |
|
276 | 0 | if (bytes > len) { |
277 | 0 | bytes = len; |
278 | 0 | key->key_size = len * 8; |
279 | 0 | } |
280 | |
|
281 | 0 | isc_nonce_buf(data, bytes); |
282 | |
|
283 | 0 | isc_buffer_init(&b, data, bytes); |
284 | 0 | isc_buffer_add(&b, bytes); |
285 | |
|
286 | 0 | result = hmac_fromdns(type, key, &b); |
287 | |
|
288 | 0 | isc_safe_memwipe(data, sizeof(data)); |
289 | |
|
290 | 0 | return result; |
291 | 0 | } |
292 | | |
293 | | static bool |
294 | 0 | hmac_isprivate(const dst_key_t *key) { |
295 | 0 | UNUSED(key); |
296 | 0 | return true; |
297 | 0 | } |
298 | | |
299 | | static void |
300 | 0 | hmac_destroy(dst_key_t *key) { |
301 | 0 | isc_hmac_key_destroy(&key->keydata.hmac_key); |
302 | 0 | } |
303 | | |
304 | | static isc_result_t |
305 | 2 | hmac_todns(const dst_key_t *key, isc_buffer_t *data) { |
306 | 2 | isc_region_t raw_key; |
307 | | |
308 | 2 | REQUIRE(key != NULL && key->keydata.hmac_key != NULL); |
309 | | |
310 | 2 | raw_key = isc_hmac_key_expose(key->keydata.hmac_key); |
311 | | |
312 | 2 | if (isc_buffer_availablelength(data) < raw_key.length) { |
313 | 0 | return ISC_R_NOSPACE; |
314 | 0 | } |
315 | | |
316 | 2 | return isc_buffer_copyregion(data, &raw_key); |
317 | 2 | } |
318 | | |
319 | | static isc_result_t |
320 | 2 | hmac_fromdns(isc_md_type_t type, dst_key_t *key, isc_buffer_t *data) { |
321 | 2 | isc_hmac_key_t *hkey = NULL; |
322 | 2 | isc_result_t result; |
323 | 2 | isc_region_t r; |
324 | | |
325 | 2 | isc_buffer_remainingregion(data, &r); |
326 | 2 | if (r.length == 0) { |
327 | 0 | return ISC_R_SUCCESS; |
328 | 0 | } |
329 | | |
330 | 2 | result = isc_hmac_key_create(type, r.base, r.length, key->mctx, &hkey); |
331 | 2 | if (result != ISC_R_SUCCESS) { |
332 | 0 | return DST_R_OPENSSLFAILURE; |
333 | 0 | } |
334 | | |
335 | 2 | key->key_size = isc_hmac_key_expose(hkey).length * 8; |
336 | 2 | key->keydata.hmac_key = hkey; |
337 | | |
338 | 2 | isc_buffer_forward(data, r.length); |
339 | | |
340 | 2 | return ISC_R_SUCCESS; |
341 | 2 | } |
342 | | |
343 | | static int |
344 | 0 | hmac__get_tag_key(isc_md_type_t type) { |
345 | 0 | switch (type) { |
346 | 0 | case ISC_MD_MD5: |
347 | 0 | return TAG_HMACMD5_KEY; |
348 | 0 | case ISC_MD_SHA1: |
349 | 0 | return TAG_HMACSHA1_KEY; |
350 | 0 | case ISC_MD_SHA224: |
351 | 0 | return TAG_HMACSHA224_KEY; |
352 | 0 | case ISC_MD_SHA256: |
353 | 0 | return TAG_HMACSHA256_KEY; |
354 | 0 | case ISC_MD_SHA384: |
355 | 0 | return TAG_HMACSHA384_KEY; |
356 | 0 | case ISC_MD_SHA512: |
357 | 0 | return TAG_HMACSHA512_KEY; |
358 | 0 | default: |
359 | 0 | UNREACHABLE(); |
360 | 0 | } |
361 | 0 | } |
362 | | |
363 | | static int |
364 | 0 | hmac__get_tag_bits(isc_md_type_t type) { |
365 | 0 | switch (type) { |
366 | 0 | case ISC_MD_MD5: |
367 | 0 | return TAG_HMACMD5_BITS; |
368 | 0 | case ISC_MD_SHA1: |
369 | 0 | return TAG_HMACSHA1_BITS; |
370 | 0 | case ISC_MD_SHA224: |
371 | 0 | return TAG_HMACSHA224_BITS; |
372 | 0 | case ISC_MD_SHA256: |
373 | 0 | return TAG_HMACSHA256_BITS; |
374 | 0 | case ISC_MD_SHA384: |
375 | 0 | return TAG_HMACSHA384_BITS; |
376 | 0 | case ISC_MD_SHA512: |
377 | 0 | return TAG_HMACSHA512_BITS; |
378 | 0 | default: |
379 | 0 | UNREACHABLE(); |
380 | 0 | } |
381 | 0 | } |
382 | | |
383 | | static isc_result_t |
384 | 0 | hmac_tofile(isc_md_type_t type, const dst_key_t *key, const char *directory) { |
385 | 0 | isc_region_t raw_key; |
386 | 0 | dst_private_t priv; |
387 | 0 | uint16_t bits; |
388 | |
|
389 | 0 | if (key->keydata.hmac_key == NULL) { |
390 | 0 | return DST_R_NULLKEY; |
391 | 0 | } |
392 | | |
393 | 0 | if (key->external) { |
394 | 0 | return DST_R_EXTERNALKEY; |
395 | 0 | } |
396 | | |
397 | 0 | raw_key = isc_hmac_key_expose(key->keydata.hmac_key); |
398 | |
|
399 | 0 | priv.elements[0].tag = hmac__get_tag_key(type); |
400 | 0 | priv.elements[0].length = raw_key.length; |
401 | 0 | priv.elements[0].data = raw_key.base; |
402 | |
|
403 | 0 | bits = htons(key->key_bits); |
404 | |
|
405 | 0 | priv.elements[1].tag = hmac__get_tag_bits(type); |
406 | 0 | priv.elements[1].length = sizeof(bits); |
407 | 0 | priv.elements[1].data = (uint8_t *)&bits; |
408 | |
|
409 | 0 | priv.nelements = 2; |
410 | |
|
411 | 0 | return dst__privstruct_writefile(key, &priv, directory); |
412 | 0 | } |
413 | | |
414 | | static int |
415 | 0 | hmac__to_dst_alg(isc_md_type_t type) { |
416 | 0 | if (type == ISC_MD_MD5) { |
417 | 0 | return DST_ALG_HMACMD5; |
418 | 0 | } else if (type == ISC_MD_SHA1) { |
419 | 0 | return DST_ALG_HMACSHA1; |
420 | 0 | } else if (type == ISC_MD_SHA224) { |
421 | 0 | return DST_ALG_HMACSHA224; |
422 | 0 | } else if (type == ISC_MD_SHA256) { |
423 | 0 | return DST_ALG_HMACSHA256; |
424 | 0 | } else if (type == ISC_MD_SHA384) { |
425 | 0 | return DST_ALG_HMACSHA384; |
426 | 0 | } else if (type == ISC_MD_SHA512) { |
427 | 0 | return DST_ALG_HMACSHA512; |
428 | 0 | } else { |
429 | 0 | UNREACHABLE(); |
430 | 0 | } |
431 | 0 | } |
432 | | |
433 | | static isc_result_t |
434 | | hmac_parse(isc_md_type_t type, dst_key_t *key, isc_lex_t *lexer, |
435 | 0 | dst_key_t *pub) { |
436 | 0 | dst_private_t priv; |
437 | 0 | isc_result_t result = ISC_R_SUCCESS, tresult; |
438 | 0 | isc_buffer_t b; |
439 | 0 | isc_mem_t *mctx = key->mctx; |
440 | 0 | unsigned int i; |
441 | |
|
442 | 0 | UNUSED(pub); |
443 | | /* read private key file */ |
444 | 0 | RETERR(dst__privstruct_parse(key, hmac__to_dst_alg(type), lexer, mctx, |
445 | 0 | &priv)); |
446 | |
|
447 | 0 | if (key->external) { |
448 | 0 | result = DST_R_EXTERNALKEY; |
449 | 0 | } |
450 | |
|
451 | 0 | key->key_bits = 0; |
452 | 0 | for (i = 0; i < priv.nelements && result == ISC_R_SUCCESS; i++) { |
453 | 0 | switch (priv.elements[i].tag) { |
454 | 0 | case TAG_HMACMD5_KEY: |
455 | 0 | case TAG_HMACSHA1_KEY: |
456 | 0 | case TAG_HMACSHA224_KEY: |
457 | 0 | case TAG_HMACSHA256_KEY: |
458 | 0 | case TAG_HMACSHA384_KEY: |
459 | 0 | case TAG_HMACSHA512_KEY: |
460 | 0 | isc_buffer_init(&b, priv.elements[i].data, |
461 | 0 | priv.elements[i].length); |
462 | 0 | isc_buffer_add(&b, priv.elements[i].length); |
463 | 0 | tresult = hmac_fromdns(type, key, &b); |
464 | 0 | if (tresult != ISC_R_SUCCESS) { |
465 | 0 | result = tresult; |
466 | 0 | } |
467 | 0 | break; |
468 | 0 | case TAG_HMACMD5_BITS: |
469 | 0 | case TAG_HMACSHA1_BITS: |
470 | 0 | case TAG_HMACSHA224_BITS: |
471 | 0 | case TAG_HMACSHA256_BITS: |
472 | 0 | case TAG_HMACSHA384_BITS: |
473 | 0 | case TAG_HMACSHA512_BITS: |
474 | 0 | tresult = getkeybits(key, &priv.elements[i]); |
475 | 0 | if (tresult != ISC_R_SUCCESS) { |
476 | 0 | result = tresult; |
477 | 0 | } |
478 | 0 | break; |
479 | 0 | default: |
480 | 0 | result = DST_R_INVALIDPRIVATEKEY; |
481 | 0 | break; |
482 | 0 | } |
483 | 0 | } |
484 | 0 | dst__privstruct_free(&priv, mctx); |
485 | 0 | isc_safe_memwipe(&priv, sizeof(priv)); |
486 | 0 | return result; |
487 | 0 | } |
488 | | |
489 | 22 | hmac_register_algorithm(md5); Line | Count | Source | 489 | | hmac_register_algorithm(md5); |
Unexecuted instantiation: hmac_link.c:hmacmd5_parse |
490 | 22 | hmac_register_algorithm(sha1); Line | Count | Source | 490 | | hmac_register_algorithm(sha1); |
Unexecuted instantiation: hmac_link.c:hmacsha1_parse |
491 | 22 | hmac_register_algorithm(sha224); Line | Count | Source | 491 | | hmac_register_algorithm(sha224); |
Unexecuted instantiation: hmac_link.c:hmacsha224_parse |
492 | 22 | hmac_register_algorithm(sha256); Line | Count | Source | 492 | | hmac_register_algorithm(sha256); |
Unexecuted instantiation: hmac_link.c:hmacsha256_parse |
493 | 22 | hmac_register_algorithm(sha384); Line | Count | Source | 493 | | hmac_register_algorithm(sha384); |
Unexecuted instantiation: hmac_link.c:hmacsha384_parse |
494 | | hmac_register_algorithm(sha512); Line | Count | Source | 494 | | hmac_register_algorithm(sha512); |
Unexecuted instantiation: hmac_link.c:hmacsha512_parse |
495 | | |
496 | | /*! \file */ |