From 07c32fb3da0731570508e519e4cfaaaaf646ccfb Mon Sep 17 00:00:00 2001 From: Ori Bernstein Date: Sun, 18 Jul 2021 15:30:35 +0000 Subject: [PATCH] auth/rsa2jwk: add code to produce jwk rsa keys This is useful for acmed, and possibly other web technologies. --- sys/src/cmd/auth/mkfile | 5 ++- sys/src/cmd/auth/rsa2jwk.c | 87 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 sys/src/cmd/auth/rsa2jwk.c diff --git a/sys/src/cmd/auth/mkfile b/sys/src/cmd/auth/mkfile index bf5dbff2d..a3a9bcd68 100644 --- a/sys/src/cmd/auth/mkfile +++ b/sys/src/cmd/auth/mkfile @@ -26,6 +26,7 @@ TARG=\ readnvram\ rsa2asn1\ rsa2csr\ + rsa2jwk\ rsa2pub\ rsa2ssh\ rsa2x509\ @@ -106,10 +107,10 @@ nuke:V: rm -f *.[$OS] *.[$OS].a [$OS].* y.tab.? y.debug y.output $TARG *.acid $O.%: $LIB -$O.rsa2asn1 $O.rsa2ssh $O.rsafill $O.rsa2x509 $O.rsa2pub $O.rsa2csr: rsa2any.$O +$O.rsa2asn1 $O.rsa2ssh $O.rsafill $O.rsa2x509 $O.rsa2pub $O.rsa2csr $O.rsa2jwk: rsa2any.$O $O.authsrv $O.guard.srv: secureidcheck.$O -rsa2asn1.$O rsa2ssh.$O rsafill.$O rsa2x509.$O rsa2pub.$O rsa2csr.$O: rsa2any.h +rsa2asn1.$O rsa2ssh.$O rsafill.$O rsa2x509.$O rsa2pub.$O rsa2csr.$O rsa2jwk.$O: rsa2any.h $BIN/netkey:V: $O.netkey cp $O.netkey /$objtype/bin/netkey diff --git a/sys/src/cmd/auth/rsa2jwk.c b/sys/src/cmd/auth/rsa2jwk.c new file mode 100644 index 000000000..606c1473b --- /dev/null +++ b/sys/src/cmd/auth/rsa2jwk.c @@ -0,0 +1,87 @@ +#include +#include +#include +#include +#include +#include "rsa2any.h" + +#define between(x,min,max) (((min-1-x) & (x-max-1))>>8) + +int +encurl64chr(int o) +{ + int c; + + c = between(o, 0, 25) & ('A'+o); + c |= between(o, 26, 51) & ('a'+(o-26)); + c |= between(o, 52, 61) & ('0'+(o-52)); + c |= between(o, 62, 62) & ('-'); + c |= between(o, 63, 63) & ('_'); + return c; +} + +char* +encurl64(void *in, int n) +{ + int lim; + char *out, *p; + + lim = 4*n/3 + 5; + if((out = malloc(lim)) == nil) + sysfatal("malloc: %r"); + enc64x(out, lim, in, n, encurl64chr); + if((p = strchr(out, '=')) != nil) + *p = 0; + return out; +} + +void +usage(void) +{ + fprint(2, "usage: auth/rsa2pub [file]\n"); + exits("usage"); +} + +void +main(int argc, char **argv) +{ + uchar nbuf[8192], ebuf[512]; + char *nstr, *estr; + RSApriv *k; + int nlen, elen; + + fmtinstall('[', encodefmt); + quotefmtinstall(); + + ARGBEGIN{ + default: + usage(); + }ARGEND + + if(argc > 1) + usage(); + + if((k = getrsakey(argc, argv, 0, nil)) == nil) + sysfatal("%r"); + + nlen = (mpsignif(k->pub.n)+7)/8; + if(nlen >= sizeof(nbuf)) + sysfatal("key too big"); + mptobe(k->pub.n, nbuf, nlen, nil); + nstr = encurl64(nbuf, nlen); + + elen = (mpsignif(k->pub.ek)+7)/8; + if(elen >= sizeof(ebuf)) + sysfatal("key too big"); + mptobe(k->pub.ek, ebuf, elen, nil); + estr = encurl64(ebuf, elen); + + print( + "{" + "\"kty\": \"RSA\"," + "\"n\": \"%s\"," + "\"e\": \"%s\"" + "}\n", + nstr, estr); + exits(nil); +}