private byte[] UpdateCertificateInfo(ClickOnceAppInfo appInfo)
        {
            appInfo.SignedByPublisher = false;

            var certBin = default(byte[]);
            var tmpPath = default(string);

            try
            {
                tmpPath = ExtractEntryPointCommandFile(appInfo.Name);
                var cert = X509Certificate.CreateFromSignedFile(tmpPath);
                if (cert != null)
                {
                    certBin = cert.GetRawCertData();
                    this.ClickOnceFileRepository.SaveFileContent(appInfo.Name, ".cer", certBin);

                    if (appInfo.PublisherName != null)
                    {
                        var sshPubKeyStr = CertificateValidater.GetSSHPubKeyStrFromGitHubAccount(appInfo.PublisherName);
                        appInfo.SignedByPublisher = CertificateValidater.EqualsPublicKey(sshPubKeyStr, cert);
                    }
                }
            }
            catch (CryptographicException) { }
            finally { if (tmpPath != null)
                      {
                          System.IO.File.Delete(tmpPath);
                      }
            }

            appInfo.HasCodeSigning = certBin != null;
            return(certBin);
        }
        public void GetSSHPublicKeyStringFromGitHubAccount_HTTPError_Test()
        {
            var nousername    = Guid.NewGuid().ToString("N");
            var pubKeysString = CertificateValidater.GetSSHPubKeyStrFromGitHubAccount(nousername);

            pubKeysString.IsNull();
        }
        public void EqualsPublicKey_NullSshPubKeyStr_False_Test()
        {
            var sshPubKeyString = default(string);
            var pathOfCertFile  = PathOf("id_rsa.cer");

            CertificateValidater.EqualsPublicKey(sshPubKeyString, pathOfCertFile)
            .IsFalse();
        }
        public void EqualsPublicKey_False_Test()
        {
            var sshPubKeyString = File.ReadAllText(PathOf("id_rsa.pub"));
            var pathOfCertFile  = PathOf("another.cer");

            CertificateValidater.EqualsPublicKey(sshPubKeyString, pathOfCertFile)
            .IsFalse();
        }
        public void GetModuleFromSSHPublicKeyString_Test()
        {
            var pubKey = File.ReadAllText(PathOf("id_rsa.pub"));

            CertificateValidater
            .GetModuleFromSSHPublicKeyString(pubKey)
            .Last()
            .Is(PublicKeyModule);
        }
        public void GetSSHPublicKeyStringFromGitHubAccount_Test()
        {
            var pubKeysString = CertificateValidater.GetSSHPubKeyStrFromGitHubAccount("Haacked");

            var _1stPubKeyStr = pubKeysString.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).First();
            var pubKeyParts   = _1stPubKeyStr.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);

            pubKeyParts.First().Is("ssh-rsa");

            var pubKeyBin = Convert.FromBase64String(pubKeyParts.Last());

            string.Join("", pubKeyBin
                        .Skip(4) // byte size of algorithm identifier
                        .Take(7) // "ssh-rsa"
                        .Select(n => (char)n))
            .Is("ssh-rsa");
        }
        public ActionResult Edit(string id, ClickOnceAppInfo model, bool disclosePublisher)
        {
            var result = GetMyAppInfo(id);

            if (result is ActionResult)
            {
                return(result as ActionResult);
            }

            if (ModelState.IsValid == false)
            {
                return(View(model));
            }

            var appInfo = result as ClickOnceAppInfo;

            appInfo.Title       = model.Title;
            appInfo.Description = model.Description;
            appInfo.ProjectURL  = model.ProjectURL;
            SetupPublisherInformtion(disclosePublisher, appInfo);

            appInfo.SignedByPublisher = false;
            if (appInfo.HasCodeSigning == null)
            {
                UpdateCertificateInfo(appInfo);
            }
            else if (appInfo.HasCodeSigning == true && appInfo.PublisherName != null)
            {
                var tmpPath = Server.MapPath($"~/App_Data/{Guid.NewGuid():N}.cer");
                try
                {
                    var certBin = this.ClickOnceFileRepository.GetFileContent(id, ".cer");
                    System.IO.File.WriteAllBytes(tmpPath, certBin);
                    var sshPubKeyStr = CertificateValidater.GetSSHPubKeyStrFromGitHubAccount(appInfo.PublisherName);
                    appInfo.SignedByPublisher = CertificateValidater.EqualsPublicKey(sshPubKeyStr, tmpPath);
                }
                finally { System.IO.File.Delete(tmpPath); }
            }

            this.ClickOnceFileRepository.SaveAppInfo(id, appInfo);

            var from = Request.QueryString["from"];

            return(from == "detail" ? RedirectToRoute("Detail", new { appId = appInfo.Name }) : RedirectToAction("MyApps", "Home"));
        }
 public void GetModuleFromX509CertificateFile_Test()
 {
     CertificateValidater
     .GetModuleFromX509CertificateFile(PathOf("id_rsa.cer"))
     .Is(PublicKeyModule);
 }
 public void GetModuleFromSSHPublicKeyString_From_NullStr_Test()
 {
     CertificateValidater
     .GetModuleFromSSHPublicKeyString(null)
     .Count().Is(0);
 }