Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CryptoLib.Benchmark/Delphi/CryptoLib.BenchmarkConsole.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ uses
ClpIX509TrustedCertificateBlock in '..\..\CryptoLib\src\Interfaces\OpenSsl\ClpIX509TrustedCertificateBlock.pas',
ClpCertificateTrustBlock in '..\..\CryptoLib\src\OpenSsl\ClpCertificateTrustBlock.pas',
ClpX509TrustedCertificateBlock in '..\..\CryptoLib\src\OpenSsl\ClpX509TrustedCertificateBlock.pas',
ClpPkixCertPathValidatorReason in '..\..\CryptoLib\src\Pkix\ClpPkixCertPathValidatorReason.pas',
ClpIOpenSslPemReader in '..\..\CryptoLib\src\Interfaces\OpenSsl\ClpIOpenSslPemReader.pas',
ClpIPemParser in '..\..\CryptoLib\src\Interfaces\Pem\ClpIPemParser.pas',
ClpIPemReader in '..\..\CryptoLib\src\Interfaces\Pem\ClpIPemReader.pas',
Expand Down
1 change: 1 addition & 0 deletions CryptoLib.Examples/Delphi.Examples/CryptoLib.Examples.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,7 @@ uses
ClpIX509TrustedCertificateBlock in '..\..\CryptoLib\src\Interfaces\OpenSsl\ClpIX509TrustedCertificateBlock.pas',
ClpCertificateTrustBlock in '..\..\CryptoLib\src\OpenSsl\ClpCertificateTrustBlock.pas',
ClpX509TrustedCertificateBlock in '..\..\CryptoLib\src\OpenSsl\ClpX509TrustedCertificateBlock.pas',
ClpPkixCertPathValidatorReason in '..\..\CryptoLib\src\Pkix\ClpPkixCertPathValidatorReason.pas',
ClpIOpenSslPemReader in '..\..\CryptoLib\src\Interfaces\OpenSsl\ClpIOpenSslPemReader.pas',
ClpIPemParser in '..\..\CryptoLib\src\Interfaces\Pem\ClpIPemParser.pas',
ClpIPemReader in '..\..\CryptoLib\src\Interfaces\Pem\ClpIPemReader.pas',
Expand Down
1 change: 1 addition & 0 deletions CryptoLib.Tests/Delphi.Tests/CryptoLib.Tests.Mobile.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ uses
ClpIX509TrustedCertificateBlock in '..\..\CryptoLib\src\Interfaces\OpenSsl\ClpIX509TrustedCertificateBlock.pas',
ClpCertificateTrustBlock in '..\..\CryptoLib\src\OpenSsl\ClpCertificateTrustBlock.pas',
ClpX509TrustedCertificateBlock in '..\..\CryptoLib\src\OpenSsl\ClpX509TrustedCertificateBlock.pas',
ClpPkixCertPathValidatorReason in '..\..\CryptoLib\src\Pkix\ClpPkixCertPathValidatorReason.pas',
ClpIOpenSslPemReader in '..\..\CryptoLib\src\Interfaces\OpenSsl\ClpIOpenSslPemReader.pas',
ClpIPemParser in '..\..\CryptoLib\src\Interfaces\Pem\ClpIPemParser.pas',
ClpIPemReader in '..\..\CryptoLib\src\Interfaces\Pem\ClpIPemReader.pas',
Expand Down
1 change: 1 addition & 0 deletions CryptoLib.Tests/Delphi.Tests/CryptoLib.Tests.Mobile.dproj
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,7 @@
<DCCReference Include="..\..\CryptoLib\src\Interfaces\OpenSsl\ClpIX509TrustedCertificateBlock.pas"/>
<DCCReference Include="..\..\CryptoLib\src\OpenSsl\ClpCertificateTrustBlock.pas"/>
<DCCReference Include="..\..\CryptoLib\src\OpenSsl\ClpX509TrustedCertificateBlock.pas"/>
<DCCReference Include="..\..\CryptoLib\src\Pkix\ClpPkixCertPathValidatorReason.pas"/>
<DCCReference Include="..\..\CryptoLib\src\Interfaces\OpenSsl\ClpIOpenSslPemReader.pas"/>
<DCCReference Include="..\..\CryptoLib\src\Interfaces\Pem\ClpIPemParser.pas"/>
<DCCReference Include="..\..\CryptoLib\src\Interfaces\Pem\ClpIPemReader.pas"/>
Expand Down
1 change: 1 addition & 0 deletions CryptoLib.Tests/Delphi.Tests/CryptoLib.Tests.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ uses
ClpIX509TrustedCertificateBlock in '..\..\CryptoLib\src\Interfaces\OpenSsl\ClpIX509TrustedCertificateBlock.pas',
ClpCertificateTrustBlock in '..\..\CryptoLib\src\OpenSsl\ClpCertificateTrustBlock.pas',
ClpX509TrustedCertificateBlock in '..\..\CryptoLib\src\OpenSsl\ClpX509TrustedCertificateBlock.pas',
ClpPkixCertPathValidatorReason in '..\..\CryptoLib\src\Pkix\ClpPkixCertPathValidatorReason.pas',
ClpIOpenSslPemReader in '..\..\CryptoLib\src\Interfaces\OpenSsl\ClpIOpenSslPemReader.pas',
ClpIPemParser in '..\..\CryptoLib\src\Interfaces\Pem\ClpIPemParser.pas',
ClpIPemReader in '..\..\CryptoLib\src\Interfaces\Pem\ClpIPemReader.pas',
Expand Down
133 changes: 133 additions & 0 deletions CryptoLib.Tests/src/Pkix/CertPathHardeningTests.pas
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ interface
ClpX509Generators,
ClpX509Asn1Objects,
ClpIX509Asn1Objects,
ClpX509Asn1Generators,
ClpIX509Asn1Generators,
ClpAsn1SignatureFactory,
ClpISignatureFactory,
ClpX509ExtensionUtilities,
Expand Down Expand Up @@ -158,6 +160,25 @@ TAnchorNameConstraintsTest = class(TCryptoLibAlgorithmTestCase)
procedure TestExplicitConstraintsCannotWidenTheCertificate;
end;

