Java Code Examples for sun.security.util.SignatureUtil#initSignWithParam()

The following examples show how to use sun.security.util.SignatureUtil#initSignWithParam() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SignatureGetInstance.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static void testDblInit(PrivateKey key1, PublicKey key2,
        boolean shouldPass, String expectedProvName) throws Exception {
    Signature sig = Signature.getInstance(SIGALG);
    SignatureUtil.initSignWithParam(sig, key1, PSSParameterSpec.DEFAULT, null);
    try {
        sig.initVerify(key2);
        if (!shouldPass) {
            throw new RuntimeException("Fail: should throw InvalidKeyException");
        }
        checkName(sig, expectedProvName);
    } catch (InvalidKeyException ike) {
        if (shouldPass) {
            System.out.println("Fail: Unexpected InvalidKeyException");
            throw ike;
        }
    }
}
 
Example 2
Source File: SignatureGetInstance.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void testDblInit(PrivateKey key1, PublicKey key2,
        boolean shouldPass, String expectedProvName) throws Exception {
    Signature sig = Signature.getInstance(SIGALG);
    SignatureUtil.initSignWithParam(sig, key1, PSSParameterSpec.DEFAULT, null);
    try {
        sig.initVerify(key2);
        if (!shouldPass) {
            throw new RuntimeException("Fail: should throw InvalidKeyException");
        }
        checkName(sig, expectedProvName);
    } catch (InvalidKeyException ike) {
        if (shouldPass) {
            System.out.println("Fail: Unexpected InvalidKeyException");
            throw ike;
        }
    }
}
 
Example 3
Source File: SignatureGetInstance.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void testSetAndInit(String provName, Key key,
        boolean shouldPass) throws Exception {
    Signature sig;
    if (provName == null) {
        sig = Signature.getInstance(SIGALG);
    } else {
        sig = Signature.getInstance(SIGALG, provName);
    }
    AlgorithmParameterSpec params = PSSParameterSpec.DEFAULT;
    boolean doSign = (key instanceof PrivateKey);
    try {
        if (doSign) {
            SignatureUtil.initSignWithParam(sig, (PrivateKey)key, params, null);
        } else {
            SignatureUtil.initVerifyWithParam(sig, (PublicKey)key, params);
        }
        if (!shouldPass) {
            throw new RuntimeException("Fail: should throw InvalidKeyException");
        }
        checkName(sig, provName);
        // check that the earlier parameter is still there
        if (sig.getParameters() == null) {
            throw new RuntimeException("Fail: parameters not preserved");
        }
    } catch (InvalidKeyException ike) {
        if (shouldPass) {
            System.out.println("Fail: Unexpected InvalidKeyException");
            throw ike;
        }
    }
}
 
Example 4
Source File: SignatureGetInstance.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void testSetAndInit(String provName, Key key,
        boolean shouldPass) throws Exception {
    Signature sig;
    if (provName == null) {
        sig = Signature.getInstance(SIGALG);
    } else {
        sig = Signature.getInstance(SIGALG, provName);
    }
    AlgorithmParameterSpec params = PSSParameterSpec.DEFAULT;
    boolean doSign = (key instanceof PrivateKey);
    try {
        if (doSign) {
            SignatureUtil.initSignWithParam(sig, (PrivateKey)key, params, null);
        } else {
            SignatureUtil.initVerifyWithParam(sig, (PublicKey)key, params);
        }
        if (!shouldPass) {
            throw new RuntimeException("Fail: should throw InvalidKeyException");
        }
        checkName(sig, provName);
        // check that the earlier parameter is still there
        if (sig.getParameters() == null) {
            throw new RuntimeException("Fail: parameters not preserved");
        }
    } catch (InvalidKeyException ike) {
        if (shouldPass) {
            System.out.println("Fail: Unexpected InvalidKeyException");
            throw ike;
        }
    }
}
 
Example 5
Source File: Main.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Creates a PKCS#10 cert signing request, corresponding to the
 * keys (and name) associated with a given alias.
 */
private void doCertReq(String alias, String sigAlgName, PrintStream out)
    throws Exception
{
    if (alias == null) {
        alias = keyAlias;
    }

    Pair<Key,char[]> objs = recoverKey(alias, storePass, keyPass);
    PrivateKey privKey = (PrivateKey)objs.fst;
    if (keyPass == null) {
        keyPass = objs.snd;
    }

    Certificate cert = keyStore.getCertificate(alias);
    if (cert == null) {
        MessageFormat form = new MessageFormat
            (rb.getString("alias.has.no.public.key.certificate."));
        Object[] source = {alias};
        throw new Exception(form.format(source));
    }
    PKCS10 request = new PKCS10(cert.getPublicKey());
    CertificateExtensions ext = createV3Extensions(null, null, v3ext, cert.getPublicKey(), null);
    // Attribute name is not significant
    request.getAttributes().setAttribute(X509CertInfo.EXTENSIONS,
            new PKCS10Attribute(PKCS9Attribute.EXTENSION_REQUEST_OID, ext));

    // Construct a Signature object, so that we can sign the request
    if (sigAlgName == null) {
        sigAlgName = getCompatibleSigAlgName(privKey.getAlgorithm());
    }

    Signature signature = Signature.getInstance(sigAlgName);
    AlgorithmParameterSpec params = AlgorithmId
            .getDefaultAlgorithmParameterSpec(sigAlgName, privKey);
    SignatureUtil.initSignWithParam(signature, privKey, params, null);

    X500Name subject = dname == null?
            new X500Name(((X509Certificate)cert).getSubjectDN().toString()):
            new X500Name(dname);

    // Sign the request and base-64 encode it
    request.encodeAndSign(subject, signature);
    request.print(out);

    checkWeak(rb.getString("the.generated.certificate.request"), request);
}
 
