- /* Load DH parameters into the context */
- dh = DH_new();
- if (!dh) {
- CtdlLogPrintf(CTDL_CRIT, "init_ssl() can't allocate a DH object: %s\n",
- ERR_reason_error_string(ERR_get_error()));
- SSL_CTX_free(ssl_ctx);
- ssl_ctx = NULL;
- return;
- }
- if (!(BN_hex2bn(&(dh->p), DH_P))) {
- CtdlLogPrintf(CTDL_CRIT, "init_ssl() can't assign DH_P: %s\n",
- ERR_reason_error_string(ERR_get_error()));
- SSL_CTX_free(ssl_ctx);
- ssl_ctx = NULL;
- return;
- }
- if (!(BN_hex2bn(&(dh->g), DH_G))) {
- CtdlLogPrintf(CTDL_CRIT, "init_ssl() can't assign DH_G: %s\n",
- ERR_reason_error_string(ERR_get_error()));
- SSL_CTX_free(ssl_ctx);
- ssl_ctx = NULL;
- return;
- }
- dh->length = DH_L;
- SSL_CTX_set_tmp_dh(ssl_ctx, dh);
- DH_free(dh);
-
- /* Get our certificates in order.
- * First, create the key/cert directory if it's not there already...
- */
- mkdir(ctdl_key_dir, 0700);
-
- /*
- * Generate a key pair if we don't have one.
- */
- if (access(file_crpt_file_key, R_OK) != 0) {
- CtdlLogPrintf(CTDL_INFO, "Generating RSA key pair.\n");
- rsa = RSA_generate_key(1024, /* modulus size */
- 65537, /* exponent */
- NULL, /* no callback */
- NULL); /* no callback */
- if (rsa == NULL) {
- CtdlLogPrintf(CTDL_CRIT, "Key generation failed: %s\n",
- ERR_reason_error_string(ERR_get_error()));
- }
- if (rsa != NULL) {
- fp = fopen(file_crpt_file_key, "w");
- if (fp != NULL) {
- chmod(file_crpt_file_key, 0600);
- if (PEM_write_RSAPrivateKey(fp, /* the file */
- rsa, /* the key */
- NULL, /* no enc */
- NULL, /* no passphr */
- 0, /* no passphr */
- NULL, /* no callbk */
- NULL /* no callbk */
- ) != 1) {
- CtdlLogPrintf(CTDL_CRIT, "Cannot write key: %s\n",
- ERR_reason_error_string(ERR_get_error()));
- unlink(file_crpt_file_key);
- }
- fclose(fp);
- }
- RSA_free(rsa);
- }
- }