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

org.xipki.security.speed.p12.P12SignLoadTest Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
/*
 *
 * Copyright (c) 2013 - 2017 Lijun Liao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.xipki.security.speed.p12;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.xipki.common.LoadExecutor;
import org.xipki.common.util.IoUtil;
import org.xipki.common.util.ParamUtil;
import org.xipki.security.ConcurrentContentSigner;
import org.xipki.security.SecurityFactory;
import org.xipki.security.SignerConf;
import org.xipki.security.util.AlgorithmUtil;

/**
 * @author Lijun Liao
 * @since 2.0.0
 */

public abstract class P12SignLoadTest extends LoadExecutor {

    class Testor implements Runnable {

        private final byte[] data = new byte[1024];

        public Testor() {
            new SecureRandom().nextBytes(data);
        }

        @Override
        public void run() {
            while (!stop() && getErrorAccout() < 1) {
                try {
                    signer.sign(data);
                    account(1, 0);
                } catch (Exception ex) {
                    account(1, 1);
                }
            }
        }

    } // class Testor

    protected static final String PASSWORD = "1234";

    private final ConcurrentContentSigner signer;

    public P12SignLoadTest(final SecurityFactory securityFactory, final String signatureAlgorithm,
            final byte[] keystore, final String description) throws Exception {
        this("PKCS12", securityFactory, signatureAlgorithm, keystore, description);
    }

    public P12SignLoadTest(final String tokenType,
            final SecurityFactory securityFactory, final String signatureAlgorithm,
            final byte[] keystore, final String description) throws Exception {
        super(description);

        ParamUtil.requireNonNull("securityFactory", securityFactory);
        ParamUtil.requireNonBlank("signatureAlgorithm", signatureAlgorithm);
        ParamUtil.requireNonNull("keystore", keystore);

        SignerConf signerConf = SignerConf.getKeystoreSignerConf(
                new ByteArrayInputStream(keystore), PASSWORD, signatureAlgorithm, 20);
        this.signer = securityFactory.createSigner(tokenType, signerConf, (X509Certificate) null);
    }

    @Override
    protected Runnable getTestor() throws Exception {
        return new Testor();
    }

    // CHECKSTYLE:SKIP
    protected static byte[] getPrecomputedRSAKeystore(final int keysize,
            final BigInteger publicExponent) throws IOException {
        return getPrecomputedKeystore("rsa-" + keysize + "-0x" + publicExponent.toString(16)
            + ".p12");
    }

    // CHECKSTYLE:SKIP
    protected static byte[] getPrecomputedDSAKeystore(final int plength, final int qlength)
            throws IOException {
        return getPrecomputedKeystore("dsa-" + plength + "-" + qlength + ".p12");
    }

    // CHECKSTYLE:SKIP
    protected static byte[] getPrecomputedECKeystore(final String curveNamOrOid)
            throws IOException {
        ASN1ObjectIdentifier oid = AlgorithmUtil.getCurveOidForCurveNameOrOid(curveNamOrOid);
        if (oid == null) {
            return null;
        }

        return getPrecomputedKeystore("ec-" + oid.getId() + ".p12");
    }

    private static byte[] getPrecomputedKeystore(final String filename) throws IOException {
        InputStream in = P12ECSignLoadTest.class.getResourceAsStream("/testkeys/" + filename);
        return (in == null) ? null : IoUtil.read(in);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy