Search in sources :

Example 76 with Signature

use of java.security.Signature in project cloudstack by apache.

the class SAMLUtils method generateSAMLRequestSignature.

public static String generateSAMLRequestSignature(final String urlEncodedString, final PrivateKey signingKey, final String sigAlgorithmName) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException, UnsupportedEncodingException {
    if (signingKey == null) {
        return urlEncodedString;
    }
    String opensamlAlgoIdSignature = SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA1;
    String javaSignatureAlgorithmName = "SHA1withRSA";
    if (sigAlgorithmName.equalsIgnoreCase("SHA256")) {
        opensamlAlgoIdSignature = SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA256;
        javaSignatureAlgorithmName = "SHA256withRSA";
    } else if (sigAlgorithmName.equalsIgnoreCase("SHA384")) {
        opensamlAlgoIdSignature = SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA384;
        javaSignatureAlgorithmName = "SHA384withRSA";
    } else if (sigAlgorithmName.equalsIgnoreCase("SHA512")) {
        opensamlAlgoIdSignature = SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA512;
        javaSignatureAlgorithmName = "SHA512withRSA";
    }
    String url = urlEncodedString + "&SigAlg=" + URLEncoder.encode(opensamlAlgoIdSignature, HttpUtils.UTF_8);
    Signature signature = Signature.getInstance(javaSignatureAlgorithmName);
    signature.initSign(signingKey);
    signature.update(url.getBytes(Charset.forName("UTF-8")));
    String signatureString = Base64.encodeBytes(signature.sign(), Base64.DONT_BREAK_LINES);
    if (signatureString != null) {
        return url + "&Signature=" + URLEncoder.encode(signatureString, HttpUtils.UTF_8);
    }
    return url;
}
Also used : Signature(java.security.Signature)

Example 77 with Signature

use of java.security.Signature in project robovm by robovm.

the class Signature2Test method test_clone.

/**
     * java.security.Signature#clone()
     */
public void test_clone() throws Exception {
    Signature s = Signature.getInstance("DSA");
    try {
        s.clone();
        fail();
    } catch (CloneNotSupportedException expected) {
    }
}
Also used : Signature(java.security.Signature)

Example 78 with Signature

use of java.security.Signature in project robovm by robovm.

the class Signature2Test method test_getParameters.

/**
     * java.security.Signature#getParameters()
     */
public void test_getParameters() throws Exception {
    Signature sig = Signature.getInstance("DSA");
    try {
        sig.getParameters();
    } catch (UnsupportedOperationException e) {
    // Could be that the operation is not supported
    }
    try {
        MySignature sig2 = new MySignature("test");
        sig2.getParameters();
        fail();
    } catch (UnsupportedOperationException expected) {
    }
    MySignature sig2 = new MySignature("ABC");
    sig2.getParameters();
}
Also used : Signature(java.security.Signature)

Example 79 with Signature

use of java.security.Signature in project robovm by robovm.

the class Signature2Test method test_setParameterLjava_lang_StringLjava_lang_Object.

/**
     * java.security.Signature#setParameter(java.lang.String,
     *        java.lang.Object)
     */
@SuppressWarnings("deprecation")
public void test_setParameterLjava_lang_StringLjava_lang_Object() throws Exception {
    Signature sig = Signature.getInstance("DSA");
    try {
        sig.setParameter("r", BigInteger.ONE);
        sig.setParameter("s", BigInteger.ONE);
    } catch (InvalidParameterException e) {
    // Could be that it's an invalid param for the found algorithm
    } catch (UnsupportedOperationException e) {
    // Could be that the operation is not supported
    }
}
Also used : InvalidParameterException(java.security.InvalidParameterException) Signature(java.security.Signature)

Example 80 with Signature

use of java.security.Signature in project robovm by robovm.

the class Signature2Test method test_verify$BII.

/**
     * java.security.Signature#verify(byte[], int, int)
     */
public void test_verify$BII() throws Exception {
    Signature sig = Signature.getInstance("DSA");
    sig.initSign(getDsaKeys().getPrivate());
    sig.update(MESSAGE.getBytes());
    byte[] signature = sig.sign();
    sig.initVerify(getDsaKeys().getPublic());
    sig.update(MESSAGE.getBytes());
    assertTrue("Sign/Verify does not pass", sig.verify(signature, 0, signature.length));
    try {
        sig.verify(null, 0, signature.length);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    try {
        sig.verify(signature, -5, signature.length);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    if (StandardNames.IS_RI) {
        try {
            sig.verify(signature, signature.length, 0);
            fail();
        } catch (SignatureException expected) {
        }
    } else {
        // Calling Signature.verify a second time should not throw
        // http://code.google.com/p/android/issues/detail?id=34933
        boolean verified = sig.verify(signature, signature.length, 0);
        assertFalse(verified);
    }
    try {
        sig.verify(signature, 0, signature.length * 2);
        fail();
    } catch (IllegalArgumentException expected) {
    }
}
Also used : Signature(java.security.Signature) SignatureException(java.security.SignatureException)

Aggregations

Signature (java.security.Signature)261 SignatureException (java.security.SignatureException)84 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)70 InvalidKeyException (java.security.InvalidKeyException)61 PublicKey (java.security.PublicKey)61 PrivateKey (java.security.PrivateKey)43 IOException (java.io.IOException)42 KeyFactory (java.security.KeyFactory)41 X509Certificate (java.security.cert.X509Certificate)26 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)23 KeyPair (java.security.KeyPair)19 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)19 GeneralSecurityException (java.security.GeneralSecurityException)16 KeyPairGenerator (java.security.KeyPairGenerator)16 MySignature1 (org.apache.harmony.security.tests.support.MySignature1)16 ByteArrayInputStream (java.io.ByteArrayInputStream)14 BigInteger (java.math.BigInteger)14 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)14 CertificateException (java.security.cert.CertificateException)14 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)14