1 /* Copyright (C) 2020 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2
3 This program is free software: you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation, either version 3 of the License, or
6 (at your option) any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <https://www.gnu.org/licenses/>.
15 */
16
17 #include <string.h>
18 #include <tap/basic.h>
19
20 #include "sample_keys.h"
21
22 #include "binary.h"
23 #include "crypto.h"
24 #include "error.h"
25 #include "key.h"
26 #include "sign.h"
27
28 static const dnssec_binary_t input_data = {
29 .size = 25,
30 .data = (uint8_t *)"Very good, young padawan."
31 };
32
33 static const dnssec_binary_t signed_rsa = { .size = 128, .data = (uint8_t []) {
34 0x21, 0xba, 0xff, 0x0c, 0x15, 0x10, 0x73, 0x25, 0xa7, 0x8e,
35 0xf4, 0x71, 0x4b, 0xd3, 0x97, 0x6d, 0x95, 0x52, 0xc2, 0x0b,
36 0x43, 0xb3, 0x7d, 0x82, 0xe4, 0x3e, 0x2a, 0xc3, 0xb7, 0x17,
37 0x5b, 0x05, 0xe9, 0x1e, 0x13, 0xac, 0x27, 0x6f, 0x20, 0x93,
38 0x1a, 0xeb, 0xe2, 0x2c, 0x72, 0x70, 0x14, 0xe6, 0x49, 0xa7,
39 0x62, 0xdd, 0x4c, 0x72, 0x1e, 0x1d, 0xd8, 0xf9, 0xba, 0xbc,
40 0x96, 0x0e, 0xc3, 0xd4, 0xc1, 0x8f, 0x95, 0xdb, 0x01, 0x18,
41 0x24, 0x43, 0xbd, 0x2b, 0x52, 0x9b, 0x10, 0x1f, 0xba, 0x0a,
42 0x76, 0xbe, 0x0e, 0xaa, 0x91, 0x27, 0x7b, 0x9f, 0xed, 0x5a,
43 0xad, 0x96, 0x1a, 0x02, 0x97, 0x42, 0x91, 0x30, 0x03, 0x2b,
44 0x5c, 0xb8, 0xcc, 0x6b, 0xcf, 0x39, 0x62, 0x5e, 0x47, 0xae,
45 0x6d, 0x5b, 0x43, 0xd2, 0xc2, 0xd8, 0x22, 0x5d, 0xf5, 0x5e,
46 0x0a, 0x97, 0x65, 0x41, 0xc7, 0xaa, 0x28, 0x67,
47 }};
48
49 static const dnssec_binary_t signed_ecdsa = { .size = 64, .data = (uint8_t []) {
50 0xa2, 0x95, 0x76, 0xb5, 0xf5, 0x7e, 0xbd, 0xdd, 0xf5, 0x62,
51 0xa2, 0xc3, 0xa4, 0x8d, 0xd4, 0x53, 0x5c, 0xba, 0x29, 0x71,
52 0x8c, 0xcc, 0x28, 0x7b, 0x58, 0xf3, 0x1e, 0x4e, 0x58, 0xe2,
53 0x36, 0x7e,
54 0xa0, 0x1a, 0xb6, 0xe6, 0x29, 0x71, 0x1b, 0xd3, 0x8c, 0x88,
55 0xc3, 0xee, 0x12, 0x0e, 0x69, 0x70, 0x55, 0x99, 0xec, 0xd5,
56 0xf6, 0x4f, 0x4b, 0xe2, 0x41, 0xd9, 0x10, 0x7e, 0x67, 0xe5,
57 0xad, 0x2f,
58 }};
59
60 #ifdef HAVE_ED25519
61 static const dnssec_binary_t signed_ed25519 = { .size = 64, .data = (uint8_t []) {
62 0x0a, 0x9e, 0x51, 0x5f, 0x16, 0x89, 0x49, 0x27,
63 0x0e, 0x98, 0x34, 0xd3, 0x48, 0xef, 0x5a, 0x6e,
64 0x85, 0x2f, 0x7c, 0xd6, 0xd7, 0xc8, 0xd0, 0xf4,
65 0x2c, 0x68, 0x8c, 0x1f, 0xf7, 0xdf, 0xeb, 0x7c,
66 0x25, 0xd6, 0x1a, 0x76, 0x3e, 0xaf, 0x28, 0x1f,
67 0x1d, 0x08, 0x10, 0x20, 0x1c, 0x01, 0x77, 0x1b,
68 0x5a, 0x48, 0xd6, 0xe5, 0x1c, 0xf9, 0xe3, 0xe0,
69 0x70, 0x34, 0x5e, 0x02, 0x49, 0xfb, 0x9e, 0x05,
70 }};
71 #endif
72
binary_set_string(char * str)73 static dnssec_binary_t binary_set_string(char *str)
74 {
75 dnssec_binary_t result = { .data = (uint8_t *)str, .size = strlen(str) };
76 return result;
77 }
78
check_key(const key_parameters_t * key_data,const dnssec_binary_t * data,const dnssec_binary_t * signature,bool signature_match)79 static void check_key(const key_parameters_t *key_data, const dnssec_binary_t *data,
80 const dnssec_binary_t *signature, bool signature_match)
81 {
82 int r;
83
84 // initialize key from public parameters
85
86 dnssec_key_t *key = NULL;
87 r = dnssec_key_new(&key);
88 ok(r == DNSSEC_EOK && key != NULL, "create key");
89 r = dnssec_key_set_rdata(key, &key_data->rdata);
90 ok(r == DNSSEC_EOK, "set RDATA");
91
92 // check validation on static signature
93
94 dnssec_sign_ctx_t *ctx = NULL;
95 r = dnssec_sign_new(&ctx, key);
96 ok(r == DNSSEC_EOK, "create signing context");
97 r = dnssec_sign_add(ctx, data);
98 ok(r == DNSSEC_EOK, "add data to be signed");
99 r = dnssec_sign_verify(ctx, false, signature);
100 ok(r == DNSSEC_EOK, "signature verified");
101
102 // create new signature and self-validate
103
104 r = dnssec_key_load_pkcs8(key, &key_data->pem);
105 ok(r == DNSSEC_EOK, "load private key");
106
107 if (signature_match) {
108 r = dnssec_sign_init(ctx);
109 ok(r == DNSSEC_EOK, "reinitialize context");
110 r = dnssec_sign_add(ctx, data);
111 ok(r == DNSSEC_EOK, "add data to be signed");
112 dnssec_binary_t new_signature = { 0 };
113 r = dnssec_sign_write(ctx, DNSSEC_SIGN_NORMAL, &new_signature);
114 ok(r == DNSSEC_EOK, "write the signature");
115 ok(dnssec_binary_cmp(signature, &new_signature) == 0,
116 "signature exact match");
117 r = dnssec_sign_verify(ctx, false, &new_signature);
118 ok(r == DNSSEC_EOK, "reverify the new signature");
119 dnssec_binary_free(&new_signature);
120 }
121
122 // context reinitialization
123
124 dnssec_binary_t tmp = { 0 };
125
126 r = dnssec_sign_init(ctx);
127 ok(r == DNSSEC_EOK, "reinitialize context");
128
129 tmp = binary_set_string("bind");
130 r = dnssec_sign_add(ctx, &tmp);
131 ok(r == DNSSEC_EOK, "add data (1)");
132
133 r = dnssec_sign_init(ctx);
134 ok(r == DNSSEC_EOK, "reinitialize context");
135
136 tmp = binary_set_string("knot");
137 r = dnssec_sign_add(ctx, &tmp);
138 ok(r == DNSSEC_EOK, "add data (2)");
139
140 tmp = binary_set_string(" is the best");
141 r = dnssec_sign_add(ctx, &tmp);
142 ok(r == DNSSEC_EOK, "add data (3)");
143
144 dnssec_binary_t new_signature = { 0 };
145 r = dnssec_sign_write(ctx, DNSSEC_SIGN_NORMAL, &new_signature);
146 ok(r == DNSSEC_EOK, "write signature");
147
148 r = dnssec_sign_init(ctx);
149 ok(r == DNSSEC_EOK, "reinitialize context");
150
151 tmp = binary_set_string("knot is the best");
152 r = dnssec_sign_add(ctx, &tmp);
153 ok(r == DNSSEC_EOK, "add data (4)");
154
155 r = dnssec_sign_verify(ctx, false, &new_signature);
156 ok(r == DNSSEC_EOK, "verify signature");
157
158 dnssec_binary_free(&new_signature);
159
160 // cleanup
161
162 dnssec_sign_free(ctx);
163 dnssec_key_free(key);
164 }
165
main(void)166 int main(void)
167 {
168 plan_lazy();
169
170 dnssec_crypto_init();
171
172 diag("RSA signing");
173 check_key(&SAMPLE_RSA_KEY, &input_data, &signed_rsa, true);
174 diag("ECDSA signing");
175 check_key(&SAMPLE_ECDSA_KEY, &input_data, &signed_ecdsa, false);
176 #ifdef HAVE_ED25519
177 diag("ED25519 signing");
178 check_key(&SAMPLE_ED25519_KEY, &input_data, &signed_ed25519, true);
179 #endif
180
181 dnssec_crypto_cleanup();
182
183 return 0;
184 }
185