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

de.rub.nds.x509attacker.x509.preparator.TbsCertificatePreparator Maven / Gradle / Ivy

Go to download

X.509-Attacker is a tool based on ASN.1 Tool for creating arbitrary certificates; including especially invalid and malformed certificates. Since X.509 certificates encode their contents in ASN.1, this tool extends the features of ASN.1 Tool in terms of certificate signing. Also, X.509-Attacker introduces a feature of referencing XML elements in order to avoid redundancies when defining certificates in XML.

The newest version!
/*
 * X.509-Attacker - A Library for Arbitrary X.509 Certificates
 *
 * Copyright 2014-2023 Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH
 *
 * Licensed under Apache License, Version 2.0
 * http://www.apache.org/licenses/LICENSE-2.0.txt
 */
package de.rub.nds.x509attacker.x509.preparator;

import de.rub.nds.asn1.model.Asn1Encodable;
import de.rub.nds.asn1.model.Asn1Integer;
import de.rub.nds.asn1.preparator.Asn1PreparatorHelper;
import de.rub.nds.x509attacker.chooser.X509Chooser;
import de.rub.nds.x509attacker.x509.model.TbsCertificate;
import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class TbsCertificatePreparator extends X509ContainerPreparator {

    private static final Logger LOGGER = LogManager.getLogger();

    public TbsCertificatePreparator(X509Chooser chooser, TbsCertificate tbsCertificate) {
        super(chooser, tbsCertificate);
    }

    @Override
    public void prepareSubComponents() {
        prepareVersion();
        prepareSerialNumber();
        prepareSignature();
        prepareIssuer();
        prepareValidity();
        prepareSubject();
        prepareSubjectPublicKeyInfo();
        prepareIssuerUniqueId();
        prepareSubjectUniqueId();
        prepareExtensions();
    }

    private void prepareVersion() {
        field.getVersion().getPreparator(chooser).prepare();
    }

    private void prepareSerialNumber() {
        Asn1Integer serialNumber = field.getSerialNumber();
        Asn1PreparatorHelper.prepareField(serialNumber, chooser.getConfig().getSerialNumber());
    }

    private void prepareSignature() {
        field.getSignature().getPreparator(chooser).prepare();
        field.getSignature().getHandler(chooser).adjustContextAfterPrepare();
    }

    private void prepareIssuer() {
        field.getIssuer().getPreparator(chooser).prepare();
        field.getIssuer().getHandler(chooser).adjustContextAfterPrepare();
    }

    private void prepareValidity() {
        field.getValidity().getPreparator(chooser).prepare();
        field.getValidity().getHandler(chooser).adjustContextAfterPrepare();
    }

    private void prepareSubject() {
        field.getSubject().getPreparator(chooser).prepare();
        field.getSubject().getHandler(chooser).adjustContextAfterPrepare();
    }

    private void prepareSubjectPublicKeyInfo() {
        field.getSubjectPublicKeyInfo().getPreparator(chooser).prepare();
        field.getSubjectPublicKeyInfo().getHandler(chooser).adjustContextAfterPrepare();
    }

    private void prepareIssuerUniqueId() {
        // IssuerUniqueID is an optional field
        if (chooser.getConfig().isIncludeIssuerUniqueId()) {
            Asn1PreparatorHelper.prepareField(
                    field.getIssuerUniqueId(), chooser.getIssuerUniqueId(), (byte) 0);
        }
    }

    private void prepareSubjectUniqueId() {
        // SubjectUniqueID is an optional field
        if (chooser.getConfig().isIncludeSubjectUniqueId()) {
            Asn1PreparatorHelper.prepareField(
                    field.getSubjectUniqueId(), chooser.getConfig().getSubjectUniqueId(), (byte) 0);
        }
    }

    private void prepareExtensions() {
        if (chooser.getConfig().isIncludeExtensions()) {
            LOGGER.warn("Extensions not supported yet");
        }
    }

    @Override
    public byte[] encodeChildrenContent() {
        List children = new ArrayList<>();
        children.add(field.getVersion());
        children.add(field.getSerialNumber());
        children.add(field.getSignature());
        children.add(field.getIssuer());
        children.add(field.getValidity());
        children.add(field.getSubject());
        children.add(field.getSubjectPublicKeyInfo());
        children.add(field.getIssuerUniqueId());
        children.add(field.getSubjectUniqueId());
        children.add(field.getExplicitExtensions());
        // Filter null values
        children.removeIf(child -> child == null);
        return encodeChildren(children);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy