Skip to content

Commit

Permalink
Merge pull request #6 from todoooo/Feature_CryptoServiceProvide_XML
Browse files Browse the repository at this point in the history
Code tidy up
  • Loading branch information
jarto authored Jan 18, 2019
2 parents 01a3606 + 238a34b commit 4a35af5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 22 deletions.
11 changes: 7 additions & 4 deletions source/LbBigInt.pas
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ TLbBigInt = class
function GetBase64Str : string;
procedure SetBase64Str(const Value: string);
procedure SetHexStr(const Value: string);
function GetASN1Text: String;
function GetASNTriplet: String;
protected {private}
FI : LbInteger;
procedure setSign(value : Boolean);
Expand Down Expand Up @@ -148,7 +148,7 @@ TLbBigInt = class
property IntStr : string read GetHexStr write SetHexStr;
property Size : integer read GetSize;
property Base64Str : string read GetBase64Str write SetBase64Str;
property ASN1Text : String read GetASN1Text;
property ASNTriplet : String read GetASNTriplet;

end;

Expand Down Expand Up @@ -2628,10 +2628,13 @@ procedure TLbBigInt.GCD(I2: TLbBigInt);
LbGreatestCommonDivisor(self, I2);
end;
{ ------------------------------------------------------------------- }
function TLbBigInt.GetASN1Text: String;
function TLbBigInt.GetASNTriplet: String;
const
INTEGER_TAG = '02';
var
ReversedBigInt : TLbBigInt;
begin
//the byte order in openSSL is reversed compared with lockbox
ReversedBigInt := TlbBigInt.Create(Size);
try
ReversedBigInt.Copy(self);
Expand All @@ -2642,7 +2645,7 @@ function TLbBigInt.GetASN1Text: String;
end;

ReversedBigInt.ReverseBytes(False); //don't trim the null byte
Result := ASN1HexSize(ReversedBigInt.Size) + ReversedBigInt.IntStr;
Result := INTEGER_TAG + ASN1HexSize(ReversedBigInt.Size) + ReversedBigInt.IntStr;
finally
ReversedBigInt.Free;
end;
Expand Down
26 changes: 8 additions & 18 deletions source/LbRSA.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1123,15 +1123,14 @@ function TLbRSA.GetCryptoServiceProviderXML(AIsForPrivateKey : Boolean): String;
Text : String;
ReversedBigInt, P1, Q1 : TLbBigInt;
begin
//the byte order in MS cryptography is reversed compared with lockbox
ReversedBigInt := TlbBigInt.Create(cLbAsymKeyBytes[FKeySize]);
try
ReversedBigInt.Copy(FPublicKey.Modulus);
ReversedBigInt.Trim;
ReversedBigInt.ReverseBytes;
Text := Format(XML_TAG, [RSA_MODULUS, ReversedBigInt.Base64Str]);

ReversedBigInt.Copy(FPublicKey.Exponent);
ReversedBigInt.Trim;
ReversedBigInt.ReverseBytes;
Text := Text + Format(XML_TAG, [RSA_PUBLIC_EXPONENT, ReversedBigInt.Base64Str]);

Expand All @@ -1143,12 +1142,10 @@ function TLbRSA.GetCryptoServiceProviderXML(AIsForPrivateKey : Boolean): String;
end;

ReversedBigInt.Copy(FFirstPrime);
ReversedBigInt.Trim;
ReversedBigInt.ReverseBytes;
Text := Text + Format(XML_TAG, [RSA_PRIME_ONE, ReversedBigInt.Base64Str]);

ReversedBigInt.Copy(FSecondPrime);
ReversedBigInt.Trim;
ReversedBigInt.ReverseBytes;
Text := Text + Format(XML_TAG, [RSA_PRIME_TWO, ReversedBigInt.Base64Str]);

Expand All @@ -1159,7 +1156,6 @@ function TLbRSA.GetCryptoServiceProviderXML(AIsForPrivateKey : Boolean): String;

ReversedBigInt.Copy(FPrivateKey.Exponent);
ReversedBigInt.Modulus(P1);
ReversedBigInt.Trim;
ReversedBigInt.ReverseBytes;
Text := Text + Format(XML_TAG, [RSA_D_MOD_PRIME_ONE, ReversedBigInt.Base64Str]);
finally
Expand All @@ -1173,7 +1169,6 @@ function TLbRSA.GetCryptoServiceProviderXML(AIsForPrivateKey : Boolean): String;

