Ugochukwu Mmaduekwe 2 недель назад
Родитель
Сommit
ee683a60a3

+ 4 - 0
CryptoLib.Tests/Delphi.Tests/CryptoLib.Tests.dpr

@@ -509,6 +509,10 @@ uses
   ClpX9IntegerConverter in '..\..\CryptoLib\src\Asn1\X9\ClpX9IntegerConverter.pas',
   ClpPkcsRsaAsn1Objects in '..\..\CryptoLib\src\Asn1\Pkcs\ClpPkcsRsaAsn1Objects.pas',
   ClpIPkcsRsaAsn1Objects in '..\..\CryptoLib\src\Interfaces\Asn1\Pkcs\ClpIPkcsRsaAsn1Objects.pas',
+  ClpIX509DsaAsn1Objects in '..\..\CryptoLib\src\Interfaces\Asn1\X509\ClpIX509DsaAsn1Objects.pas',
+  ClpIX509RsaAsn1Objects in '..\..\CryptoLib\src\Interfaces\Asn1\X509\ClpIX509RsaAsn1Objects.pas',
+  ClpX509DsaAsn1Objects in '..\..\CryptoLib\src\Asn1\X509\ClpX509DsaAsn1Objects.pas',
+  ClpX509RsaAsn1Objects in '..\..\CryptoLib\src\Asn1\X509\ClpX509RsaAsn1Objects.pas',
   ClpFixedSecureRandom in '..\src\Utils\ClpFixedSecureRandom.pas',
   ClpIFixedSecureRandom in '..\src\Utils\ClpIFixedSecureRandom.pas',
   ClpIShortenedDigest in '..\src\Utils\ClpIShortenedDigest.pas',

+ 2 - 0
CryptoLib.Tests/src/Crypto/DSATests.pas

@@ -76,6 +76,8 @@ uses
   ClpAsn1Objects,
   ClpDSADigestSigner,
   ClpIDSADigestSigner,
+  ClpX509DsaAsn1Objects,
+  ClpIX509DsaAsn1Objects,
   ClpISecureRandom,
   ClpSecureRandom,
   ClpFixedSecureRandom,

+ 0 - 277
CryptoLib/src/Asn1/X509/ClpX509Asn1Objects.pas

@@ -1057,42 +1057,6 @@ type
 
   end;
 
-  /// <summary>
-  /// The RsaPublicKeyStructure object.
-  /// </summary>
-  TRsaPublicKeyStructure = class(TAsn1Encodable, IRsaPublicKeyStructure)
-
-  strict private
-  var
-    FModulus: TBigInteger;
-    FPublicExponent: TBigInteger;
-
-  strict protected
-    function GetModulus: TBigInteger;
-    function GetPublicExponent: TBigInteger;
-
-  public
-    class function GetInstance(AObj: TObject): IRsaPublicKeyStructure; overload; static;
-    /// <summary>
-    /// Get instance from ASN.1 convertible.
-    /// </summary>
-    class function GetInstance(const AObj: IAsn1Convertible): IRsaPublicKeyStructure; overload; static;
-    class function GetInstance(const AEncoded: TCryptoLibByteArray): IRsaPublicKeyStructure; overload; static;
-    class function GetInstance(const AObj: IAsn1TaggedObject;
-      AExplicitly: Boolean): IRsaPublicKeyStructure; overload; static;
-    class function GetTagged(const ATaggedObject: IAsn1TaggedObject;
-      ADeclaredExplicit: Boolean): IRsaPublicKeyStructure; static;
-
-    constructor Create(const ASeq: IAsn1Sequence); overload;
-    constructor Create(const AModulus, APublicExponent: TBigInteger); overload;
-
-    function ToAsn1Object: IAsn1Object; override;
-
-    property Modulus: TBigInteger read GetModulus;
-    property PublicExponent: TBigInteger read GetPublicExponent;
-
-  end;
-
   /// <summary>
   /// The SubjectAltPublicKeyInfo object.
   /// </summary>
@@ -1630,53 +1594,6 @@ type
 
   end;
 
-  /// <summary>
-  /// The DsaParameter object.
-  /// </summary>
-  TDsaParameter = class(TAsn1Encodable, IDsaParameter)
-
-  strict private
-  var
-    FP, FQ, FG: IDerInteger;
-
-    function GetG: TBigInteger; inline;
-    function GetP: TBigInteger; inline;
-    function GetQ: TBigInteger; inline;
-
-    constructor Create(const ASeq: IAsn1Sequence); overload;
-
-  public
-    constructor Create(const AP, AQ, AG: TBigInteger); overload;
-
-    function ToAsn1Object(): IAsn1Object; override;
-
-    property P: TBigInteger read GetP;
-    property Q: TBigInteger read GetQ;
-    property G: TBigInteger read GetG;
-
-    /// <summary>
-    /// Parse a DsaParameter from an object.
-    /// </summary>
-    class function GetInstance(AObj: TObject): IDsaParameter; overload; static;
-    /// <summary>
-    /// Get instance from ASN.1 convertible.
-    /// </summary>
-    class function GetInstance(const AObj: IAsn1Convertible): IDsaParameter; overload; static;
-    /// <summary>
-    /// Parse a DsaParameter from DER-encoded bytes.
-    /// </summary>
-    class function GetInstance(const AEncoded: TCryptoLibByteArray): IDsaParameter; overload; static;
-    /// <summary>
-    /// Parse a DsaParameter from a tagged object.
-    /// </summary>
-    class function GetInstance(const AObj: IAsn1TaggedObject; AExplicitly: Boolean): IDsaParameter; overload; static;
-    /// <summary>
-    /// Get tagged DsaParameter.
-    /// </summary>
-    class function GetTagged(const ATaggedObject: IAsn1TaggedObject; ADeclaredExplicit: Boolean): IDsaParameter; static;
-
-  end;
-
   /// <summary>
   /// The AttCertIssuer object.
   /// </summary>
@@ -3170,107 +3087,6 @@ begin
   Result := TDerSequence.Create([FAlgorithm, FPublicKey]);
 end;
 
-{ TRsaPublicKeyStructure }
-
-class function TRsaPublicKeyStructure.GetInstance(AObj: TObject): IRsaPublicKeyStructure;
-begin
-  if AObj = nil then
-  begin
-    Result := nil;
-    Exit;
-  end;
-
-  if Supports(AObj, IRsaPublicKeyStructure, Result) then
-    Exit;
-
-  Result := TRsaPublicKeyStructure.Create(TAsn1Sequence.GetInstance(AObj));
-end;
-
-class function TRsaPublicKeyStructure.GetInstance(const AObj: IAsn1Convertible): IRsaPublicKeyStructure;
-begin
-  if AObj = nil then
-  begin
-    Result := nil;
-    Exit;
-  end;
-
-  if Supports(AObj, IRsaPublicKeyStructure, Result) then
-    Exit;
-
-  Result := TRsaPublicKeyStructure.Create(TAsn1Sequence.GetInstance(AObj));
-end;
-
-class function TRsaPublicKeyStructure.GetInstance(const AEncoded: TCryptoLibByteArray): IRsaPublicKeyStructure;
-begin
-  Result := TRsaPublicKeyStructure.Create(TAsn1Sequence.GetInstance(AEncoded));
-end;
-
-
-class function TRsaPublicKeyStructure.GetInstance(const AObj: IAsn1TaggedObject;
-  AExplicitly: Boolean): IRsaPublicKeyStructure;
-begin
-  Result := TRsaPublicKeyStructure.Create(TAsn1Sequence.GetInstance(AObj, AExplicitly));
-end;
-
-class function TRsaPublicKeyStructure.GetTagged(const ATaggedObject: IAsn1TaggedObject;
-  ADeclaredExplicit: Boolean): IRsaPublicKeyStructure;
-begin
-  Result := TRsaPublicKeyStructure.Create(TAsn1Sequence.GetTagged(ATaggedObject, ADeclaredExplicit));
-end;
-
-constructor TRsaPublicKeyStructure.Create(const ASeq: IAsn1Sequence);
-var
-  LCount: Int32;
-begin
-  inherited Create();
-
-  LCount := ASeq.Count;
-  if LCount <> 2 then
-  begin
-    raise EArgumentCryptoLibException.CreateResFmt(@SBadSequenceSize, [LCount]);
-  end;
-
-  // Note: we are accepting technically incorrect (i.e. negative) values here
-  FModulus := TDerInteger.GetInstance(ASeq[0]).PositiveValue;
-  FPublicExponent := TDerInteger.GetInstance(ASeq[1]).PositiveValue;
-end;
-
-constructor TRsaPublicKeyStructure.Create(const AModulus, APublicExponent: TBigInteger);
-begin
-  inherited Create();
-
-  if AModulus.IsInitialized = False then
-    raise EArgumentNilCryptoLibException.Create('modulus');
-  if APublicExponent.IsInitialized = False then
-    raise EArgumentNilCryptoLibException.Create('publicExponent');
-  if AModulus.SignValue <= 0 then
-    raise EArgumentCryptoLibException.Create(SNotCA);
-  if APublicExponent.SignValue <= 0 then
-    raise EArgumentCryptoLibException.Create(SNotValidPublicExponent);
-
-  FModulus := AModulus;
-  FPublicExponent := APublicExponent;
-end;
-
-function TRsaPublicKeyStructure.GetModulus: TBigInteger;
-begin
-  Result := FModulus;
-end;
-
-function TRsaPublicKeyStructure.GetPublicExponent: TBigInteger;
-begin
-  Result := FPublicExponent;
-end;
-
-function TRsaPublicKeyStructure.ToAsn1Object: IAsn1Object;
-var
-  LModulus, LExponent: IDerInteger;
-begin
-  LModulus := TDerInteger.Create(FModulus);
-  LExponent := TDerInteger.Create(FPublicExponent);
-  Result := TDerSequence.Create([LModulus, LExponent]);
-end;
-
 { TSubjectAltPublicKeyInfo }
 
 class function TSubjectAltPublicKeyInfo.GetInstance(AObj: TObject): ISubjectAltPublicKeyInfo;
