Dobry den,
Potřeboval bych poradit s odeslání podepsaného emailu (používá se Windows-MY
keystore).
Kód na získaní Certificate chain a PrivateKey:
KeyStore keystore = KeyStore.getInstance("Windows-MY", "SunMSCAPI");
keystore.load(null, null);
PrivateKey key = (PrivateKey) keystore.getKey(alias, pass);
Certificate[] chain = keystore.getCertificateChain(alias);
Problém je, ze daný kód vrátí PrivateKey jako třídu
sun.security.mscapi.RSAPrivateKey,
a u PKCS#12 jako třídu RSAPrivateCrtKeyImpl.
Následně mám na tvorbu podepsaného emailu kód, kde se použije BouncyCastle
S/MIME:
Multipart mp = createMultipart(message_tex);
SMIMESignedGenerator sign_gen = new SMIMESignedGenerator();
sign_gen.addSigner(key, (X509Certificate)chain[0],
SMIMESignedGenerator.DIGEST_SHA1);
List certList = new ArrayList();
for(int i = 0; i < chain.length; i++) {
certList.add((X509Certificate)chain[i]);
}
CollectionCertStoreParameters csparams = new
CollectionCertStoreParameters(certList);
CertStore certStore = CertStore.getInstance("Collection", csparams);
sign_gen.addCertificatesAndCRLs(certStore);
MimeBodyPart to_sign = new MimeBodyPart();
to_sign.setContent(mp);
MimeMultipart signed = sign_gen.generate(to_sign, "BC");
sendMultipart(subject, signed);
Kód ale vyhodí v případě PrivateKey ziskaneho z Windows-MY výjimku:
javax.mail.MessagingException: IOException while sending message;
nested exception is:
java.io.IOException: java.security.InvalidKeyException: Supplied key
(sun.security.mscapi.RSAPrivateKey) is not a RSAPrivateKey instance
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:676)
at javax.mail.Transport.send0(Transport.java:189)
at javax.mail.Transport.send(Transport.java:118)
Pro PKCS#12 keystore, posílání bez problémů projde.
Díky za každý tip.
Vítek