mod_session_crypto.c revision fed63d1b62cc7e56aad77b70ee5b5cc7f5c6aade
d7759bdad133e1e92fe41aab75dd8c1584eda38cjim/* Licensed to the Apache Software Foundation (ASF) under one or more
d7759bdad133e1e92fe41aab75dd8c1584eda38cjim * contributor license agreements. See the NOTICE file distributed with
d7759bdad133e1e92fe41aab75dd8c1584eda38cjim * this work for additional information regarding copyright ownership.
4e191199a0aeab09d78df8f5579e745572e8b7bcwsanchez * The ASF licenses this file to You under the Apache License, Version 2.0
f3a5934ca0fb0f0f813bd9d9d06af8937e3f401fjim * (the "License"); you may not use this file except in compliance with
f3a5934ca0fb0f0f813bd9d9d06af8937e3f401fjim * the License. You may obtain a copy of the License at
d7759bdad133e1e92fe41aab75dd8c1584eda38cjim * Unless required by applicable law or agreed to in writing, software
b3155b9e3b06b20030ec6c16d98f8dabf12ead9cianh * distributed under the License is distributed on an "AS IS" BASIS,
b3155b9e3b06b20030ec6c16d98f8dabf12ead9cianh * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
b3155b9e3b06b20030ec6c16d98f8dabf12ead9cianh * See the License for the specific language governing permissions and
b3155b9e3b06b20030ec6c16d98f8dabf12ead9cianh * limitations under the License.
ff1234e45aca1b8171d711ecb87f58b9d9100a99ianh#include "apr_base64.h" /* for apr_base64_decode et al */
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe * Structure to carry the per-dir session config.
b999f6ba2a266bf9a92687f31bb7e76021ac5891ianhtypedef struct {
2ceedfca3a2fdfdb5ff60ca17f030ce91f6331cbwrowe const char *passphrase;
7d7931c1f4d46b041ad2ad6fdb9499289063611eianh const char *certfile;
b9b69856aec9ea58ea1b1e5aff669e8eaf2ebce4rbb const char *keyfile;
b9b69856aec9ea58ea1b1e5aff669e8eaf2ebce4rbb const char *cipher;
1f3a44f2fd7f9fee00b80c7ddcf1028ea145f91drbb const char *digest;
7d7931c1f4d46b041ad2ad6fdb9499289063611eianh const char *engine;
43ea154f46345c9722c2b25b2b9f5086ec79eaeeorlikowski * Initialise the encryption as per the current config.
0c233c76f21b358f4a0d81e0f956339ca727c14cchuck * Returns APR_SUCCESS if successful.
0c233c76f21b358f4a0d81e0f956339ca727c14cchuck#if APU_MAJOR_VERSION > 1 || (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 3)
0c233c76f21b358f4a0d81e0f956339ca727c14cchuckstatic apr_status_t crypt_init(request_rec * r, apr_evp_factory_t ** f, apr_evp_crypt_key_e * key, session_crypto_dir_conf * conf)
0c233c76f21b358f4a0d81e0f956339ca727c14cchuck if (!conf->certfile_set && !conf->keyfile_set && !conf->passphrase_set) {
53c2eb831bfe47860e3f5ec9190b15cb92f15181chuck ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, LOG_PREFIX
c1635d9f723f28fed4b95e5d9693e554a79e8d77orlikowski "encryption not configured, "
c1635d9f723f28fed4b95e5d9693e554a79e8d77orlikowski "no passphrase or certfile/keyfile set");
c1635d9f723f28fed4b95e5d9693e554a79e8d77orlikowski /* set up */
4f7dd0949d92462a8adc31eee8aff266eea55204chuck res = apr_evp_factory_create(f, conf->keyfile, conf->certfile, NULL,
4f7dd0949d92462a8adc31eee8aff266eea55204chuck NULL, NULL, conf->digest, APR_EVP_FACTORY_ASYM, r->pool);
4f7dd0949d92462a8adc31eee8aff266eea55204chuck ap_log_rerror(APLOG_MARK, APLOG_ERR, res, r, LOG_PREFIX
75b3ed55173b29dbdf9e2fb6ec5462bfceee21aechuck "generic public/private key encryption is not supported by "
75b3ed55173b29dbdf9e2fb6ec5462bfceee21aechuck "this version of APR. session encryption not possible");
0c233c76f21b358f4a0d81e0f956339ca727c14cchuck res = apr_evp_factory_create(f, NULL, NULL, conf->cipher,
68bcde9c52e9e749482df2800dbdff09559115e0chuck conf->passphrase, NULL, conf->digest, APR_EVP_FACTORY_SYM, r->pool);
68bcde9c52e9e749482df2800dbdff09559115e0chuck ap_log_rerror(APLOG_MARK, APLOG_ERR, res, r, LOG_PREFIX
68bcde9c52e9e749482df2800dbdff09559115e0chuck "generic symmetrical encryption is not supported by this "
68bcde9c52e9e749482df2800dbdff09559115e0chuck "version of APR. session encryption not possible");
68bcde9c52e9e749482df2800dbdff09559115e0chuck ap_log_rerror(APLOG_MARK, APLOG_ERR, res, r, LOG_PREFIX
68bcde9c52e9e749482df2800dbdff09559115e0chuck ap_log_rerror(APLOG_MARK, APLOG_ERR, res, r, LOG_PREFIX
68bcde9c52e9e749482df2800dbdff09559115e0chuck ap_log_rerror(APLOG_MARK, APLOG_ERR, res, r, LOG_PREFIX
68bcde9c52e9e749482df2800dbdff09559115e0chuck "the public and private key could not be extracted from "
68bcde9c52e9e749482df2800dbdff09559115e0chuck "the certificates");
68bcde9c52e9e749482df2800dbdff09559115e0chuck ap_log_rerror(APLOG_MARK, APLOG_ERR, res, r, LOG_PREFIX
68bcde9c52e9e749482df2800dbdff09559115e0chuck ap_log_rerror(APLOG_MARK, APLOG_ERR, res, r, LOG_PREFIX
68bcde9c52e9e749482df2800dbdff09559115e0chuck "encryption could not be configured. Please check the "
68bcde9c52e9e749482df2800dbdff09559115e0chuck "certificates and/or passphrase as appropriate");
68bcde9c52e9e749482df2800dbdff09559115e0chuck * Encrypt the string given as per the current config.
68bcde9c52e9e749482df2800dbdff09559115e0chuck * Returns APR_SUCCESS if successful.
68bcde9c52e9e749482df2800dbdff09559115e0chuckstatic apr_status_t encrypt_string(request_rec * r, const char *in, char **out)
68bcde9c52e9e749482df2800dbdff09559115e0chuck#if APU_MAJOR_VERSION > 1 || (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 3)
68bcde9c52e9e749482df2800dbdff09559115e0chuck session_crypto_dir_conf *conf = ap_get_module_config(r->per_dir_config,
68bcde9c52e9e749482df2800dbdff09559115e0chuck /* don't attempt to encrypt an empty string, trying to do so causes a segfault */
68bcde9c52e9e749482df2800dbdff09559115e0chuck res = apr_evp_crypt_init(f, &e, APR_EVP_ENCRYPT, key, r->pool);
68bcde9c52e9e749482df2800dbdff09559115e0chuck ap_log_rerror(APLOG_MARK, APLOG_ERR, res, r, LOG_PREFIX
68bcde9c52e9e749482df2800dbdff09559115e0chuck "encryption could be configured but not initialised");
68bcde9c52e9e749482df2800dbdff09559115e0chuck /* encrypt the given string */
68bcde9c52e9e749482df2800dbdff09559115e0chuck res = apr_evp_crypt(e, &encrypt, &encryptlen, (unsigned char *) in, strlen(in));
68bcde9c52e9e749482df2800dbdff09559115e0chuck ap_log_rerror(APLOG_MARK, APLOG_ERR, res, r, LOG_PREFIX
68bcde9c52e9e749482df2800dbdff09559115e0chuck "attempt to encrypt failed");
68bcde9c52e9e749482df2800dbdff09559115e0chuck res = apr_evp_crypt_finish(e, encrypt + encryptlen, &tlen);
68bcde9c52e9e749482df2800dbdff09559115e0chuck ap_log_rerror(APLOG_MARK, APLOG_ERR, res, r, LOG_PREFIX
68bcde9c52e9e749482df2800dbdff09559115e0chuck "attempt to finish the encryption failed");
68bcde9c52e9e749482df2800dbdff09559115e0chuck /* base64 encode the result */
68bcde9c52e9e749482df2800dbdff09559115e0chuck base64 = apr_pcalloc(r->pool, apr_base64_encode_len(encryptlen + 1) * sizeof(char));
68bcde9c52e9e749482df2800dbdff09559115e0chuck apr_base64_encode(base64, (const char *) encrypt, encryptlen);
0c233c76f21b358f4a0d81e0f956339ca727c14cchuck /* clean up afterwards */
68bcde9c52e9e749482df2800dbdff09559115e0chuck ap_log_rerror(APLOG_MARK, APLOG_ERR, APR_ENOTIMPL, r, LOG_PREFIX
68bcde9c52e9e749482df2800dbdff09559115e0chuck "crypto is not supported by APR on this platform");
68bcde9c52e9e749482df2800dbdff09559115e0chuck * Decrypt the string given as per the current config.
68bcde9c52e9e749482df2800dbdff09559115e0chuck * Returns APR_SUCCESS if successful.
68bcde9c52e9e749482df2800dbdff09559115e0chuckstatic apr_status_t decrypt_string(request_rec * r, const char *in, char **out)
68bcde9c52e9e749482df2800dbdff09559115e0chuck#if APU_MAJOR_VERSION > 1 || (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 3)
68bcde9c52e9e749482df2800dbdff09559115e0chuck session_crypto_dir_conf *conf = ap_get_module_config(r->per_dir_config,
68bcde9c52e9e749482df2800dbdff09559115e0chuck res = apr_evp_crypt_init(f, &e, APR_EVP_DECRYPT, key, r->pool);
4f7dd0949d92462a8adc31eee8aff266eea55204chuck ap_log_rerror(APLOG_MARK, APLOG_ERR, res, r, LOG_PREFIX
68bcde9c52e9e749482df2800dbdff09559115e0chuck "decryption could be configured but not initialised");
if (res) {
return res;
return res;
return APR_SUCCESS;
return APR_ENOTIMPL;
return res;
return OK;
return res;
return OK;
apr_ssl_init();
return (void *) new;
session_crypto_dir_conf *new = (session_crypto_dir_conf *) apr_pcalloc(p, sizeof(session_crypto_dir_conf));
return new;
if (!filepath) {
return NULL;
return NULL;
if (!res) {
return res;
if (!res) {
return res;
return NULL;
return NULL;
return NULL;
AP_INIT_TAKE1("SessionCryptoCertificateFile", set_crypto_certificate_file, NULL, RSRC_CONF|OR_AUTHCFG,
AP_INIT_TAKE1("SessionCryptoCertificateKeyFile", set_crypto_certificate_keyfile, NULL, RSRC_CONF|OR_AUTHCFG,
{NULL}