Java源码示例:sun.security.util.SignatureUtil

示例1
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;
        }
    }
}
 
示例2
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;
        }
    }
}
 
示例3
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;
        }
    }
}
 
示例4
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;
        }
    }
}
 
示例5
/**
 * 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);
}
 
示例6
public static void main(String[] args) throws Exception {
    Signature sig = new SpecialSigImpl();
    SignatureUtil.initVerifyWithParam(sig, (PublicKey) null, null);
    SignatureUtil.initSignWithParam(sig, null, null, null);
}
 
示例7
/**
 * 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);
}
 
示例8
/**
 * 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);
}
 
示例9
public static void main(String[] args) throws Exception {
    Signature sig = new SpecialSigImpl();
    SignatureUtil.initVerifyWithParam(sig, (PublicKey) null, null);
    SignatureUtil.initSignWithParam(sig, null, null, null);
}