Giovanni Bajo | 9940aba | 2012-04-23 00:32:01 +0200 | [diff] [blame] | 1 | #include <string.h> |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 2 | #include "dnsmasq.h" |
| 3 | #include "dnssec-crypto.h" |
Giovanni Bajo | 9940aba | 2012-04-23 00:32:01 +0200 | [diff] [blame] | 4 | #include <openssl/evp.h> |
| 5 | |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 6 | typedef struct VACTX_rsasha1 |
Giovanni Bajo | 9940aba | 2012-04-23 00:32:01 +0200 | [diff] [blame] | 7 | { |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 8 | VerifyAlgCtx base; |
| 9 | unsigned char *sig; |
| 10 | unsigned siglen; |
| 11 | union |
| 12 | { |
| 13 | EVP_MD_CTX hash; |
| 14 | unsigned char digest[20]; |
| 15 | }; |
| 16 | } VACTX_rsasha1; |
| 17 | |
| 18 | typedef struct VACTX_rsasha256 |
| 19 | { |
| 20 | VerifyAlgCtx base; |
| 21 | unsigned char *sig; |
| 22 | unsigned siglen; |
Giovanni Bajo | 9940aba | 2012-04-23 00:32:01 +0200 | [diff] [blame] | 23 | union |
| 24 | { |
| 25 | EVP_MD_CTX hash; |
Giovanni Bajo | b573aeb | 2012-04-24 02:21:50 +0200 | [diff] [blame] | 26 | unsigned char digest[32]; |
Giovanni Bajo | 9940aba | 2012-04-23 00:32:01 +0200 | [diff] [blame] | 27 | }; |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 28 | } VACTX_rsasha256; |
Giovanni Bajo | 9940aba | 2012-04-23 00:32:01 +0200 | [diff] [blame] | 29 | |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 30 | #define POOL_SIZE 1 |
| 31 | static union _Pool |
Giovanni Bajo | 9940aba | 2012-04-23 00:32:01 +0200 | [diff] [blame] | 32 | { |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 33 | VACTX_rsasha1 rsasha1; |
| 34 | VACTX_rsasha256 rsasha256; |
| 35 | } Pool[POOL_SIZE]; |
| 36 | static char pool_used = 0; |
| 37 | |
| 38 | static int rsasha1_set_signature(VerifyAlgCtx *ctx_, unsigned char *data, unsigned len) |
| 39 | { |
| 40 | VACTX_rsasha1 *ctx = (VACTX_rsasha1 *)ctx_; |
| 41 | ctx->sig = data; |
| 42 | ctx->siglen = len; |
Giovanni Bajo | b573aeb | 2012-04-24 02:21:50 +0200 | [diff] [blame] | 43 | return 1; |
| 44 | } |
| 45 | |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 46 | static int rsasha256_set_signature(VerifyAlgCtx *ctx_, unsigned char *data, unsigned len) |
Giovanni Bajo | b573aeb | 2012-04-24 02:21:50 +0200 | [diff] [blame] | 47 | { |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 48 | VACTX_rsasha256 *ctx = (VACTX_rsasha256 *)ctx_; |
| 49 | ctx->sig = data; |
| 50 | ctx->siglen = len; |
Giovanni Bajo | 9940aba | 2012-04-23 00:32:01 +0200 | [diff] [blame] | 51 | return 1; |
| 52 | } |
| 53 | |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 54 | static void rsasha1_begin_data(VerifyAlgCtx *ctx_) |
Giovanni Bajo | 9940aba | 2012-04-23 00:32:01 +0200 | [diff] [blame] | 55 | { |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 56 | VACTX_rsasha1 *ctx = (VACTX_rsasha1 *)ctx_; |
| 57 | EVP_MD_CTX_init(&ctx->hash); |
| 58 | EVP_DigestInit_ex(&ctx->hash, EVP_sha1(), NULL); |
Giovanni Bajo | b573aeb | 2012-04-24 02:21:50 +0200 | [diff] [blame] | 59 | } |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 60 | static void rsasha256_begin_data(VerifyAlgCtx *ctx_) |
Giovanni Bajo | b573aeb | 2012-04-24 02:21:50 +0200 | [diff] [blame] | 61 | { |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 62 | VACTX_rsasha256 *ctx = (VACTX_rsasha256 *)ctx_; |
| 63 | EVP_MD_CTX_init(&ctx->hash); |
| 64 | EVP_DigestInit_ex(&ctx->hash, EVP_sha256(), NULL); |
Giovanni Bajo | 9940aba | 2012-04-23 00:32:01 +0200 | [diff] [blame] | 65 | } |
| 66 | |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 67 | static void rsasha1_add_data(VerifyAlgCtx *ctx_, void *data, unsigned len) |
Giovanni Bajo | 9940aba | 2012-04-23 00:32:01 +0200 | [diff] [blame] | 68 | { |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 69 | VACTX_rsasha1 *ctx = (VACTX_rsasha1 *)ctx_; |
| 70 | EVP_DigestUpdate(&ctx->hash, data, len); |
Giovanni Bajo | b573aeb | 2012-04-24 02:21:50 +0200 | [diff] [blame] | 71 | } |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 72 | static void rsasha256_add_data(VerifyAlgCtx *ctx_, void *data, unsigned len) |
Giovanni Bajo | b573aeb | 2012-04-24 02:21:50 +0200 | [diff] [blame] | 73 | { |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 74 | VACTX_rsasha256 *ctx = (VACTX_rsasha256 *)ctx_; |
| 75 | EVP_DigestUpdate(&ctx->hash, data, len); |
Giovanni Bajo | 9940aba | 2012-04-23 00:32:01 +0200 | [diff] [blame] | 76 | } |
| 77 | |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 78 | static void rsasha1_end_data(VerifyAlgCtx *ctx_) |
Giovanni Bajo | 9940aba | 2012-04-23 00:32:01 +0200 | [diff] [blame] | 79 | { |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 80 | VACTX_rsasha1 *ctx = (VACTX_rsasha1 *)ctx_; |
Giovanni Bajo | 9940aba | 2012-04-23 00:32:01 +0200 | [diff] [blame] | 81 | unsigned char digest[20]; |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 82 | EVP_DigestFinal(&ctx->hash, digest, NULL); |
| 83 | memcpy(ctx->digest, digest, 20); |
Giovanni Bajo | b573aeb | 2012-04-24 02:21:50 +0200 | [diff] [blame] | 84 | } |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 85 | static void rsasha256_end_data(VerifyAlgCtx *ctx_) |
Giovanni Bajo | b573aeb | 2012-04-24 02:21:50 +0200 | [diff] [blame] | 86 | { |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 87 | VACTX_rsasha256 *ctx = (VACTX_rsasha256 *)ctx_; |
Giovanni Bajo | b573aeb | 2012-04-24 02:21:50 +0200 | [diff] [blame] | 88 | unsigned char digest[32]; |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 89 | EVP_DigestFinal(&ctx->hash, digest, NULL); |
| 90 | memcpy(ctx->digest, digest, 32); |
Giovanni Bajo | 9940aba | 2012-04-23 00:32:01 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 93 | static int rsasha1_verify(VerifyAlgCtx *ctx_, unsigned char *key, unsigned key_len) |
Giovanni Bajo | 9940aba | 2012-04-23 00:32:01 +0200 | [diff] [blame] | 94 | { |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 95 | VACTX_rsasha1 *ctx = (VACTX_rsasha1 *)ctx_; |
Giovanni Bajo | 9940aba | 2012-04-23 00:32:01 +0200 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 99 | static int rsasha256_verify(VerifyAlgCtx *ctx_, unsigned char *key, unsigned key_len) |
Giovanni Bajo | b573aeb | 2012-04-24 02:21:50 +0200 | [diff] [blame] | 100 | { |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 101 | VACTX_rsasha256 *ctx = (VACTX_rsasha256 *)ctx_; |
Giovanni Bajo | b573aeb | 2012-04-24 02:21:50 +0200 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 105 | #define DEFINE_VALG(alg) \ |
| 106 | int alg ## _set_signature(VerifyAlgCtx *ctx, unsigned char *data, unsigned len); \ |
| 107 | void alg ## _begin_data(VerifyAlgCtx *ctx); \ |
| 108 | void alg ## _add_data(VerifyAlgCtx *ctx, void *data, unsigned len); \ |
| 109 | void alg ## _end_data(VerifyAlgCtx *ctx); \ |
| 110 | int alg ## _verify(VerifyAlgCtx *ctx, unsigned char *key, unsigned key_len) \ |
| 111 | /**/ |
| 112 | |
| 113 | #define VALG_VTABLE(alg) { \ |
| 114 | alg ## _set_signature, \ |
| 115 | alg ## _begin_data, \ |
| 116 | alg ## _add_data, \ |
| 117 | alg ## _end_data, \ |
| 118 | alg ## _verify \ |
| 119 | } /**/ |
| 120 | |
| 121 | DEFINE_VALG(rsasha1); |
| 122 | DEFINE_VALG(rsasha256); |
| 123 | |
| 124 | /* Updated registry that merges various RFCs: |
| 125 | https://www.iana.org/assignments/dns-sec-alg-numbers/dns-sec-alg-numbers.xml */ |
| 126 | static const VerifyAlg valgs[] = |
| 127 | { |
| 128 | {0,0,0,0,0}, /* 0: reserved */ |
| 129 | {0,0,0,0,0}, /* 1: RSAMD5 */ |
| 130 | {0,0,0,0,0}, /* 2: DH */ |
| 131 | {0,0,0,0,0}, /* 3: DSA */ |
| 132 | {0,0,0,0,0}, /* 4: ECC */ |
| 133 | VALG_VTABLE(rsasha1), /* 5: RSASHA1 */ |
| 134 | {0,0,0,0,0}, /* 6: DSA-NSEC3-SHA1 */ |
| 135 | {0,0,0,0,0}, /* 7: RSASHA1-NSEC3-SHA1 */ |
| 136 | VALG_VTABLE(rsasha256), /* 8: RSASHA256 */ |
| 137 | {0,0,0,0,0}, /* 9: unassigned */ |
| 138 | {0,0,0,0,0}, /* 10: RSASHA512 */ |
| 139 | {0,0,0,0,0}, /* 11: unassigned */ |
| 140 | {0,0,0,0,0}, /* 12: ECC-GOST */ |
| 141 | {0,0,0,0,0}, /* 13: ECDSAP256SHA256 */ |
| 142 | {0,0,0,0,0}, /* 14: ECDSAP384SHA384 */ |
| 143 | }; |
| 144 | |
| 145 | static const int valgctx_size[] = |
| 146 | { |
| 147 | 0, /* 0: reserved */ |
| 148 | 0, /* 1: RSAMD5 */ |
| 149 | 0, /* 2: DH */ |
| 150 | 0, /* 3: DSA */ |
| 151 | 0, /* 4: ECC */ |
| 152 | sizeof(VACTX_rsasha1), /* 5: RSASHA1 */ |
| 153 | 0, /* 6: DSA-NSEC3-SHA1 */ |
| 154 | 0, /* 7: RSASHA1-NSEC3-SHA1 */ |
| 155 | sizeof(VACTX_rsasha256), /* 8: RSASHA256 */ |
| 156 | 0, /* 9: unassigned */ |
| 157 | 0, /* 10: RSASHA512 */ |
| 158 | 0, /* 11: unassigned */ |
| 159 | 0, /* 12: ECC-GOST */ |
| 160 | 0, /* 13: ECDSAP256SHA256 */ |
| 161 | 0, /* 14: ECDSAP384SHA384 */ |
| 162 | }; |
| 163 | |
| 164 | int verifyalg_supported(int algo) |
| 165 | { |
| 166 | return (algo < countof(valgctx_size) && valgctx_size[algo] != 0); |
| 167 | } |
| 168 | |
| 169 | VerifyAlgCtx* verifyalg_alloc(int algo) |
| 170 | { |
| 171 | int i; |
| 172 | VerifyAlgCtx *ret = 0; |
| 173 | |
| 174 | if (!verifyalg_supported(algo)) |
| 175 | return 0; |
| 176 | |
| 177 | if (pool_used == (1<<POOL_SIZE)-1) |
| 178 | ret = whine_malloc(valgctx_size[algo]); |
| 179 | else |
| 180 | for (i = 0; i < POOL_SIZE; ++i) |
| 181 | if (!(pool_used & (1 << i))) |
| 182 | { |
Giovanni Bajo | 776fd04 | 2012-04-26 14:37:10 +0200 | [diff] [blame^] | 183 | ret = (VerifyAlgCtx*)&Pool[i]; |
Giovanni Bajo | 366dfcb | 2012-04-25 17:40:47 +0200 | [diff] [blame] | 184 | pool_used |= 1 << i; |
| 185 | break; |
| 186 | } |
| 187 | |
| 188 | if (ret) |
| 189 | ret->vtbl = &valgs[algo]; |
| 190 | return ret; |
| 191 | } |
| 192 | |
| 193 | void verifyalg_free(VerifyAlgCtx *a) |
| 194 | { |
| 195 | int pool_idx = ((char*)a - (char*)&Pool[0]) / sizeof(Pool[0]); |
| 196 | if (pool_idx < 0 || pool_idx >= POOL_SIZE) |
| 197 | { |
| 198 | free(a); |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | pool_used &= ~(1 << pool_idx); |
| 203 | } |
Giovanni Bajo | 6759b99 | 2012-04-25 18:03:24 +0200 | [diff] [blame] | 204 | |
| 205 | int verifyalg_algonum(VerifyAlgCtx *a) |
| 206 | { |
| 207 | int num = a->vtbl - valgs; |
| 208 | if (num < 0 || num >= countof(valgs)) |
| 209 | return -1; |
| 210 | return num; |
| 211 | } |