Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
634 views
in Technique[技术] by (71.8m points)

jakarta mail - Java: Convert DKIM private key from RSA to DER for JavaMail

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

    BufferedReader br = new BufferedReader (new FileReader (oldpem_file));
    StringBuilder b64 = null;
    String line;
    while( (line = br.readLine()) != null )
        if( line.equals("-----BEGIN RSA PRIVATE KEY-----") )
            b64 = new StringBuilder ();
        else if( line.equals("-----END RSA PRIVATE KEY-----" ) )
            break;
        else if( b64 != null ) b64.append(line);
    br.close();
    if( b64 == null || line == null ) 
        throw new Exception ("didn't find RSA PRIVATE KEY block in input");

    // b64 now contains the base64 "body" of the PEM-PKCS#1 file
    byte[] oldder = Base64.decode (b64.toString().toCharArray());

    // concatenate the mostly-fixed prefix plus the PKCS#1 data 
    final byte[] prefix = {0x30,(byte)0x82,0,0, 2,1,0, // SEQUENCE(lenTBD) and version INTEGER 
            0x30,0x0d, 6,9,0x2a,(byte)0x86,0x48,(byte)0x86,(byte)0xf7,0x0d,1,1,1, 5,0, // AlgID for rsaEncryption,NULL
            4,(byte)0x82,0,0 }; // OCTETSTRING(lenTBD) 
    byte[] newder = new byte [prefix.length + oldder.length];
    System.arraycopy (prefix,0, newder,0, prefix.length);
    System.arraycopy (oldder,0, newder,prefix.length, oldder.length);
    // and patch the (variable) lengths to be correct
    int len = oldder.length, loc = prefix.length-2; 
    newder[loc] = (byte)(len>>8); newder[loc+1] = (byte)len;
    len = newder.length-4; loc = 2;
    newder[loc] = (byte)(len>>8); newder[loc+1] = (byte)len;

    FileOutputStream fo = new FileOutputStream (newder_file);
    fo.write (newder); fo.close();
    System.out.println ("converted length " + newder.length);

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...