summaryrefslogtreecommitdiff
path: root/appl/lib
diff options
context:
space:
mode:
Diffstat (limited to 'appl/lib')
-rw-r--r--appl/lib/factotum.b120
-rw-r--r--appl/lib/spki/spki.b50
2 files changed, 153 insertions, 17 deletions
diff --git a/appl/lib/factotum.b b/appl/lib/factotum.b
index a2cec879..0b3ff1a8 100644
--- a/appl/lib/factotum.b
+++ b/appl/lib/factotum.b
@@ -306,3 +306,123 @@ getuserpasswd(keyspec: string): (string, string)
}
return (hd flds, hd tl flds);
}
+
+parseattrs(s: string): list of ref Attr
+{
+ str := load String String->PATH;
+ fld := str->unquoted(s);
+ rfld := fld;
+ for(fld = nil; rfld != nil; rfld = tl rfld)
+ fld = (hd rfld) :: fld;
+ attrs: list of ref Attr;
+ for(; fld != nil; fld = tl fld){
+ n := hd fld;
+ a := "";
+ tag := Aattr;
+ for(i:=0; i<len n; i++)
+ if(n[i] == '='){
+ a = n[i+1:];
+ n = n[0:i];
+ tag = Aval;
+ }
+ if(len n == 0)
+ continue;
+ if(tag == Aattr && len n > 1 && n[len n-1] == '?'){
+ tag = Aquery;
+ n = n[0:len n-1];
+ }
+ attrs = ref Attr(tag, n, a) :: attrs;
+ }
+ # TO DO: eliminate answered queries
+ return attrs;
+}
+
+Attr.text(a: self ref Attr): string
+{
+ case a.tag {
+ Aattr =>
+ return a.name;
+ Aval =>
+ return sys->sprint("%q=%q", a.name, a.val);
+ Aquery =>
+ return sys->sprint("%q?", a.name);
+ * =>
+ return "??";
+ }
+}
+
+attrtext(attrs: list of ref Attr): string
+{
+ s := "";
+ for(; attrs != nil; attrs = tl attrs){
+ if(s != nil)
+ s[len s] = ' ';
+ s += (hd attrs).text();
+ }
+ return s;
+}
+
+findattr(attrs: list of ref Attr, n: string): ref Attr
+{
+ for(; attrs != nil; attrs = tl attrs)
+ if((a := hd attrs).tag != Aquery && a.name == n)
+ return a;
+ return nil;
+}
+
+findattrval(attrs: list of ref Attr, n: string): string
+{
+ if((a := findattr(attrs, n)) != nil)
+ return a.val;
+ return nil;
+}
+
+delattr(l: list of ref Attr, n: string): list of ref Attr
+{
+ rl: list of ref Attr;
+ for(; l != nil; l = tl l)
+ if((hd l).name != n)
+ rl = hd l :: rl;
+ return rev(rl);
+}
+
+copyattrs(l: list of ref Attr): list of ref Attr
+{
+ rl: list of ref Attr;
+ for(; l != nil; l = tl l)
+ rl = hd l :: rl;
+ return rev(rl);
+}
+
+takeattrs(l: list of ref Attr, names: list of string): list of ref Attr
+{
+ rl: list of ref Attr;
+ for(; l != nil; l = tl l){
+ n := (hd l).name;
+ for(nl := names; nl != nil; nl = tl nl)
+ if((hd nl) == n){
+ rl = hd l :: rl;
+ break;
+ }
+ }
+ return rev(rl);
+}
+
+publicattrs(l: list of ref Attr): list of ref Attr
+{
+ rl: list of ref Attr;
+ for(; l != nil; l = tl l){
+ a := hd l;
+ if(a.tag != Aquery || a.val == nil)
+ rl = a :: rl;
+ }
+ return rev(rl);
+}
+
+rev[T](l: list of T): list of T
+{
+ rl: list of T;
+ for(; l != nil; l = tl l)
+ rl = hd l :: rl;
+ return rl;
+}
diff --git a/appl/lib/spki/spki.b b/appl/lib/spki/spki.b
index 18a2d68a..615aa48e 100644
--- a/appl/lib/spki/spki.b
+++ b/appl/lib/spki/spki.b
@@ -629,7 +629,7 @@ signbytes(data: array of byte, sigalg: string, key: ref Key): (ref Signature, st
l: list of (string, array of byte);
for(; vals != nil; vals = tl vals){
(n, v) := hd vals;
- l = (f2s(n), v) :: l;
+ l = (f2s("rsa", n), v) :: l;
}
sig.sig = revt(l);
return (sig, nil);
@@ -1095,7 +1095,7 @@ Key.sexp(k: self ref Key): ref Sexp
for(; els != nil; els = tl els){
(n, v) := hd els;
a := pre0(v.iptobebytes());
- rl = ref Sexp.List(ref Sexp.String(f2s(n),nil) :: ref Sexp.Binary(a,nil) :: nil) :: rl;
+ rl = ref Sexp.List(ref Sexp.String(f2s("rsa", n),nil) :: ref Sexp.Binary(a,nil) :: nil) :: rl;
}
return ref Sexp.List(ref Sexp.String(sort, nil) ::
ref Sexp.List(ref Sexp.String(k.sigalg(),nil) :: rev(rl)) :: nil);
@@ -2093,9 +2093,12 @@ Keyrep.pk(pk: ref Keyring->PK): ref Keyrep.PK
"rsa" =>
return ref Keyrep.PK(hd flds, hd tl flds,
keyextract(tl tl flds, list of {("ek",1), ("n",0)}));
- "elgamal" or "dsa" =>
+ "elgamal" =>
return ref Keyrep.PK(hd flds, hd tl flds,
keyextract(tl tl flds, list of {("p",0), ("alpha",1), ("key",2)}));
+ "dsa" =>
+ return ref Keyrep.PK(hd flds, hd tl flds,
+ keyextract(tl tl flds, list of {("p",0), ("alpha",2), ("q",1), ("key",3)}));
* =>
return nil;
}
@@ -2112,9 +2115,12 @@ Keyrep.sk(pk: ref Keyring->SK): ref Keyrep.SK
"rsa" =>
return ref Keyrep.SK(hd flds, hd tl flds,
keyextract(tl tl flds,list of {("ek",1), ("n",0), ("!dk",2), ("!q",4), ("!p",3), ("!kq",6), ("!kp",5), ("!c2",7)})); # see comment elsewhere about p, q
- "elgamal" or "dsa" =>
+ "elgamal" =>
return ref Keyrep.SK(hd flds, hd tl flds,
keyextract(tl tl flds, list of {("p",0), ("alpha",1), ("key",2), ("!secret",3)}));
+ "dsa" =>
+ return ref Keyrep.SK(hd flds, hd tl flds,
+ keyextract(tl tl flds, list of {("p",0), ("alpha",2), ("q",1), ("key",3), ("!secret",4)}));
* =>
return nil;
}
@@ -2122,7 +2128,7 @@ Keyrep.sk(pk: ref Keyring->SK): ref Keyrep.SK
Keyrep.get(k: self ref Keyrep, n: string): ref IPint
{
- n1 := f2s(n);
+ n1 := f2s("rsa", n);
for(el := k.els; el != nil; el = tl el)
if((hd el).t0 == n || (hd el).t0 == n1)
return (hd el).t1;
@@ -2191,21 +2197,31 @@ s2f(s: string): string
}
}
-f2s(s: string): string
+f2s(alg: string, s: string): string
{
- case s {
- "ek" => return "e";
- "!p" => return "q"; # see above
- "!q" => return "p";
- "!dk" => return "d";
- "!kp" => return "b";
- "!kq" => return "a";
- "!c2" => return "c";
+ case alg {
+ "rsa" =>
+ case s {
+ "ek" => return "e";
+ "!p" => return "q"; # see above
+ "!q" => return "p";
+ "!dk" => return "d";
+ "!kp" => return "b";
+ "!kq" => return "a";
+ "!c2" => return "c";
+ }
+ "dsa" =>
+ case s {
+ "p" or "q" => return s;
+ "alpha" => return "g";
+ "key" => return "y";
+ }
* =>
- if(s != nil && s[0] == '!')
- return s[1:];
- return s;
+ ;
}
+ if(s != nil && s[0] == '!')
+ return s[1:];
+ return s;
}
Keyrep.eq(k1: self ref Keyrep, k2: ref Keyrep): int