Error on chapter8 SignedMailExample - android
I'm now working on making message sign app for android
First, the following code works successfully on my desktop with JDK 1.6.
(pure java environment)
However,when I trying to run on eclipse for android platform,
It shows "The method setKeyEntry(String, Key, char[], Certificate[]) in the type KeyStore is not applicable for the arguments (String, PrivateKey, char[], Certificate[])" around the line " store.setKeyEntry();"..
I know it is due to the desktop use JDK security provider, while the android use spongy castle security provider.
thus, could you give me any advise on
1. using JDK security provider manually on android.
=> for example I use "Security.addProvider(new org.spongycastle.jce.provider.BouncyCastleProvider ());" to use bouncy castle provider
2. how to modify the above error with "The method setKeyEntry()..."
regards
-SignedMailExample.java
package exam.blowfishcipher;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.security.*;
import java.security.cert.*;
import java.util.Arrays;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.spongycastle.asn1.ASN1EncodableVector;
import org.spongycastle.asn1.cms.AttributeTable;
import org.spongycastle.asn1.smime.SMIMECapabilitiesAttri bute;
import org.spongycastle.asn1.smime.SMIMECapability;
import org.spongycastle.asn1.smime.SMIMECapabilityVector;
import org.spongycastle.asn1.smime.SMIMEEncryptionKeyPref erenceAttribute;
import org.spongycastle.asn1.x509.Certificate;
import org.spongycastle.jce.PKCS10CertificationRequest;
import org.spongycastle.mail.smime.SMIMESigned;
import org.spongycastle.mail.smime.SMIMESignedGenerator;
import org.spongycastle.mail.smime.SMIMEUtil;
import org.spongycastle.openssl.PEMReader;
import org.spongycastle.openssl.PEMWriter;
import android.os.*;
import android.util.*;
/**
* a simple example that creates and processes a signed mail message.
*/
public class SignedMailExample
extends SignedDataProcessor
{
//static String userCert;
//static String userPrivate;
//add 20130510
//static String signedMail;
static {
Security.addProvider(new org.spongycastle.jce.provider.BouncyCastleProvider ());
}
public static MimeMultipart createMultipartWithSignature(
PrivateKey key,
X509Certificate cert,
CertStore certsAndCRLs,
MimeBodyPart dataPart)
throws Exception
{
// create some smime capabilities in case someone wants to respond
ASN1EncodableVector signedAttrs = new ASN1EncodableVector();
SMIMECapabilityVector caps = new SMIMECapabilityVector();
caps.addCapability(SMIMECapability.aES256_CBC);
caps.addCapability(SMIMECapability.dES_EDE3_CBC);
caps.addCapability(SMIMECapability.rC2_CBC, 128);
signedAttrs.add(new SMIMECapabilitiesAttribute(caps));
signedAttrs.add(new SMIMEEncryptionKeyPreferenceAttribute(SMIMEUtil.cr eateIssuerAndSerialNumberFor(cert)));
// set up the generator
SMIMESignedGenerator gen = new SMIMESignedGenerator();
gen.addSigner(key, cert, SMIMESignedGenerator.DIGEST_SHA256, new AttributeTable(signedAttrs), null);
gen.addCertificatesAndCRLs(certsAndCRLs);
// create the signed message
return gen.generate(dataPart, "BC");
}
////////////////////////////////////////////added by jeon
public static void pemEncodeToFile(String filename, Object obj, char[] password) throws Exception{
PEMWriter pw = new PEMWriter(new FileWriter(filename));
if (password != null && password.length > 0) {
pw.writeObject(obj, "DESEDE", password, new SecureRandom());
} else {
pw.writeObject(obj);
}
pw.flush();
pw.close();
}
////////////////////////////////////////////added by jeon
////////////////////////////////////////////added by jeon
public static KeyStore createCredentials_modified()
throws Exception
{
KeyStore store = KeyStore.getInstance("BKS");
store.load(null, null);
X500PrivateCredential rootCredential = createRootCredential_modified();
store.setCertificateEntry(rootCredential.getAlias( ), rootCredential.getCertificate());
store.setKeyEntry(rootCredential.getAlias(), rootCredential.getPrivateKey(), "password".toCharArray(),
new Certificate[] { rootCredential.getCertificate(), rootCredential.getCertificate(), rootCredential.getCertificate() });
return store;
}
public static X500PrivateCredential createRootCredential_modified()
throws Exception
{
PEMReader rootPriva = new PEMReader(
new InputStreamReader(
new FileInputStream(Environment.getExternalStorageDire ctory()+"/pkcs10priv.key"))); //modified 20130510
PEMReader rootCerti = new PEMReader(
new InputStreamReader(
new FileInputStream(Environment.getExternalStorageDire ctory()+"/userCert.cer")));
KeyPair rootPrivate = (KeyPair)rootPriva.readObject();
X509Certificate rootCert = (X509Certificate)rootCerti.readObject();
//KeyPair rootPair = generateRSAKeyPair();
//X509Certificate rootCert = generateRootCert(rootPair);
return new X500PrivateCredential(rootCert, rootPrivate.getPrivate(), "root"); //?????? root
}
////////////////////////////////////////////added by jeon
public static String signMail(String plainMessage) throws Exception{
KeyStore credentials = createCredentials_modified();
PrivateKey key = (PrivateKey)credentials.getKey("root", "password".toCharArray());
java.security.cert.Certificate[] chain = credentials.getCertificateChain("root");
CertStore certsAndCRLs = CertStore.getInstance("Collection",
new CollectionCertStoreParameters(Arrays.asList(chain) ), "BC");
X509Certificate cert = (X509Certificate)chain[0];
Log.e("Position", "position1");
// create the message we want signed
MimeBodyPart dataPart = new MimeBodyPart();
dataPart.setText(plainMessage);
Log.e("Position", "position2");
// create the signed message
MimeMultipart multiPart = createMultipartWithSignature(key, cert, certsAndCRLs, dataPart);
// create the mail message
MimeMessage mail = Utils.createMimeMessage("my signed message", multiPart, multiPart.getContentType());
Log.e("Position", "position3");
//added by JEON
FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDir ectory()+"/SignedSMS.mail");
String SingedSMS = mail.toString();
fos.write(SingedSMS.getBytes());
fos.close();
////
return SingedSMS;
}
////
}
|