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.
 
 
 
 
 
 

136 lines
3.4 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // See accompanying file COPYING.TXT file for licensing details.
  6. //
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #include <cppcms/json.h>
  9. #include <cppcms/urandom.h>
  10. #include <cppcms/crypto.h>
  11. #include <iostream>
  12. #include <fstream>
  13. #include "tohex.h"
  14. void help()
  15. {
  16. std::cerr <<
  17. "Usage: cppcms_make_key --hmac HMAC [--hmac-file hmac_key_file] [--cbc CBC] \n"
  18. " [--cbc-file cbc_key_file.txt] [-h] [--output output_file.js]\n"
  19. " -h: display help\n"
  20. " HMAC: is one of md5, sha1, sha224, sah256, sha384 or sha512\n"
  21. " CBC: is one of aes, aes128, aes192, aes256\n"
  22. "\n"
  23. " If the options --hmac-file or --cbc-file are not given \n"
  24. " the keys are created inline withing the json configuration\n"
  25. " --output output configuration file, default stdout\n"
  26. "\n"
  27. "For example:\n"
  28. " cppcms_make_key --hmac sha1 --hmac-file hmac.txt --output config.js\n"
  29. " cppcms_make_key --hmac sha1 --cbc aes --hmac-file hmac.txt --cbc-file cbc.txt\n";
  30. }
  31. std::string make_key(int size)
  32. {
  33. std::vector<char> key(size,0);
  34. std::vector<char> res(size*2+1,0);
  35. cppcms::urandom_device r;
  36. r.generate(&key[0],size);
  37. cppcms::impl::tohex(&key[0],size,&res[0]);
  38. std::string s = &res[0];
  39. return s;
  40. }
  41. bool make_entry(cppcms::json::value &val,std::string type,std::string algo,std::string file,int bytes)
  42. {
  43. val["session"]["client"][type]=algo;
  44. std::string key=make_key(bytes);
  45. if(!file.empty()) {
  46. std::ofstream f(file.c_str());
  47. if(!f) {
  48. std::cerr << "Failed to open file " << file << std::endl;
  49. return false;
  50. }
  51. f << key << std::endl;
  52. f.close();
  53. val["session"]["client"][type +"_key_file"]=file;
  54. }
  55. else {
  56. val["session"]["client"][type+"_key"] = key;
  57. }
  58. return true;
  59. }
  60. int main(int argc,char **argv)
  61. {
  62. std::string hmac,hmac_file;
  63. std::string cbc,cbc_file;
  64. std::string output;
  65. for(int i=1;i<argc;i++) {
  66. std::string arg=argv[i];
  67. if(arg=="-h") {
  68. help();
  69. return 0;
  70. }
  71. else if(i+1==argc) {
  72. help();
  73. return 1;
  74. }
  75. i++;
  76. std::string next = argv[i];
  77. if(arg=="--hmac")
  78. hmac = next;
  79. else if(arg=="--cbc")
  80. cbc = next;
  81. else if(arg=="--hmac-file")
  82. hmac_file = next;
  83. else if(arg=="--hmac-cbc")
  84. cbc_file = next;
  85. else if(arg=="--output")
  86. output = next;
  87. else {
  88. help();
  89. return 1;
  90. }
  91. }
  92. if(hmac.empty()) {
  93. help();
  94. return 1;
  95. }
  96. cppcms::json::value val;
  97. std::auto_ptr<cppcms::crypto::message_digest> digest = cppcms::crypto::message_digest::create_by_name(hmac);
  98. if(!digest.get()) {
  99. std::cerr << "Unsupported HMAC " << hmac << std::endl;
  100. return 1;
  101. }
  102. if(!make_entry(val,"hmac",hmac,hmac_file,digest->digest_size()))
  103. return 1;
  104. if(!cbc.empty()) {
  105. std::auto_ptr<cppcms::crypto::cbc> p = cppcms::crypto::cbc::create(cbc);
  106. if(!p.get()) {
  107. std::cerr << "Unsupported CBC " << cbc << std::endl;
  108. return 1;
  109. }
  110. if(!make_entry(val,"cbc",cbc,cbc_file,p->key_size()))
  111. return 1;
  112. }
  113. if(output.empty()) {
  114. val.save(std::cout,cppcms::json::readable);
  115. }
  116. else {
  117. std::ofstream out(output.c_str());
  118. if(!out) {
  119. std::cerr << "Failed to create a file " << output << std::endl;
  120. return 1;
  121. }
  122. val.save(out,cppcms::json::readable);
  123. }
  124. return 0;
  125. }