@@ -26,6 +26,7 @@ You should have received a copy of the GNU Affero General Public License
2626using iText . Commons . Utils ;
2727using iText . Signatures ;
2828using iText . Signatures . Testutils ;
29+ using iText . Signatures . Testutils . Client ;
2930using iText . Signatures . Validation . V1 . Context ;
3031using iText . Signatures . Validation . V1 . Extensions ;
3132using iText . Signatures . Validation . V1 . Mocks ;
@@ -469,5 +470,110 @@ public virtual void RootCertificateTrustedForTimestampTest() {
469470 ) ) . HasLogItem ( ( l ) => l . WithMessage ( CertificateChainValidator . ISSUER_MISSING , ( i ) => rootCert . GetSubjectDN
470471 ( ) ) ) ) ;
471472 }
473+
474+ [ NUnit . Framework . Test ]
475+ public virtual void TrustStoreFailureTest ( ) {
476+ String chainName = CERTS_SRC + "chain.pem" ;
477+ IX509Certificate [ ] certificateChain = PemFileHelper . ReadFirstChain ( chainName ) ;
478+ IX509Certificate signingCert = ( IX509Certificate ) certificateChain [ 0 ] ;
479+ IX509Certificate intermediateCert = ( IX509Certificate ) certificateChain [ 1 ] ;
480+ IX509Certificate rootCert = ( IX509Certificate ) certificateChain [ 2 ] ;
481+ MockIssuingCertificateRetriever mockCertificateRetriever = new MockIssuingCertificateRetriever ( certificateRetriever
482+ ) . OnGetTrustedCertificatesStoreDo ( ( ) => {
483+ throw new Exception ( "Test trust store failure" ) ;
484+ }
485+ ) ;
486+ validatorChainBuilder . WithIssuingCertificateRetriever ( mockCertificateRetriever ) ;
487+ CertificateChainValidator validator = validatorChainBuilder . BuildCertificateChainValidator ( ) ;
488+ certificateRetriever . AddKnownCertificates ( JavaCollectionsUtil . SingletonList < IX509Certificate > ( intermediateCert
489+ ) ) ;
490+ certificateRetriever . SetTrustedCertificates ( JavaCollectionsUtil . SingletonList < IX509Certificate > ( rootCert ) ) ;
491+ ValidationReport report = validator . ValidateCertificate ( baseContext , signingCert , TimeTestUtil . TEST_DATE_TIME
492+ ) ;
493+ AssertValidationReport . AssertThat ( report , ( a ) => a . HasStatus ( ValidationReport . ValidationResult . INDETERMINATE
494+ ) . HasLogItems ( 1 , 10 , ( la ) => la . WithMessage ( CertificateChainValidator . TRUSTSTORE_RETRIEVAL_FAILED ) ) ) ;
495+ }
496+
497+ [ NUnit . Framework . Test ]
498+ public virtual void IssuerRetrievalFailureTest ( ) {
499+ String chainName = CERTS_SRC + "chain.pem" ;
500+ IX509Certificate [ ] certificateChain = PemFileHelper . ReadFirstChain ( chainName ) ;
501+ IX509Certificate signingCert = ( IX509Certificate ) certificateChain [ 0 ] ;
502+ IX509Certificate intermediateCert = ( IX509Certificate ) certificateChain [ 1 ] ;
503+ IX509Certificate rootCert = ( IX509Certificate ) certificateChain [ 2 ] ;
504+ MockIssuingCertificateRetriever mockCertificateRetriever = new MockIssuingCertificateRetriever ( certificateRetriever
505+ ) . OnRetrieveIssuerCertificateDo ( ( c ) => {
506+ throw new Exception ( "Test issuer retrieval failure" ) ;
507+ }
508+ ) ;
509+ validatorChainBuilder . WithIssuingCertificateRetriever ( mockCertificateRetriever ) ;
510+ CertificateChainValidator validator = validatorChainBuilder . BuildCertificateChainValidator ( ) ;
511+ certificateRetriever . AddKnownCertificates ( JavaCollectionsUtil . SingletonList < IX509Certificate > ( intermediateCert
512+ ) ) ;
513+ certificateRetriever . SetTrustedCertificates ( JavaCollectionsUtil . SingletonList < IX509Certificate > ( rootCert ) ) ;
514+ ValidationReport report = validator . ValidateCertificate ( baseContext , signingCert , TimeTestUtil . TEST_DATE_TIME
515+ ) ;
516+ AssertValidationReport . AssertThat ( report , ( a ) => a . HasStatus ( ValidationReport . ValidationResult . INDETERMINATE
517+ ) . HasLogItems ( 1 , 10 , ( la ) => la . WithMessage ( CertificateChainValidator . ISSUER_RETRIEVAL_FAILED ) ) ) ;
518+ }
519+
520+ [ NUnit . Framework . Test ]
521+ public virtual void RevocationValidationFailureTest ( ) {
522+ String chainName = CERTS_SRC + "chain.pem" ;
523+ IX509Certificate [ ] certificateChain = PemFileHelper . ReadFirstChain ( chainName ) ;
524+ IX509Certificate signingCert = ( IX509Certificate ) certificateChain [ 0 ] ;
525+ IX509Certificate intermediateCert = ( IX509Certificate ) certificateChain [ 1 ] ;
526+ IX509Certificate rootCert = ( IX509Certificate ) certificateChain [ 2 ] ;
527+ mockRevocationDataValidator . OnValidateDo ( ( c ) => {
528+ throw new Exception ( "Test revocation validation failure" ) ;
529+ }
530+ ) ;
531+ CertificateChainValidator validator = validatorChainBuilder . BuildCertificateChainValidator ( ) ;
532+ certificateRetriever . AddKnownCertificates ( JavaCollectionsUtil . SingletonList < IX509Certificate > ( intermediateCert
533+ ) ) ;
534+ certificateRetriever . SetTrustedCertificates ( JavaCollectionsUtil . SingletonList < IX509Certificate > ( rootCert ) ) ;
535+ ValidationReport report = validator . ValidateCertificate ( baseContext , signingCert , TimeTestUtil . TEST_DATE_TIME
536+ ) ;
537+ AssertValidationReport . AssertThat ( report , ( a ) => a . HasStatus ( ValidationReport . ValidationResult . INDETERMINATE
538+ ) . HasLogItems ( 1 , 10 , ( la ) => la . WithMessage ( CertificateChainValidator . REVOCATION_VALIDATION_FAILED ) ) ) ;
539+ }
540+
541+ [ NUnit . Framework . Test ]
542+ public virtual void AddCrlClientPasstroughTest ( ) {
543+ CertificateChainValidator validator = validatorChainBuilder . BuildCertificateChainValidator ( ) ;
544+ validator . AddCrlClient ( new TestCrlClient ( ) ) ;
545+ NUnit . Framework . Assert . AreEqual ( 1 , mockRevocationDataValidator . crlClientsAdded . Count ) ;
546+ }
547+
548+ [ NUnit . Framework . Test ]
549+ public virtual void AddOcdpClientPasstroughTest ( ) {
550+ CertificateChainValidator validator = validatorChainBuilder . BuildCertificateChainValidator ( ) ;
551+ validator . AddOcspClient ( new TestOcspClient ( ) ) ;
552+ NUnit . Framework . Assert . AreEqual ( 1 , mockRevocationDataValidator . ocspClientsAdded . Count ) ;
553+ }
554+
555+ [ NUnit . Framework . Test ]
556+ public virtual void TestStopOnInvalidRevocationResultTest ( ) {
557+ mockRevocationDataValidator . OnValidateDo ( ( c ) => c . report . AddReportItem ( new ReportItem ( "test" , "test" , ReportItem . ReportItemStatus
558+ . INVALID ) ) ) ;
559+ String chainName = CERTS_SRC + "chain.pem" ;
560+ IX509Certificate [ ] certificateChain = PemFileHelper . ReadFirstChain ( chainName ) ;
561+ IX509Certificate signingCert = ( IX509Certificate ) certificateChain [ 0 ] ;
562+ IX509Certificate intermediateCert = ( IX509Certificate ) certificateChain [ 1 ] ;
563+ IX509Certificate rootCert = ( IX509Certificate ) certificateChain [ 2 ] ;
564+ properties . SetContinueAfterFailure ( ValidatorContexts . All ( ) , CertificateSources . All ( ) , false ) ;
565+ MockIssuingCertificateRetriever mockCertificateRetriever = new MockIssuingCertificateRetriever ( certificateRetriever
566+ ) ;
567+ validatorChainBuilder . WithIssuingCertificateRetriever ( mockCertificateRetriever ) ;
568+ CertificateChainValidator validator = validatorChainBuilder . BuildCertificateChainValidator ( ) ;
569+ certificateRetriever . AddKnownCertificates ( JavaCollectionsUtil . SingletonList < IX509Certificate > ( intermediateCert
570+ ) ) ;
571+ certificateRetriever . SetTrustedCertificates ( JavaCollectionsUtil . SingletonList < IX509Certificate > ( rootCert ) ) ;
572+ ValidationReport report = validator . ValidateCertificate ( baseContext , signingCert , TimeTestUtil . TEST_DATE_TIME
573+ ) ;
574+ AssertValidationReport . AssertThat ( report , ( a ) => a . HasStatus ( ValidationReport . ValidationResult . INVALID ) ) ;
575+ NUnit . Framework . Assert . AreEqual ( 0 , mockCertificateRetriever . getCrlIssuerCertificatesCalls . Count ) ;
576+ NUnit . Framework . Assert . AreEqual ( 1 , mockRevocationDataValidator . calls . Count ) ;
577+ }
472578 }
473579}
0 commit comments