| 3138 | } |
| 3139 | |
| 3140 | static void handle_ui_readiness(cbm_http_server_t *srv, cbm_http_conn_t *c, |
| 3141 | const cbm_http_req_t *req) { |
| 3142 | if (!srv->readiness_secret_set) { |
| 3143 | cbm_http_replyf(c, 503, "Cache-Control: no-store\r\n", "%s", "readiness proof unavailable"); |
| 3144 | return; |
| 3145 | } |
| 3146 | char challenge_hex[CBM_SHA256_HEX_LEN + 1U]; |
| 3147 | uint8_t challenge[CBM_SHA256_DIGEST_LEN]; |
| 3148 | static const char prefix[] = "challenge="; |
| 3149 | if (strncmp(req->query, prefix, sizeof(prefix) - 1U) != 0 || |
| 3150 | strlen(req->query) != sizeof(prefix) - 1U + CBM_SHA256_HEX_LEN) { |
| 3151 | cbm_http_replyf(c, 400, "Cache-Control: no-store\r\n", "%s", "invalid challenge"); |
| 3152 | return; |
| 3153 | } |
| 3154 | memcpy(challenge_hex, req->query + sizeof(prefix) - 1U, CBM_SHA256_HEX_LEN); |
| 3155 | challenge_hex[CBM_SHA256_HEX_LEN] = '\0'; |
| 3156 | if (!readiness_hex_decode(challenge_hex, challenge)) { |
| 3157 | cbm_secure_zero(challenge, sizeof(challenge)); |
| 3158 | cbm_http_replyf(c, 400, "Cache-Control: no-store\r\n", "%s", "invalid challenge"); |
| 3159 | return; |
| 3160 | } |
| 3161 | uint8_t proof[CBM_SHA256_DIGEST_LEN]; |
| 3162 | char proof_hex[CBM_SHA256_HEX_LEN + 1U]; |
| 3163 | cbm_hmac_sha256(srv->readiness_secret, sizeof(srv->readiness_secret), challenge, |
| 3164 | sizeof(challenge), proof); |
| 3165 | readiness_hex_encode(proof, proof_hex); |
| 3166 | cbm_http_replyf(c, 200, |
| 3167 | "Content-Type: text/plain; charset=utf-8\r\n" |
| 3168 | "Cache-Control: no-store\r\n" |
| 3169 | "X-Content-Type-Options: nosniff\r\n", |
| 3170 | "%s", proof_hex); |
| 3171 | cbm_secure_zero(challenge, sizeof(challenge)); |
| 3172 | cbm_secure_zero(proof, sizeof(proof)); |
| 3173 | cbm_secure_zero(proof_hex, sizeof(proof_hex)); |
| 3174 | } |
| 3175 | |
| 3176 | static bool request_passes_http_security(cbm_http_server_t *srv, cbm_http_conn_t *c, |
| 3177 | const cbm_http_req_t *req) { |
no test coverage detected