@@ -7133,99 +6949,6 @@ begin
   end;
 end;
 
-{ TDsaParameter }
-
-function TDsaParameter.GetP: TBigInteger;
-begin
-  result := FP.PositiveValue;
-end;
-
-function TDsaParameter.GetQ: TBigInteger;
-begin
-  result := FQ.PositiveValue;
-end;
-
-function TDsaParameter.GetG: TBigInteger;
-begin
-  result := FG.PositiveValue;
-end;
-
-function TDsaParameter.ToAsn1Object: IAsn1Object;
-begin
-  result := TDerSequence.Create([FP, FQ, FG]);
-end;
-
-constructor TDsaParameter.Create(const ASeq: IAsn1Sequence);
-begin
-  Inherited Create();
-  if (ASeq.Count <> 3) then
-  begin
-    raise EArgumentCryptoLibException.CreateResFmt(@SBadSequenceSize,
-      [ASeq.Count]);
-  end;
-
-  FP := TDerInteger.GetInstance(ASeq[0]);
-  FQ := TDerInteger.GetInstance(ASeq[1]);
-  FG := TDerInteger.GetInstance(ASeq[2]);
-end;
-
-constructor TDsaParameter.Create(const AP, AQ, AG: TBigInteger);
-begin
-  Inherited Create();
-  FP := TDerInteger.Create(AP);
-  FQ := TDerInteger.Create(AQ);
-  FG := TDerInteger.Create(AG);
-end;
-
-class function TDsaParameter.GetInstance(AObj: TObject): IDsaParameter;
-begin
-  if AObj = nil then
-  begin
-    Result := nil;
-    Exit;
-  end;
-
-  if Supports(AObj, IDsaParameter, Result) then
-    Exit;
-
-  Result := TDsaParameter.Create(TAsn1Sequence.GetInstance(AObj));
-end;
-
-class function TDsaParameter.GetInstance(const AObj: IAsn1Convertible): IDsaParameter;
-begin
-  if AObj = nil then
-  begin
-    Result := nil;
-    Exit;
-  end;
-
-  if Supports(AObj, IDsaParameter, Result) then
-    Exit;
-
-  Result := TDsaParameter.Create(TAsn1Sequence.GetInstance(AObj));
-end;
-
-class function TDsaParameter.GetInstance(const AEncoded: TCryptoLibByteArray): IDsaParameter;
-begin
-  if AEncoded = nil then
-  begin
-    Result := nil;
-    Exit;
-  end;
-
-  Result := TDsaParameter.Create(TAsn1Sequence.GetInstance(AEncoded));
-end;
-
-class function TDsaParameter.GetInstance(const AObj: IAsn1TaggedObject; AExplicitly: Boolean): IDsaParameter;
-begin
-  Result := TDsaParameter.Create(TAsn1Sequence.GetInstance(AObj, AExplicitly));
-end;
-
-class function TDsaParameter.GetTagged(const ATaggedObject: IAsn1TaggedObject; ADeclaredExplicit: Boolean): IDsaParameter;
-begin
-  Result := TDsaParameter.Create(TAsn1Sequence.GetTagged(ATaggedObject, ADeclaredExplicit));
-end;
-
 { TDistributionPointName }
 
 class function TDistributionPointName.GetInstance(AObj: TObject): IDistributionPointName;

