This function is taken from the libsecp256k1 distribution and implements * DER parsing for ECDSA signatures, while supporting an arbitrary subset of * format violations. * * Supported violations include negative integers, excessive padding, garbage * at the end, and overly long length descriptors. This is safe to use in * Bitcoin because since the activation of BIP66, signatures are ver
| 33 | * violations present in the blockchain before that point. |
| 34 | */ |
| 35 | int ecdsa_signature_parse_der_lax(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input, size_t inputlen) { |
| 36 | size_t rpos, rlen, spos, slen; |
| 37 | size_t pos = 0; |
| 38 | size_t lenbyte; |
| 39 | unsigned char tmpsig[64] = {0}; |
| 40 | int overflow = 0; |
| 41 | |
| 42 | /* Hack to initialize sig with a correctly-parsed but invalid signature. */ |
| 43 | secp256k1_ecdsa_signature_parse_compact(ctx, sig, tmpsig); |
| 44 | |
| 45 | /* Sequence tag byte */ |
| 46 | if (pos == inputlen || input[pos] != 0x30) { |
| 47 | return 0; |
| 48 | } |
| 49 | pos++; |
| 50 | |
| 51 | /* Sequence length bytes */ |
| 52 | if (pos == inputlen) { |
| 53 | return 0; |
| 54 | } |
| 55 | lenbyte = input[pos++]; |
| 56 | if (lenbyte & 0x80) { |
| 57 | lenbyte -= 0x80; |
| 58 | if (lenbyte > inputlen - pos) { |
| 59 | return 0; |
| 60 | } |
| 61 | pos += lenbyte; |
| 62 | } |
| 63 | |
| 64 | /* Integer tag byte for R */ |
| 65 | if (pos == inputlen || input[pos] != 0x02) { |
| 66 | return 0; |
| 67 | } |
| 68 | pos++; |
| 69 | |
| 70 | /* Integer length for R */ |
| 71 | if (pos == inputlen) { |
| 72 | return 0; |
| 73 | } |
| 74 | lenbyte = input[pos++]; |
| 75 | if (lenbyte & 0x80) { |
| 76 | lenbyte -= 0x80; |
| 77 | if (lenbyte > inputlen - pos) { |
| 78 | return 0; |
| 79 | } |
| 80 | while (lenbyte > 0 && input[pos] == 0) { |
| 81 | pos++; |
| 82 | lenbyte--; |
| 83 | } |
| 84 | static_assert(sizeof(size_t) >= 4, "size_t too small"); |
| 85 | if (lenbyte >= 4) { |
| 86 | return 0; |
| 87 | } |
| 88 | rlen = 0; |
| 89 | while (lenbyte > 0) { |
| 90 | rlen = (rlen << 8) + input[pos]; |
| 91 | pos++; |
| 92 | lenbyte--; |
no test coverage detected