Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1/***********************************************************************2* Copyright (c) 2015 Pieter Wuille *3* Distributed under the MIT software license, see the accompanying *4* file COPYING or https://www.opensource.org/licenses/mit-license.php.*5***********************************************************************/7/****8* Please do not link this file directly. It is not part of the libsecp256k19* project and does not promise any stability in its API, functionality or10* presence. Projects which use this code should instead copy this header11* and its accompanying .c file directly into their codebase.12****/14/* This file defines a function that parses DER with various errors and15* violations. This is not a part of the library itself, because the allowed16* violations are chosen arbitrarily and do not follow or establish any17* standard.18*19* In many places it matters that different implementations do not only accept20* the same set of valid signatures, but also reject the same set of signatures.21* The only means to accomplish that is by strictly obeying a standard, and not22* accepting anything else.23*24* Nonetheless, sometimes there is a need for compatibility with systems that25* use signatures which do not strictly obey DER. The snippet below shows how26* certain violations are easily supported. You may need to adapt it.27*28* Do not use this for new systems. Use well-defined DER or compact signatures29* instead if you have the choice (see secp256k1_ecdsa_signature_parse_der and30* secp256k1_ecdsa_signature_parse_compact).31*32* The supported violations are:33* - All numbers are parsed as nonnegative integers, even though X.609-020734* section 8.3.3 specifies that integers are always encoded as two's35* complement.36* - Integers can have length 0, even though section 8.3.1 says they can't.37* - Integers with overly long padding are accepted, violation section38* 8.3.2.39* - 127-byte long length descriptors are accepted, even though section40* 8.1.3.5.c says that they are not.41* - Trailing garbage data inside or after the signature is ignored.42* - The length descriptor of the sequence is ignored.43*44* Compared to for example OpenSSL, many violations are NOT supported:45* - Using overly long tag descriptors for the sequence or integers inside,46* violating section 8.1.2.2.47* - Encoding primitive integers as constructed values, violating section48* 8.3.1.49*/51#ifndef SECP256K1_CONTRIB_LAX_DER_PARSING_H52#define SECP256K1_CONTRIB_LAX_DER_PARSING_H54/* #include secp256k1.h only when it hasn't been included yet.55This enables this file to be #included directly in other project56files (such as tests.c) without the need to set an explicit -I flag,57which would be necessary to locate secp256k1.h. */58#ifndef SECP256K1_H59#include <secp256k1.h>60#endif62#ifdef __cplusplus63extern "C" {64#endif66/** Parse a signature in "lax DER" format67*68* Returns: 1 when the signature could be parsed, 0 otherwise.69* Args: ctx: a secp256k1 context object70* Out: sig: pointer to a signature object71* In: input: pointer to the signature to be parsed72* inputlen: the length of the array pointed to be input73*74* This function will accept any valid DER encoded signature, even if the75* encoded numbers are out of range. In addition, it will accept signatures76* which violate the DER spec in various ways. Its purpose is to allow77* validation of the Bitcoin blockchain, which includes non-DER signatures78* from before the network rules were updated to enforce DER. Note that79* the set of supported violations is a strict subset of what OpenSSL will80* accept.81*82* After the call, sig will always be initialized. If parsing failed or the83* encoded numbers are out of range, signature validation with it is84* guaranteed to fail for every message and public key.85*/86int ecdsa_signature_parse_der_lax(87const secp256k1_context* ctx,88secp256k1_ecdsa_signature* sig,89const unsigned char *input,90size_t inputlen91) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);93#ifdef __cplusplus94}95#endif97#endif /* SECP256K1_CONTRIB_LAX_DER_PARSING_H */
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.