/// <summary>
/// An indirect CRL that revokes a serial number under one issuer must not report a certificate
/// sharing that serial number under a different issuer as revoked.
/// </summary>
TIndirectCrlSerialCollisionTest = class(TCryptoLibAlgorithmTestCase)
strict private
const
SigAlgorithm = 'SHA256WITHRSA';
var
FRandom: ISecureRandom;
function MakeCert(const ASerial: TBigInteger; const AIssuer, ASubject: IX509Name;
const AKey: IAsymmetricCipherKeyPair): IX509Certificate;
protected
procedure SetUp; override;
published
procedure TestSameSerialUnderDifferentIssuerIsNotMasked;
procedure TestDirectCrlRevokesBySerial;
end;

implementation

{ TCertPathLoopTest }
Expand Down Expand Up @@ -741,16 +762,128 @@ procedure TAnchorNameConstraintsTest.TestExplicitConstraintsCannotWidenTheCertif
'explicit constraints do not lift the certificate''s own constraints');
end;

{ TIndirectCrlSerialCollisionTest }

procedure TIndirectCrlSerialCollisionTest.SetUp;
begin
inherited SetUp;
FRandom := TSecureRandom.Create() as ISecureRandom;
end;

function TIndirectCrlSerialCollisionTest.MakeCert(const ASerial: TBigInteger;
const AIssuer, ASubject: IX509Name; const AKey: IAsymmetricCipherKeyPair): IX509Certificate;
var
LGen: IX509V3CertificateGenerator;
LNow: TDateTime;
begin
// IsRevoked matches on issuer and serial only, so a minimally-populated certificate is enough
LNow := Now.ToUniversalTime();
LGen := TX509V3CertificateGenerator.Create;
LGen.SetIssuerDN(AIssuer);
LGen.SetSerialNumber(ASerial);
LGen.SetNotBeforeUtc(IncDay(LNow, -1));
LGen.SetNotAfterUtc(IncYear(LNow, 1));
LGen.SetSubjectDN(ASubject);
LGen.SetPublicKey(AKey.Public as IAsymmetricKeyParameter);
Result := LGen.Generate(TAsn1SignatureFactory.Create(SigAlgorithm,
AKey.Private as IAsymmetricKeyParameter, FRandom) as ISignatureFactory);
end;

procedure TIndirectCrlSerialCollisionTest.TestSameSerialUnderDifferentIssuerIsNotMasked;
var
LKeyA, LKeyB, LSignerKey: IAsymmetricCipherKeyPair;
LDnA, LDnB, LSignerDn, LSubject: IX509Name;
LSerial: TBigInteger;
LCertA, LCertB: IX509Certificate;
LCrlGen: IX509V2CrlGenerator;
LExtGen: IX509ExtensionsGenerator;
LCrl: IX509Crl;
LNow: TDateTime;
begin
LKeyA := TCertTestUtilities.GenerateRsaKeyPair(1024);
LKeyB := TCertTestUtilities.GenerateRsaKeyPair(1024);
LSignerKey := TCertTestUtilities.GenerateRsaKeyPair(1024);

