shithub: riscv

Download patch

ref: 0a3695ba844972e332dc28efa1ecb232a8eff9ba
parent: 8a67560183b3b726c827bcac02632b2b8e3ba59d
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Fri Oct 6 16:55:57 EDT 2017

rsa: add auth/rsa2asn1, check write error in auth/rsa2x509 and auth/rsa2pub, document in rsa(8)

--- a/sys/man/8/rsa
+++ b/sys/man/8/rsa
@@ -1,6 +1,6 @@
 .TH RSA 8
 .SH NAME
-rsagen, rsafill, asn12rsa, rsa2pub, rsa2ssh, rsa2x509, rsa2csr \- generate and format rsa keys
+rsagen, rsafill, asn12rsa, rsa2asn1, rsa2pub, rsa2ssh, rsa2x509, rsa2csr \- generate and format rsa keys
 .SH SYNOPSIS
 .B rsagen
 [
@@ -26,6 +26,11 @@
 .I file
 ]
 .PP
+.B rsa2asn1
+[
+.I file
+]
+.PP
 .B rsa2pub
 [
 .I file
@@ -172,6 +177,11 @@
 reads a Plan 9 RSA public or private key,
 removes the private attributes, and prints the resulting public key.
 Comment attributes are preserved.
+.PP
+.I Rsa2asn1
+is like
+.I rsa2pub
+but outputs the public key in ASN.1/DER format.
 .PP
 .I Rsa2ssh
 reads a Plan 9 RSA public or private key and prints the public portion 
--- a/sys/src/cmd/auth/mkfile
+++ b/sys/src/cmd/auth/mkfile
@@ -27,6 +27,7 @@
 	pemencode\
 	printnetkey\
 	readnvram\
+	rsa2asn1\
 	rsa2csr\
 	rsa2pub\
 	rsa2ssh\
@@ -108,10 +109,10 @@
 
 $O.%:	$LIB
 $O.dsa2ssh $O.dsafill $O.dsa2x509 $O.dsa2pub $O.dsa2csr: rsa2any.$O
-$O.rsa2ssh $O.rsafill $O.rsa2x509 $O.rsa2pub $O.rsa2csr: rsa2any.$O
+$O.rsa2asn1 $O.rsa2ssh $O.rsafill $O.rsa2x509 $O.rsa2pub $O.rsa2csr: rsa2any.$O
 $O.authsrv $O.guard.srv: secureidcheck.$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: rsa2any.h
 
 $BIN/netkey:V:	$O.netkey
 	cp $O.netkey /$objtype/bin/netkey
--- /dev/null
+++ b/sys/src/cmd/auth/rsa2asn1.c
@@ -1,0 +1,37 @@
+#include <u.h>
+#include <libc.h>
+#include <auth.h>
+#include <mp.h>
+#include <libsec.h>
+#include "rsa2any.h"
+
+void
+usage(void)
+{
+	fprint(2, "usage: auth/rsa2asn1 [file]\n");
+	exits("usage");
+}
+
+void
+main(int argc, char **argv)
+{
+	uchar buf[16*1024];
+	RSApriv *k;
+	int n;
+
+	ARGBEGIN{
+	default:
+		usage();
+	}ARGEND
+
+	if(argc > 1)
+		usage();
+
+	if((k = getrsakey(argc, argv, 0, nil)) == nil)
+		sysfatal("%r");
+	if((n = asn1encodeRSApub(&k->pub, buf, sizeof(buf))) < 0)
+		sysfatal("asn1encodeRSApub: %r");
+	if(write(1, buf, n) != n)
+		sysfatal("write: %r");
+	exits(nil);
+}
--- a/sys/src/cmd/auth/rsa2pub.c
+++ b/sys/src/cmd/auth/rsa2pub.c
@@ -18,6 +18,7 @@
 	RSApriv *key;
 	Attr *a;
 	char *s;
+	int n;
 
 	fmtinstall('A', _attrfmt);
 	fmtinstall('B', mpfmt);
@@ -34,11 +35,11 @@
 	if((key = getrsakey(argc, argv, 0, &a)) == nil)
 		sysfatal("%r");
 
-	s = smprint("key %A size=%d ek=%B n=%B\n",
-		a, 
-		mpsignif(key->pub.n), key->pub.ek, key->pub.n);
-	if(s == nil)
+	if((s = smprint("key %A size=%d ek=%B n=%B\n", a, 
+		mpsignif(key->pub.n), key->pub.ek, key->pub.n)) == nil)
 		sysfatal("smprint: %r");
-	write(1, s, strlen(s));
+	n = strlen(s);
+	if(write(1, s, n) != n)
+		sysfatal("write: %r");
 	exits(nil);
 }
--- a/sys/src/cmd/auth/rsa2x509.c
+++ b/sys/src/cmd/auth/rsa2x509.c
@@ -45,6 +45,8 @@
 	if(cert == nil)
 		sysfatal("X509rsagen: %r");
 
-	write(1, cert, len);
-	exits(0);
+	if(write(1, cert, len) != len)
+		sysfatal("write: %r");
+
+	exits(nil);
 }