+ 180 - 0
CryptoLib/src/Asn1/X509/ClpX509DsaAsn1Objects.pas

@@ -0,0 +1,180 @@
+{ *********************************************************************************** }
+{ *                              CryptoLib Library                                  * }
+{ *                Copyright (c) 2018 - 20XX Ugochukwu Mmaduekwe                    * }
+{ *                 Github Repository <https://github.com/Xor-el>                   * }
+
+{ *  Distributed under the MIT software license, see the accompanying file LICENSE  * }
+{ *          or visit http://www.opensource.org/licenses/mit-license.php.           * }
+
+{ *                              Acknowledgements:                                  * }
+{ *                                                                                 * }
+{ *      Thanks to Sphere 10 Software (http://www.sphere10.com/) for sponsoring     * }
+{ *                           development of this library                           * }
+
+{ * ******************************************************************************* * }
+
+(* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *)
+
+unit ClpX509DsaAsn1Objects;
+
+{$I ..\..\Include\CryptoLib.inc}
+
+interface
+
+uses
+  SysUtils,
+  ClpBigInteger,
+  ClpAsn1Objects,
+  ClpIAsn1Objects,
+  ClpAsn1Core,
+  ClpIAsn1Core,
+  ClpIX509DsaAsn1Objects,
+  ClpCryptoLibTypes;
+
+resourcestring
+  SBadSequenceSize = 'Bad sequence size: %d';
+
+type
+  /// <summary>
+  /// The DsaParameter object.
+  /// </summary>
+  TDsaParameter = class(TAsn1Encodable, IDsaParameter)
+
+  strict private
+  var
+    FP, FQ, FG: IDerInteger;
+
+    function GetG: TBigInteger; inline;
+    function GetP: TBigInteger; inline;
+    function GetQ: TBigInteger; inline;
+
+    constructor Create(const ASeq: IAsn1Sequence); overload;
+
+  public
+    constructor Create(const AP, AQ, AG: TBigInteger); overload;
+
+    function ToAsn1Object(): IAsn1Object; override;
+
+    property P: TBigInteger read GetP;
+    property Q: TBigInteger read GetQ;
+    property G: TBigInteger read GetG;
+
+    /// <summary>
+    /// Parse a DsaParameter from an object.
+    /// </summary>
+    class function GetInstance(AObj: TObject): IDsaParameter; overload; static;
+    /// <summary>
+    /// Get instance from ASN.1 convertible.
+    /// </summary>
+    class function GetInstance(const AObj: IAsn1Convertible): IDsaParameter; overload; static;
+    /// <summary>
+    /// Parse a DsaParameter from DER-encoded bytes.
+    /// </summary>
+    class function GetInstance(const AEncoded: TCryptoLibByteArray): IDsaParameter; overload; static;
+    /// <summary>
+    /// Parse a DsaParameter from a tagged object.
+    /// </summary>
+    class function GetInstance(const AObj: IAsn1TaggedObject; AExplicitly: Boolean): IDsaParameter; overload; static;
+    /// <summary>
+    /// Get tagged DsaParameter.
+    /// </summary>
+    class function GetTagged(const ATaggedObject: IAsn1TaggedObject; ADeclaredExplicit: Boolean): IDsaParameter; static;
+
+  end;
+
+implementation
+
+{ TDsaParameter }
+
+function TDsaParameter.GetP: TBigInteger;
+begin
+  result := FP.PositiveValue;
+end;
+
+function TDsaParameter.GetQ: TBigInteger;
+begin
+  result := FQ.PositiveValue;
+end;
+
+function TDsaParameter.GetG: TBigInteger;
+begin
+  result := FG.PositiveValue;
+end;
+
+function TDsaParameter.ToAsn1Object: IAsn1Object;
+begin
+  result := TDerSequence.Create([FP, FQ, FG]);
+end;
+
+constructor TDsaParameter.Create(const ASeq: IAsn1Sequence);
+begin
+  Inherited Create();
+  if (ASeq.Count <> 3) then
+  begin
+    raise EArgumentCryptoLibException.CreateResFmt(@SBadSequenceSize,
+      [ASeq.Count]);
+  end;
+
+  FP := TDerInteger.GetInstance(ASeq[0]);
+  FQ := TDerInteger.GetInstance(ASeq[1]);
+  FG := TDerInteger.GetInstance(ASeq[2]);
+end;
+
+constructor TDsaParameter.Create(const AP, AQ, AG: TBigInteger);
+begin
+  Inherited Create();
+  FP := TDerInteger.Create(AP);
+  FQ := TDerInteger.Create(AQ);
+  FG := TDerInteger.Create(AG);
+end;
+
+class function TDsaParameter.GetInstance(AObj: TObject): IDsaParameter;
+begin
+  if AObj = nil then
+  begin
+    Result := nil;
+    Exit;
+  end;
+
+  if Supports(AObj, IDsaParameter, Result) then
+    Exit;
+
+  Result := TDsaParameter.Create(TAsn1Sequence.GetInstance(AObj));
+end;
+
+class function TDsaParameter.GetInstance(const AObj: IAsn1Convertible): IDsaParameter;
+begin
+  if AObj = nil then
+  begin
+    Result := nil;
+    Exit;
+  end;
+
+  if Supports(AObj, IDsaParameter, Result) then
+    Exit;
+
+  Result := TDsaParameter.Create(TAsn1Sequence.GetInstance(AObj));
+end;
+
+class function TDsaParameter.GetInstance(const AEncoded: TCryptoLibByteArray): IDsaParameter;
+begin
+  if AEncoded = nil then
+  begin
+    Result := nil;
+    Exit;
+  end;
+
+  Result := TDsaParameter.Create(TAsn1Sequence.GetInstance(AEncoded));
+end;
+
+class function TDsaParameter.GetInstance(const AObj: IAsn1TaggedObject; AExplicitly: Boolean): IDsaParameter;
+begin
+  Result := TDsaParameter.Create(TAsn1Sequence.GetInstance(AObj, AExplicitly));
+end;
+
+class function TDsaParameter.GetTagged(const ATaggedObject: IAsn1TaggedObject; ADeclaredExplicit: Boolean): IDsaParameter;
+begin
+  Result := TDsaParameter.Create(TAsn1Sequence.GetTagged(ATaggedObject, ADeclaredExplicit));
+end;
+
+end.

+ 178 - 0
CryptoLib/src/Asn1/X509/ClpX509RsaAsn1Objects.pas

@@ -0,0 +1,178 @@
+{ *********************************************************************************** }
+{ *                              CryptoLib Library                                  * }
+{ *                Copyright (c) 2018 - 20XX Ugochukwu Mmaduekwe                    * }
+{ *                 Github Repository <https://github.com/Xor-el>                   * }
+
+{ *  Distributed under the MIT software license, see the accompanying file LICENSE  * }
+{ *          or visit http://www.opensource.org/licenses/mit-license.php.           * }
+
+{ *                              Acknowledgements:                                  * }
+{ *                                                                                 * }
+{ *      Thanks to Sphere 10 Software (http://www.sphere10.com/) for sponsoring     * }
+{ *                           development of this library                           * }
+
+{ * ******************************************************************************* * }
+
+(* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *)
+
+unit ClpX509RsaAsn1Objects;
+
+{$I ..\..\Include\CryptoLib.inc}
+
+interface
+
+uses
+  SysUtils,
+  ClpBigInteger,
+  ClpAsn1Objects,
+  ClpIAsn1Objects,
+  ClpAsn1Core,
+  ClpIAsn1Core,
+  ClpIX509RsaAsn1Objects,
+  ClpCryptoLibTypes;
+
+resourcestring
+  SBadSequenceSize = 'Bad sequence size: %d';
+  SNotCA = 'Not a valid RSA modulus';
+  SNotValidPublicExponent = 'Not a valid RSA public exponent';
+
+type
+  /// <summary>
+  /// The RsaPublicKeyStructure object.
+  /// </summary>
+  TRsaPublicKeyStructure = class(TAsn1Encodable, IRsaPublicKeyStructure)
+
+  strict private
+  var
+    FModulus: TBigInteger;
+    FPublicExponent: TBigInteger;
+
+  strict protected
+    function GetModulus: TBigInteger;
+    function GetPublicExponent: TBigInteger;
+
+  public
+    class function GetInstance(AObj: TObject): IRsaPublicKeyStructure; overload; static;
+    /// <summary>
+    /// Get instance from ASN.1 convertible.
+    /// </summary>
+    class function GetInstance(const AObj: IAsn1Convertible): IRsaPublicKeyStructure; overload; static;
+    class function GetInstance(const AEncoded: TCryptoLibByteArray): IRsaPublicKeyStructure; overload; static;
+    class function GetInstance(const AObj: IAsn1TaggedObject;
+      AExplicitly: Boolean): IRsaPublicKeyStructure; overload; static;
+    class function GetTagged(const ATaggedObject: IAsn1TaggedObject;
+      ADeclaredExplicit: Boolean): IRsaPublicKeyStructure; static;
+
+    constructor Create(const ASeq: IAsn1Sequence); overload;
+    constructor Create(const AModulus, APublicExponent: TBigInteger); overload;
+
+    function ToAsn1Object: IAsn1Object; override;
+
+    property Modulus: TBigInteger read GetModulus;
+    property PublicExponent: TBigInteger read GetPublicExponent;
+
+  end;
+
+implementation
+
+{ TRsaPublicKeyStructure }
+
+class function TRsaPublicKeyStructure.GetInstance(AObj: TObject): IRsaPublicKeyStructure;
+begin
+  if AObj = nil then
+  begin
+    Result := nil;
+    Exit;
+  end;
+
+  if Supports(AObj, IRsaPublicKeyStructure, Result) then
+    Exit;
+
+  Result := TRsaPublicKeyStructure.Create(TAsn1Sequence.GetInstance(AObj));
+end;
+
+class function TRsaPublicKeyStructure.GetInstance(const AObj: IAsn1Convertible): IRsaPublicKeyStructure;
+begin
+  if AObj = nil then
+  begin
+    Result := nil;
+    Exit;
+  end;
+
+  if Supports(AObj, IRsaPublicKeyStructure, Result) then
+    Exit;
+
+  Result := TRsaPublicKeyStructure.Create(TAsn1Sequence.GetInstance(AObj));
+end;
+
+class function TRsaPublicKeyStructure.GetInstance(const AEncoded: TCryptoLibByteArray): IRsaPublicKeyStructure;
+begin
+  Result := TRsaPublicKeyStructure.Create(TAsn1Sequence.GetInstance(AEncoded));
+end;
+
+class function TRsaPublicKeyStructure.GetInstance(const AObj: IAsn1TaggedObject;
+  AExplicitly: Boolean): IRsaPublicKeyStructure;
+begin
+  Result := TRsaPublicKeyStructure.Create(TAsn1Sequence.GetInstance(AObj, AExplicitly));
+end;
+
+class function TRsaPublicKeyStructure.GetTagged(const ATaggedObject: IAsn1TaggedObject;
+  ADeclaredExplicit: Boolean): IRsaPublicKeyStructure;
+begin
+  Result := TRsaPublicKeyStructure.Create(TAsn1Sequence.GetTagged(ATaggedObject, ADeclaredExplicit));
+end;
+
+constructor TRsaPublicKeyStructure.Create(const ASeq: IAsn1Sequence);
+var
+  LCount: Int32;
+begin
+  inherited Create();
+
+  LCount := ASeq.Count;
+  if LCount <> 2 then
+  begin
+    raise EArgumentCryptoLibException.CreateResFmt(@SBadSequenceSize, [LCount]);
+  end;
+
+  // Note: we are accepting technically incorrect (i.e. negative) values here
+  FModulus := TDerInteger.GetInstance(ASeq[0]).PositiveValue;
+  FPublicExponent := TDerInteger.GetInstance(ASeq[1]).PositiveValue;
+end;
+
+constructor TRsaPublicKeyStructure.Create(const AModulus, APublicExponent: TBigInteger);
+begin
+  inherited Create();
+
+  if AModulus.IsInitialized = False then
+    raise EArgumentNilCryptoLibException.Create('modulus');
+  if APublicExponent.IsInitialized = False then
+    raise EArgumentNilCryptoLibException.Create('publicExponent');
+  if AModulus.SignValue <= 0 then
+    raise EArgumentCryptoLibException.Create(SNotCA);
+  if APublicExponent.SignValue <= 0 then
+    raise EArgumentCryptoLibException.Create(SNotValidPublicExponent);
+
+  FModulus := AModulus;
+  FPublicExponent := APublicExponent;
+end;
+
+function TRsaPublicKeyStructure.GetModulus: TBigInteger;
+begin
+  Result := FModulus;
+end;
+
+function TRsaPublicKeyStructure.GetPublicExponent: TBigInteger;
+begin
+  Result := FPublicExponent;
+end;
+
+function TRsaPublicKeyStructure.ToAsn1Object: IAsn1Object;
+var
+  LModulus, LExponent: IDerInteger;
+begin
+  LModulus := TDerInteger.Create(FModulus);
+  LExponent := TDerInteger.Create(FPublicExponent);
+  Result := TDerSequence.Create([LModulus, LExponent]);
+end;
+
+end.

+ 2 - 0
CryptoLib/src/Factories/ClpPrivateKeyFactory.pas

@@ -45,7 +45,9 @@ uses
   ClpIECDomainParameters,
   ClpIDsaParameters,
   ClpIX509Asn1Objects,
+  ClpIX509DsaAsn1Objects,
   ClpX509Asn1Objects,
+  ClpX509DsaAsn1Objects,
   ClpSecECAsn1Objects,
   ClpISecECAsn1Objects,
   ClpBigInteger,

+ 4 - 0
CryptoLib/src/Factories/ClpPublicKeyFactory.pas

@@ -41,7 +41,11 @@ uses
   ClpIECDomainParameters,
   ClpIDsaParameters,
   ClpIX509Asn1Objects,
+  ClpIX509RsaAsn1Objects,
+  ClpIX509DsaAsn1Objects,
   ClpX509Asn1Objects,
+  ClpX509RsaAsn1Objects,
+  ClpX509DsaAsn1Objects,
   ClpCryptoLibTypes;
 
 type

+ 4 - 0
CryptoLib/src/Factories/ClpSubjectPublicKeyInfoFactory.pas

@@ -37,7 +37,11 @@ uses
   ClpEdECObjectIdentifiers,
   ClpIDsaParameters,
   ClpIX509Asn1Objects,
+  ClpIX509RsaAsn1Objects,
+  ClpIX509DsaAsn1Objects,
   ClpX509Asn1Objects,
+  ClpX509RsaAsn1Objects,
+  ClpX509DsaAsn1Objects,
   ClpX9ECAsn1Objects,
   ClpIX9ECAsn1Objects,
   ClpCryptoLibTypes;

+ 0 - 32
CryptoLib/src/Interfaces/Asn1/X509/ClpIX509Asn1Objects.pas

@@ -40,7 +40,6 @@ type
   IGeneralName = interface;
   IGeneralNames = interface;
   IKeyUsage = interface;
-  IRsaPublicKeyStructure = interface;
   ISubjectAltPublicKeyInfo = interface;
   ISubjectKeyIdentifier = interface;
   ISubjectPublicKeyInfo = interface;
@@ -53,7 +52,6 @@ type
   IIssuerSerial = interface;
   IObjectDigestInfo = interface;
   IDistributionPoint = interface;
-  IDsaParameter = interface;
   IV2Form = interface;
   IDistributionPointName = interface;
   IReasonFlags = interface;
@@ -202,19 +200,6 @@ type
     ['{A7B8C9D0-E1F2-3456-0123-456789ABCDEF}']
   end;
 
-  /// <summary>
-  /// Interface for RsaPublicKeyStructure.
-  /// </summary>
-  IRsaPublicKeyStructure = interface(IAsn1Encodable)
-    ['{B8C9D0E1-F2A3-4567-1234-56789ABCDEF0}']
-
-    function GetModulus: TBigInteger;
-    function GetPublicExponent: TBigInteger;
-
-    property Modulus: TBigInteger read GetModulus;
-    property PublicExponent: TBigInteger read GetPublicExponent;
-  end;
-
   /// <summary>
   /// Interface for SubjectAltPublicKeyInfo.
   /// </summary>
@@ -575,23 +560,6 @@ type
     property CrlIssuer: IGeneralNames read GetCrlIssuer;
   end;
 
-  /// <summary>
-  /// Interface for DsaParameter.
-  /// </summary>
-  IDsaParameter = interface(IAsn1Encodable)
-    ['{037E0113-0BD6-4A61-8BDC-DBEBE6136A6C}']
-
-    function GetP: TBigInteger;
-    property P: TBigInteger read GetP;
-
-    function GetG: TBigInteger;
-    property G: TBigInteger read GetG;
-
-    function GetQ: TBigInteger;
-    property Q: TBigInteger read GetQ;
-
-  end;
-
   /// <summary>
   /// Interface for DistributionPointName.
   /// </summary>

+ 50 - 0
CryptoLib/src/Interfaces/Asn1/X509/ClpIX509DsaAsn1Objects.pas

@@ -0,0 +1,50 @@
+{ *********************************************************************************** }
+{ *                              CryptoLib Library                                  * }
+{ *                Copyright (c) 2018 - 20XX Ugochukwu Mmaduekwe                    * }
+{ *                 Github Repository <https://github.com/Xor-el>                   * }
+
+{ *  Distributed under the MIT software license, see the accompanying file LICENSE  * }
+{ *          or visit http://www.opensource.org/licenses/mit-license.php.           * }
+
+{ *                              Acknowledgements:                                  * }
+{ *                                                                                 * }
+{ *      Thanks to Sphere 10 Software (http://www.sphere10.com/) for sponsoring     * }
+{ *                           development of this library                           * }
+
+{ * ******************************************************************************* * }
+
+(* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *)
+
+unit ClpIX509DsaAsn1Objects;
+
+{$I ..\..\..\Include\CryptoLib.inc}
+
+interface
+
+uses
+  ClpIAsn1Objects,
+  ClpIAsn1Core,
+  ClpBigInteger,
+  ClpCryptoLibTypes;
+
+type
+  /// <summary>
+  /// Interface for DsaParameter.
+  /// </summary>
+  IDsaParameter = interface(IAsn1Encodable)
+    ['{037E0113-0BD6-4A61-8BDC-DBEBE6136A6C}']
+
+    function GetP: TBigInteger;
+    property P: TBigInteger read GetP;
+
+    function GetG: TBigInteger;
+    property G: TBigInteger read GetG;
+
+    function GetQ: TBigInteger;
+    property Q: TBigInteger read GetQ;
+
+  end;
+
+implementation
+
+end.

+ 46 - 0
CryptoLib/src/Interfaces/Asn1/X509/ClpIX509RsaAsn1Objects.pas

@@ -0,0 +1,46 @@
+{ *********************************************************************************** }
+{ *                              CryptoLib Library                                  * }
+{ *                Copyright (c) 2018 - 20XX Ugochukwu Mmaduekwe                    * }
+{ *                 Github Repository <https://github.com/Xor-el>                   * }
+
+{ *  Distributed under the MIT software license, see the accompanying file LICENSE  * }
+{ *          or visit http://www.opensource.org/licenses/mit-license.php.           * }
+
+{ *                              Acknowledgements:                                  * }
+{ *                                                                                 * }
+{ *      Thanks to Sphere 10 Software (http://www.sphere10.com/) for sponsoring     * }
+{ *                           development of this library                           * }
+
+{ * ******************************************************************************* * }
+
+(* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *)
+
+unit ClpIX509RsaAsn1Objects;
+
+{$I ..\..\..\Include\CryptoLib.inc}
+
+interface
+
+uses
+  ClpIAsn1Objects,
+  ClpIAsn1Core,
+  ClpBigInteger,
+  ClpCryptoLibTypes;
+
+type
+  /// <summary>
+  /// Interface for RsaPublicKeyStructure.
+  /// </summary>
+  IRsaPublicKeyStructure = interface(IAsn1Encodable)
+    ['{B8C9D0E1-F2A3-4567-1234-56789ABCDEF0}']
+
+    function GetModulus: TBigInteger;
+    function GetPublicExponent: TBigInteger;
+
+    property Modulus: TBigInteger read GetModulus;
+    property PublicExponent: TBigInteger read GetPublicExponent;
+  end;
+
+implementation
+
+end.