asn12dsa.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 <bio.h>
  12. #include <mp.h>
  13. #include <libsec.h>
  14. void
  15. usage(void)
  16. {
  17. fprint(2, "auth/asn12dsa [-t tag] [file]\n");
  18. exits("usage");
  19. }
  20. void
  21. main(int argc, char **argv)
  22. {
  23. char *s;
  24. unsigned char *buf;
  25. int fd;
  26. int32_t n, tot;
  27. char *tag, *file;
  28. DSApriv *key;
  29. fmtinstall('B', mpfmt);
  30. tag = nil;
  31. ARGBEGIN{
  32. case 't':
  33. tag = EARGF(usage());
  34. break;
  35. default:
  36. usage();
  37. }ARGEND
  38. if(argc != 0 && argc != 1)
  39. usage();
  40. if(argc == 1)
  41. file = argv[0];
  42. else
  43. file = "/dev/stdin";
  44. if((fd = open(file, OREAD)) < 0)
  45. sysfatal("open %s: %r", file);
  46. buf = nil;
  47. tot = 0;
  48. for(;;){
  49. buf = realloc(buf, tot+8192);
  50. if(buf == nil)
  51. sysfatal("realloc: %r");
  52. if((n = read(fd, buf+tot, 8192)) < 0)
  53. sysfatal("read: %r");
  54. if(n == 0)
  55. break;
  56. tot += n;
  57. }
  58. key = asn1toDSApriv(buf, tot);
  59. if(key == nil)
  60. sysfatal("couldn't parse asn1 key");
  61. s = smprint("key proto=dsa %s%sp=%B q=%B alpha=%B key=%B !secret=%B\n",
  62. tag ? tag : "", tag ? " " : "",
  63. key->pub.p, key->pub.q, key->pub.alpha, key->pub.key,
  64. key->secret);
  65. if(s == nil)
  66. sysfatal("smprint: %r");
  67. write(1, s, strlen(s));
  68. exits(0);
  69. }