| 1 |
|
| /* po_key.h - ingest keys. |
| 2 |
|
| * |
| 3 |
|
| * A self-hosted box exposing the ingest prefix to the network wants that |
| 4 |
|
| * endpoint authenticated, and the exporter side already has the channel: a |
| 5 |
|
| * bearer token in `Authorization`, which is what `OTEL_EXPORTER_OTLP_HEADERS` |
| 6 |
|
| * gives every OpenTelemetry SDK for free. No new mechanism, no client |
| 7 |
|
| * change. |
| 8 |
|
| * |
| 9 |
|
| * A KEY IS STORED AS A HASH, NEVER AS THE TOKEN. |
| 10 |
|
| * |
| 11 |
|
| * The key file lives in `/etc`, gets backed up, and ends up in a |
| 12 |
|
| * configuration-management repository. As a list of tokens it is a list of |
| 13 |
|
| * credentials; as a list of hashes it is a list of nothing useful. The token |
| 14 |
|
| * exists once, at the moment it is issued. |
| 15 |
|
| * |
| 16 |
|
| * A KEY COMPARED WITH `eq` IS A TIMING ORACLE. |
| 17 |
|
| * |
| 18 |
|
| * `memcmp` and every string compare in every language return as soon as two |
| 19 |
|
| * bytes differ, so the time taken leaks the length of the matching prefix, |
| 20 |
|
| * and an attacker recovers a key one byte at a time. The comparison here is |
| 21 |
|
| * constant-time by construction: it always reads every byte and accumulates |
| 22 |
|
| * differences rather than branching on them. |
| 23 |
|
| * |
| 24 |
|
| * That is asserted structurally rather than by measurement. A timing test on |
| 25 |
|
| * a loaded smoker measures the smoker. |
| 26 |
|
| * |
| 27 |
|
| * AN INGEST KEY IS NOT THE SESSION CREDENTIAL. |
| 28 |
|
| * |
| 29 |
|
| * Keys are scoped to ingest and cannot read the UI. A key that could do both |
| 30 |
|
| * leaks a whole installation the first time it is baked into a container |
| 31 |
|
| * image, which is where ingest keys go. |
| 32 |
|
| */ |
| 33 |
|
| #ifndef PO_KEY_H |
| 34 |
|
| #define PO_KEY_H |
| 35 |
|
| |
| 36 |
|
| #include "punk_observe/po_compat.h" |
| 37 |
|
| #include "punk_observe/po_hash.h" |
| 38 |
|
| |
| 39 |
|
| #define PO_KEY_MAX 256 /* a presented token, before hashing */ |
| 40 |
|
| #define PO_KEY_NAME_MAX 64 |
| 41 |
|
| #define PO_KEYS_MAX 256 |
| 42 |
|
| |
| 43 |
|
| /* A 128-bit content hash. Not a password KDF, and that is a deliberate |
| 44 |
|
| * choice with a reason: an ingest key is high-entropy machine-generated |
| 45 |
|
| * material, not a human-chosen password, so there is no dictionary to |
| 46 |
|
| * stretch against. What the hash buys is that the file is not a credential. |
| 47 |
|
| * |
| 48 |
|
| * If keys were ever allowed to be user-chosen this would be the wrong |
| 49 |
|
| * primitive, which is why they are generated and never accepted. */ |
| 50 |
|
| typedef struct { |
| 51 |
|
| char name[PO_KEY_NAME_MAX + 1]; |
| 52 |
|
| size_t name_len; |
| 53 |
|
| po_u64 h_hi, h_lo; |
| 54 |
|
| int revoked; |
| 55 |
|
| } po_key; |
| 56 |
|
| |
| 57 |
|
| typedef struct { |
| 58 |
|
| po_key k[PO_KEYS_MAX]; |
| 59 |
|
| uint32_t n; |
| 60 |
|
| int required; /* 0 = the endpoint is open, and says so */ |
| 61 |
|
| } po_keyring; |
| 62 |
|
| |
| 63 |
14 |
| static void po_keyring_init(po_keyring *r) { memset(r, 0, sizeof(*r)); } |
| 64 |
|
| |
| 65 |
45 |
| static void po_key_hash(const char *tok, size_t len, po_u64 *hi, po_u64 *lo) { |
| 66 |
|
| /* A FIXED seed, so a hash computed at issue time matches one computed at |
| 67 |
|
| * check time in another process. A per-process seed would make every |
| 68 |
|
| * stored hash useless after a restart, which is the kind of bug that only |
| 69 |
|
| * shows up on the first reboot after a deployment. */ |
| 70 |
45 |
| po_h128 h = po_murmur3_128(tok, len, 0x6f627365u); |
| 71 |
45 |
| *hi = h.hi; *lo = h.lo; |
| 72 |
45 |
| } |
| 73 |
|
| |
| 74 |
26 |
| static int po_keyring_add(po_keyring *r, const char *name, size_t nlen, |
| 75 |
|
| const char *tok, size_t tlen) { |
| 76 |
|
| po_key *k; |
| 77 |
26 |
▶ | if (r->n >= PO_KEYS_MAX) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->n >= PO_KEYS_MAX) return 0; |
0 |
26 |
26 |
|
| 78 |
26 |
▶ | if (!tok || !tlen || tlen > PO_KEY_MAX) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!tok || !tlen || tlen > PO_KEY_MAX) return 0; |
26 |
0 |
26 |
| if (!tok || !tlen || tlen > PO_KEY_MAX) return 0; |
26 |
0 |
26 |
| if (!tok || !tlen || tlen > PO_KEY_MAX) return 0; |
0 |
26 |
26 |
|
| 79 |
26 |
▶ | if (nlen > PO_KEY_NAME_MAX) nlen = PO_KEY_NAME_MAX; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nlen > PO_KEY_NAME_MAX) nlen = PO_KEY_NAME_MAX; |
0 |
26 |
26 |
|
| 80 |
26 |
| k = &r->k[r->n++]; |
| 81 |
26 |
| memset(k, 0, sizeof(*k)); |
| 82 |
26 |
▶ | if (nlen) memcpy(k->name, name, nlen); |
Branch: 1 branch
| Branch | True | False | Total |
| if (nlen) memcpy(k->name, name, nlen); |
26 |
0 |
26 |
|
| 83 |
26 |
| k->name_len = nlen; |
| 84 |
26 |
| po_key_hash(tok, tlen, &k->h_hi, &k->h_lo); |
| 85 |
26 |
| r->required = 1; |
| 86 |
26 |
| return 1; |
| 87 |
|
| } |
| 88 |
|
| |
| 89 |
|
| /* Add an already-hashed key, which is what reading the key file does. The |
| 90 |
|
| * file holds hashes; nothing on disk holds a token. */ |
| 91 |
0 |
| static int po_keyring_add_hash(po_keyring *r, const char *name, size_t nlen, |
| 92 |
|
| po_u64 hi, po_u64 lo) { |
| 93 |
|
| po_key *k; |
| 94 |
0 |
▶ | if (r->n >= PO_KEYS_MAX) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->n >= PO_KEYS_MAX) return 0; |
0 |
0 |
0 |
|
| 95 |
0 |
▶ | if (nlen > PO_KEY_NAME_MAX) nlen = PO_KEY_NAME_MAX; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nlen > PO_KEY_NAME_MAX) nlen = PO_KEY_NAME_MAX; |
0 |
0 |
0 |
|
| 96 |
0 |
| k = &r->k[r->n++]; |
| 97 |
0 |
| memset(k, 0, sizeof(*k)); |
| 98 |
0 |
▶ | if (nlen) memcpy(k->name, name, nlen); |
Branch: 1 branch
| Branch | True | False | Total |
| if (nlen) memcpy(k->name, name, nlen); |
0 |
0 |
0 |
|
| 99 |
0 |
| k->name_len = nlen; |
| 100 |
0 |
| k->h_hi = hi; k->h_lo = lo; |
| 101 |
0 |
| r->required = 1; |
| 102 |
0 |
| return 1; |
| 103 |
|
| } |
| 104 |
|
| |
| 105 |
|
| /* CONSTANT TIME, and structurally so. |
| 106 |
|
| * |
| 107 |
|
| * No early return, no branch on the data, and the accumulator is or-ed rather |
| 108 |
|
| * than tested. The compiler is free to vectorise it; what it is not free to |
| 109 |
|
| * do is short-circuit, because there is no condition to short-circuit on. */ |
| 110 |
23 |
| static int po_ct_eq128(po_u64 a_hi, po_u64 a_lo, po_u64 b_hi, po_u64 b_lo) { |
| 111 |
23 |
| po_u64 d = (a_hi ^ b_hi) | (a_lo ^ b_lo); |
| 112 |
|
| /* Fold to one bit without a comparison: d is zero exactly when equal. */ |
| 113 |
23 |
| d |= d >> 32; d |= d >> 16; d |= d >> 8; |
| 114 |
23 |
| d |= d >> 4; d |= d >> 2; d |= d >> 1; |
| 115 |
23 |
| return (int)((d & 1) ^ 1); |
| 116 |
|
| } |
| 117 |
|
| |
| 118 |
|
| /* Bearer parsing. The scheme is matched case-insensitively because RFC 7235 |
| 119 |
|
| * says it is case-insensitive, and an exporter sending `bearer` lowercase is |
| 120 |
|
| * not a client to break. */ |
| 121 |
23 |
| static int po_bearer(const char *hdr, size_t len, const char **tok, |
| 122 |
|
| size_t *tlen) { |
| 123 |
23 |
| size_t i = 0; |
| 124 |
|
| static const char pre[] = "bearer"; |
| 125 |
23 |
▶ | if (!hdr || len < 7) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!hdr || len < 7) return 0; |
23 |
0 |
23 |
| if (!hdr || len < 7) return 0; |
4 |
19 |
23 |
|
| 126 |
123 |
▶ | for (i = 0; i < 6; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 6; i++) { |
106 |
17 |
123 |
|
| 127 |
106 |
| char c = hdr[i]; |
| 128 |
106 |
▶ | if (c >= 'A' && c <= 'Z') c = (char)(c - 'A' + 'a'); |
Branch: 2 branches
| Branch | True | False | Total |
| if (c >= 'A' && c <= 'Z') c = (char)(c - 'A' + 'a'); |
106 |
0 |
106 |
| if (c >= 'A' && c <= 'Z') c = (char)(c - 'A' + 'a'); |
23 |
83 |
106 |
|
| 129 |
106 |
▶ | if (c != pre[i]) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c != pre[i]) return 0; |
2 |
104 |
106 |
|
| 130 |
|
| } |
| 131 |
17 |
| i = 6; |
| 132 |
17 |
▶ | if (hdr[i] != ' ' && hdr[i] != '\t') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (hdr[i] != ' ' && hdr[i] != '\t') return 0; |
2 |
15 |
17 |
| if (hdr[i] != ' ' && hdr[i] != '\t') return 0; |
1 |
1 |
2 |
|
| 133 |
34 |
▶ | while (i < len && (hdr[i] == ' ' || hdr[i] == '\t')) i++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (i < len && (hdr[i] == ' ' || hdr[i] == '\t')) i++; |
32 |
2 |
34 |
| while (i < len && (hdr[i] == ' ' || hdr[i] == '\t')) i++; |
17 |
15 |
32 |
| while (i < len && (hdr[i] == ' ' || hdr[i] == '\t')) i++; |
1 |
14 |
15 |
|
| 134 |
16 |
▶ | if (i >= len) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i >= len) return 0; |
2 |
14 |
16 |
|
| 135 |
14 |
| *tok = hdr + i; |
| 136 |
14 |
| *tlen = len - i; |
| 137 |
14 |
| return 1; |
| 138 |
|
| } |
| 139 |
|
| |
| 140 |
|
| #define PO_AUTH_OK 0 |
| 141 |
|
| #define PO_AUTH_MISSING 1 |
| 142 |
|
| #define PO_AUTH_BAD 2 |
| 143 |
|
| #define PO_AUTH_REVOKED 3 |
| 144 |
|
| |
| 145 |
|
| /* Check a presented token. |
| 146 |
|
| * |
| 147 |
|
| * EVERY KEY IS EXAMINED even after a match, so the time taken does not depend |
| 148 |
|
| * on WHICH key matched or on how many are configured. A loop that returned on |
| 149 |
|
| * the first hit would leak the position of a key in the ring, which is a |
| 150 |
|
| * smaller oracle than a byte-at-a-time one but is free to close. */ |
| 151 |
14 |
| static int po_keyring_check(const po_keyring *r, const char *tok, size_t tlen, |
| 152 |
|
| const char **name, size_t *nlen) { |
| 153 |
|
| po_u64 hi, lo; |
| 154 |
|
| uint32_t i; |
| 155 |
14 |
| int found = 0, revoked = 0; |
| 156 |
14 |
| const char *hit_name = NULL; |
| 157 |
14 |
| size_t hit_len = 0; |
| 158 |
|
| |
| 159 |
14 |
▶ | if (!r->required) return PO_AUTH_OK; /* configured open, deliberately */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r->required) return PO_AUTH_OK; /* configured open, deliberately */ |
1 |
13 |
14 |
|
| 160 |
13 |
▶ | if (!tok || !tlen || tlen > PO_KEY_MAX) return PO_AUTH_MISSING; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!tok || !tlen || tlen > PO_KEY_MAX) return PO_AUTH_MISSING; |
9 |
4 |
13 |
| if (!tok || !tlen || tlen > PO_KEY_MAX) return PO_AUTH_MISSING; |
9 |
0 |
9 |
| if (!tok || !tlen || tlen > PO_KEY_MAX) return PO_AUTH_MISSING; |
0 |
9 |
9 |
|
| 161 |
|
| |
| 162 |
9 |
| po_key_hash(tok, tlen, &hi, &lo); |
| 163 |
27 |
▶ | for (i = 0; i < r->n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < r->n; i++) { |
18 |
9 |
27 |
|
| 164 |
18 |
| int eq = po_ct_eq128(hi, lo, r->k[i].h_hi, r->k[i].h_lo); |
| 165 |
18 |
▶ | if (eq) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eq) { |
4 |
14 |
18 |
|
| 166 |
4 |
| found = 1; |
| 167 |
4 |
| revoked = r->k[i].revoked; |
| 168 |
4 |
| hit_name = r->k[i].name; |
| 169 |
4 |
| hit_len = r->k[i].name_len; |
| 170 |
|
| } |
| 171 |
|
| } |
| 172 |
9 |
▶ | if (!found) return PO_AUTH_BAD; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!found) return PO_AUTH_BAD; |
5 |
4 |
9 |
|
| 173 |
4 |
▶ | if (revoked) return PO_AUTH_REVOKED; |
Branch: 1 branch
| Branch | True | False | Total |
| if (revoked) return PO_AUTH_REVOKED; |
1 |
3 |
4 |
|
| 174 |
3 |
▶ | if (name) *name = hit_name; |
Branch: 1 branch
| Branch | True | False | Total |
| if (name) *name = hit_name; |
3 |
0 |
3 |
|
| 175 |
3 |
▶ | if (nlen) *nlen = hit_len; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nlen) *nlen = hit_len; |
3 |
0 |
3 |
|
| 176 |
3 |
| return PO_AUTH_OK; |
| 177 |
|
| } |
| 178 |
|
| |
| 179 |
2 |
| static int po_keyring_revoke(po_keyring *r, const char *name, size_t nlen) { |
| 180 |
|
| uint32_t i; |
| 181 |
2 |
| int n = 0; |
| 182 |
6 |
▶ | for (i = 0; i < r->n; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < r->n; i++) |
4 |
2 |
6 |
|
| 183 |
4 |
▶ | if (r->k[i].name_len == nlen |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->k[i].name_len == nlen |
2 |
2 |
4 |
|
| 184 |
2 |
▶ | && (nlen == 0 || memcmp(r->k[i].name, name, nlen) == 0)) { |
Branch: 2 branches
| Branch | True | False | Total |
| && (nlen == 0 || memcmp(r->k[i].name, name, nlen) == 0)) { |
2 |
0 |
2 |
| && (nlen == 0 || memcmp(r->k[i].name, name, nlen) == 0)) { |
2 |
0 |
2 |
|
| 185 |
2 |
| r->k[i].revoked = 1; |
| 186 |
2 |
| n++; |
| 187 |
|
| } |
| 188 |
2 |
| return n; |
| 189 |
|
| } |
| 190 |
|
| |
| 191 |
|
| #endif /* PO_KEY_H */ |