LDnA := TX509Name.Create('CN=CA-A, O=Test-PKI, C=DE');
LDnB := TX509Name.Create('CN=CA-B, O=Test-PKI, C=DE');
LSignerDn := TX509Name.Create('CN=Indirect-CRL-Signer, O=Test-PKI, C=DE');
LSubject := TX509Name.Create('CN=EU, O=Test-PKI, C=DE');

// the same serial number issued independently by two different CAs
LSerial := TBigInteger.ValueOf(4660);
LCertA := MakeCert(LSerial, LDnA, LSubject, LKeyA);
LCertB := MakeCert(LSerial, LDnB, LSubject, LKeyB);

// an indirect CRL that revokes the serial, naming CA-A as the entry's certificate issuer
LNow := Now.ToUniversalTime();
LCrlGen := TX509V2CrlGenerator.Create() as IX509V2CrlGenerator;
LCrlGen.SetIssuerDN(LSignerDn);
LCrlGen.SetThisUpdateUtc(IncHour(LNow, -1));
LCrlGen.SetNextUpdateUtc(IncMonth(LNow, 1));
LCrlGen.AddExtension(TX509Extensions.IssuingDistributionPoint, True,
TIssuingDistributionPoint.Create(nil, False, False, nil, True, False) as IIssuingDistributionPoint);

LExtGen := TX509ExtensionsGenerator.Create() as IX509ExtensionsGenerator;
LExtGen.AddExtension(TX509Extensions.CertificateIssuer, True,
TGeneralNames.Create(TGeneralName.Create(TGeneralName.DirectoryName, LDnA as IAsn1Encodable)
as IGeneralName) as IGeneralNames as IAsn1Encodable);
LCrlGen.AddCrlEntryUtc(LSerial, IncHour(LNow, -1), LExtGen.Generate());

LCrl := LCrlGen.Generate(TAsn1SignatureFactory.Create(SigAlgorithm,
LSignerKey.Private as IAsymmetricKeyParameter, FRandom) as ISignatureFactory);

CheckTrue(LCrl.IsRevoked(LCertA), 'the certificate from the named issuer is revoked');
CheckFalse(LCrl.IsRevoked(LCertB),
'a certificate sharing the serial number under a different issuer is not masked as revoked');
end;

procedure TIndirectCrlSerialCollisionTest.TestDirectCrlRevokesBySerial;
var
LCaKey, LSubjectKey: IAsymmetricCipherKeyPair;
LCaDn, LSubject: IX509Name;
LRevokedSerial, LOtherSerial: TBigInteger;
LCertRevoked, LCertOther: IX509Certificate;
LCrlGen: IX509V2CrlGenerator;
LCrl: IX509Crl;
LNow: TDateTime;
begin
LCaKey := TCertTestUtilities.GenerateRsaKeyPair(1024);
LSubjectKey := TCertTestUtilities.GenerateRsaKeyPair(1024);
LCaDn := TX509Name.Create('CN=CA, O=Test-PKI, C=DE');
LSubject := TX509Name.Create('CN=EU, O=Test-PKI, C=DE');

LRevokedSerial := TBigInteger.ValueOf(4660);
LOtherSerial := TBigInteger.ValueOf(4661);
LCertRevoked := MakeCert(LRevokedSerial, LCaDn, LSubject, LSubjectKey);
LCertOther := MakeCert(LOtherSerial, LCaDn, LSubject, LSubjectKey);

// an ordinary (direct) CRL issued by the CA; its entries carry no per-entry issuer
LNow := Now.ToUniversalTime();
LCrlGen := TX509V2CrlGenerator.Create() as IX509V2CrlGenerator;
LCrlGen.SetIssuerDN(LCaDn);
LCrlGen.SetThisUpdateUtc(IncHour(LNow, -1));
LCrlGen.SetNextUpdateUtc(IncMonth(LNow, 1));
LCrlGen.AddCrlEntryUtc(LRevokedSerial, IncHour(LNow, -1), TCrlReason.KeyCompromise);
LCrl := LCrlGen.Generate(TAsn1SignatureFactory.Create(SigAlgorithm,
LCaKey.Private as IAsymmetricKeyParameter, FRandom) as ISignatureFactory);

