how to use windows keystore to establise a ssl connection.
i have write a applet and signed it. this applet should download a file from a server by https and signed this file and then upload this file to server by https.
the certificate used to signed is the same to establish the connection https.
the orign probleme is: when my applet use a keystore like pkcs12 and jks it works perfectely. but when it try use the keysore of windows, i always get a
javax.net.ssl.SSLHandshakeException: Received fatal alert: decrypt_error
i can not paste all my programme here but i paste the most important.
i got my kstore ks by:
try {
// System.out.println("Test windows : "isWindows()" // test keystore : "type.equals("Keystore_Windows"));
if(isWindows() && type.equals("Keystore_Windows")){
System.out.println("Passé windows");
ks=KeyStore.getInstance("Windows-MY");
ks.load(null,null);
keystoreloaded=true;
}if(isMacOs()){
ks=KeyStore.getInstance("KeychainStore");
ks.load(null,null);
keystoreloaded=true;
}if((isWindows() && type.equals("Keystore_logiciel")) || isLinux()){
test();
char[] passwd=null;
JFileChooser keystorechooser = new JFileChooser();
keystorechooser.removeChoosableFileFilter(keystore chooser.getAcceptAllFileFilter());
keystorechooser.setDialogTitle("Ouverture du certificat");
keystorechooser.setFileFilter(new FileFilter() {
@Override
public String getDescription() {
return ".p12, .jks";
}
@Override
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
} else {
String filename = f.getName().toUpperCase();
return filename.endsWith(".PFX") || filename.endsWith(".P12") || filename.endsWith(".JKS");
}
}
});
if(keystorechooser.showOpenDialog(null)==JFileChoo ser.APPROVE_OPTION){
File fileselected=keystorechooser.getSelectedFile();
if(fileselected.getName().toUpperCase().endsWith(" JKS")){
ks = KeyStore.getInstance("JKS");
typekeystore="JKS";
}else{
ks = KeyStore.getInstance("PKCS12");
typekeystore="P12";
passwd = getPassword(keystorechooser.getSelectedFile().getN ame());
}
InputStream is = new FileInputStream(fileselected);
System.out.println("Keystore utilisé : "+fileselected.getName());
if(!getKeystoreAnnule())
ks.load(is, passwd);
else
ks=null;
is.close();
here i get my pass of keystore
if(isWindows() && Type.equals("Keystore_Windows")){
pass="".toCharArray();
}else{
JPasswordField Field = new JPasswordField();
if (JOptionPane.OK_OPTION == JOptionPane.showOptionDialog(null,
Field, "Pass phrase : "+file, JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE, null, null, null)) {
pass = Field.getPassword();
}
here i get my alias
Vector<String> vect=new Vector<String>();
String alias="";
Enumeration<String> enumalias = ks.aliases();
for (; enumalias.hasMoreElements(); ) {
alias = (String)enumalias.nextElement();
if (ks.entryInstanceOf(alias, KeyStore.PrivateKeyEntry.class)) {
System.out.println("Certificat Alias:"+alias);
vect.add(alias);
}
}
if(typekeystore=="P12")
Alias=alias;
if(typekeystore=="JKS" || type.equals("Keystore_Windows"))
Alias=choix(vect);
=====
the function choix:
String[] elements=new String[vect.size()];
for(int i=0;i<vect.size();i+)
elements[i]=vect.elementAt(i);
String s=null;
s = (String)JOptionPane.showInputDialog(
null,
"Choisir l'Alias du certificat",
"Keystore",
JOptionPane.QUESTION_MESSAGE,
null,
elements, // les possibilités
1);
return s;
=============
here is the cod to establish the connection:
downloadFileUrl="https://.....";
Protocol myhttps = new Protocol("https", new AuthSSLProtocolSocketFactory(ks,alias,pass), 443);
Protocol.registerProtocol("https", myhttps);
HttpClient client = new HttpClient();
GetMethod get=new GetMethod(downloadFileUrl);
client.executeMethod(get);