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

de.rub.nds.x509attacker.filesystem.CertificateFileWriter 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.filesystem;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class CertificateFileWriter {

    public static final String CERTIFICATE_PEM_PREFIX = "-----BEGIN CERTIFICATE-----\r\n";

    public static final String CERTIFICATE_PEM_SUFFIX = "\r\n-----END CERTIFICATE-----\r\n";

    private final FileOutputStream outputStream;

    public CertificateFileWriter(final File file) throws IOException {
        outputStream = new FileOutputStream(file);
    }

    public void writeCertificate(final byte[] certificateBytes) throws IOException {
        this.writeCertificate(
                new String(
                        Base64.getMimeEncoder(64, "\r\n".getBytes()).encode(certificateBytes),
                        StandardCharsets.UTF_8));
    }

    public void writeCertificate(final String certificateBase64String) throws IOException {
        outputStream.write(CERTIFICATE_PEM_PREFIX.getBytes());
        outputStream.write(certificateBase64String.getBytes());
        outputStream.write(CERTIFICATE_PEM_SUFFIX.getBytes());
    }

    public void close() throws IOException {
        outputStream.close();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy