示例#1
0
        private void btRegister_Click(object sender, EventArgs e)
        {
            string urlHost = tbHost.Text;
            string urlPort = tbPort.Text;
            string urlUri  = tbUrl.Text;

            if (String.IsNullOrEmpty(urlHost) || string.IsNullOrEmpty(urlPort) || string.IsNullOrEmpty(urlUri))
            {
                statusStrip1.Items[0].Text = "Not all host information entered";
                return;
            }
            string url = string.Format("https://{0}:{1}/{2}", urlHost, urlPort, urlUri);

            // TODO Production: Do not ignore all TLS errors!
            ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
            byte[] challengeBytes = new byte[32];
            (new Random()).NextBytes(challengeBytes);
            try
            {
                CrySilU2FRequest u2fRequest = new CrySilU2FRequest {
                    appId = AppId, version = "U2F_V2", challenge = Helpers.Base64UrlEncode(challengeBytes)
                };
                var response = new RegisterExternalHandler(new RegisterInternalHandler(new CrySilForwarder(url))).Handle(u2fRequest.ToJSON());
                DataContractJsonSerializer jsonSerializerResponse = new DataContractJsonSerializer(typeof(CrySilU2FResponse));
                object            objResponse  = jsonSerializerResponse.ReadObject(new MemoryStream(new UTF8Encoding().GetBytes(response)));
                CrySilU2FResponse jsonResponse = objResponse as CrySilU2FResponse;
                if (jsonResponse != null)
                {
                    jsonResponse.ExtractValues(AppId, challengeBytes);
                    Helpers.WriteRegistryValue(RegistryKeyHandle, jsonResponse.keyHandle, cbUser.SelectedValue.ToString());
                    Helpers.WriteRegistryValue(RegistryPublicKey, jsonResponse.publicKey, cbUser.SelectedValue.ToString());
                    statusStrip1.Items[0].Text = "New key successfully registered";
                }
            }
            catch (Exception ex)
            {
                statusStrip1.Items[0].Text = "Error: " + ex.Message;
            }
            Init();
        }
示例#2
0
文件: Form1.cs 项目: IAIK/CrySIL
 private void btRegister_Click(object sender, EventArgs e)
 {
     string urlHost = tbHost.Text;
     string urlPort = tbPort.Text;
     string urlUri = tbUrl.Text;
     if (String.IsNullOrEmpty(urlHost) || string.IsNullOrEmpty(urlPort) || string.IsNullOrEmpty(urlUri))
     {
         statusStrip1.Items[0].Text = "Not all host information entered";
         return;
     }
     string url = string.Format("https://{0}:{1}/{2}", urlHost, urlPort, urlUri);
     // TODO Production: Do not ignore all TLS errors!
     ServicePointManager.ServerCertificateValidationCallback += (s, cert, chain, sslPolicyErrors) => true;
     byte[] challengeBytes = new byte[32];
     (new Random()).NextBytes(challengeBytes);
     try
     {
         CrySilU2FRequest u2fRequest = new CrySilU2FRequest { appId = AppId, version = "U2F_V2", challenge = Helpers.Base64UrlEncode(challengeBytes) };
         var response = new RegisterExternalHandler(new RegisterInternalHandler(new CrySilForwarder(url))).Handle(u2fRequest.ToJSON());
         DataContractJsonSerializer jsonSerializerResponse = new DataContractJsonSerializer(typeof(CrySilU2FResponse));
         object objResponse = jsonSerializerResponse.ReadObject(new MemoryStream(new UTF8Encoding().GetBytes(response)));
         CrySilU2FResponse jsonResponse = objResponse as CrySilU2FResponse;
         if (jsonResponse != null)
         {
             jsonResponse.ExtractValues(AppId, challengeBytes);
             Helpers.WriteRegistryValue(RegistryKeyHandle, jsonResponse.keyHandle, cbUser.SelectedValue.ToString());
             Helpers.WriteRegistryValue(RegistryPublicKey, jsonResponse.publicKey, cbUser.SelectedValue.ToString());
             statusStrip1.Items[0].Text = "New key successfully registered";
         }
     }
     catch (Exception ex)
     {
         statusStrip1.Items[0].Text = "Error: " + ex.Message;
     }
     Init();
 }