Skip to content

Commit

Permalink
Merge branch 'develop' into devsecops
Browse files Browse the repository at this point in the history
  • Loading branch information
aleks-ivanov committed Dec 10, 2024
2 parents 8c010df + a0db26c commit cd3a6c1
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ You should have received a copy of the GNU Affero General Public License
*/
using System;
using System.Collections.Generic;
using System.IO;
using iText.Bouncycastleconnector;
using iText.Commons.Bouncycastle;
using iText.Commons.Bouncycastle.Cert;
Expand Down Expand Up @@ -53,6 +54,15 @@ public virtual TestOcspClient AddBuilderForCertIssuer(IX509Certificate cert, Tes
}

public virtual byte[] GetEncoded(IX509Certificate checkCert, IX509Certificate issuerCert, String url) {
if (url != null && !String.IsNullOrEmpty(url)) {
// Treat as file path
try {
return File.ReadAllBytes(System.IO.Path.Combine(url));
}
catch (Exception) {
}
}
// Sometimes we pass http url here in tests (though it's not used) so skipping any errors
byte[] bytes = null;
try {
ICertID id = SignTestPortUtil.GenerateCertificateId(issuerCert, checkCert.GetSerialNumber(), BOUNCY_CASTLE_FACTORY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,21 @@ public virtual void AuthorizedOcspResponderDoesNotHaveOcspSigningExtensionTest()
));
}

[NUnit.Framework.Test]
public virtual void OcspResponseWithoutHashAlgoParametersTest() {
TestOcspClient ocspClient = new TestOcspClient();
IBasicOcspResponse caBasicOCSPResp = FACTORY.CreateBasicOCSPResponse(FACTORY.CreateASN1Primitive(ocspClient
.GetEncoded(checkCert, caCert, SOURCE_FOLDER + "ocspResponseWithoutHashAlgoParameters.dat")));
ValidationReport report = new ValidationReport();
// Configure OCSP signing authority for the certificate in question
certificateRetriever.AddTrustedCertificates(JavaCollectionsUtil.SingletonList(caCert));
OCSPValidator validator = validatorChainBuilder.BuildOCSPValidator();
validator.Validate(report, baseContext, checkCert, caBasicOCSPResp.GetResponses()[0], caBasicOCSPResp, TimeTestUtil
.TEST_DATE_TIME, TimeTestUtil.TEST_DATE_TIME);
AssertValidationReport.AssertThat(report, (a) => a.HasNumberOfFailures(0).HasStatus(ValidationReport.ValidationResult
.VALID));
}

