/* */ #ifndef _D_LIBSSL_ARC4_CONTEXT_H_ #define _D_LIBSSL_ARC4_CONTEXT_H_ #include "common.h" #include "DlAbortEx.h" #include "StringFormat.h" #include #include namespace aria2 { class LibsslARC4Context { private: EVP_CIPHER_CTX* _cipherCtx; void handleError() const { throw DlAbortEx (StringFormat("Exception in libssl routine(ARC4Context class): %s", ERR_error_string(ERR_get_error(), 0)).str()); } public: LibsslARC4Context():_cipherCtx(0) {} ~LibsslARC4Context() { if(_cipherCtx) { EVP_CIPHER_CTX_cleanup(_cipherCtx); } delete _cipherCtx; } EVP_CIPHER_CTX* getCipherContext() const { return _cipherCtx; } // enc == 1: encryption // enc == 0: decryption void init(const unsigned char* key, size_t keyLength, int enc) { if(_cipherCtx) { EVP_CIPHER_CTX_cleanup(_cipherCtx); } delete _cipherCtx; _cipherCtx = new EVP_CIPHER_CTX; EVP_CIPHER_CTX_init(_cipherCtx); if(!EVP_CipherInit_ex(_cipherCtx, EVP_rc4(), 0, 0, 0, enc)) { handleError(); } if(!EVP_CIPHER_CTX_set_key_length(_cipherCtx, keyLength)) { handleError(); } if(!EVP_CipherInit_ex(_cipherCtx, 0, 0, key, 0, -1)) { handleError(); } } }; } // namespace aria2 #endif // _D_LIBSSL_ARC4_CONTEXT_H_