shithub: riscv

Download patch

ref: d06196ab8760ec567a9f043b757a789755a6aed7
parent: 58914b4b0b0c4c3e5e16a1ba043ccdbb7e76ac84
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sun Feb 4 22:21:51 EST 2018

auth/asn12rsa: also convert ASN.1 encoded public key to plan9 format

--- a/sys/man/8/rsa
+++ b/sys/man/8/rsa
@@ -149,7 +149,7 @@
 and prints a full key.
 .PP
 .I Asn12rsa
-reads an RSA private key stored as ASN.1
+reads an RSA private or public key stored as ASN.1
 encoded in the binary Distinguished Encoding Rules (DER)
 and prints a Plan 9 RSA key,
 inserting
--- a/sys/src/cmd/auth/asn12rsa.c
+++ b/sys/src/cmd/auth/asn12rsa.c
@@ -20,6 +20,7 @@
 	long n, tot;
 	char *tag, *file;
 	RSApriv *key;
+	RSApub *pub;
 
 	fmtinstall('B', mpfmt);
 
@@ -54,16 +55,21 @@
 			break;
 		tot += n;
 	}
-
 	key = asn1toRSApriv(buf, tot);
-	if(key == nil)
-		sysfatal("couldn't parse asn1 key");
-
-	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",
-		tag ? tag : "", tag ? " " : "",
-		mpsignif(key->pub.n), key->pub.ek,
-		key->dk, key->pub.n, key->p, key->q,
-		key->kp, key->kq, key->c2);
+	if(key != nil){
+		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",
+			tag ? tag : "", tag ? " " : "",
+			mpsignif(key->pub.n), key->pub.ek,
+			key->dk, key->pub.n, key->p, key->q,
+			key->kp, key->kq, key->c2);
+	} else {
+		pub = asn1toRSApub(buf, tot);
+		if(pub == nil)
+			sysfatal("couldn't parse asn1 key");
+		s = smprint("key proto=rsa %s%ssize=%d ek=%B n=%B\n",
+			tag ? tag : "", tag ? " " : "",
+			mpsignif(pub->n), pub->ek, pub->n);
+	}
 	if(s == nil)
 		sysfatal("smprint: %r");
 	write(1, s, strlen(s));