All Downloads are FREE. Search and download functionalities are using the official Maven repository.

no.difi.sdp.client2.internal.CertificateValidator Maven / Gradle / Ivy

There is a newer version: 7.0.0-RC2
Show newest version
package no.difi.sdp.client2.internal;

import no.difi.sdp.client2.domain.Databehandler;
import no.difi.sdp.client2.domain.Miljo;
import no.difi.sdp.client2.domain.exceptions.SertifikatException;
import no.digipost.security.cert.CertStatus;
import no.digipost.security.cert.CertificateValidatorConfig;
import no.digipost.security.cert.Trust;

import java.security.cert.X509Certificate;
import java.text.MessageFormat;

import static no.digipost.security.cert.OcspSetting.NO_OCSP;

public class CertificateValidator {

    public static void validate(Miljo miljo, X509Certificate certificate) {
        if (miljo.getGodkjenteKjedeSertifikater() == null) {
            return;
        }

        Trust trusteChainCertificates = miljo.getGodkjenteKjedeSertifikater();

        CertificateValidatorConfig certificateValidatorConfig = CertificateValidatorConfig.MOST_STRICT.with(NO_OCSP);
        no.digipost.security.cert.CertificateValidator certificateValidator = new no.digipost.security.cert.CertificateValidator(certificateValidatorConfig, trusteChainCertificates, null);

        CertStatus certStatus = certificateValidator.validateCert(certificate);

        if (certStatus != CertStatus.OK) {
            String message = MessageFormat
                    .format("Sertifikatet som brukes for {0} er ikke gyldig. Prøver du å sende med et testsertifikat i produksjonsmiljø eller omvendt, eller er sertifikatet utgått?", Databehandler.class.getSimpleName());
            throw new SertifikatException(message);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy