summaryrefslogtreecommitdiff
path: root/appl/cmd
diff options
context:
space:
mode:
authorCharles.Forsyth <devnull@localhost>2007-08-07 14:43:11 +0000
committerCharles.Forsyth <devnull@localhost>2007-08-07 14:43:11 +0000
commitcb2ee803f523429101329ed677dc59535ede63db (patch)
tree83d52bd5e99caae41f643664823ed6dc0e23b7d2 /appl/cmd
parent2d38c201c6560ebe0d757100636acc88ec40fec7 (diff)
20070807-1542
Diffstat (limited to 'appl/cmd')
-rw-r--r--appl/cmd/auth/rsagen.b76
1 files changed, 76 insertions, 0 deletions
diff --git a/appl/cmd/auth/rsagen.b b/appl/cmd/auth/rsagen.b
new file mode 100644
index 00000000..c553d102
--- /dev/null
+++ b/appl/cmd/auth/rsagen.b
@@ -0,0 +1,76 @@
+implement Rsagen;
+
+include "sys.m";
+ sys: Sys;
+
+include "draw.m";
+
+include "keyring.m";
+ kr: Keyring;
+
+include "arg.m";
+
+Rsagen: module
+{
+ init: fn(nil: ref Draw->Context, nil: list of string);
+};
+
+init(nil: ref Draw->Context, args: list of string)
+{
+ sys = load Sys Sys->PATH;
+ kr = load Keyring Keyring->PATH;
+
+ arg := load Arg Arg->PATH;
+ arg->init(args);
+ arg->setusage("auth/rsagen [-b bits] [-t 'attr=value attr=value ...']");
+ tag: string;
+ nbits := 1024;
+ while((o := arg->opt()) != 0)
+ case o {
+ 'b' =>
+ nbits = int arg->earg();
+ if(nbits <= 0)
+ arg->usage();
+ if(nbits > 4096)
+ error("bits must be no greater than 4096");
+ 't' =>
+ tag = arg->earg();
+ * =>
+ arg->usage();
+ }
+ args = arg->argv();
+ if(args != nil)
+ arg->usage();
+ arg = nil;
+
+ sk := kr->genSK("rsa", "", nbits);
+ if(sk == nil)
+ error("unable to generate key");
+ s := kr->sktoattr(sk);
+ # need to fix the attr interface so the following isn't needed:
+ s = skip(s, "alg");
+ s = skip(s, "owner");
+ if(tag != nil)
+ s = tag+" "+s;
+ a := sys->aprint("key proto=rsa size=%d %s\n", nbits, s);
+ if(sys->write(sys->fildes(1), a, len a) != len a)
+ error(sys->sprint("error writing key: %r"));
+}
+
+skip(s: string, attr: string): string
+{
+ for(i := 0; i < len s && s[i] != ' '; i++)
+ {}
+ if(i >= len s)
+ return s;
+ (nf, fld) := sys->tokenize(s[0:i], "=");
+ if(nf == 2 && hd fld == attr)
+ s = s[i+1:];
+ return s;
+}
+
+error(s: string)
+{
+ sys->fprint(sys->fildes(2), "rsagen: %s\n", s);
+ raise "fail:error";
+}