OpenSSLSignaturepublic class OpenSSLSignature extends Signature Implements the JDK MessageDigest interface using OpenSSL's EVP API. |
Fields Summary |
---|
private int | ctxHolds a pointer to the native message digest context. | private int | dsaHolds a pointer to the native DSA key. | private int | rsaHolds a pointer to the native RSA key. | private String | evpAlgorithmHolds the OpenSSL name of the algorithm (lower case, no dashes). | private byte[] | singleByteHolds 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.
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.Object | engineGetParameter(java.lang.String param)
return null;
| protected void | engineInitSign(java.security.PrivateKey privateKey)
throw new UnsupportedOperationException();
| protected void | engineInitVerify(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 void | engineSetParameter(java.lang.String param, java.lang.Object value)
| protected byte[] | engineSign()
throw new UnsupportedOperationException();
| protected void | engineUpdate(byte input)
singleByte[0] = input;
engineUpdate(singleByte, 0, 1);
| protected void | engineUpdate(byte[] input, int offset, int len)
if (state == SIGN) {
throw new UnsupportedOperationException();
} else {
NativeCrypto.EVP_VerifyUpdate(ctx, input, offset, len);
}
| protected boolean | engineVerify(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 void | finalize()
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.OpenSSLSignature | getInstance(java.lang.String algorithm)Creates a new OpenSSLSignature instance for the given algorithm name.
//log("OpenSSLSignature", "getInstance() invoked with " + algorithm);
return new OpenSSLSignature(algorithm);
| private static void | log(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.
}
|
|