ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

111 lines
2.6 KiB

  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6. #include "cppcms_error.h"
  7. #include "aes_encryptor.h"
  8. #include "base64.h"
  9. using namespace std;
  10. namespace cppcms {
  11. namespace aes {
  12. namespace {
  13. class load {
  14. public:
  15. load() {
  16. gcry_check_version(NULL);
  17. }
  18. };
  19. } // anon namespace
  20. cipher::cipher(string k) :
  21. encryptor(k)
  22. {
  23. bool in=false,out=false;
  24. in=gcry_cipher_open(&hd_in,GCRY_CIPHER_AES,GCRY_CIPHER_MODE_CBC,0) == 0;
  25. out=gcry_cipher_open(&hd_out,GCRY_CIPHER_AES,GCRY_CIPHER_MODE_CBC,0) == 0;
  26. if(!in || !out){
  27. goto error_exit;
  28. }
  29. if( gcry_cipher_setkey(hd_in,&key.front(),16) != 0) {
  30. goto error_exit;
  31. }
  32. if( gcry_cipher_setkey(hd_out,&key.front(),16) != 0)
  33. goto error_exit;
  34. char iv[16];
  35. gcry_create_nonce(iv,sizeof(iv));
  36. gcry_cipher_setiv(hd_out,iv,sizeof(iv));
  37. return;
  38. error_exit:
  39. if(in) gcry_cipher_close(hd_in);
  40. if(out) gcry_cipher_close(hd_out);
  41. throw cppcms_error("AES cipher initialization failed");
  42. }
  43. cipher::~cipher()
  44. {
  45. gcry_cipher_close(hd_in);
  46. gcry_cipher_close(hd_out);
  47. }
  48. string cipher::encrypt(string const &plain,time_t timeout)
  49. {
  50. size_t block_size=(plain.size() + 15) / 16 * 16;
  51. vector<unsigned char> data(sizeof(aes_hdr)+sizeof(info)+block_size,0);
  52. copy(plain.begin(),plain.end(),data.begin() + sizeof(aes_hdr)+sizeof(info));
  53. aes_hdr &aes_header=*(aes_hdr*)(&data.front());
  54. info &header=*(info *)(&data.front()+sizeof(aes_hdr));
  55. header.timeout=timeout;
  56. header.size=plain.size();
  57. memset(&aes_header,0,16);
  58. gcry_md_hash_buffer(GCRY_MD_MD5,&aes_header.md5,&header,block_size+sizeof(info));
  59. gcry_cipher_encrypt(hd_out,&data.front(),data.size(),NULL,0);
  60. return base64_enc(data);
  61. }
  62. bool cipher::decrypt(string const &cipher,string &plain,time_t *timeout)
  63. {
  64. vector<unsigned char> data;
  65. base64_dec(cipher,data);
  66. size_t norm_size=b64url::decoded_size(cipher.size());
  67. if(norm_size<sizeof(info)+sizeof(aes_hdr) || norm_size % 16 !=0)
  68. return false;
  69. gcry_cipher_decrypt(hd_in,&data.front(),data.size(),NULL,0);
  70. gcry_cipher_reset(hd_in);
  71. vector<char> md5(16,0);
  72. gcry_md_hash_buffer(GCRY_MD_MD5,&md5.front(),&data.front()+sizeof(aes_hdr),data.size()-sizeof(aes_hdr));
  73. aes_hdr &aes_header = *(aes_hdr*)&data.front();
  74. if(!std::equal(md5.begin(),md5.end(),aes_header.md5)) {
  75. return false;
  76. }
  77. info &header=*(info *)(&data.front()+sizeof(aes_hdr));
  78. if(time(NULL)>header.timeout)
  79. return false;
  80. if(timeout) *timeout=header.timeout;
  81. vector<unsigned char>::iterator data_start=data.begin()+sizeof(aes_hdr)+sizeof(info),
  82. data_end=data_start+header.size;
  83. plain.assign(data_start,data_end);
  84. return true;
  85. }
  86. } // namespace aes
  87. } // namespace cppcms