2
0

keygen.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * RSA key generate.
  3. *
  4. * The contents of this file are subject to the Initial
  5. * Developer's Public License Version 1.0 (the "License");
  6. * you may not use this file except in compliance with the
  7. * License. You may obtain a copy of the License at
  8. * https://www.firebirdsql.org/en/initial-developer-s-public-license-version-1-0/
  9. *
  10. * Software distributed under the License is distributed AS IS,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing rights
  13. * and limitations under the License.
  14. *
  15. * The Original Code was created by Alexander Peshkoff
  16. * for the Firebird Open Source RDBMS project.
  17. *
  18. * Copyright (c) 2020 Alexander Peshkoff <[email protected]>
  19. * and all contributors signed below.
  20. *
  21. * All Rights Reserved.
  22. * Contributor(s): ______________________________________.
  23. */
  24. #include "TcWrapper.h"
  25. int main(int ac, char** av)
  26. {
  27. try
  28. {
  29. int len = ac > 1 ? atoi(av[1]) : 2048;
  30. PseudoRandom pseudoRand;
  31. pseudoRand.init(NULL);
  32. rsa_key key;
  33. check(NULL, rsa_make_key(&pseudoRand.state, pseudoRand.index, len / 8, 65537, &key),
  34. "Error making RSA key");
  35. unsigned char outbuf[4096];
  36. unsigned long outlen = sizeof outbuf;
  37. check(NULL, rsa_export(outbuf, &outlen, PK_PRIVATE, &key),
  38. "Error exporting private RSA key");
  39. const char* const file = "fbSampleExtAuth.conf";
  40. FILE* conf = fopen(file, "w");
  41. if (!conf)
  42. {
  43. perror(file);
  44. return 1;
  45. }
  46. fprintf(conf, "Key = ");
  47. for (unsigned i = 0; i < outlen; ++i)
  48. fprintf(conf, "%02x", outbuf[i]);
  49. fprintf(conf, "\n#IgnoreLogin = No\n#IgnorePassword = No\n");
  50. if (fclose(conf) != 0)
  51. {
  52. perror(file);
  53. return 1;
  54. }
  55. printf("Wrote configuration file %s\n", file);
  56. }
  57. catch (const char* message)
  58. {
  59. fprintf(stderr, "%s\n", message);
  60. return 1;
  61. }
  62. return 0;
  63. }