CheckTrue(LCrl.IsRevoked(LCertRevoked), 'a direct CRL reports its revoked certificate as revoked');
CheckFalse(LCrl.IsRevoked(LCertOther),
'a direct CRL does not report an unrevoked serial as revoked');
end;

initialization

{$IFDEF FPC}
RegisterTest(TCertPathLoopTest);
RegisterTest(TIndirectCrlSignerTest);
RegisterTest(TAnchorNameConstraintsTest);
RegisterTest(TIndirectCrlSerialCollisionTest);
{$ELSE}
RegisterTest(TCertPathLoopTest.Suite);
RegisterTest(TIndirectCrlSignerTest.Suite);
RegisterTest(TAnchorNameConstraintsTest.Suite);
RegisterTest(TIndirectCrlSerialCollisionTest.Suite);
{$ENDIF FPC}

end.
6 changes: 6 additions & 0 deletions CryptoLib.Tests/src/Pkix/CertPathValidatorTests.pas
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ interface
ClpNullable,
ClpCryptoLibTypes,
ClpCryptoLibExceptions,
ClpPkixCertPathValidatorReason,
ClpCryptoLibConfig,
CertVectors,
CryptoLibTestBase;
Expand Down Expand Up @@ -268,6 +269,7 @@ procedure TCertPathValidatorTest.TestInvalidPathContainingAValidOne;
LValidator: IPkixCertPathValidator;
LRejected: Boolean;
LIndex: Int32;
LReason: TPkixCertPathValidatorReason;
begin
// the end entity is not issued by the intermediate, so its signature cannot verify
LRootCert := ReadCert('PkixBrokenChainRootCa');
Expand All @@ -287,6 +289,7 @@ procedure TCertPathValidatorTest.TestInvalidPathContainingAValidOne;

LRejected := False;
LIndex := -1;
LReason := TPkixCertPathValidatorReason.Unspecified;
LValidator := TPkixCertPathValidator.Create() as IPkixCertPathValidator;
try
LValidator.Validate(LPath, LParams);
Expand All @@ -295,12 +298,15 @@ procedure TCertPathValidatorTest.TestInvalidPathContainingAValidOne;
begin
LRejected := True;
LIndex := E.Index;
LReason := E.Reason;
end;
end;

CheckTrue(LRejected, 'a path whose end entity signature does not verify is rejected');
if LIndex >= 0 then
CheckEquals(0, LIndex, 'the end entity certificate is blamed');
CheckTrue(LReason = TPkixCertPathValidatorReason.InvalidSignature,
'the failure is reported as an invalid signature');
end;

procedure TCertPathValidatorTest.TestCircularCrlIssuerProcessing;
Expand Down
22 changes: 18 additions & 4 deletions CryptoLib.Tests/src/Pkix/PkixFoundationTests.pas
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,27 @@ procedure TPkixFoundationTest.TestCertStatusAndReasonsMask;
LMask, LOther: IReasonsMask;
begin
LStatus := TCertStatus.Create();
CheckEquals(TCertStatus.Unrevoked, LStatus.Status, 'a fresh status is unrevoked');
CheckFalse(LStatus.IsRevoked, 'a fresh status is not revoked');
CheckFalse(LStatus.RevocationDate.HasValue, 'a fresh status has no revocation date');
CheckFalse(LStatus.ReasonCode.HasValue, 'a fresh status has no reason code');

LStatus.Status := TCertStatus.Undetermined;
LStatus.RevocationDate := TNullable<TDateTime>.Some(FRootCert.NotBefore);
CheckEquals(TCertStatus.Undetermined, LStatus.Status, 'the status round-trips');
// a known reason code round-trips and maps to its name
LStatus.SetRevoked(FRootCert.NotBefore, TNullable<Int32>.Some(Int32(TCrlReason.KeyCompromise)));
CheckTrue(LStatus.IsRevoked, 'the revocation is recorded');
CheckTrue(LStatus.RevocationDate.HasValue, 'the revocation date round-trips');
CheckEquals(TCrlReason.KeyCompromise, LStatus.Reason, 'the effective reason is the given code');
CheckEquals('keyCompromise', LStatus.GetReasonText, 'a known reason maps to its name');

// an out-of-range reason code is processed as unspecified but reported verbatim, so it can never
// be mistaken for a status
LStatus.SetRevoked(FRootCert.NotBefore, TNullable<Int32>.Some(Int32(99)));
CheckTrue(LStatus.IsRevoked, 'an unknown reason code is still a revocation');
CheckEquals(TCrlReason.Unspecified, LStatus.Reason, 'an unknown code is processed as unspecified');
CheckEquals('(unknown code: 99)', LStatus.GetReasonText, 'an unknown code is reported verbatim');

