Wednesday, December 8, 2021
HomeBitcoinblockchain - Block 9 Transaction 1 Output 0 ScriptPubKey openssl validation fails

blockchain – Block 9 Transaction 1 Output 0 ScriptPubKey openssl validation fails


I'm trying to check the public key which lies in the ScriptPubKey of output #0 of transaction #1 bloc #9 with openssl. I'm using Python to create a DER file, and openssl to check it.

ScriptPubKey value is:

0411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3

My Python code to create the DER file is:

scriptPubKey = block9.transactions[0].outputs[0].scriptPubKey[1:-1]
x = scriptPubKey[1:1+32]
y = scriptPubKey[1+32:]
publicKey = bytearray ([0x03, len (scriptPubKey) + 1, 0x00]) + scriptPubKey
algorithmIdentifierId = b'x06x07x2ax86x48xcex3dx02x01'
algorithmIdentifierParameters = b'x06x08x2ax86x48xcex3dx03x01x07'
algorithmIdentifier = algorithmIdentifierId + algorithmIdentifierParameters
publicKey = bytearray ([0x30, len (algorithmIdentifier)]) + algorithmIdentifier + publicKey
publicKey = bytearray ([0x30, len (publicKey)]) + publicKey
file = open (f'public.der', 'wb')
file.write (publicKey)
file.close ()

After running this code, I get a public.der which I check using openssl. First; I check the ASN.1 syntax, which is correct:

$ openssl asn1parse -inform der -in public.der 
    0:d=0  hl=2 l=  89 cons: SEQUENCE          
    2:d=1  hl=2 l=  19 cons: SEQUENCE          
    4:d=2  hl=2 l=   7 prim: OBJECT            :id-ecPublicKey
   13:d=2  hl=2 l=   8 prim: OBJECT            :prime256v1
   23:d=1  hl=2 l=  66 prim: BIT STRING

Then I check the key itself, and this doesn't work:

$ openssl ec -pubin -inform der -in public.der -text -noout
read EC key
unable to load Key
140542543959360:error:1012606B:elliptic curve routines:EC_POINT_set_affine_coordinates:point is not on curve:../crypto/ec/ec_lib.c:812:
140542543959360:error:10098010:elliptic curve routines:o2i_ECPublicKey:EC lib:../crypto/ec/ec_asn1.c:1158:
140542543959360:error:100D708E:elliptic curve routines:eckey_pub_decode:decode error:../crypto/ec/ec_ameth.c:157:
140542543959360:error:0B09407D:x509 certificate routines:x509_pubkey_decode:public key decode error:../crypto/x509/x_pubkey.c:125:

This code relies on this post. It works well if I use the x and y coordinates which the author of the post gives as an example:

x = b'x81x01xECxE4x74x64xA6xEAxD7x0CxF6x9Ax6Ex2BxD3xD8x86x91xA3x26x2Dx22xCBxA4xF7x63x5ExAFxF2x66x80xA8'
y = b'xD8xA1x2BxA6x1Dx59x92x35xF6x7Dx9CxB4xD5x8Fx17x83xD3xCAx43xE7x8Fx0Ax5AxBAxA6x24x07x99x36xC0xC3xA9'
scriptPubKey = b'x04' + x + y
publicKey = bytearray ([0x03, len (scriptPubKey) + 1, 0x00]) + scriptPubKey
algorithmIdentifierId = b'x06x07x2ax86x48xcex3dx02x01'
algorithmIdentifierParameters = b'x06x08x2ax86x48xcex3dx03x01x07'
algorithmIdentifier = algorithmIdentifierId + algorithmIdentifierParameters
publicKey = bytearray ([0x30, len (algorithmIdentifier)]) + algorithmIdentifier + publicKey
publicKey = bytearray ([0x30, len (publicKey)]) + publicKey
file = open (f'public.der', 'wb')
file.write (publicKey)
file.close ()

No problemo for openssl here:

$ openssl ec -pubin -inform der -in public.der -text -noout
read EC key
Public-Key: (256 bit)
pub:
    04:81:01:ec:e4:74:64:a6:ea:d7:0c:f6:9a:6e:2b:
    d3:d8:86:91:a3:26:2d:22:cb:a4:f7:63:5e:af:f2:
    66:80:a8:d8:a1:2b:a6:1d:59:92:35:f6:7d:9c:b4:
    d5:8f:17:83:d3:ca:43:e7:8f:0a:5a:ba:a6:24:07:
    99:36:c0:c3:a9
ASN1 OID: prime256v1
NIST CURVE: P-256

May somebody tell me what I missed? I tried to inverse the byte order of x and y, and/or to exchange x and y, but I could not make it work with the x and y values from ScriptPubKey…

According to pages 73 to 74 of this book, x and y are supposed to be given in this order, and in big endian…

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments