Compare commits

..
1 Commits
Author SHA1 Message Date
Sergio R. Caprile 9322227886 avoid IPv6 querying forever 2026-11-17 11:53:09 -03:00
3 changed files with 28 additions and 26 deletions

No files matched your search

+14 -13
View File
@@ -2146,12 +2146,13 @@ bool mg_dns_parse(const uint8_t *buf, size_t len, struct mg_dns_message *dm) {
dm->txnid = mg_ntohs(h->txnid); dm->txnid = mg_ntohs(h->txnid);
is_response = mg_ntohs(h->flags) & 0x8000; is_response = mg_ntohs(h->flags) & 0x8000;
for (i = 0; i < mg_ntohs(h->num_questions); i++) { for (i = 0; i < mg_ntohs(h->num_questions); i++) { // advance past queries
if ((n = mg_dns_parse_rr(buf, len, ofs, true, &rr)) == 0) return false; if ((n = mg_dns_parse_rr(buf, len, ofs, true, &rr)) == 0) return false;
// MG_INFO(("Q %lu %lu %hu/%hu", ofs, n, rr.atype, rr.aclass)); // MG_INFO(("Q %lu %lu %hu/%hu", ofs, n, rr.atype, rr.aclass));
dm->addr.is_ip6 = (rr.atype == MG_DNS_RTYPE_AAAA);
mg_dns_parse_name(buf, len, ofs, dm->name, sizeof(dm->name)); mg_dns_parse_name(buf, len, ofs, dm->name, sizeof(dm->name));
ofs += n; ofs += n;
} } // on unresolved responses, we got the name and query type, at least
if (!is_response) { if (!is_response) {
// For queries, there is no need to parse the answers. In this way, // For queries, there is no need to parse the answers. In this way,
@@ -19166,8 +19167,8 @@ static bool mg_tls_server_send_hello(struct mg_connection *c) {
// calculate P-256 keyshare and the shared secret // calculate P-256 keyshare and the shared secret
pub[0] = 0x04; // uncompressed point 0x04 || X || Y pub[0] = 0x04; // uncompressed point 0x04 || X || Y
if (mg_p256_make_key(pub + 1, prv) == 0 || if (!mg_p256_make_key(pub + 1, prv) ||
mg_p256_shared_secret(tls->p256_peer, prv, tls->ecdhe_sec) == 0) { !mg_p256_shared_secret(tls->p256_peer, prv, tls->ecdhe_sec)) {
mg_bzero(prv, sizeof(prv)); mg_bzero(prv, sizeof(prv));
mg_error(c, "bad key"); mg_error(c, "bad key");
return false; return false;
@@ -19582,7 +19583,7 @@ static bool mg_tls_send_cert_verify(struct mg_connection *c, bool is_client) {
size_t sigsz, verifysz = 0; size_t sigsz, verifysz = 0;
int adj1, adj2; int adj1, adj2;
uint8_t sig[64] = {0}; uint8_t sig[64] = {0};
if (mg_p256_sign(tls->ec_key, hash, sizeof(hash), sig) != 1) { if (!mg_p256_sign(tls->ec_key, hash, sizeof(hash), sig)) {
mg_error(c, "P-256 sign"); mg_error(c, "P-256 sign");
return false; return false;
} }
@@ -19714,7 +19715,7 @@ static bool mg_tls_client_send_hello(struct mg_connection *c) {
} }
// calc+fill keyshare; X || Y, sent as an uncompressed point 0x04 || X || Y // calc+fill keyshare; X || Y, sent as an uncompressed point 0x04 || X || Y
if (mg_p256_make_key(msg_client_hello + 95, tls->p256_cli) == 0) { if (!mg_p256_make_key(msg_client_hello + 95, tls->p256_cli)) {
mg_error(c, "P-256 key"); mg_error(c, "P-256 key");
return false; return false;
} }
@@ -19804,8 +19805,8 @@ static int mg_tls_client_recv_hello(struct mg_connection *c) {
return -1; return -1;
} }
if (key_exchange_len != 65 || key_exchange[0] != 0x04 || if (key_exchange_len != 65 || key_exchange[0] != 0x04 ||
mg_p256_shared_secret(key_exchange + 1, tls->p256_cli, !mg_p256_shared_secret(key_exchange + 1, tls->p256_cli,
tls->ecdhe_sec) == 0) { // Validates the point tls->ecdhe_sec)) { // Validates the point
mg_error(c, "bad key"); mg_error(c, "bad key");
return -1; return -1;
} }
@@ -20186,14 +20187,14 @@ static bool mg_tls_verify_cert_signature(const struct mg_tls_cert *cert,
} }
} }
static bool mg_tls_verify_cert_cn(struct mg_der_tlv *subj, const char *host) { static int mg_tls_verify_cert_cn(struct mg_der_tlv *subj, const char *host) {
struct mg_der_tlv v; struct mg_der_tlv v;
bool matched = false; int matched = 0;
if (mg_der_find_oid(subj, (uint8_t *) "\x55\x04\x03", 3, &v) > 0) { if (mg_der_find_oid(subj, (uint8_t *) "\x55\x04\x03", 3, &v) > 0) {
struct mg_str hn, cn; struct mg_str hn, cn;
MG_VERBOSE(("using CN: %.*s <-> %s", v.len, v.value, host)); MG_VERBOSE(("using CN: %.*s <-> %s", v.len, v.value, host));
hn = mg_str(host), cn = mg_str_n((char *) v.value, v.len); hn = mg_str(host), cn = mg_str_n((char *) v.value, v.len);
matched = (countdots(hn) == countdots(cn) && mg_match(hn, cn, NULL)); matched = (int) (countdots(hn) == countdots(cn) && mg_match(hn, cn, NULL));
} }
return matched; return matched;
} }
@@ -20316,8 +20317,8 @@ static int mg_tls_recv_cert(struct mg_connection *c, bool is_client) {
san = mg_tls_verify_cert_san(cert, certsz, tls->hostname, san = mg_tls_verify_cert_san(cert, certsz, tls->hostname,
is_addr ? &addr : NULL); is_addr ? &addr : NULL);
if (san < 0 || // RFC 6125 6.4.4 allow legacy fallback to CN if (san < 0 || // RFC 6125 6.4.4 allow legacy fallback to CN
(san == 0 && (san == 0 && (is_addr || mg_tls_verify_cert_cn(
(is_addr || !mg_tls_verify_cert_cn(&ci->subj, tls->hostname)))) { &ci->subj, tls->hostname) <= 0))) {
mg_error(c, "failed to verify host"); mg_error(c, "failed to verify host");
return -1; return -1;
} }
+3 -2
View File
@@ -136,12 +136,13 @@ bool mg_dns_parse(const uint8_t *buf, size_t len, struct mg_dns_message *dm) {
dm->txnid = mg_ntohs(h->txnid); dm->txnid = mg_ntohs(h->txnid);
is_response = mg_ntohs(h->flags) & 0x8000; is_response = mg_ntohs(h->flags) & 0x8000;
for (i = 0; i < mg_ntohs(h->num_questions); i++) { for (i = 0; i < mg_ntohs(h->num_questions); i++) { // advance past queries
if ((n = mg_dns_parse_rr(buf, len, ofs, true, &rr)) == 0) return false; if ((n = mg_dns_parse_rr(buf, len, ofs, true, &rr)) == 0) return false;
// MG_INFO(("Q %lu %lu %hu/%hu", ofs, n, rr.atype, rr.aclass)); // MG_INFO(("Q %lu %lu %hu/%hu", ofs, n, rr.atype, rr.aclass));
dm->addr.is_ip6 = (rr.atype == MG_DNS_RTYPE_AAAA);
mg_dns_parse_name(buf, len, ofs, dm->name, sizeof(dm->name)); mg_dns_parse_name(buf, len, ofs, dm->name, sizeof(dm->name));
ofs += n; ofs += n;
} } // on unresolved responses, we got the name and query type, at least
if (!is_response) { if (!is_response) {
// For queries, there is no need to parse the answers. In this way, // For queries, there is no need to parse the answers. In this way,
+11 -11
View File
@@ -761,8 +761,8 @@ static bool mg_tls_server_send_hello(struct mg_connection *c) {
// calculate P-256 keyshare and the shared secret // calculate P-256 keyshare and the shared secret
pub[0] = 0x04; // uncompressed point 0x04 || X || Y pub[0] = 0x04; // uncompressed point 0x04 || X || Y
if (mg_p256_make_key(pub + 1, prv) == 0 || if (!mg_p256_make_key(pub + 1, prv) ||
mg_p256_shared_secret(tls->p256_peer, prv, tls->ecdhe_sec) == 0) { !mg_p256_shared_secret(tls->p256_peer, prv, tls->ecdhe_sec)) {
mg_bzero(prv, sizeof(prv)); mg_bzero(prv, sizeof(prv));
mg_error(c, "bad key"); mg_error(c, "bad key");
return false; return false;
@@ -1177,7 +1177,7 @@ static bool mg_tls_send_cert_verify(struct mg_connection *c, bool is_client) {
size_t sigsz, verifysz = 0; size_t sigsz, verifysz = 0;
int adj1, adj2; int adj1, adj2;
uint8_t sig[64] = {0}; uint8_t sig[64] = {0};
if (mg_p256_sign(tls->ec_key, hash, sizeof(hash), sig) != 1) { if (!mg_p256_sign(tls->ec_key, hash, sizeof(hash), sig)) {
mg_error(c, "P-256 sign"); mg_error(c, "P-256 sign");
return false; return false;
} }
@@ -1309,7 +1309,7 @@ static bool mg_tls_client_send_hello(struct mg_connection *c) {
} }
// calc+fill keyshare; X || Y, sent as an uncompressed point 0x04 || X || Y // calc+fill keyshare; X || Y, sent as an uncompressed point 0x04 || X || Y
if (mg_p256_make_key(msg_client_hello + 95, tls->p256_cli) == 0) { if (!mg_p256_make_key(msg_client_hello + 95, tls->p256_cli)) {
mg_error(c, "P-256 key"); mg_error(c, "P-256 key");
return false; return false;
} }
@@ -1399,8 +1399,8 @@ static int mg_tls_client_recv_hello(struct mg_connection *c) {
return -1; return -1;
} }
if (key_exchange_len != 65 || key_exchange[0] != 0x04 || if (key_exchange_len != 65 || key_exchange[0] != 0x04 ||
mg_p256_shared_secret(key_exchange + 1, tls->p256_cli, !mg_p256_shared_secret(key_exchange + 1, tls->p256_cli,
tls->ecdhe_sec) == 0) { // Validates the point tls->ecdhe_sec)) { // Validates the point
mg_error(c, "bad key"); mg_error(c, "bad key");
return -1; return -1;
} }
@@ -1781,14 +1781,14 @@ static bool mg_tls_verify_cert_signature(const struct mg_tls_cert *cert,
} }
} }
static bool mg_tls_verify_cert_cn(struct mg_der_tlv *subj, const char *host) { static int mg_tls_verify_cert_cn(struct mg_der_tlv *subj, const char *host) {
struct mg_der_tlv v; struct mg_der_tlv v;
bool matched = false; int matched = 0;
if (mg_der_find_oid(subj, (uint8_t *) "\x55\x04\x03", 3, &v) > 0) { if (mg_der_find_oid(subj, (uint8_t *) "\x55\x04\x03", 3, &v) > 0) {
struct mg_str hn, cn; struct mg_str hn, cn;
MG_VERBOSE(("using CN: %.*s <-> %s", v.len, v.value, host)); MG_VERBOSE(("using CN: %.*s <-> %s", v.len, v.value, host));
hn = mg_str(host), cn = mg_str_n((char *) v.value, v.len); hn = mg_str(host), cn = mg_str_n((char *) v.value, v.len);
matched = (countdots(hn) == countdots(cn) && mg_match(hn, cn, NULL)); matched = (int) (countdots(hn) == countdots(cn) && mg_match(hn, cn, NULL));
} }
return matched; return matched;
} }
@@ -1911,8 +1911,8 @@ static int mg_tls_recv_cert(struct mg_connection *c, bool is_client) {
san = mg_tls_verify_cert_san(cert, certsz, tls->hostname, san = mg_tls_verify_cert_san(cert, certsz, tls->hostname,
is_addr ? &addr : NULL); is_addr ? &addr : NULL);
if (san < 0 || // RFC 6125 6.4.4 allow legacy fallback to CN if (san < 0 || // RFC 6125 6.4.4 allow legacy fallback to CN
(san == 0 && (san == 0 && (is_addr || mg_tls_verify_cert_cn(
(is_addr || !mg_tls_verify_cert_cn(&ci->subj, tls->hostname)))) { &ci->subj, tls->hostname) <= 0))) {
mg_error(c, "failed to verify host"); mg_error(c, "failed to verify host");
return -1; return -1;
} }