asn12dsa.c 1.1 KB

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