00001 /* Licensed to the Apache Software Foundation (ASF) under one or more 00002 * contributor license agreements. See the NOTICE file distributed with 00003 * this work for additional information regarding copyright ownership. 00004 * The ASF licenses this file to You under the Apache License, Version 2.0 00005 * (the "License"); you may not use this file except in compliance with 00006 * the License. You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef APR_CRYPTO_H 00018 #define APR_CRYPTO_H 00019 00020 #include "apu.h" 00021 #include "apr_pools.h" 00022 #include "apr_tables.h" 00023 #include "apu_errno.h" 00024 00025 #ifdef __cplusplus 00026 extern "C" { 00027 #endif 00028 00029 /** 00030 * @file apr_crypto.h 00031 * @brief APR-UTIL Crypto library 00032 */ 00033 /** 00034 * @defgroup APR_Util_Crypto Crypto routines 00035 * @ingroup APR_Util 00036 * @{ 00037 */ 00038 00039 /** CA certificate type unknown */ 00040 #define APR_CRYPTO_CA_TYPE_UNKNOWN 0 00041 /** binary DER encoded CA certificate */ 00042 #define APR_CRYPTO_CA_TYPE_DER 1 00043 /** PEM encoded CA certificate */ 00044 #define APR_CRYPTO_CA_TYPE_BASE64 2 00045 /** Netscape/Mozilla cert7.db CA certificate database */ 00046 #define APR_CRYPTO_CA_TYPE_CERT7_DB 3 00047 /** Netscape/Mozilla secmod file */ 00048 #define APR_CRYPTO_CA_TYPE_SECMOD 4 00049 /** Client certificate type unknown */ 00050 #define APR_CRYPTO_CERT_TYPE_UNKNOWN 5 00051 /** binary DER encoded client certificate */ 00052 #define APR_CRYPTO_CERT_TYPE_DER 6 00053 /** PEM encoded client certificate */ 00054 #define APR_CRYPTO_CERT_TYPE_BASE64 7 00055 /** Netscape/Mozilla key3.db client certificate database */ 00056 #define APR_CRYPTO_CERT_TYPE_KEY3_DB 8 00057 /** Netscape/Mozilla client certificate nickname */ 00058 #define APR_CRYPTO_CERT_TYPE_NICKNAME 9 00059 /** Private key type unknown */ 00060 #define APR_CRYPTO_KEY_TYPE_UNKNOWN 10 00061 /** binary DER encoded private key */ 00062 #define APR_CRYPTO_KEY_TYPE_DER 11 00063 /** PEM encoded private key */ 00064 #define APR_CRYPTO_KEY_TYPE_BASE64 12 00065 /** PKCS#12 encoded client certificate */ 00066 #define APR_CRYPTO_CERT_TYPE_PFX 13 00067 /** PKCS#12 encoded private key */ 00068 #define APR_CRYPTO_KEY_TYPE_PFX 14 00069 /** Openldap directory full of base64-encoded cert 00070 * authorities with hashes in corresponding .0 directory 00071 */ 00072 #define APR_CRYPTO_CA_TYPE_CACERTDIR_BASE64 15 00073 /** CMS Key Database with private key and cert chain */ 00074 #define APR_CRYPTO_CA_TYPE_CMS 16 00075 /** Symmetrical key */ 00076 #define APR_CRYPTO_KEY_TYPE_SYM 17 00077 /** Netscape/Mozilla certificate database directory */ 00078 #define APR_CRYPTO_CA_TYPE_DIR 18 00079 /** Crypto engine */ 00080 #define APR_CRYPTO_ENGINE 101 00081 00082 #if APU_HAVE_CRYPTO 00083 00084 #ifndef APU_CRYPTO_RECOMMENDED_DRIVER 00085 #if APU_HAVE_OPENSSL 00086 #define APU_CRYPTO_RECOMMENDED_DRIVER "openssl" 00087 #else 00088 #if APU_HAVE_NSS 00089 #define APU_CRYPTO_RECOMMENDED_DRIVER "nss" 00090 #else 00091 #if APU_HAVE_MSCNG 00092 #define APU_CRYPTO_RECOMMENDED_DRIVER "mscng" 00093 #else 00094 #if APU_HAVE_MSCAPI 00095 #define APU_CRYPTO_RECOMMENDED_DRIVER "mscapi" 00096 #else 00097 #endif 00098 #endif 00099 #endif 00100 #endif 00101 #endif 00102 00103 /** 00104 * Symmetric Key types understood by the library. 00105 * 00106 * NOTE: It is expected that this list will grow over time. 00107 * 00108 * Interoperability Matrix: 00109 * 00110 * The matrix is based on the testcrypto.c unit test, which attempts to 00111 * test whether a simple encrypt/decrypt will succeed, as well as testing 00112 * whether an encrypted string by one library can be decrypted by the 00113 * others. 00114 * 00115 * Some libraries will successfully encrypt and decrypt their own data, 00116 * but won't decrypt data from another library. It is hoped that over 00117 * time these anomalies will be found and fixed, but until then it is 00118 * recommended that ciphers are chosen that interoperate across platform. 00119 * 00120 * An X below means the test passes, it does not necessarily mean that 00121 * encryption performed is correct or secure. Applications should stick 00122 * to ciphers that pass the interoperablity tests on the right hand side 00123 * of the table. 00124 * 00125 * Aligned data is data whose length is a multiple of the block size for 00126 * the chosen cipher. Padded data is data that is not aligned by block 00127 * size and must be padded by the crypto library. 00128 * 00129 * OpenSSL NSS Interop 00130 * Align Pad Align Pad Align Pad 00131 * 3DES_192/CBC X X X X X X 00132 * 3DES_192/ECB X X 00133 * AES_256/CBC X X X X X X 00134 * AES_256/ECB X X X X 00135 * AES_192/CBC X X X X 00136 * AES_192/ECB X X X 00137 * AES_128/CBC X X X X 00138 * AES_128/ECB X X X 00139 * 00140 * Conclusion: for padded data, use 3DES_192/CBC or AES_256/CBC. For 00141 * aligned data, use 3DES_192/CBC, AES_256/CBC or AES_256/ECB. 00142 */ 00143 00144 typedef enum { 00145 KEY_NONE, KEY_3DES_192, /** 192 bit (3-Key) 3DES */ 00146 KEY_AES_128, /** 128 bit AES */ 00147 KEY_AES_192, /** 192 bit AES */ 00148 KEY_AES_256 00149 /** 256 bit AES */ 00150 } apr_crypto_block_key_type_e; 00151 00152 typedef enum { 00153 MODE_NONE, /** An error condition */ 00154 MODE_ECB, /** Electronic Code Book */ 00155 MODE_CBC 00156 /** Cipher Block Chaining */ 00157 } apr_crypto_block_key_mode_e; 00158 00159 /** 00160 * Certificate and private key structure. 00161 * 00162 * The various crypto backends expect certificates and keys in a wide 00163 * array of formats. This structure is analogous to apr_ldap_opt_tls_cert_t 00164 * from the LDAP interface. Ultimately that interface should be meshed with 00165 * this one. 00166 * @param type Type of certificate APR_CRYPTO_*_TYPE_* 00167 * @param path Path, file or nickname of the certificate 00168 * @param password Optional password, can be NULL 00169 */ 00170 typedef struct apr_crypto_param_t { 00171 int type; 00172 const char *path; 00173 const char *password; 00174 } apr_crypto_param_t; 00175 00176 /* These are opaque structs. Instantiation is up to each backend */ 00177 typedef struct apr_crypto_driver_t apr_crypto_driver_t; 00178 typedef struct apr_crypto_config_t apr_crypto_config_t; 00179 typedef struct apr_crypto_key_t apr_crypto_key_t; 00180 typedef struct apr_crypto_block_t apr_crypto_block_t; 00181 00182 /** 00183 * Public factory API, common to all backends. 00184 */ 00185 typedef struct apr_crypto_t { 00186 apr_pool_t *pool; 00187 apu_err_t *result; 00188 apr_array_header_t *keys; 00189 apr_crypto_config_t *config; 00190 } apr_crypto_t; 00191 00192 /** 00193 * @brief Perform once-only initialisation. Call once only. 00194 * 00195 * @param pool - pool to register any shutdown cleanups, etc 00196 * @return APR_NOTIMPL in case of no crypto support. 00197 */ 00198 APR_DECLARE(apr_status_t) apr_crypto_init(apr_pool_t *pool, 00199 const apr_array_header_t *params); 00200 00201 /** 00202 * @brief Get the driver struct for a name 00203 * 00204 * @param pool - (process) pool to register cleanup 00205 * @param name - driver name 00206 * @param driver - pointer to driver struct. 00207 * @return APR_SUCCESS for success 00208 * @return APR_ENOTIMPL for no driver (when DSO not enabled) 00209 * @return APR_EDSOOPEN if DSO driver file can't be opened 00210 * @return APR_ESYMNOTFOUND if the driver file doesn't contain a driver 00211 */ 00212 APR_DECLARE(apr_status_t) apr_crypto_get_driver(apr_pool_t *pool, const char *name, 00213 const apr_crypto_driver_t **driver, const apr_array_header_t *params, 00214 const apu_err_t **result); 00215 00216 /** 00217 * @brief Return the name of the driver. 00218 * 00219 * @param driver - The driver in use. 00220 * @return The name of the driver. 00221 */ 00222 APR_DECLARE(const char *)apr_crypto_driver_name (const apr_crypto_driver_t *driver); 00223 00224 /** 00225 * @brief Get the result of the last operation on a factory. If the result 00226 * is NULL, the operation was successful. 00227 * @param driver - driver to use 00228 * @param factory - factory pointer will be written here 00229 * @param result - the result structure 00230 * @return APR_SUCCESS for success 00231 */ 00232 APR_DECLARE(apr_status_t) apr_crypto_error(const apr_crypto_t *f, 00233 const apu_err_t **result); 00234 00235 /** 00236 * @brief Create a context for supporting encryption. Keys, certificates, 00237 * algorithms and other parameters will be set per context. More than 00238 * one context can be created at one time. A cleanup will be automatically 00239 * registered with the given pool to guarantee a graceful shutdown. 00240 * @param driver - driver to use 00241 * @param pool - process pool 00242 * @param params - array of key parameters 00243 * @param factory - factory pointer will be written here 00244 * @return APR_ENOENGINE when the engine specified does not exist. APR_EINITENGINE 00245 * if the engine cannot be initialised. 00246 */ 00247 APR_DECLARE(apr_status_t) apr_crypto_factory(const apr_crypto_driver_t *driver, 00248 apr_pool_t *pool, const apr_array_header_t *params, apr_crypto_t **f); 00249 00250 /** 00251 * @brief Create a key from the given passphrase. By default, the PBKDF2 00252 * algorithm is used to generate the key from the passphrase. It is expected 00253 * that the same pass phrase will generate the same key, regardless of the 00254 * backend crypto platform used. The key is cleaned up when the context 00255 * is cleaned, and may be reused with multiple encryption or decryption 00256 * operations. 00257 * @note If *key is NULL, a apr_crypto_key_t will be created from a pool. If 00258 * *key is not NULL, *key must point at a previously created structure. 00259 * @param driver - driver to use 00260 * @param p The pool to use. 00261 * @param f The context to use. 00262 * @param pass The passphrase to use. 00263 * @param passLen The passphrase length in bytes 00264 * @param salt The salt to use. 00265 * @param saltLen The salt length in bytes 00266 * @param type 3DES_192, AES_128, AES_192, AES_256. 00267 * @param mode Electronic Code Book / Cipher Block Chaining. 00268 * @param doPad Pad if necessary. 00269 * @param key The key returned, see note. 00270 * @param ivSize The size of the initialisation vector will be returned, based 00271 * on whether an IV is relevant for this type of crypto. 00272 * @return Returns APR_ENOKEY if the pass phrase is missing or empty, or if a backend 00273 * error occurred while generating the key. APR_ENOCIPHER if the type or mode 00274 * is not supported by the particular backend. APR_EKEYTYPE if the key type is 00275 * not known. APR_EPADDING if padding was requested but is not supported. 00276 * APR_ENOTIMPL if not implemented. 00277 */ 00278 APR_DECLARE(apr_status_t) apr_crypto_passphrase(const apr_crypto_driver_t *driver, 00279 apr_pool_t *p, const apr_crypto_t *f, const char *pass, 00280 apr_size_t passLen, const unsigned char * salt, apr_size_t saltLen, 00281 const apr_crypto_block_key_type_e type, 00282 const apr_crypto_block_key_mode_e mode, const int doPad, 00283 const int iterations, apr_crypto_key_t **key, apr_size_t *ivSize); 00284 00285 /** 00286 * @brief Initialise a context for encrypting arbitrary data using the given key. 00287 * @note If *ctx is NULL, a apr_crypto_block_t will be created from a pool. If 00288 * *ctx is not NULL, *ctx must point at a previously created structure. 00289 * @param driver - driver to use 00290 * @param p The pool to use. 00291 * @param f The block factory to use. 00292 * @param key The key structure to use. 00293 * @param iv Optional initialisation vector. If the buffer pointed to is NULL, 00294 * an IV will be created at random, in space allocated from the pool. 00295 * If the buffer pointed to is not NULL, the IV in the buffer will be 00296 * used. 00297 * @param ctx The block context returned, see note. 00298 * @param blockSize The block size of the cipher. 00299 * @return Returns APR_ENOIV if an initialisation vector is required but not specified. 00300 * Returns APR_EINIT if the backend failed to initialise the context. Returns 00301 * APR_ENOTIMPL if not implemented. 00302 */ 00303 APR_DECLARE(apr_status_t) apr_crypto_block_encrypt_init( 00304 const apr_crypto_driver_t *driver, apr_pool_t *p, 00305 const apr_crypto_t *f, const apr_crypto_key_t *key, 00306 const unsigned char **iv, apr_crypto_block_t **ctx, 00307 apr_size_t *blockSize); 00308 00309 /** 00310 * @brief Encrypt data provided by in, write it to out. 00311 * @note The number of bytes written will be written to outlen. If 00312 * out is NULL, outlen will contain the maximum size of the 00313 * buffer needed to hold the data, including any data 00314 * generated by apr_crypto_block_encrypt_finish below. If *out points 00315 * to NULL, a buffer sufficiently large will be created from 00316 * the pool provided. If *out points to a not-NULL value, this 00317 * value will be used as a buffer instead. 00318 * @param driver - driver to use 00319 * @param ctx The block context to use. 00320 * @param out Address of a buffer to which data will be written, 00321 * see note. 00322 * @param outlen Length of the output will be written here. 00323 * @param in Address of the buffer to read. 00324 * @param inlen Length of the buffer to read. 00325 * @return APR_ECRYPT if an error occurred. Returns APR_ENOTIMPL if 00326 * not implemented. 00327 */ 00328 APR_DECLARE(apr_status_t) apr_crypto_block_encrypt( 00329 const apr_crypto_driver_t *driver, apr_crypto_block_t *ctx, 00330 unsigned char **out, apr_size_t *outlen, const unsigned char *in, 00331 apr_size_t inlen); 00332 00333 /** 00334 * @brief Encrypt final data block, write it to out. 00335 * @note If necessary the final block will be written out after being 00336 * padded. Typically the final block will be written to the 00337 * same buffer used by apr_crypto_block_encrypt, offset by the 00338 * number of bytes returned as actually written by the 00339 * apr_crypto_block_encrypt() call. After this call, the context 00340 * is cleaned and can be reused by apr_crypto_block_encrypt_init(). 00341 * @param driver - driver to use 00342 * @param ctx The block context to use. 00343 * @param out Address of a buffer to which data will be written. This 00344 * buffer must already exist, and is usually the same 00345 * buffer used by apr_evp_crypt(). See note. 00346 * @param outlen Length of the output will be written here. 00347 * @return APR_ECRYPT if an error occurred. 00348 * @return APR_EPADDING if padding was enabled and the block was incorrectly 00349 * formatted. 00350 * @return APR_ENOTIMPL if not implemented. 00351 */ 00352 APR_DECLARE(apr_status_t) apr_crypto_block_encrypt_finish( 00353 const apr_crypto_driver_t *driver, apr_crypto_block_t *ctx, 00354 unsigned char *out, apr_size_t *outlen); 00355 00356 /** 00357 * @brief Initialise a context for decrypting arbitrary data using the given key. 00358 * @note If *ctx is NULL, a apr_crypto_block_t will be created from a pool. If 00359 * *ctx is not NULL, *ctx must point at a previously created structure. 00360 * @param driver - driver to use 00361 * @param p The pool to use. 00362 * @param f The block factory to use. 00363 * @param key The key structure to use. 00364 * @param iv Optional initialisation vector. 00365 * @param ctx The block context returned, see note. 00366 * @param blockSize The block size of the cipher. 00367 * @return Returns APR_ENOIV if an initialisation vector is required but not specified. 00368 * Returns APR_EINIT if the backend failed to initialise the context. Returns 00369 * APR_ENOTIMPL if not implemented. 00370 */ 00371 APR_DECLARE(apr_status_t) apr_crypto_block_decrypt_init( 00372 const apr_crypto_driver_t *driver, apr_pool_t *p, 00373 const apr_crypto_t *f, const apr_crypto_key_t *key, 00374 const unsigned char *iv, apr_crypto_block_t **ctx, 00375 apr_size_t *blockSize); 00376 00377 /** 00378 * @brief Decrypt data provided by in, write it to out. 00379 * @note The number of bytes written will be written to outlen. If 00380 * out is NULL, outlen will contain the maximum size of the 00381 * buffer needed to hold the data, including any data 00382 * generated by apr_crypto_block_decrypt_finish below. If *out points 00383 * to NULL, a buffer sufficiently large will be created from 00384 * the pool provided. If *out points to a not-NULL value, this 00385 * value will be used as a buffer instead. 00386 * @param driver - driver to use 00387 * @param ctx The block context to use. 00388 * @param out Address of a buffer to which data will be written, 00389 * see note. 00390 * @param outlen Length of the output will be written here. 00391 * @param in Address of the buffer to read. 00392 * @param inlen Length of the buffer to read. 00393 * @return APR_ECRYPT if an error occurred. Returns APR_ENOTIMPL if 00394 * not implemented. 00395 */ 00396 APR_DECLARE(apr_status_t) apr_crypto_block_decrypt( 00397 const apr_crypto_driver_t *driver, apr_crypto_block_t *ctx, 00398 unsigned char **out, apr_size_t *outlen, const unsigned char *in, 00399 apr_size_t inlen); 00400 00401 /** 00402 * @brief Decrypt final data block, write it to out. 00403 * @note If necessary the final block will be written out after being 00404 * padded. Typically the final block will be written to the 00405 * same buffer used by apr_crypto_block_decrypt, offset by the 00406 * number of bytes returned as actually written by the 00407 * apr_crypto_block_decrypt() call. After this call, the context 00408 * is cleaned and can be reused by apr_crypto_block_decrypt_init(). 00409 * @param driver - driver to use 00410 * @param ctx The block context to use. 00411 * @param out Address of a buffer to which data will be written. This 00412 * buffer must already exist, and is usually the same 00413 * buffer used by apr_evp_crypt(). See note. 00414 * @param outlen Length of the output will be written here. 00415 * @return APR_ECRYPT if an error occurred. 00416 * @return APR_EPADDING if padding was enabled and the block was incorrectly 00417 * formatted. 00418 * @return APR_ENOTIMPL if not implemented. 00419 */ 00420 APR_DECLARE(apr_status_t) apr_crypto_block_decrypt_finish( 00421 const apr_crypto_driver_t *driver, apr_crypto_block_t *ctx, 00422 unsigned char *out, apr_size_t *outlen); 00423 00424 /** 00425 * @brief Clean encryption / decryption context. 00426 * @note After cleanup, a context is free to be reused if necessary. 00427 * @param driver - driver to use 00428 * @param ctx The block context to use. 00429 * @return Returns APR_ENOTIMPL if not supported. 00430 */ 00431 APR_DECLARE(apr_status_t) apr_crypto_block_cleanup( 00432 const apr_crypto_driver_t *driver, apr_crypto_block_t *ctx); 00433 00434 /** 00435 * @brief Clean encryption / decryption factory. 00436 * @note After cleanup, a factory is free to be reused if necessary. 00437 * @param driver - driver to use 00438 * @param f The factory to use. 00439 * @return Returns APR_ENOTIMPL if not supported. 00440 */ 00441 APR_DECLARE(apr_status_t) apr_crypto_cleanup(const apr_crypto_driver_t *driver, 00442 apr_crypto_t *f); 00443 00444 /** 00445 * @brief Shutdown the crypto library. 00446 * @note After shutdown, it is expected that the init function can be called again. 00447 * @param driver - driver to use 00448 * @param p The pool to use. 00449 * @return Returns APR_ENOTIMPL if not supported. 00450 */ 00451 APR_DECLARE(apr_status_t) apr_crypto_shutdown(const apr_crypto_driver_t *driver, 00452 apr_pool_t *p); 00453 00454 #endif /* APU_HAVE_CRYPTO */ 00455 00456 /** @} */ 00457 00458 #ifdef __cplusplus 00459 } 00460 #endif 00461 00462 #endif
1.5.8