summaryrefslogtreecommitdiff
path: root/sys/src/libsec/port
diff options
context:
space:
mode:
authorcinap_lenrek <cinap_lenrek@felloff.net>2019-08-30 07:34:35 +0200
committercinap_lenrek <cinap_lenrek@felloff.net>2019-08-30 07:34:35 +0200
commit85216d3d95505c19a57d3bbe74e8a7eca109a8e6 (patch)
tree11be5cfbb3a1ab2854013be14f0728ea2d2a346a /sys/src/libsec/port
parent7bb1a9a18566ea9c8ae7f6c2fa99e448026521d2 (diff)
auth/rsa2asn1: implement private key export with -a flag (thanks kvik)
kvik writes: I needed to convert the RSA private key that was laying around in secstore into a format understood by UNIX® tools like SSH. With asn12rsa(8) we can go from the ASN.1/DER to Plan 9 format, but not back - so I wrote the libsec function asn1encodeRSApriv(2) and used it in rsa2asn1(8) by adding the -a flag which causes the full private key to be encoded and output.
Diffstat (limited to 'sys/src/libsec/port')
-rw-r--r--sys/src/libsec/port/x509.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/sys/src/libsec/port/x509.c b/sys/src/libsec/port/x509.c
index 873d19186..5b4d24952 100644
--- a/sys/src/libsec/port/x509.c
+++ b/sys/src/libsec/port/x509.c
@@ -2689,6 +2689,27 @@ encode_rsapubkey(RSApub *pk)
return b;
}
+static Bytes*
+encode_rsaprivkey(RSApriv *k)
+{
+ Bytes *b = nil;
+ RSApub *pk = &k->pub;
+ Elem e = mkseq(
+ mkel(mkint(0),
+ mkel(mkbigint(pk->n),
+ mkel(mpsignif(pk->ek)<32 ? mkint(mptoi(pk->ek)) : mkbigint(pk->ek),
+ mkel(mkbigint(k->dk),
+ mkel(mkbigint(k->p),
+ mkel(mkbigint(k->q),
+ mkel(mkbigint(k->kp),
+ mkel(mkbigint(k->kq),
+ mkel(mkbigint(k->c2),
+ nil))))))))));
+ encode(e, &b);
+ freevalfields(&e.val);
+ return b;
+}
+
int
asn1encodeRSApub(RSApub *pk, uchar *buf, int len)
{
@@ -2705,6 +2726,23 @@ asn1encodeRSApub(RSApub *pk, uchar *buf, int len)
return len;
}
+int
+asn1encodeRSApriv(RSApriv *k, uchar *buf, int len)
+{
+ Bytes *b;
+ b = encode_rsaprivkey(k);
+ if(b == nil)
+ return -1;
+ if(b->len > len){
+ freebytes(b);
+ werrstr("buffer too small");
+ return -1;
+ }
+ memmove(buf, b->data, len = b->len);
+ freebytes(b);
+ return len;
+}
+
uchar*
X509rsagen(RSApriv *priv, char *subj, ulong valid[2], int *certlen)
{