public void setKey(keyInfo key) { byte[] y = null; if (key.pub.childs[1].childs[0].childs[0].payload[0] == 0) { y = key.pub.childs[1].childs[0].childs[0].payload.Skip(1).ToArray(); } else { y = key.pub.childs[1].childs[0].childs[0].payload; } n = Convert.ToBase64String(y).TrimEnd('=').Replace('+', '-').Replace('/', '_'); if (key.pub.childs[1].childs[0].childs[1].payload[0] == 0) { y = key.pub.childs[1].childs[0].childs[1].payload.Skip(1).ToArray(); } else { y = key.pub.childs[1].childs[0].childs[1].payload; } e = Convert.ToBase64String(y).TrimEnd('=').Replace('+', '-').Replace('/', '_'); json_jwk_raw = String.Format("{{\"e\":\"{0}\",\"kty\":\"RSA\",\"n\":\"{1}\"}}", e, n); thumbprint = Convert.ToBase64String(SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(json_jwk_raw))).TrimEnd('=').Replace('+', '-').Replace('/', '_'); json_jwk = "{\"alg\":\"RS256\",\"jwk\":" + json_jwk_raw + "}"; e = null; n = null; me = this; _key = key; }
static public byte[] makeRootCertFromPriv(string KeyName) { keyInfo key = State.session.keys[KeyName]; Ber cert = new Ber(new byte[] { 0x30, 0x80, 0x30, 0x80, 0xA0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x00, 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x00, 0x00, 0x30, 0x80, 0x31, 0x80, 0x30, 0x80, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x80, 0x17, 0x00, 0x17, 0x00, 0x00, 0x00, 0x30, 0x80, 0x31, 0x80, 0x30, 0x80, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA3, 0x80, 0x30, 0x80, 0x30, 0x0F, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x01, 0x01, 0xFF, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x00, 0x00, 0x03, 0x01, 0, 0x00, 0x00 }); cert.childs[0].childs[4].childs[0].payload = Encoding.UTF8.GetBytes(DateTime.UtcNow.ToString(@"yyMMddhhmmssZ")); cert.childs[0].childs[4].childs[1].payload = Encoding.UTF8.GetBytes(DateTime.UtcNow.AddDays(365).ToString(@"yyMMddhhmmssZ")); cert.childs[0].childs[3].childs[0].childs[0].childs[1].payload = Encoding.UTF8.GetBytes(KeyName); cert.childs[0].childs[5].childs[0].childs[0].childs[1].payload = cert.childs[0].childs[3].childs[0].childs[0].childs[1].payload; cert.childs[0].childs[1].payload = cert.childs[0].childs[5].childs[0].childs[0].childs[1].payload;//BitConverter.GetBytes(DateTime.UtcNow.ToUniversalTime().Subtract(new DateTime(1970, 1, 1)).TotalSeconds); cert.childs[0].childs.Insert(6, key.pub); cert.childs[2].payload = makeSign(key, cert.childs[0].makeDer()); return(cert.makeDer()); }
static public byte[] makeCSR(keyInfo key, string def, DomainInfo dom, System.ComponentModel.BackgroundWorker worker, bool star) { worker.ReportProgress(101, new object[] { -3, "Pattern parse.." }); Ber csr = new Ber(BigInteger.Parse( "3042302e020100300b3109300706035504030c00a01c301a06092a864886f70d01090e310d300b30090603551d1104023000300d06092a864886f70d01010b0500030100", NumberStyles.AllowHexSpecifier).ToByteArray().Reverse().ToArray()); worker.ReportProgress(101, new object[] { -3, "OK\r\nEdit CN.." }); if (star) { csr.childs[0].childs[1].childs[0].childs[0].childs[1].payload = Encoding.UTF8.GetBytes((def == "@") ? dom.dns : String.Format("{0}.{1}", def, dom.dns)); } else { csr.childs[0].childs[1].childs[0].childs[0].childs[1].payload = Encoding.UTF8.GetBytes((def == "@") ? dom.dns : String.Format("{0}.{1}", def, dom.dns).Replace("*.", "")); } Ber subs = csr.childs[0].childs[2].childs[0].childs[1].childs[0].childs[0].childs[1].childs[0]; csr.childs[0].childs.Insert(2, key.pub.cloneAsParrent()); worker.ReportProgress(101, new object[] { -3, "OK\r\nEdit subs.." }); foreach (string sub in dom.subs2) { if (Regex.IsMatch(sub, "\\*")) { subs.addChild(new Ber(BerClass.CONTEXT, BerTags.INTEGER, false, Encoding.UTF8.GetBytes(String.Format("{0}.{1}", sub, dom.dns)))); subs.addChild(new Ber(BerClass.CONTEXT, BerTags.INTEGER, false, Encoding.UTF8.GetBytes((sub == "*") ? dom.dns : String.Format("{0}.{1}", sub, dom.dns)))); } else { subs.addChild(new Ber(BerClass.CONTEXT, BerTags.INTEGER, false, Encoding.UTF8.GetBytes((sub == "@") ? dom.dns : String.Format("{0}.{1}", sub, dom.dns)))); } } ; worker.ReportProgress(101, new object[] { -3, "OK\r\nMake sign.." }); csr.childs[2].payload = makeSign(key, csr.childs[0].makeDer()); worker.ReportProgress(101, new object[] { -3, "OK\r\nGet DER.." }); return(csr.makeDer()); }