Browse Source

UCrypto bug

Solved possible UCrypto bug on decoding private keys
PascalCoin 7 years ago
parent
commit
3992780877
2 changed files with 13 additions and 7 deletions
  1. 11 4
      src/core/UCrypto.pas
  2. 2 3
      src/core/UWallet.pas

+ 11 - 4
src/core/UCrypto.pas

@@ -234,10 +234,15 @@ begin
         try
         try
           Result := TECPrivateKey.Create;
           Result := TECPrivateKey.Create;
           Try
           Try
-            Result.SetPrivateKeyFromHexa(ECID,PAC);
+            If Not Result.SetPrivateKeyFromHexa(ECID,PAC) then begin
+              FreeAndNil(Result);
+            end;
           Except
           Except
-            FreeAndNil(Result);
-            Raise;
+            On E:Exception do begin
+              FreeAndNil(Result);
+              // Note: Will not raise Exception, only will log it
+              TLog.NewLog(lterror,ClassName,'Error importing private key from '+TCrypto.ToHexaString(raw)+' ECID:'+IntToStr(ECID)+' ('+E.ClassName+'): '+E.Message);
+            end;
           end;
           end;
         finally
         finally
           OpenSSL_free(PAC);
           OpenSSL_free(PAC);
@@ -296,6 +301,7 @@ var bn : PBIGNUM;
   ctx : PBN_CTX;
   ctx : PBN_CTX;
   pub_key : PEC_POINT;
   pub_key : PEC_POINT;
 begin
 begin
+  Result := False;
   bn := BN_new;
   bn := BN_new;
   try
   try
     if BN_hex2bn(@bn,PAnsiChar(hexa))=0 then Raise ECryptoException.Create('Invalid Hexadecimal value:'+hexa);
     if BN_hex2bn(@bn,PAnsiChar(hexa))=0 then Raise ECryptoException.Create('Invalid Hexadecimal value:'+hexa);
@@ -303,7 +309,7 @@ begin
     if Assigned(FPrivateKey) then EC_KEY_free(FPrivateKey);
     if Assigned(FPrivateKey) then EC_KEY_free(FPrivateKey);
     FEC_OpenSSL_NID := EC_OpenSSL_NID;
     FEC_OpenSSL_NID := EC_OpenSSL_NID;
     FPrivateKey := EC_KEY_new_by_curve_name(EC_OpenSSL_NID);
     FPrivateKey := EC_KEY_new_by_curve_name(EC_OpenSSL_NID);
-
+    If Not Assigned(FPrivateKey) then Exit;
     if EC_KEY_set_private_key(FPrivateKey,bn)<>1 then raise ECryptoException.Create('Invalid num to set as private key');
     if EC_KEY_set_private_key(FPrivateKey,bn)<>1 then raise ECryptoException.Create('Invalid num to set as private key');
     //
     //
     ctx := BN_CTX_new;
     ctx := BN_CTX_new;
@@ -318,6 +324,7 @@ begin
   finally
   finally
     BN_free(bn);
     BN_free(bn);
   end;
   end;
+  Result := True;
 end;
 end;
 
 
 { TCrypto }
 { TCrypto }

+ 2 - 3
src/core/UWallet.pas

@@ -762,7 +762,7 @@ begin
   decrypt := '';
   decrypt := '';
   If (TAESComp.EVP_Decrypt_AES256(TCrypto.HexaToRaw(AKeyText),AKeyPassword,decrypt)) AND (decrypt<>'') then begin
   If (TAESComp.EVP_Decrypt_AES256(TCrypto.HexaToRaw(AKeyText),AKeyPassword,decrypt)) AND (decrypt<>'') then begin
     AKey := TECPrivateKey.ImportFromRaw(decrypt);
     AKey := TECPrivateKey.ImportFromRaw(decrypt);
-    Result := true;
+    Result := Assigned(AKey);
   end else begin
   end else begin
     Result := false;
     Result := false;
   end;
   end;
@@ -777,8 +777,7 @@ class function TWallet.TryParseHexKey(const AHexString : AnsiString; AEncryption
 begin
 begin
   AKey := TECPrivateKey.Create;
   AKey := TECPrivateKey.Create;
   Try
   Try
-    AKey.SetPrivateKeyFromHexa(AEncryptionTypeNID, AHexString);
-    Result := True;
+    Result := AKey.SetPrivateKeyFromHexa(AEncryptionTypeNID, AHexString);
   Except
   Except
     On E:Exception do begin
     On E:Exception do begin
       FreeAndNil(AKey);
       FreeAndNil(AKey);