<commit_before>package org.digidoc4j.impl.asic;

import org.bouncycastle.asn1.x500.RDN;
import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.asn1.x500.style.BCStyle;
import org.bouncycastle.asn1.x500.style.IETFUtils;
import org.bouncycastle.cert.jcajce.JcaX509CertificateHolder;
import org.digidoc4j.Configuration;
import org.digidoc4j.SignatureProfile;
import org.digidoc4j.X509Cert;
import org.digidoc4j.exceptions.CertificateNotFoundException;
import org.digidoc4j.exceptions.DigiDoc4JException;
import org.digidoc4j.impl.asic.xades.XadesSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.cert.CertificateEncodingException;

/**
 * Validator of OCSP responder for TM
 */
public class OcspResponderValidator {
    private static final Logger LOGGER = LoggerFactory.getLogger(OcspResponderValidator.class);

    private XadesSignature signature;
    private Configuration configuration;

    /**
     * Constructor of the validator
     *
     * @param signature Xades signature object
     */
    public OcspResponderValidator(XadesSignature signature, Configuration configuration) {
        this.signature = signature;
        this.configuration = configuration;
    }

    /**
     * Method for asking if OCSP responder is valid or not.
     *
     * @return True if OCSP response is valid, false otherwise.
     */
    public boolean isValid() {
        if (SignatureProfile.LT_TM != signature.getProfile()) {
            return true;
        }
        try {
            if (signature.getOCSPCertificate() == null) {
                LOGGER.debug("OCSP certificate was not found in signature: " + signature.getId());
                return true;
            }
            return isOcspResponserCommonNameValid(signature.getOCSPCertificate());
        } catch (CertificateNotFoundException e) {
            return false;
        }

    }

    private boolean isOcspResponserCommonNameValid(X509Cert ocspCertificate) {
        try {
            X500Name x500name = new JcaX509CertificateHolder(ocspCertificate.getX509Certificate()).getSubject();
            RDN dn = x500name.getRDNs(BCStyle.CN)[0];
            String commonName =  IETFUtils.valueToString(dn.getFirst().getValue());
            return configuration.getAllowedOcspRespondersForTM().contains(commonName);<commit_msg>removed unnecessary ocsp ecrtificate check<commit_after>package org.digidoc4j.impl.asic;

import org.bouncycastle.asn1.x500.RDN;
import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.asn1.x500.style.BCStyle;
import org.bouncycastle.asn1.x500.style.IETFUtils;
import org.bouncycastle.cert.jcajce.JcaX509CertificateHolder;
import org.digidoc4j.Configuration;
import org.digidoc4j.SignatureProfile;
import org.digidoc4j.X509Cert;
import org.digidoc4j.exceptions.CertificateNotFoundException;
import org.digidoc4j.exceptions.DigiDoc4JException;
import org.digidoc4j.impl.asic.xades.XadesSignature;

import java.security.cert.CertificateEncodingException;

/**
 * Validator of OCSP responder for TM
 */
public class OcspResponderValidator {

    private XadesSignature signature;
    private Configuration configuration;

    /**
     * Constructor of the validator
     *
     * @param signature Xades signature object
     */
    public OcspResponderValidator(XadesSignature signature, Configuration configuration) {
        this.signature = signature;
        this.configuration = configuration;
    }

    /**
     * Method for asking if OCSP responder is valid or not.
     *
     * @return True if OCSP response is valid, false otherwise.
     */
    public boolean isValid() {
        if (SignatureProfile.LT_TM != signature.getProfile()) {
            return true;
        }
        try {
            return isOcspResponserCommonNameValid(signature.getOCSPCertificate());
        } catch (CertificateNotFoundException e) {
            return false;
        }

    }

    private boolean isOcspResponserCommonNameValid(X509Cert ocspCertificate) {
        try {
            X500Name x500name = new JcaX509CertificateHolder(ocspCertificate.getX509Certificate()).getSubject();
            RDN dn = x500name.getRDNs(BCStyle.CN)[0];
            String commonName =  IETFUtils.valueToString(dn.getFirst().getValue());
            return configuration.getAllowedOcspRespondersForTM().contains(commonName);
{'fraction_non_alphanumeric': 0.0857077205882353, 'fraction_numerical': 0.024356617647058824, 'mean_word_length': 3.3925327951564075, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3753216', 'n_tokens_mistral': 1353, 'n_tokens_neox': 1273, 'n_words': 248}