ssh_genkey.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "ssh.h"
  2. char *base = "/sys/lib/ssh/hostkey";
  3. RSApriv *key;
  4. void
  5. usage(void)
  6. {
  7. fprint(2, "usage: aux/ssh_genkey [basename]\n");
  8. fprint(2, "\tdefault basename is /sys/lib/ssh/hostkey\n");
  9. exits("usage");
  10. }
  11. void
  12. printsecret(int fd)
  13. {
  14. fprint(fd, "%d %B %B %B %B %B %B %B %B",
  15. mpsignif(key->pub.n), key->pub.ek,
  16. key->dk, key->pub.n, key->p, key->q,
  17. key->kp, key->kq, key->c2);
  18. }
  19. void
  20. printsecretfactotum(int fd)
  21. {
  22. fprint(fd, "key proto=sshrsa size=%d ek=%B !dk=%B n=%B !p=%B !q=%B !kp=%B !kq=%B !c2=%B",
  23. mpsignif(key->pub.n), key->pub.ek,
  24. key->dk, key->pub.n, key->p, key->q,
  25. key->kp, key->kq, key->c2);
  26. }
  27. /*
  28. void
  29. printpublic(int fd)
  30. {
  31. fprint(fd, "%d %B %B\n", mpsignif(key->pub.n), key->pub.ek, key->pub.n);
  32. }
  33. */
  34. void
  35. printpublic(int fd)
  36. {
  37. fprint(fd, "%d %.10B %.10B\n", mpsignif(key->pub.n), key->pub.ek, key->pub.n);
  38. }
  39. void
  40. writefile(char *fmt, int perm, void (*fn)(int))
  41. {
  42. int fd;
  43. char *s;
  44. s = smprint(fmt, base);
  45. if(s == nil)
  46. sysfatal("smprint: %r");
  47. if((fd = create(s, OWRITE, perm)) < 0)
  48. sysfatal("create %s: %r", s);
  49. (*fn)(fd);
  50. close(fd);
  51. free(s);
  52. }
  53. void
  54. main(int argc, char **argv)
  55. {
  56. fmtinstall('B', mpfmt);
  57. ARGBEGIN{
  58. default:
  59. usage();
  60. }ARGEND
  61. switch(argc){
  62. default:
  63. usage();
  64. case 1:
  65. base = argv[0];
  66. case 0:
  67. break;
  68. }
  69. do
  70. key = rsagen(1024, 6, 0);
  71. while(mpsignif(key->pub.n) != 1024);
  72. writefile("%s.secret", 0600, printsecret);
  73. writefile("%s.secret.factotum", 0600, printsecretfactotum);
  74. writefile("%s.public", 0666, printpublic);
  75. // writefile("%s.public10", 0666, printpublic10);
  76. }