예제 #1
0
            public ApiCACert(string FileName)
            {
                data = File.ReadAllText(FileName);
                var Cert = CertStore.GetCert(data);

                hash   = Cert.Thumbprint;
                name   = CertStore.GetName(data);
                pubkey = CertCommands.GetPubKey(data, true);
                start  = Cert.NotBefore;
                end    = Cert.NotAfter;
            }
예제 #2
0
            public ApiCert(string FileName, string[] ParentFiles)
            {
                data = File.ReadAllText(FileName);
                var Cert = CertStore.GetCert(data);

                hash   = Cert.Thumbprint;
                san    = CertStore.GetSan(data);
                domain = CertStore.GetName(data);
                name   = Cert.Subject;
                pubkey = CertCommands.GetPubKey(data, true);
                issuer = CertStore.GetSignerCertHash(data, ParentFiles);
                start  = Cert.NotBefore;
                end    = Cert.NotAfter;
            }
예제 #3
0
        private void GenCert(HttpListenerContext ctx)
        {
            if (ctx.Request.HasEntityBody)
            {
                var Req = ctx.Request.InputStream.ReadAllText(ctx.Request.ContentEncoding).FromJson <ApiCertCreate>();
                if (Req != null && Req.Valid())
                {
                    string Key          = null;
                    string RootCert     = null;
                    string RootKey      = null;
                    var    KeyFileName  = Path.Combine(Base, Req.id.ToString() + ".key");
                    var    RootFileName = Path.Combine(Base, Req.parent.ToString() + ".ca.crt");
                    if (File.Exists(KeyFileName))
                    {
                        if (File.Exists(RootFileName))
                        {
                            try
                            {
                                Key = File.ReadAllText(KeyFileName);
                            }
                            catch (Exception ex)
                            {
                                Logger.Error("HTTP: Unable to read file {0}. Reason: {1}", KeyFileName, ex.Message);
                                SendJson(ctx, "Unable to read key file", false);
                                return;
                            }
                            try
                            {
                                RootCert = File.ReadAllText(RootFileName);
                            }
                            catch (Exception ex)
                            {
                                Logger.Error("HTTP: Unable to read file {0}. Reason: {1}", RootFileName, ex.Message);
                                SendJson(ctx, "Unable to read CA file", false);
                                return;
                            }

                            //Figure out the matching private key for the given root certificate
                            var RootPub = CertCommands.GetPubKey(RootCert, true);
                            RootKey = Directory.GetFiles(Base, "*.key")
                                      .Select(m => File.ReadAllText(m))
                                      .FirstOrDefault(m => CertCommands.GetPubKey(m, false) == RootPub);

                            if (!string.IsNullOrEmpty(RootKey))
                            {
                                try
                                {
                                    var Cert         = CertCommands.GenerateCertificate(RootKey, RootCert, Key, Req.cn, Req.san, Req.exp, Req.sha256, Req.cc, Req.st, Req.l, Req.o, Req.ou, Req.e);
                                    var Id           = CertStore.GetThumb(Cert);
                                    var CertFileName = Path.Combine(Base, Id + ".cli.crt");
                                    File.WriteAllText(CertFileName, Cert);
                                    SendJson(ctx, new ApiCert(CertFileName, new string[] { RootCert }), true);
                                    return;
                                }
                                catch (Exception ex)
                                {
                                    SendJson(ctx, string.Format("CA creation error: {0}", ex.Message), false);
                                    return;
                                }
                            }
                            SendJson(ctx, "Unable to locate private key of the give nroot certificate", false);
                            return;
                        }
                        SendJson(ctx, "Invalid root Thumbprint", false);
                        return;
                    }
                    SendJson(ctx, "Invalid Key ID", false);
                    return;
                }
                SendJson(ctx, "Invalid Request Content", false);
                return;
            }
            SendJson(ctx, "Invalid Request Method", false);
        }
예제 #4
0
 public ApiRsaKey(string FileName)
 {
     key    = File.ReadAllText(FileName);
     id     = Path.GetFileNameWithoutExtension(FileName);
     pubkey = CertCommands.GetPubKey(key, false);
 }