Example #1
0
        public uint GenerateKeyPair(int KeySize)
        {
            CspParameters CSPParam = new CspParameters();

            CSPParam.Flags = CspProviderFlags.UseMachineKeyStore;

            UInt32 KeyID = 1;

            if (_keys.Keys.Count > 0)
            {
                KeyID = _keys.Keys.Max <UInt32>() + 1;
            }
            using (var csp = new RSACryptoServiceProvider(KeySize, CSPParam))
            {
                var pubKey  = new KeyData(KeyID, csp.ExportCspBlob(false), KeyType.Public, _area);
                var privKey = new KeyData(KeyID, csp.ExportCspBlob(true), KeyType.Private, _area);

                SecretUpdate.SecretUpdate pubSecret  = pubKey.ToSecretUpdate();
                SecretUpdate.SecretUpdate privSecret = privKey.ToSecretUpdate();

                SaveSecret(pubSecret, (Guid.NewGuid().ToString())).Wait();
                SaveSecret(privSecret, (Guid.NewGuid().ToString())).Wait();

                _keys.Add(KeyID, new KeyPair(pubKey, privKey));
                return(KeyID);
            }
        }
Example #2
0
        public SecretUpdate.SecretUpdate ToSecretUpdate()
        {
            SecretUpdate.SecretUpdate sec = new SecretUpdate.SecretUpdate();
            sec.value = this.ToString();
            sec.attributes.enabled = "true";
            sec.attributes.exp     = null;
            sec.attributes.nbf     = null;

            sec.contentType = null;

            sec.tags.Area    = this.area;
            sec.tags.KeyID   = this.Id.ToString();
            sec.tags.KeyType = this.type.ToString();

            return(sec);
        }
Example #3
0
        protected async Task SaveSecret(SecretUpdate.SecretUpdate secret, string secretName)
        {
            AuthenticationResult result = Authenticate();

            UriBuilder ub = new UriBuilder(_vaultUri);

            ub.Path  = "/secrets/" + secretName;
            ub.Query = "api-version=" + _apiVersion;


            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(SecretUpdate.SecretUpdate));

            using (var stream = new System.IO.MemoryStream())
            {
                serializer.WriteObject(stream, secret);
                stream.Position = 0;

                using (StreamContent content = new StreamContent(stream))
                    using (var client = new HttpClient())
                    {
                        content.Headers.Add("Content-type", "application/json");

                        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
                        client.DefaultRequestHeaders.Accept.Clear();
                        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                        HttpResponseMessage response = await client.PutAsync(ub.Uri, content);

                        if (!response.IsSuccessStatusCode)
                        {
                            throw new KeyStoreException(response.ReasonPhrase);
                        }
                        var data = await response.Content.ReadAsStringAsync();
                    }
            }
        }