asn12rsa.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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/asn12rsa [-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. RSApriv *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 = "#d/0";
  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 = asn1toRSApriv(buf, tot);
  51. if(key == nil)
  52. sysfatal("couldn't parse asn1 key");
  53. s = smprint("key proto=rsa %s%ssize=%d ek=%B !dk=%B n=%B !p=%B !q=%B !kp=%B !kq=%B !c2=%B\n",
  54. tag ? tag : "", tag ? " " : "",
  55. mpsignif(key->pub.n), key->pub.ek,
  56. key->dk, key->pub.n, key->p, key->q,
  57. key->kp, key->kq, key->c2);
  58. if(s == nil)
  59. sysfatal("smprint: %r");
  60. write(1, s, strlen(s));
  61. exits(0);
  62. }