Decodes and checks signature; returns NULL on error. */
| 978 | |
| 979 | /* Decodes and checks signature; returns NULL on error. */ |
| 980 | struct bolt11 *bolt11_decode(const tal_t *ctx, const char *str, |
| 981 | const struct feature_set *our_features, |
| 982 | const char *description, |
| 983 | const struct chainparams *must_be_chain, |
| 984 | const char **fail) |
| 985 | { |
| 986 | const u5 *sigdata; |
| 987 | size_t data_len; |
| 988 | u8 sig_and_recid[65]; |
| 989 | secp256k1_ecdsa_recoverable_signature sig; |
| 990 | struct bolt11 *b11; |
| 991 | struct sha256 hash; |
| 992 | bool have_n; |
| 993 | const char *err; |
| 994 | |
| 995 | b11 = bolt11_decode_nosig(ctx, str, our_features, description, |
| 996 | must_be_chain, &hash, &sigdata, &have_n, |
| 997 | fail); |
| 998 | if (!b11) |
| 999 | return NULL; |
| 1000 | |
| 1001 | /* BOLT #11: |
| 1002 | * |
| 1003 | * A writer...MUST set `signature` to a valid |
| 1004 | * compact ECDSA signature over secp256k1 of the SHA-256 hash of: |
| 1005 | * the human-readable part (as UTF-8 bytes) concatenated with the data part |
| 1006 | * (excluding the signature), with 0 bits appended to pad to a byte boundary. |
| 1007 | * The signature is encoded as 64 bytes (R || S), followed by a trailing 1-byte |
| 1008 | * recovery id in {0,1,2,3}. |
| 1009 | */ |
| 1010 | data_len = tal_count(sigdata); |
| 1011 | err = pull_bits(NULL, &sigdata, &data_len, sig_and_recid, 520, false); |
| 1012 | if (err) |
| 1013 | return decode_fail(b11, fail, "can't read signature: %s", |
| 1014 | err); |
| 1015 | |
| 1016 | assert(data_len == 0); |
| 1017 | |
| 1018 | if (!valid_recovery_id(sig_and_recid[64])) |
| 1019 | return decode_fail(b11, fail, "invalid recovery ID: %u", |
| 1020 | sig_and_recid[64]); |
| 1021 | |
| 1022 | if (!secp256k1_ecdsa_recoverable_signature_parse_compact |
| 1023 | (secp256k1_ctx, &sig, sig_and_recid, sig_and_recid[64])) |
| 1024 | return decode_fail(b11, fail, "signature invalid"); |
| 1025 | |
| 1026 | secp256k1_ecdsa_recoverable_signature_convert(secp256k1_ctx, |
| 1027 | &b11->sig, &sig); |
| 1028 | |
| 1029 | /* BOLT #11: |
| 1030 | * |
| 1031 | * A reader: |
| 1032 | * - MUST check that the `signature` is valid (see the `n` tagged field specified below). |
| 1033 | */ |
| 1034 | /* BOLT #11: |
| 1035 | * |
| 1036 | * A reader: |
| 1037 | * ... |