|
| 1 | +#include <stdlib.h> |
| 2 | +#include <stdint.h> |
| 3 | + |
| 4 | +/* Parse a DER signature with arbitrary |
| 5 | +/** This function is taken from the libsecp256k1 distribution and implements |
| 6 | + * DER parsing for ECDSA signatures, while supporting an arbitrary subset of |
| 7 | + * format violations. |
| 8 | + * |
| 9 | + * Supported violations include negative integers, excessive padding, garbage |
| 10 | + * at the end, and overly long length descriptors. This is safe to use in |
| 11 | + * Bitcoin because since the activation of BIP66, signatures are verified to be |
| 12 | + * strict DER before being passed to this module, and we know it supports all |
| 13 | + * violations present in the blockchain before that point. |
| 14 | + */ |
| 15 | +int ecdsa_signature_parse_der_lax(const secp256k1_context* ctx, secp256k1_ecdsa_signature* sig, const unsigned char *input, size_t inputlen) { |
| 16 | + size_t rpos, rlen, spos, slen; |
| 17 | + size_t pos = 0; |
| 18 | + size_t lenbyte; |
| 19 | + unsigned char tmpsig[64] = {0}; |
| 20 | + int overflow = 0; |
| 21 | + |
| 22 | + /* Hack to initialize sig with a correctly-parsed but invalid signature. */ |
| 23 | + secp256k1_ecdsa_signature_parse_compact(ctx, sig, tmpsig); |
| 24 | + |
| 25 | + /* Sequence tag byte */ |
| 26 | + if (pos == inputlen || input[pos] != 0x30) { |
| 27 | + return 0; |
| 28 | + } |
| 29 | + pos++; |
| 30 | + |
| 31 | + /* Sequence length bytes */ |
| 32 | + if (pos == inputlen) { |
| 33 | + return 0; |
| 34 | + } |
| 35 | + lenbyte = input[pos++]; |
| 36 | + if (lenbyte & 0x80) { |
| 37 | + lenbyte -= 0x80; |
| 38 | + if (pos + lenbyte > inputlen) { |
| 39 | + return 0; |
| 40 | + } |
| 41 | + pos += lenbyte; |
| 42 | + } |
| 43 | + |
| 44 | + /* Integer tag byte for R */ |
| 45 | + if (pos == inputlen || input[pos] != 0x02) { |
| 46 | + return 0; |
| 47 | + } |
| 48 | + pos++; |
| 49 | + |
| 50 | + /* Integer length for R */ |
| 51 | + if (pos == inputlen) { |
| 52 | + return 0; |
| 53 | + } |
| 54 | + lenbyte = input[pos++]; |
| 55 | + if (lenbyte & 0x80) { |
| 56 | + lenbyte -= 0x80; |
| 57 | + if (pos + lenbyte > inputlen) { |
| 58 | + return 0; |
| 59 | + } |
| 60 | + while (lenbyte > 0 && input[pos] == 0) { |
| 61 | + pos++; |
| 62 | + lenbyte--; |
| 63 | + } |
| 64 | + if (lenbyte >= sizeof(size_t)) { |
| 65 | + return 0; |
| 66 | + } |
| 67 | + rlen = 0; |
| 68 | + while (lenbyte > 0) { |
| 69 | + rlen = (rlen << 8) + input[pos]; |
| 70 | + pos++; |
| 71 | + lenbyte--; |
| 72 | + } |
| 73 | + } else { |
| 74 | + rlen = lenbyte; |
| 75 | + } |
| 76 | + if (rlen > inputlen - pos) { |
| 77 | + return 0; |
| 78 | + } |
| 79 | + rpos = pos; |
| 80 | + pos += rlen; |
| 81 | + |
| 82 | + /* Integer tag byte for S */ |
| 83 | + if (pos == inputlen || input[pos] != 0x02) { |
| 84 | + return 0; |
| 85 | + } |
| 86 | + pos++; |
| 87 | + |
| 88 | + /* Integer length for S */ |
| 89 | + if (pos == inputlen) { |
| 90 | + return 0; |
| 91 | + } |
| 92 | + lenbyte = input[pos++]; |
| 93 | + if (lenbyte & 0x80) { |
| 94 | + lenbyte -= 0x80; |
| 95 | + if (pos + lenbyte > inputlen) { |
| 96 | + return 0; |
| 97 | + } |
| 98 | + while (lenbyte > 0 && input[pos] == 0) { |
| 99 | + pos++; |
| 100 | + lenbyte--; |
| 101 | + } |
| 102 | + if (lenbyte >= sizeof(size_t)) { |
| 103 | + return 0; |
| 104 | + } |
| 105 | + slen = 0; |
| 106 | + while (lenbyte > 0) { |
| 107 | + slen = (slen << 8) + input[pos]; |
| 108 | + pos++; |
| 109 | + lenbyte--; |
| 110 | + } |
| 111 | + } else { |
| 112 | + slen = lenbyte; |
| 113 | + } |
| 114 | + if (slen > inputlen - pos) { |
| 115 | + return 0; |
| 116 | + } |
| 117 | + spos = pos; |
| 118 | + pos += slen; |
| 119 | + |
| 120 | + /* Ignore leading zeroes in R */ |
| 121 | + while (rlen > 0 && input[rpos] == 0) { |
| 122 | + rlen--; |
| 123 | + rpos++; |
| 124 | + } |
| 125 | + /* Copy R value */ |
| 126 | + if (rlen > 32) { |
| 127 | + overflow = 1; |
| 128 | + } else { |
| 129 | + memcpy(tmpsig + 32 - rlen, input + rpos, rlen); |
| 130 | + } |
| 131 | + |
| 132 | + /* Ignore leading zeroes in S */ |
| 133 | + while (slen > 0 && input[spos] == 0) { |
| 134 | + slen--; |
| 135 | + spos++; |
| 136 | + } |
| 137 | + /* Copy S value */ |
| 138 | + if (slen > 32) { |
| 139 | + overflow = 1; |
| 140 | + } else { |
| 141 | + memcpy(tmpsig + 64 - slen, input + spos, slen); |
| 142 | + } |
| 143 | + |
| 144 | + if (!overflow) { |
| 145 | + overflow = !secp256k1_ecdsa_signature_parse_compact(ctx, sig, tmpsig); |
| 146 | + } |
| 147 | + if (overflow) { |
| 148 | + /* Overwrite the result again with a correctly-parsed but invalid |
| 149 | + signature if parsing failed. */ |
| 150 | + memset(tmpsig, 0, 64); |
| 151 | + secp256k1_ecdsa_signature_parse_compact(ctx, sig, tmpsig); |
| 152 | + } |
| 153 | + return 1; |
| 154 | +} |
0 commit comments