Home > front end >  Error reading private key file using openssl lib
Error reading private key file using openssl lib

Time:11-05

I'm trying to read a private key in order to use later in signing some data. I'm stuck at reading this private key file (Bad Key File). I did read another private key file (Good Key File) with no errors. I am new to OpenSSL.

good Key File


-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,EB51377407AB034BA6C4554097E3454C

7pONu/34Ky4JYGGLYL1PqCSKl87JUvt/X5wMN4upEPPUb1rTmHHS3eHNKMoqHnfL
cAFGxdE38v/9YCO6jeuuyjXcLXt4KxWqY8cRH/gd1HwGJE1UR2w7Tb230J5y1jr6
skAiVfDB1ePfG5Vjn2D1jQj MKKAYExBn 00ZdMA21dUDn6lFFAP74qMy0D8FiHz
m29sDO7gyFvcapwugqfB/xG1MwjMWsY2ZO5D0XIlrh9N7yDY/cjHsFL86bD88AcB
Wwr3aXa Id2ymQ d KqeYYvXtnjfa1y/NX4J5GTuGYkQfBeidEsxbqIDkJc5yb4R
0VVefPZvhRbCVdrgR1l4AOM /jFwGO4nlhF0zrRYjA5Y63VjMhqzprWm n 7oEyv
aSmGEJ8nbG5FVZkPLfOD83bI7yZkJEJXitdBUYEA8eeclocQ80i9NJ4OzvJKxkMO
Fa9SIEA7BX1 n2dYVf6M7fFHSJTy3lnyaM/tPY2Z46nwWlbJosIUVm/MqqxjWBes
2Fx/qxLCCPoNJpbGMKfFtWj2PO jQizsxR1GJMYstfLwwruXE6EQxHUnDWNkycyd
GrjiR6ma6mYu/xfawtCHwSKyXqqHAi8849nY7z r12LZIouOV2RbQHOv/7rBKpHW
7I4de7stlmMkS22kbi sck1zJTALPvjhuaMqzcaIjVrQ4o75dNrDchKkxhmp 0zK
5 862LeW42hjfi4NP/Cdql4yQ8tppmbYBh0YjIvEVNywCooJEOculZb7EpXmut25
mzssKvNZgJoaRyHTc/JYFV9GmdjxxaLuzSC25g0lkG4/q BImkUeS sWQPFIUaFM
UOHKMKYfwUstI93oRHjh Og1DSOuCmFyVCMUqc vkugdSWPpaofrqZoIJY6GNfDz
g7IHB75AozSgO3tvQnPKMJYPe0S0KiTUolRqQ8D4yi2jizzaomvHRcHBOvmHcGuL
LkDkDc9iQh3HstV3UVgNdzyaYS0zXQj39Sy1rTLxcUz cPSTm5YyaN5jJyK8zfJi
PEqUoQ1/A4OkSCRjzlTlA1zxrrMxooBFbnYYnHOiSRdtaRKWn lgm7iFBDMpbZOt
pFez OTHvdPp2dWvdrxecL3tOHQ0cXV pG8JcqXvkKZdBjKrLYpCtX22PrscGLR3
8B2A6KiZJX1135Ce1wPQ/lEF5vZE rgZya0IvjtwAVX3jQYiSizq0zQzm7TkicbS
4BkK7FFmH rR9LYHpQFWTBI4C/DVfCag5oC8hTsaWh9TL6zndprRSjW7hCKlyviD
2H4FCJsEbg GOOmO60wK9WsZuly46jSlObkPgMiRRYZIHXWUkdav5K/1zgKsQ4Qy
HPp H/0iAOBZTV6fdQMNdPT1ssLig4m1Zcg1DwPIoMiZQ05ErlQ1 3i1u9TsXlMA
845SbcRFLDQMBcWmHARFpGhwtHXtaQU8w5kitazyXCnkXvFyKSWuJvCjZwiqIMQK
XYvibAqdk8QnZHBNXx32T3BAsjyPqcb2oo129sk3qaEMenmwFuyyVo ArJEV0YMX
WZZhB5c 3xMjQvMTA5c7l05tUs44q9WYQJhppPyxbU8kScOcw87nHOUbrCqqJr7Y
-----END RSA PRIVATE KEY-----

Bad Key File


-----BEGIN EC PRIVATE KEY-----
MIGNAgEAMBAGByqGSM49AgEGBSuBBAAKBHYwdAIBAQQgvuua2biCekB5C2oW/Ip3
Hi7YVb9nyaQp7GKpb8OomWOgBwYFK4EEAAqhRANCAATGVHYUkO49tiWNbhQHbmtC
l1mW9L0btglwvhld9zFwQTNHq3g07RM4s4 DTmiQWBPtrF6S8xJekFiSs9dN6zl9
-----END EC PRIVATE KEY-----


My Code

var
  bp: PBIO;
  PrivKeyFile, FCertPassword : AnsiString;
begin
  Result := False;
  PrivKeyFile := 'private.pem'; 
  FCertPassword := '123456789';
  bp := BIO_new_file(PAnsiChar(PrivKeyFile), 'r');
  if CheckError() then Exit;
  try
      FPrivateKeyData := PEM_read_bio_PrivateKey(bp, nil, nil, PAnsiChar(FCertPassword));
      if CheckError() then Exit;
 finally
      BIO_free(bp);
  end;
end;

Error message

error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag

CodePudding user response:

As dave-thompson-085 advised me, (Delete the EC from the header and trailer) and it is working now.

  • Related