I'm using DKIM for JavaMail to sign outgoing mail with DKIM.
My private DKIM key is generated with opendkim-genkey -s default -d example.com
and looks like this:
-----BEGIN RSA PRIVATE KEY-----
ABCCXQ...[long string]...SdQaZw9
-----END RSA PRIVATE KEY-----
The DKIM for JavaMail library needs the private DKIM key in DER format as stated in their readme file:
DKIM for JavaMail needs the private key in DER format, you can transform a PEM key with openssl:
openssl pkcs8 -topk8 -nocrypt -in private.key.pem -out private.key.der -outform der
I am looking for a way to avoid having to use openssl to convert my key to DER format. Instead I would like to do the conversion in Java directly.
I have tried different suggestions (1, 2, 3) but nothing has worked so far.
DKIM for Java processes the DER file like this:
File privKeyFile = new File(privkeyFilename);
// read private key DER file
DataInputStream dis = new DataInputStream(new FileInputStream(privKeyFile));
byte[] privKeyBytes = new byte[(int) privKeyFile.length()];
dis.read(privKeyBytes);
dis.close();
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
// decode private key
PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(privKeyBytes);
RSAPrivateKey privKey = (RSAPrivateKey) keyFactory.generatePrivate(privSpec);
So in the end what I need is the RSAPrivateKey
.
How can I easily generate this RSAPrivateKey
that DKIM for JavaMail requires from my RSA private key?
Your reference 3 (only) is correct; as it says your problem is not just converting PEM to DER (which as @Jim says is basically just base64 to binary) but converting PEM containing openssl "traditional" or "legacy" or "PKCS#1" format key data to DER containing PKCS#8 (and specifically PKCS#8 clear/unencrypted) format key data.
The http://juliusdavies.ca/commons-ssl/pkcs8.html pointed to by Alistair's answer looks like it might be a possibility, but I didn't examine in detail. Since PKCS#8 clear (PrivateKeyInfo) for RSA is just a simple ASN.1 wrapper around the PKCS#1, the following (kinda) quick and (very) dirty code provides a minimal solution. Alter the input-reading logic (and error handling) to taste and substitute an available base64 decoder.
Aside: I assume the ABCC in your posted data was redacted. Any valid and reasonable PKCS#1 (clear) RSA key must begin with bytes 0x30 0x82 x where x is from 2 to about 9; when converted to base64 this must begin with MIIC to MIIJ.
The PEM format is just the DER bytes encoded in Base64. You can just read the PEM file as a text file, and take only the lines in between "-----BEGIN RSA PRIVATE KEY-----" and "-----END RSA PRIVATE KEY-----".
Comments are allowed in the PEM file, so you may need to skip additional lines from the beginning until you reach the "-----BEGIN RSA PRIVATE KEY-----".
Here is some code in Ruby that converts PEM to DER just by Base64-decoding the text. You can translate this into Java:
Here is openssl displaying an RSA key I just created:
Here is openssl displaying the DER file that the Ruby code wrote:
This was the PEM (middle part omitted):