LStatus.SetUnrevoked();
CheckFalse(LStatus.IsRevoked, 'a removeFromCRL entry can clear the revocation');
CheckFalse(LStatus.RevocationDate.HasValue, 'clearing drops the revocation date');

LMask := TReasonsMask.Create();
CheckFalse(LMask.IsAllReasons, 'an empty mask does not cover every reason');
Expand Down
30 changes: 21 additions & 9 deletions CryptoLib/src/Interfaces/Pkix/ClpIPkixTypes.pas
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,31 @@ interface
property Parent: IPkixPolicyNode read GetParent write SetParent;
end;

/// <summary>
/// Mutable revocation status carried through path validation.
/// </summary>
/// <summary>Revocation status accumulated over the CRLs checked for a certificate. The reason code
/// is kept apart from whether the certificate is revoked. "Undetermined" is not stored: it is
/// concluded when processing ends with the certificate not revoked and not all reasons covered.</summary>
ICertStatus = interface(IInterface)
['{B2016D68-7C59-4FA3-8E4B-1D8F26C593E7}']

function GetStatus: Int32;
procedure SetStatus(AValue: Int32);
function GetIsRevoked: Boolean;
function GetRevocationDate: TNullable<TDateTime>;
procedure SetRevocationDate(const AValue: TNullable<TDateTime>);

property Status: Int32 read GetStatus write SetStatus;
property RevocationDate: TNullable<TDateTime> read GetRevocationDate write SetRevocationDate;
function GetReasonCode: TNullable<Int32>;
function GetReason: Int32;
function GetReasonText: String;

/// <summary>Record a revocation. <paramref name="AReasonCode" /> is the entry's reason code as
/// given, or None if absent.</summary>
procedure SetRevoked(const ARevocationDate: TDateTime; const AReasonCode: TNullable<Int32>);
/// <summary>Clear a removeFromCRL revocation (RFC 5280 6.3.3 (k)).</summary>
procedure SetUnrevoked();

property IsRevoked: Boolean read GetIsRevoked;
property RevocationDate: TNullable<TDateTime> read GetRevocationDate;
/// <summary>The entry's reason code as given, or None if absent, out of range, or not revoked.</summary>
property ReasonCode: TNullable<Int32> read GetReasonCode;
/// <summary>The effective CRLReason to process the revocation by: the reason code if defined,
/// else unspecified.</summary>
property Reason: Int32 read GetReason;
end;

/// <summary>
Expand Down
6 changes: 5 additions & 1 deletion CryptoLib/src/Interfaces/X509/ClpIX509Crl.pas
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ interface
function GetIssuerDN: IX509Name;
function GetThisUpdate: TDateTime;
function GetNextUpdate: TNullable<TDateTime>;
function GetRevokedCertificate(const ASerialNumber: TBigInteger): IX509CrlEntry;
function GetRevokedCertificate(const ASerialNumber: TBigInteger): IX509CrlEntry; overload;
function GetRevokedCertificate(const ACert: IX509Certificate): IX509CrlEntry; overload;
function GetRevokedCertificates: TCryptoLibGenericArray<IX509CrlEntry>;
function GetTbsCertList: TCryptoLibByteArray;
function GetNonCriticalExtensionOids: TCryptoLibStringArray;
Expand All @@ -60,6 +61,7 @@ interface
procedure Verify(const AVerifierProvider: IVerifierFactoryProvider); overload;
procedure VerifyAltSignature(const AVerifierProvider: IVerifierFactoryProvider);

function GetIsIndirect: Boolean;
function IsRevoked(const ACert: IX509Certificate): Boolean;
function Equals(const AOther: TObject): Boolean; overload;
/// <summary>Value equality on the encoded CRL.</summary>
Expand All @@ -72,6 +74,8 @@ interface
property IssuerDN: IX509Name read GetIssuerDN;
property ThisUpdate: TDateTime read GetThisUpdate;
property NextUpdate: TNullable<TDateTime> read GetNextUpdate;
/// <summary>Whether this is an indirect CRL (its IssuingDistributionPoint says so).</summary>
property IsIndirect: Boolean read GetIsIndirect;
property SigAlgName: String read GetSigAlgName;
property SigAlgOid: String read GetSigAlgOid;
property SignatureAlgorithm: IAlgorithmIdentifier read GetSignatureAlgorithm;
Expand Down
Loading
Loading