private ValidationReport ValidateTest(DateTime checkDate) {
DateTime thisUpdate = checkDate.AddDays(1);
TestOcspResponseBuilder builder = new TestOcspResponseBuilder(responderCert, ocspRespPrivateKey);
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ public IDigest CreateIDigest(string hashAlgorithm) {

/// <summary><inheritDoc/></summary>
public ICertID CreateCertificateID(string hashAlgorithm, IX509Certificate issuerCert, IBigInteger serialNumber) {
return new CertIDBC(hashAlgorithm, issuerCert, serialNumber);
return new CertIDBC(new AlgorithmIdentifier(new DerObjectIdentifier(hashAlgorithm), DerNull.Instance), issuerCert, serialNumber);
}

/// <summary><inheritDoc/></summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,39 @@ public CertIDBC(CertID certificateID) {
/// <see cref="Org.BouncyCastle.Asn1.Ocsp.CertID"/>.
/// </summary>
/// <param name="hashAlgorithm">
/// hash algorithm to create
/// hash algorithm
/// <see cref="Org.BouncyCastle.Asn1.Ocsp.CertID"/>
/// </param>
/// <param name="issuerCert">
/// X509Certificate wrapper to create
/// X509Certificate wrapper
/// <see cref="Org.BouncyCastle.Asn1.Ocsp.CertID"/>
/// </param>
/// <param name="serialNumber">
/// serial number to create
/// serial number
/// <see cref="Org.BouncyCastle.Asn1.Ocsp.CertID"/>
/// </param>
public CertIDBC(string hashAlgorithm, IX509Certificate issuerCert, IBigInteger serialNumber) {
AlgorithmIdentifier hashAlgId = new AlgorithmIdentifier(new DerObjectIdentifier(hashAlgorithm), DerNull.Instance);
public CertIDBC(string hashAlgorithm, IX509Certificate issuerCert, IBigInteger serialNumber)
: this(new AlgorithmIdentifier(new DerObjectIdentifier(hashAlgorithm), DerNull.Instance), issuerCert, serialNumber) {
}

/// <summary>
/// Creates new wrapper instance for
/// <see cref="Org.BouncyCastle.Asn1.Ocsp.CertID"/>.
/// </summary>
/// <param name="hashAlgId">
/// hash algorithm indentifier
/// <see cref="Org.BouncyCastle.Asn1.X509.AlgorithmIdentifier"/>
/// </param>
/// <param name="issuerCert">
/// X509Certificate wrapper
/// <see cref="iText.Commons.Bouncycastle.Cert.IX509Certificate"/>
/// </param>
/// <param name="serialNumber">
/// serial number
/// </param>
public CertIDBC(AlgorithmIdentifier hashAlgId, IX509Certificate issuerCert, IBigInteger serialNumber) {
X509Name issuerName = PrincipalUtilities.GetSubjectX509Principal(((X509CertificateBC)issuerCert).GetCertificate());
string hashAlgorithm = hashAlgId.Algorithm.Id;
byte[] issuerNameHash = DigestUtilities.CalculateDigest(hashAlgorithm, issuerName.GetEncoded());

AsymmetricKeyParameter issuerKey = ((X509CertificateBC)issuerCert).GetCertificate().GetPublicKey();
Expand Down Expand Up @@ -124,7 +142,7 @@ public virtual string GetHashSha1() {

/// <summary><inheritDoc/></summary>
public virtual bool MatchesIssuer(IX509Certificate issuerCert) {
return new CertIDBC(certificateID.HashAlgorithm.Algorithm.Id, issuerCert, new BigIntegerBC(
return new CertIDBC(certificateID.HashAlgorithm, issuerCert, new BigIntegerBC(
certificateID.SerialNumber.Value)).GetCertID().Equals(certificateID);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ public IDigest CreateIDigest(string hashAlgorithm) {

/// <summary><inheritDoc/></summary>
public ICertID CreateCertificateID(string hashAlgorithm, IX509Certificate issuerCert, IBigInteger serialNumber) {
return new CertIDBCFips(hashAlgorithm, issuerCert, serialNumber);
return new CertIDBCFips(new AlgorithmIdentifier(new DerObjectIdentifier(hashAlgorithm), DerNull.Instance), issuerCert, serialNumber);
}

/// <summary><inheritDoc/></summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,39 @@ public CertIDBCFips(CertID certificateID) {
/// <see cref="Org.BouncyCastle.Asn1.Ocsp.CertID"/>.
/// </summary>
/// <param name="hashAlgorithm">
/// hash algorithm to create
/// hash algorithm
/// <see cref="Org.BouncyCastle.Asn1.Ocsp.CertID"/>
/// </param>
/// <param name="issuerCert">
/// X509Certificate wrapper to create
/// X509Certificate wrapper
/// <see cref="Org.BouncyCastle.Asn1.Ocsp.CertID"/>
/// </param>
/// <param name="serialNumber">
/// serial number to create
/// serial number
/// <see cref="Org.BouncyCastle.Asn1.Ocsp.CertID"/>
/// </param>
public CertIDBCFips(string hashAlgorithm, IX509Certificate issuerCert, IBigInteger serialNumber) {
AlgorithmIdentifier hashAlgId = new AlgorithmIdentifier(new DerObjectIdentifier(hashAlgorithm), DerNull.Instance);
public CertIDBCFips(string hashAlgorithm, IX509Certificate issuerCert, IBigInteger serialNumber)
: this(new AlgorithmIdentifier(new DerObjectIdentifier(hashAlgorithm), DerNull.Instance), issuerCert, serialNumber) {
}

/// <summary>
/// Creates new wrapper instance for
/// <see cref="Org.BouncyCastle.Asn1.Ocsp.CertID"/>.
/// </summary>
/// <param name="hashAlgId">
/// hash algorithm identifier
/// <see cref="Org.BouncyCastle.Asn1.X509.AlgorithmIdentifier"/>
/// </param>
/// <param name="issuerCert">
/// X509Certificate wrapper
/// <see cref="iText.Commons.Bouncycastle.Cert.IX509Certificate"/>
/// </param>
/// <param name="serialNumber">
/// serial number
/// </param>
public CertIDBCFips(AlgorithmIdentifier hashAlgId, IX509Certificate issuerCert, IBigInteger serialNumber) {
X500Name issuerName = ((X509CertificateBCFips)issuerCert).GetCertificate().SubjectDN;
string hashAlgorithm = hashAlgId.Algorithm.Id;
byte[] issuerNameHash = new DigestBCFips(hashAlgorithm).Digest(issuerName.GetEncoded());

IAsymmetricPublicKey issuerKey = ((X509CertificateBCFips)issuerCert).GetCertificate().GetPublicKey();
Expand Down Expand Up @@ -123,7 +141,7 @@ public virtual string GetHashSha1() {

/// <summary><inheritDoc/></summary>
public virtual bool MatchesIssuer(IX509Certificate issuerCert) {
return new CertIDBCFips(certificateID.HashAlgorithm.Algorithm.Id, issuerCert, new BigIntegerBCFips(
return new CertIDBCFips(certificateID.HashAlgorithm, issuerCert, new BigIntegerBCFips(
certificateID.SerialNumber.Value)).GetCertificateID().Equals(certificateID);
}

Expand Down
2 changes: 1 addition & 1 deletion port-hash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
d313db98840a9c9bb563ae7c61e7e1a7b16fbf44
988c534b726839dcf4d939206ec91b811628a709

0 comments on commit cd3a6c1

Please sign in to comment.