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