FileDocCategorySizeDatePackage
OpenSSLSignature.javaAPI DocAndroid 1.5 API7288Wed May 06 22:41:06 BST 2009org.apache.harmony.xnet.provider.jsse

OpenSSLSignature

public class OpenSSLSignature extends Signature
Implements the JDK MessageDigest interface using OpenSSL's EVP API.

Fields Summary
private int
ctx
Holds a pointer to the native message digest context.
private int
dsa
Holds a pointer to the native DSA key.
private int
rsa
Holds a pointer to the native RSA key.
private String
evpAlgorithm
Holds the OpenSSL name of the algorithm (lower case, no dashes).
private byte[]
singleByte
Holds a dummy buffer for writing single bytes to the digest.
Constructors Summary
private OpenSSLSignature(String algorithm)
Creates a new OpenSSLSignature instance for the given algorithm name.

param
algorithm The name of the algorithm, e.g. "SHA1".

        super(algorithm);
        
        int i = algorithm.indexOf("with"); 
        if (i == -1) {
            throw new NoSuchAlgorithmException(algorithm);
        }

        // For the special combination of DSA and SHA1, we need to pass the
        // algorithm name as a pair consisting of crypto algorithm and hash
        // algorithm. For all other (RSA) cases, passing the hash algorithm
        // alone is not only sufficient, but actually necessary. OpenSSL
        // doesn't accept something like RSA-SHA1.
        if ("1.3.14.3.2.26with1.2.840.10040.4.1".equals(algorithm)
                || "SHA1withDSA".equals(algorithm)
                || "SHAwithDSA".equals(algorithm)) {
            evpAlgorithm = "DSA-SHA";
        } else {
            evpAlgorithm = algorithm.substring(0, i).replace("-", "").toUpperCase();
        }

        ctx = NativeCrypto.EVP_new();
    
Methods Summary
protected java.lang.ObjectengineGetParameter(java.lang.String param)

        return null;
    
protected voidengineInitSign(java.security.PrivateKey privateKey)

        throw new UnsupportedOperationException();
    
protected voidengineInitVerify(java.security.PublicKey publicKey)

        //log("OpenSSLSignature", "engineInitVerify() invoked with " + publicKey.getClass().getCanonicalName());
        
        if (publicKey instanceof DSAPublicKey) {
            try {
                DSAPublicKey dsaPublicKey = (DSAPublicKey)publicKey;
                DSAParams dsaParams = dsaPublicKey.getParams();
                dsa = NativeCrypto.EVP_PKEY_new_DSA(dsaParams.getP().toByteArray(), 
                        dsaParams.getQ().toByteArray(), dsaParams.getG().toByteArray(),
                        dsaPublicKey.getY().toByteArray(), null);

            } catch (Exception ex) {
                throw new InvalidKeyException(ex.toString());
            }
        } else if (publicKey instanceof RSAPublicKey) {
            try {
                RSAPublicKey rsaPublicKey = (RSAPublicKey)publicKey;
                rsa = NativeCrypto.EVP_PKEY_new_RSA(rsaPublicKey.getModulus().toByteArray(),
                        rsaPublicKey.getPublicExponent().toByteArray(), null, null, null);

            } catch (Exception ex) {
                throw new InvalidKeyException(ex.toString());
            }
        } else {
            throw new InvalidKeyException("Need DSA or RSA public key");
        }
        
        try {
            NativeCrypto.EVP_VerifyInit(ctx, evpAlgorithm);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    
protected voidengineSetParameter(java.lang.String param, java.lang.Object value)

    
protected byte[]engineSign()

        throw new UnsupportedOperationException();
    
protected voidengineUpdate(byte input)

        singleByte[0] = input;
        engineUpdate(singleByte, 0, 1);
    
protected voidengineUpdate(byte[] input, int offset, int len)

        if (state == SIGN) {
            throw new UnsupportedOperationException();
        } else {
            NativeCrypto.EVP_VerifyUpdate(ctx, input, offset, len);
        }
    
protected booleanengineVerify(byte[] sigBytes)

        int handle = (rsa != 0) ? rsa : dsa;
        
        if (handle == 0) {
            // This can't actually happen, but you never know...
            throw new SignatureException("Need DSA or RSA public key");
        }
        
        try {
            int result = NativeCrypto.EVP_VerifyFinal(ctx, sigBytes, 0, sigBytes.length, handle);
            return result == 1;
        } catch (Exception ex) {
            throw new SignatureException(ex);
        }
        
    
protected voidfinalize()

        super.finalize();
        
        if (dsa != 0) {
            NativeCrypto.EVP_PKEY_free(dsa);
        }

        if (rsa != 0) {
            NativeCrypto.EVP_PKEY_free(rsa);
        }
        
        if (ctx != 0) {
            NativeCrypto.EVP_free(ctx);
        }
    
public static org.apache.harmony.xnet.provider.jsse.OpenSSLSignaturegetInstance(java.lang.String algorithm)
Creates a new OpenSSLSignature instance for the given algorithm name.

param
algorithm The name of the algorithm, e.g. "SHA1".
return
The new OpenSSLSignature instance.
throws
RuntimeException In case of problems.


                                           
           
        //log("OpenSSLSignature", "getInstance() invoked with " + algorithm);
        return new OpenSSLSignature(algorithm);
    
private static voidlog(java.lang.String tag, java.lang.String msg)

        try {
            Class clazz = Class.forName("android.util.Log");
            Method method = clazz.getMethod("d", new Class[] {
                    String.class, String.class
            });
            method.invoke(null, new Object[] {
                    tag, msg
            });
        } catch (Exception ex) {
            // Silently ignore.
        }