↑ arvau

Stack Overflow / How to generate a certificate with DH parameters

I need to support Diffie Hellman encryption, now in order to test this i need to create a certificate with DH key parameters eg. key-length - 2048 etc.

Now as i understand DH doesn't work with self-signed certificates, so basically i need to create a certificate issued by some trusted third party containing DH key parameters.

I searched a lot but can't seem to find proper direction, no where can i find a way to create a cert with DH parameters.

Can someone point me in right direction?? Thanks in advance!!

Stack Overflow / How to generate a certificate with DH parameters

I need to support Diffie Hellman encryption

DH is key exchange (or key agreement) protocol, not encryption. DH is used to securely generate a common key between two parties, other algorithms are used for encryption itself.

I need to create a certificate with DH key parameters eg. key-length - 2048 etc

There is nothing like DH parameters in a certificate.

DH is only one of ways how a public key can be used. You may generate a DH public key with specified length (e.g. 2048 bit) and execute the DH exchange, but it has nothing to do with certificate parameters. (didn't you mean to generate a keypair, not a certificate?).

Indeed the DH key exchange needs other parameters (p, g), but the parameters are part of the protocol, not the certificate. In TLS even the DH parameters can be random and authenticated by the certificate's public key - it is called Ephemeral Diffie-Hellman key exchange.

You could generate DH parameters (p, g) separately:

 openssl dhparam -out dhparams.pem 4096

Can someone point me in right direction??

Now I assume you want to establish an encrypted channel (TLS) using DH. The easiest way would be to specify allowed parameters for SSL for the library. This is an example httpd configuration, where you can enforce DH key exchange. Every reasonable SSL framework or server has option to set the parameters.

If you want to do the DH key exchange yourself (not as part of TLS), I'd advice to use an out-of-box mature library for your programming language.

Stack Overflow / How to generate a certificate with DH parameters

Yes, it seems true that OpenSSL will not create a certificate for DH keys. You'll get an error like: operation not supported for this keytype. The reason someone would want to do this is that they want to store the DH public key in a keystore; which seems like a reasonable place to store them. In my case, I want my client application to be deployed with the server's public DH key. The problem is that keystores don't let you store public keys in them. They do however, let you store certificates in them. Hence the need for creating a certificate that contains a DH public key. Since you can't do this using OpenSSL, you'll have to do it in code. Here's the code that I use to create a certificate. If you pass in a DH public key, you'll be able to add this certificate to a keystore:

public X509Certificate createCert (PublicKey publicKey, PrivateKey caKey, X509Certificate caCert , String subject) throws Exception {
    Date now = new Date();
    Date exp = new Date(now.getTime()+2555*86400*1000); // 2555 days validity
    BigInteger.valueOf(now.getTime());

    ContentSigner signer = new JcaContentSignerBuilder("SHA256with"+caKey.getAlgorithm()).build(caKey);
    SubjectPublicKeyInfo subPubKeyInfo = SubjectPublicKeyInfo.getInstance(publicKey.getEncoded());


    byte[] enc = new X509v3CertificateBuilder(
            /*issuer*/ new X500Name(caCert.getIssuerX500Principal().getName()),
            /*serial*/BigInteger.valueOf(now.getTime()),
            /*validity*/now, exp,
            /*subject*/new X500Name (subject),
            /*spki*/subPubKeyInfo).build(signer).getEncoded();
    X509Certificate cert = (X509Certificate)CertificateFactory.getInstance ("X.509") .generateCertificate(new ByteArrayInputStream(enc));
    return cert;
}

The code uses several BouncyCastle classes. Besides passing in the DH public key, you have to pass in the CA private key and the CA certificate and a String that contains the subject for the certificate that you generate. Hope that helps for those wanting to store a DH public key in a keystore.