convkeys.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <ctype.h>
  12. #include <authsrv.h>
  13. #include <mp.h>
  14. #include <libsec.h>
  15. #include <bio.h>
  16. #include "authcmdlib.h"
  17. char authkey[DESKEYLEN];
  18. int verb;
  19. int usepass;
  20. int convert(char*, char*, int);
  21. int dofcrypt(int, char*, char*, int);
  22. void usage(void);
  23. void
  24. main(int argc, char *argv[])
  25. {
  26. Dir *d;
  27. char *p, *file, key[DESKEYLEN];
  28. int fd, len;
  29. ARGBEGIN{
  30. case 'p':
  31. usepass = 1;
  32. break;
  33. case 'v':
  34. verb = 1;
  35. break;
  36. default:
  37. usage();
  38. }ARGEND
  39. if(argc != 1)
  40. usage();
  41. file = argv[0];
  42. /* get original key */
  43. if(usepass){
  44. print("enter password file is encoded with\n");
  45. getpass(authkey, nil, 0, 1);
  46. } else
  47. getauthkey(authkey);
  48. if(!verb){
  49. print("enter password to reencode with\n");
  50. getpass(key, nil, 0, 1);
  51. }
  52. fd = open(file, ORDWR);
  53. if(fd < 0)
  54. error("can't open %s: %r\n", file);
  55. d = dirfstat(fd);
  56. if(d == nil)
  57. error("can't stat %s: %r\n", file);
  58. len = d->length;
  59. p = malloc(len);
  60. if(!p)
  61. error("out of memory");
  62. if(read(fd, p, len) != len)
  63. error("can't read key file: %r\n");
  64. len = convert(p, key, len);
  65. if(verb)
  66. exits(0);
  67. if(pwrite(fd, p, len, 0) != len)
  68. error("can't write key file: %r\n");
  69. close(fd);
  70. exits(0);
  71. }
  72. void
  73. randombytes(uint8_t *p, int len)
  74. {
  75. int i, fd;
  76. fd = open("/dev/random", OREAD);
  77. if(fd < 0){
  78. fprint(2, "convkeys: can't open /dev/random, using rand()\n");
  79. srand(time(0));
  80. for(i = 0; i < len; i++)
  81. p[i] = rand();
  82. return;
  83. }
  84. read(fd, p, len);
  85. close(fd);
  86. }
  87. void
  88. oldCBCencrypt(char *key7, char *p, int len)
  89. {
  90. uint8_t ivec[8];
  91. uint8_t key[8];
  92. DESstate s;
  93. memset(ivec, 0, 8);
  94. des56to64((uint8_t*)key7, key);
  95. setupDESstate(&s, key, ivec);
  96. desCBCencrypt((uint8_t*)p, len, &s);
  97. }
  98. void
  99. oldCBCdecrypt(char *key7, char *p, int len)
  100. {
  101. uint8_t ivec[8];
  102. uint8_t key[8];
  103. DESstate s;
  104. memset(ivec, 0, 8);
  105. des56to64((uint8_t*)key7, key);
  106. setupDESstate(&s, key, ivec);
  107. desCBCdecrypt((uint8_t*)p, len, &s);
  108. }
  109. static int
  110. badname(char *s)
  111. {
  112. int n;
  113. Rune r;
  114. for (; *s != '\0'; s += n) {
  115. n = chartorune(&r, s);
  116. if (n == 1 && r == Runeerror)
  117. return 1;
  118. }
  119. return 0;
  120. }
  121. int
  122. convert(char *p, char *key, int len)
  123. {
  124. int i;
  125. len -= KEYDBOFF;
  126. if(len % KEYDBLEN){
  127. fprint(2, "convkeys: file odd length; not converting %d bytes\n",
  128. len % KEYDBLEN);
  129. len -= len % KEYDBLEN;
  130. }
  131. len += KEYDBOFF;
  132. oldCBCdecrypt(authkey, p, len);
  133. for(i = KEYDBOFF; i < len; i += KEYDBLEN)
  134. if (badname(&p[i])) {
  135. print("bad name %.30s... - aborting\n", &p[i]);
  136. return 0;
  137. }
  138. if(verb)
  139. for(i = KEYDBOFF; i < len; i += KEYDBLEN)
  140. print("%s\n", &p[i]);
  141. randombytes((uint8_t*)p, 8);
  142. oldCBCencrypt(key, p, len);
  143. return len;
  144. }
  145. void
  146. usage(void)
  147. {
  148. fprint(2, "usage: convkeys keyfile\n");
  149. exits("usage");
  150. }