00001 /*************************************************************************** 00002 * Copyright (C) 2005-2007 Stefan Schwarzer, Jens Schneider, * 00003 * Matthias Hardt, Guido Madaus * 00004 * * 00005 * Copyright (C) 2007-2008 BerLinux Solutions GbR * 00006 * Stefan Schwarzer & Guido Madaus * 00007 * * 00008 * Copyright (C) 2009-2013 BerLinux Solutions GmbH * 00009 * * 00010 * Authors: * 00011 * Stefan Schwarzer <stefan.schwarzer@diskohq.org>, * 00012 * Matthias Hardt <matthias.hardt@diskohq.org>, * 00013 * Jens Schneider <jens.schneider@diskohq.org>, * 00014 * Guido Madaus <guido.madaus@diskohq.org>, * 00015 * Patrick Helterhoff <patrick.helterhoff@diskohq.org>, * 00016 * René Bählkow <rene.baehlkow@diskohq.org> * 00017 * * 00018 * This library is free software; you can redistribute it and/or * 00019 * modify it under the terms of the GNU Lesser General Public * 00020 * License version 2.1 as published by the Free Software Foundation. * 00021 * * 00022 * This library is distributed in the hope that it will be useful, * 00023 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00025 * Lesser General Public License for more details. * 00026 * * 00027 * You should have received a copy of the GNU Lesser General Public * 00028 * License along with this library; if not, write to the * 00029 * Free Software Foundation, Inc., * 00030 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * 00031 **************************************************************************/ 00032 00033 #ifdef __HAVE_MMSCRYPT__ 00034 #ifndef MMSCRYPT_H_ 00035 #define MMSCRYPT_H_ 00036 00037 #include <openssl/evp.h> 00038 #include "mmstools/mmserror.h" 00039 00040 /** 00041 * @file mmscrypt.h 00042 * 00043 * Header file for MMSCrypt class. 00044 * 00045 * @ingroup mmstools 00046 */ 00047 00048 MMS_CREATEERROR(MMSCryptError); 00049 00050 #define MMSCRYPT_DEFAULT_KEY_FILENAME "./.key" 00051 00052 /** 00053 * Basic crypthographic methods based on OpenSSL functionality. 00054 * 00055 * This class simply provides helper methods for de- and encrypting buffers. 00056 * 00057 * @note To use MMSCrypt you have to build disko with 'enable_crypt=y' 00058 * which depends on having OpenSSL development libraries installed. 00059 */ 00060 class MMSCrypt { 00061 public: 00062 /** 00063 * Constructor for accessing cryptographic functions. 00064 * 00065 * @param keyfile filename of user key 00066 * 00067 * @exception MMSCryptError keyfile could not be opened or created 00068 */ 00069 MMSCrypt(string keyfile = MMSCRYPT_DEFAULT_KEY_FILENAME); 00070 00071 /** 00072 * Destructor of MMSCrypt class. 00073 * 00074 * Frees all used resources. 00075 */ 00076 ~MMSCrypt(); 00077 00078 /** 00079 * Encrypts a given buffer. 00080 * 00081 * @param in buffer to encrypt 00082 * @param size size of buffer (in) to encrypt 00083 * @param useMMSCtx if set to true private disko context will be used, otherwise use user key 00084 * 00085 * @returns Encrypted buffer if the call was successful. 00086 * 00087 * @exception MMSCryptError An error occured while encrypting 00088 * (call MMSError::getMessage() for a detailed error message. 00089 * 00090 * @see decrypt() 00091 */ 00092 unsigned char* encrypt(unsigned char *in, unsigned int size = 0, bool useMMSCtx = false); 00093 00094 /** 00095 * Decrypts a given buffer. 00096 * 00097 * @param in buffer to decrypt 00098 * @param size size of buffer to decrypt 00099 * @param useMMSCtx if set to true private disko context will be used, otherwise use user key 00100 * 00101 * @return Decrypted buffer if the call was successful. 00102 * 00103 * @exception MMSCryptError Not enough memory for decrypting the message. 00104 * 00105 * @see encrypt() 00106 */ 00107 unsigned char* decrypt(unsigned char *in, unsigned int size = 0, bool useMMSCtx = false); 00108 00109 private: 00110 EVP_CIPHER_CTX mmsCtx, /**< private disko cipher context */ 00111 userCtx; /**< user cipher context */ 00112 00113 /** 00114 * Creates an SSL key that will be saved in the given file. 00115 * 00116 * @param keyfile save encrypted key to this file 00117 * 00118 * @note The memory for the returned key has to be freed. 00119 * 00120 * @return Unencrypted key (NULL if error occured). 00121 * 00122 * @see getUserKey() 00123 */ 00124 unsigned char* createUserKey(string keyfile); 00125 00126 /** 00127 * Returns an SSL key that was stored in the given file. 00128 * If the file doesn't exist, a new key will be generated 00129 * and saved. 00130 * 00131 * @param keyfile read encrypted key from this file 00132 * 00133 * @note The memory for the returned key has to be freed. 00134 * 00135 * @return Unencrypted key (NULL if error occured). 00136 * 00137 * @see createUserKey() 00138 * 00139 * @exception MMSCryptError File could not be opened. 00140 */ 00141 unsigned char* getUserKey(string keyfile); 00142 }; 00143 00144 #endif /* MMSCRYPT_H_ */ 00145 #endif /* __HAVE_MMSCRYPT__ */