Example 6
Source File: SetNullSigParams.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    Signature sig = new SpecialSigImpl();
    SignatureUtil.initVerifyWithParam(sig, (PublicKey) null, null);
    SignatureUtil.initSignWithParam(sig, null, null, null);
}
 
Example 7
Source File: Main.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a PKCS#10 cert signing request, corresponding to the
 * keys (and name) associated with a given alias.
 */
private void doCertReq(String alias, String sigAlgName, PrintStream out)
    throws Exception
{
    if (alias == null) {
        alias = keyAlias;
    }

    Pair<Key,char[]> objs = recoverKey(alias, storePass, keyPass);
    PrivateKey privKey = (PrivateKey)objs.fst;
    if (keyPass == null) {
        keyPass = objs.snd;
    }

    Certificate cert = keyStore.getCertificate(alias);
    if (cert == null) {
        MessageFormat form = new MessageFormat
            (rb.getString("alias.has.no.public.key.certificate."));
        Object[] source = {alias};
        throw new Exception(form.format(source));
    }
    PKCS10 request = new PKCS10(cert.getPublicKey());
    CertificateExtensions ext = createV3Extensions(null, null, v3ext, cert.getPublicKey(), null);
    // Attribute name is not significant
    request.getAttributes().setAttribute(X509CertInfo.EXTENSIONS,
            new PKCS10Attribute(PKCS9Attribute.EXTENSION_REQUEST_OID, ext));

    // Construct a Signature object, so that we can sign the request
    if (sigAlgName == null) {
        sigAlgName = getCompatibleSigAlgName(privKey);
    }

    Signature signature = Signature.getInstance(sigAlgName);
    AlgorithmParameterSpec params = AlgorithmId
            .getDefaultAlgorithmParameterSpec(sigAlgName, privKey);
    SignatureUtil.initSignWithParam(signature, privKey, params, null);

    X500Name subject = dname == null?
            new X500Name(((X509Certificate)cert).getSubjectDN().toString()):
            new X500Name(dname);

    // Sign the request and base-64 encode it
    request.encodeAndSign(subject, signature);
    request.print(out);

    checkWeak(rb.getString("the.generated.certificate.request"), request);
}
 
Example 8
Source File: Main.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Creates a PKCS#10 cert signing request, corresponding to the
 * keys (and name) associated with a given alias.
 */
private void doCertReq(String alias, String sigAlgName, PrintStream out)
    throws Exception
{
    if (alias == null) {
        alias = keyAlias;
    }

    Pair<Key,char[]> objs = recoverKey(alias, storePass, keyPass);
    PrivateKey privKey = (PrivateKey)objs.fst;
    if (keyPass == null) {
        keyPass = objs.snd;
    }

    Certificate cert = keyStore.getCertificate(alias);
    if (cert == null) {
        MessageFormat form = new MessageFormat
            (rb.getString("alias.has.no.public.key.certificate."));
        Object[] source = {alias};
        throw new Exception(form.format(source));
    }
    PKCS10 request = new PKCS10(cert.getPublicKey());
    CertificateExtensions ext = createV3Extensions(null, null, v3ext, cert.getPublicKey(), null);
    // Attribute name is not significant
    request.getAttributes().setAttribute(X509CertInfo.EXTENSIONS,
            new PKCS10Attribute(PKCS9Attribute.EXTENSION_REQUEST_OID, ext));

    // Construct a Signature object, so that we can sign the request
    if (sigAlgName == null) {
        sigAlgName = getCompatibleSigAlgName(privKey.getAlgorithm());
    }

    Signature signature = Signature.getInstance(sigAlgName);
    AlgorithmParameterSpec params = AlgorithmId
            .getDefaultAlgorithmParameterSpec(sigAlgName, privKey);
    SignatureUtil.initSignWithParam(signature, privKey, params, null);

    X500Name subject = dname == null?
            new X500Name(((X509Certificate)cert).getSubjectDN().toString()):
            new X500Name(dname);

    // Sign the request and base-64 encode it
    request.encodeAndSign(subject, signature);
    request.print(out);

    checkWeak(rb.getString("the.generated.certificate.request"), request);
}
 
Example 9
Source File: SetNullSigParams.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    Signature sig = new SpecialSigImpl();
    SignatureUtil.initVerifyWithParam(sig, (PublicKey) null, null);
    SignatureUtil.initSignWithParam(sig, null, null, null);
}