1 1.3 rillig /* $NetBSD: sign.h,v 1.3 2021/11/27 22:30:26 rillig Exp $ */ 2 1.1 christos 3 1.1 christos /*- 4 1.1 christos * Copyright (c) 2008 The NetBSD Foundation, Inc. 5 1.1 christos * All rights reserved. 6 1.1 christos * 7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation 8 1.1 christos * by Martin Schtte. 9 1.1 christos * 10 1.1 christos * Redistribution and use in source and binary forms, with or without 11 1.1 christos * modification, are permitted provided that the following conditions 12 1.1 christos * are met: 13 1.1 christos * 1. Redistributions of source code must retain the above copyright 14 1.1 christos * notice, this list of conditions and the following disclaimer. 15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 christos * notice, this list of conditions and the following disclaimer in the 17 1.1 christos * documentation and/or other materials provided with the distribution. 18 1.1 christos * 3. All advertising materials mentioning features or use of this software 19 1.1 christos * must display the following acknowledgement: 20 1.1 christos * This product includes software developed by the NetBSD 21 1.1 christos * Foundation, Inc. and its contributors. 22 1.1 christos * 4. Neither the name of The NetBSD Foundation nor the names of its 23 1.1 christos * contributors may be used to endorse or promote products derived 24 1.1 christos * from this software without specific prior written permission. 25 1.1 christos * 26 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 1.1 christos * POSSIBILITY OF SUCH DAMAGE. 37 1.1 christos */ 38 1.1 christos /* 39 1.1 christos * sign.h 40 1.2 minskim * 41 1.1 christos */ 42 1.1 christos #ifndef SIGN_H_ 43 1.1 christos #define SIGN_H_ 44 1.1 christos 45 1.1 christos #include <netinet/in.h> 46 1.1 christos #include <resolv.h> 47 1.1 christos #include <openssl/x509v3.h> 48 1.1 christos #include <openssl/err.h> 49 1.1 christos #include <openssl/rand.h> 50 1.1 christos #include <openssl/pem.h> 51 1.1 christos 52 1.1 christos /* default Signature Group value, 53 1.1 christos * defines signature strategy: 54 1.1 christos * 0 one global SG 55 1.1 christos * 1 one SG per PRI 56 1.1 christos * 2 SGs for PRI ranges 57 1.1 christos * 3 other (SGs not defined by PRI) 58 1.2 minskim * 59 1.1 christos * We use '3' and assign one SG to every destination (=struct filed) 60 1.1 christos */ 61 1.1 christos #define SIGN_SG 3 62 1.1 christos 63 1.1 christos /* maximum value for several counters in -sign */ 64 1.1 christos #define SIGN_MAX_COUNT 9999999999 65 1.1 christos 66 1.1 christos /* 67 1.1 christos * many of these options could be made user configurable if desired, 68 1.1 christos * but I do not see the need for that 69 1.1 christos */ 70 1.1 christos 71 1.1 christos /* redundancy options */ 72 1.2 minskim /* 73 1.1 christos * note on the implementation of redundancy: 74 1.1 christos * - certificate blocks: sending the first CB just before first SB. 75 1.1 christos * after that domark() triggers resends until resend count is reached. 76 1.1 christos * - signature blocks: to send every hash n times I use a sliding window. 77 1.1 christos * the hashes in every SB are grouped into n divisions: 78 1.1 christos * * the 1st hashcount/n hashes are sent for the 1st time 79 1.1 christos * * the 2nd hashcount/n hashes are sent for the 2nd time 80 1.1 christos * * ... 81 1.1 christos * * the n-th hashcount/n hashes are sent for the n-th time 82 1.1 christos * (and deleted thereafter) 83 1.2 minskim */ 84 1.1 christos #define SIGN_RESENDCOUNT_CERTBLOCK 2 85 1.1 christos #define SIGN_RESENDCOUNT_HASHES 3 86 1.1 christos 87 1.1 christos /* maximum length of syslog-sign messages should be <= 2048 by standard 88 1.1 christos * and should be >= 1024 to be long enough. 89 1.1 christos * be careful with small values because there is no check for a lower bound 90 1.1 christos * thus the following derived values would become negative. 91 1.1 christos */ 92 1.1 christos #define SIGN_MAX_LENGTH 2048 93 1.1 christos /* the length we can use for the SD and keep the 94 1.1 christos * message length with header below 2048 octets */ 95 1.1 christos #define SIGN_MAX_SD_LENGTH (SIGN_MAX_LENGTH - 1 - HEADER_LEN_MAX) 96 1.1 christos /* length of signature, currently only for DSA */ 97 1.1 christos #define SIGN_B64SIGLEN_DSS 64+1 98 1.1 christos /* the maximum length of one payload fragment: 99 1.1 christos * max.SD len - text - max. field lengths - sig len */ 100 1.1 christos #define SIGN_MAX_FRAG_LENGTH (SIGN_MAX_SD_LENGTH - 82 - 38 - SIGN_B64SIGLEN_DSS) 101 1.1 christos /* the maximum length of one signature block: 102 1.1 christos * max.SD len - text - max. field lens - sig len */ 103 1.1 christos #define SIGN_MAX_SB_LENGTH (SIGN_MAX_SD_LENGTH - 72 - 40 - SIGN_B64SIGLEN_DSS) 104 1.1 christos /* the maximum number of hashes pec signature block */ 105 1.1 christos #define SIGN_MAX_HASH_NUM (SIGN_MAX_SB_LENGTH / (GlobalSign.md_len_b64+1)) 106 1.1 christos /* number of hashes in one signature block */ 107 1.1 christos #define SIGN_HASH_NUM_WANT 100 108 1.1 christos /* make sure to consider SIGN_MAX_HASH_NUM and 109 1.1 christos * to have a SIGN_HASH_NUM that is a multiple of SIGN_HASH_DIVISION_NUM */ 110 1.1 christos #define SIGN_HASH_DIVISION_NUM (MIN(SIGN_HASH_NUM_WANT, SIGN_MAX_HASH_NUM) \ 111 1.1 christos / SIGN_RESENDCOUNT_HASHES) 112 1.2 minskim #define SIGN_HASH_NUM (SIGN_HASH_DIVISION_NUM * SIGN_RESENDCOUNT_HASHES) 113 1.1 christos 114 1.1 christos /* the length of payload strings 115 1.1 christos * since the payload is fragmented there is no technical limit 116 1.1 christos * it just has to be big enough to hold big b64 encoded PKIX certificates 117 1.1 christos */ 118 1.1 christos #define SIGN_MAX_PAYLOAD_LENGTH 20480 119 1.1 christos 120 1.1 christos /* length of generated DSA keys for signing */ 121 1.1 christos #define SIGN_GENCERT_BITS 1024 122 1.1 christos 123 1.1 christos #define SSL_CHECK_ONE(exp) do { \ 124 1.1 christos if ((exp) != 1) { \ 125 1.1 christos DPRINTF(D_SIGN, #exp " failed in %d: %s\n", __LINE__, \ 126 1.1 christos ERR_error_string(ERR_get_error(), NULL)); \ 127 1.1 christos return 1; \ 128 1.1 christos } \ 129 1.3 rillig } while (0) 130 1.1 christos 131 1.1 christos /* structs use uint_fast64_t in different places because the standard 132 1.1 christos * requires values in interval [0:9999999999 = SIGN_MAX_COUNT] */ 133 1.2 minskim 134 1.1 christos /* queue of C-Strings (here used for hashes) */ 135 1.1 christos struct string_queue { 136 1.1 christos uint_fast64_t key; 137 1.1 christos char *data; 138 1.1 christos STAILQ_ENTRY(string_queue) entries; 139 1.1 christos }; 140 1.1 christos STAILQ_HEAD(string_queue_head, string_queue); 141 1.1 christos 142 1.1 christos /* queue of destinations (used associate SGs and fileds) */ 143 1.1 christos struct filed_queue { 144 1.1 christos struct filed *f; 145 1.1 christos STAILQ_ENTRY(filed_queue) entries; 146 1.1 christos }; 147 1.1 christos STAILQ_HEAD(filed_queue_head, filed_queue); 148 1.1 christos 149 1.1 christos /* queue of Signature Groups */ 150 1.1 christos struct signature_group_t { 151 1.1 christos unsigned spri; 152 1.1 christos unsigned resendcount; 153 1.1 christos uint_fast64_t last_msg_num; 154 1.1 christos struct string_queue_head hashes; 155 1.1 christos struct filed_queue_head files; 156 1.1 christos STAILQ_ENTRY(signature_group_t) entries; 157 1.1 christos }; 158 1.1 christos STAILQ_HEAD(signature_group_head, signature_group_t); 159 1.1 christos 160 1.1 christos /* all global variables for sign */ 161 1.1 christos /* note that there is one object of this type which might only be 162 1.1 christos * partially filled. 163 1.1 christos * The fields .sg and .sig2_delims are set by init() and are always 164 1.1 christos * valid. A value >0 in field .rsid indicates whether the rest of the 165 1.1 christos * structure was already set by sign_global_init(). 166 1.1 christos */ 167 1.1 christos struct sign_global_t { 168 1.1 christos /* params for signature block, named as in RFC nnnn */ 169 1.1 christos const char *ver; 170 1.1 christos uint_fast64_t rsid; 171 1.1 christos int sg; 172 1.1 christos uint_fast64_t gbc; 173 1.1 christos struct signature_group_head SigGroups; 174 1.1 christos struct string_queue_head sig2_delims; 175 1.1 christos 176 1.1 christos EVP_PKEY *privkey; 177 1.1 christos EVP_PKEY *pubkey; 178 1.1 christos char *pubkey_b64; 179 1.1 christos char keytype; 180 1.2 minskim 181 1.1 christos EVP_MD_CTX *mdctx; /* hashing context */ 182 1.1 christos const EVP_MD *md; /* hashing method/algorithm */ 183 1.1 christos unsigned md_len_b64; /* length of b64 hash value */ 184 1.1 christos 185 1.1 christos EVP_MD_CTX *sigctx; /* signature context */ 186 1.1 christos const EVP_MD *sig; /* signature method/algorithm */ 187 1.1 christos unsigned sig_len_b64; /* length of b64 signature */ 188 1.1 christos }; 189 1.1 christos 190 1.1 christos bool sign_global_init(struct filed*); 191 1.1 christos bool sign_sg_init(struct filed*); 192 1.1 christos bool sign_get_keys(void); 193 1.1 christos void sign_global_free(void); 194 1.1 christos struct signature_group_t* sign_get_sg(int, struct filed*); 195 1.1 christos bool sign_send_certificate_block(struct signature_group_t*); 196 1.1 christos unsigned sign_send_signature_block(struct signature_group_t*, bool); 197 1.1 christos void sign_free_hashes(struct signature_group_t*); 198 1.1 christos void sign_free_string_queue(struct string_queue_head*); 199 1.1 christos bool sign_msg_hash(char*, char**); 200 1.1 christos bool sign_append_hash(char*, struct signature_group_t*); 201 1.1 christos bool sign_msg_sign(struct buf_msg**, char*, size_t); 202 1.1 christos bool sign_string_sign(char*, char**); 203 1.1 christos void sign_new_reboot_session(void); 204 1.1 christos void sign_inc_gbc(void); 205 1.1 christos uint_fast64_t sign_assign_msg_num(struct signature_group_t*); 206 1.1 christos 207 1.1 christos #endif /* SIGN_H_ */ 208