ReversedBigInt.Copy(FPrivateKey.Exponent);
ReversedBigInt.Modulus(Q1);
ReversedBigInt.Trim;
ReversedBigInt.ReverseBytes;
Text := Text + Format(XML_TAG, [RSA_D_MOD_PRIME_TWO, ReversedBigInt.Base64Str]);
finally
Expand All @@ -1182,12 +1177,10 @@ function TLbRSA.GetCryptoServiceProviderXML(AIsForPrivateKey : Boolean): String;

ReversedBigInt.Copy(FSecondPrime);
ReversedBigInt.ModInv(FFirstPrime);
ReversedBigInt.Trim;
ReversedBigInt.ReverseBytes;
Text := Text + Format(XML_TAG, [RSA_PRIME_TWO_INVERSE, ReversedBigInt.Base64Str]);

ReversedBigInt.Copy(FPrivateKey.Exponent);
ReversedBigInt.Trim;
ReversedBigInt.ReverseBytes;
Text := Text + Format(XML_TAG, [RSA_PRIVATE_EXPONENT, ReversedBigInt.Base64Str]);
end;
Expand All @@ -1202,27 +1195,24 @@ function TLbRSA.GetOpenSSLText(AIsForPrivateKey: Boolean): String;
const
PRIVACY_TO_TEXT : array[Boolean] of string = ('PUBLIC','PRIVATE');
SEQUENCE_TAG = '30';
INTEGER_TAG = '02';
BLOCK_FORMAT = '-----%s RSA %s KEY-----' + sLineBreak;
MAX_CHARACTERS_PER_LINE = 64;
var
Text, PrivacyText, HeaderLine, FooterLine, BlockText : String;
P1, Q1, DP, DQ, QInv, TempBigInt : TLbBigInt;
Index, LastIndex : Integer;
begin
Text := INTEGER_TAG + FPublicKey.Modulus.ASN1Text;
Text := Text + INTEGER_TAG + FPublicKey.Exponent.ASN1Text;
Text := FPublicKey.Modulus.ASNTriplet + FPublicKey.Exponent.ASNTriplet;
if AIsForPrivateKey then
begin
Text := Text + INTEGER_TAG + FPrivateKey.Exponent.ASN1Text;
Text := Text + FPrivateKey.Exponent.ASNTriplet;

if not CalculatePQ then
begin
raise Exception.Create('Cannot calculate prime numbers');
end;

Text := Text + INTEGER_TAG + FFirstPrime.ASN1Text;
Text := Text + INTEGER_TAG + FSecondPrime.ASN1Text;
Text := Text + FFirstPrime.ASNTriplet + FSecondPrime.ASNTriplet;

P1 := TLbBigInt.Create(FFirstPrime.Size);
try
Expand All @@ -1233,7 +1223,7 @@ function TLbRSA.GetOpenSSLText(AIsForPrivateKey: Boolean): String;
try
DP.Copy(FPrivateKey.Exponent);
DP.Modulus(P1);
Text := Text + INTEGER_TAG + DP.ASN1Text;
Text := Text + DP.ASNTriplet;
finally
DP.Free;
end;
Expand All @@ -1250,7 +1240,7 @@ function TLbRSA.GetOpenSSLText(AIsForPrivateKey: Boolean): String;
try
DQ.Copy(FPrivateKey.Exponent);
DQ.Modulus(Q1);
Text := Text + INTEGER_TAG + DQ.ASN1Text;
Text := Text + DQ.ASNTriplet;
finally
DQ.Free;
end;
Expand All @@ -1262,7 +1252,7 @@ function TLbRSA.GetOpenSSLText(AIsForPrivateKey: Boolean): String;
try
QInv.Copy(FSecondPrime);
QInv.ModInv(FFirstPrime);
Text := Text + INTEGER_TAG + QInv.ASN1Text;
Text := Text + QInv.ASNTriplet;
finally
QInv.Free;
end;
Expand All @@ -1274,7 +1264,7 @@ function TLbRSA.GetOpenSSLText(AIsForPrivateKey: Boolean): String;
try
//prepend version 0
TempBigInt.AppendByte(0);
Text := INTEGER_TAG + TempBigInt.ASN1Text + Text;
Text := TempBigInt.ASNTriplet + Text;
finally
TempBigInt.Free;
end;
Expand Down

0 comments on commit 4a35af5

Please sign in to comment.