diff --git a/mongoose.c b/mongoose.c index a6778b60..5189f6d6 100644 --- a/mongoose.c +++ b/mongoose.c @@ -15933,10 +15933,9 @@ int gcm_crypt_and_tag( prepare the gcm context with the keying material, we simply invoke each of the three GCM sub-functions in turn... */ - gcm_start(ctx, mode, iv, iv_len, add, add_len); - gcm_update(ctx, length, input, output); - gcm_finish(ctx, tag, tag_len); - return (0); + if (gcm_start(ctx, mode, iv, iv_len, add, add_len) != 0) return -1; + if (gcm_update(ctx, length, input, output) != 0) return -1; + return gcm_finish(ctx, tag, tag_len); } /****************************************************************************** @@ -18338,7 +18337,9 @@ static int mg_rsa_parse_key(const uint8_t *der, size_t dersz, // Parse version (should be 0) MG_VERBOSE(("Before version: offset=%d, bytes: %02x %02x %02x %02x", - (int) (p - der), p[0], p[1], p[2], p[3])); + (int) (p - der), p < end ? p[0] : 0xFF, + p + 1 < end ? p[1] : 0xFF, p + 2 < end ? p[2] : 0xFF, + p + 3 < end ? p[3] : 0xFF)); if (mg_rsa_parse_der_int(&p, end, &version) < 0) { MG_ERROR(("Failed to parse version")); return -1; @@ -18349,7 +18350,10 @@ static int mg_rsa_parse_key(const uint8_t *der, size_t dersz, // Parse the 8 components: n, e, d, p, q, dP, dQ, qInv MG_VERBOSE(("Before n: offset=%d, bytes: %02x %02x %02x %02x %02x %02x", - (int) (p - der), p[0], p[1], p[2], p[3], p[4], p[5])); + (int) (p - der), p < end ? p[0] : 0xFF, + p + 1 < end ? p[1] : 0xFF, p + 2 < end ? p[2] : 0xFF, + p + 3 < end ? p[3] : 0xFF, p + 4 < end ? p[4] : 0xFF, + p + 5 < end ? p[5] : 0xFF)); if (mg_rsa_parse_der_int(&p, end, &key->n) < 0) { MG_ERROR(("Failed to parse n (modulus)")); return -1; @@ -18357,10 +18361,14 @@ static int mg_rsa_parse_key(const uint8_t *der, size_t dersz, MG_VERBOSE(("Parsed n: %d bytes, offset now=%d, consumed=%d bytes total", (int) key->n.len, (int) (p - der), (int) (p - der))); MG_VERBOSE((" First 8 bytes of n: %02x %02x %02x %02x %02x %02x %02x %02x", - (unsigned char) key->n.buf[0], (unsigned char) key->n.buf[1], - (unsigned char) key->n.buf[2], (unsigned char) key->n.buf[3], - (unsigned char) key->n.buf[4], (unsigned char) key->n.buf[5], - (unsigned char) key->n.buf[6], (unsigned char) key->n.buf[7])); + key->n.len > 0 ? (unsigned char) key->n.buf[0] : 0xFF, + key->n.len > 1 ? (unsigned char) key->n.buf[1] : 0xFF, + key->n.len > 2 ? (unsigned char) key->n.buf[2] : 0xFF, + key->n.len > 3 ? (unsigned char) key->n.buf[3] : 0xFF, + key->n.len > 4 ? (unsigned char) key->n.buf[4] : 0xFF, + key->n.len > 5 ? (unsigned char) key->n.buf[5] : 0xFF, + key->n.len > 6 ? (unsigned char) key->n.buf[6] : 0xFF, + key->n.len > 7 ? (unsigned char) key->n.buf[7] : 0xFF)); MG_VERBOSE((" Next bytes after n: %02x %02x %02x %02x %02x %02x", p < end ? p[0] : 0xFF, p + 1 < end ? p[1] : 0xFF, p + 2 < end ? p[2] : 0xFF, p + 3 < end ? p[3] : 0xFF, diff --git a/src/tls_aes128.c b/src/tls_aes128.c index 0e6566b6..811d4eb3 100644 --- a/src/tls_aes128.c +++ b/src/tls_aes128.c @@ -1061,10 +1061,9 @@ int gcm_crypt_and_tag( prepare the gcm context with the keying material, we simply invoke each of the three GCM sub-functions in turn... */ - gcm_start(ctx, mode, iv, iv_len, add, add_len); - gcm_update(ctx, length, input, output); - gcm_finish(ctx, tag, tag_len); - return (0); + if (gcm_start(ctx, mode, iv, iv_len, add, add_len) != 0) return -1; + if (gcm_update(ctx, length, input, output) != 0) return -1; + return gcm_finish(ctx, tag, tag_len); } /****************************************************************************** diff --git a/src/tls_builtin.c b/src/tls_builtin.c index 99d4efcf..e976bfed 100644 --- a/src/tls_builtin.c +++ b/src/tls_builtin.c @@ -2324,7 +2324,9 @@ static int mg_rsa_parse_key(const uint8_t *der, size_t dersz, // Parse version (should be 0) MG_VERBOSE(("Before version: offset=%d, bytes: %02x %02x %02x %02x", - (int) (p - der), p[0], p[1], p[2], p[3])); + (int) (p - der), p < end ? p[0] : 0xFF, + p + 1 < end ? p[1] : 0xFF, p + 2 < end ? p[2] : 0xFF, + p + 3 < end ? p[3] : 0xFF)); if (mg_rsa_parse_der_int(&p, end, &version) < 0) { MG_ERROR(("Failed to parse version")); return -1; @@ -2335,7 +2337,10 @@ static int mg_rsa_parse_key(const uint8_t *der, size_t dersz, // Parse the 8 components: n, e, d, p, q, dP, dQ, qInv MG_VERBOSE(("Before n: offset=%d, bytes: %02x %02x %02x %02x %02x %02x", - (int) (p - der), p[0], p[1], p[2], p[3], p[4], p[5])); + (int) (p - der), p < end ? p[0] : 0xFF, + p + 1 < end ? p[1] : 0xFF, p + 2 < end ? p[2] : 0xFF, + p + 3 < end ? p[3] : 0xFF, p + 4 < end ? p[4] : 0xFF, + p + 5 < end ? p[5] : 0xFF)); if (mg_rsa_parse_der_int(&p, end, &key->n) < 0) { MG_ERROR(("Failed to parse n (modulus)")); return -1; @@ -2343,10 +2348,14 @@ static int mg_rsa_parse_key(const uint8_t *der, size_t dersz, MG_VERBOSE(("Parsed n: %d bytes, offset now=%d, consumed=%d bytes total", (int) key->n.len, (int) (p - der), (int) (p - der))); MG_VERBOSE((" First 8 bytes of n: %02x %02x %02x %02x %02x %02x %02x %02x", - (unsigned char) key->n.buf[0], (unsigned char) key->n.buf[1], - (unsigned char) key->n.buf[2], (unsigned char) key->n.buf[3], - (unsigned char) key->n.buf[4], (unsigned char) key->n.buf[5], - (unsigned char) key->n.buf[6], (unsigned char) key->n.buf[7])); + key->n.len > 0 ? (unsigned char) key->n.buf[0] : 0xFF, + key->n.len > 1 ? (unsigned char) key->n.buf[1] : 0xFF, + key->n.len > 2 ? (unsigned char) key->n.buf[2] : 0xFF, + key->n.len > 3 ? (unsigned char) key->n.buf[3] : 0xFF, + key->n.len > 4 ? (unsigned char) key->n.buf[4] : 0xFF, + key->n.len > 5 ? (unsigned char) key->n.buf[5] : 0xFF, + key->n.len > 6 ? (unsigned char) key->n.buf[6] : 0xFF, + key->n.len > 7 ? (unsigned char) key->n.buf[7] : 0xFF)); MG_VERBOSE((" Next bytes after n: %02x %02x %02x %02x %02x %02x", p < end ? p[0] : 0xFF, p + 1 < end ? p[1] : 0xFF, p + 2 < end ? p[2] : 0xFF, p + 3 < end ? p[3] : 0xFF,