/// <summary> /// Deletes the specified key /// </summary> /// <param name="vault">The vault name, e.g. https://myvault.vault.azure.net</param> /// <param name="keyName">The key name</param> /// <returns>The public part of the deleted key</returns> public async Task <KeyBundle> DeleteKeyAsync(string vault, string keyName) { if (string.IsNullOrEmpty(vault)) { throw new ArgumentNullException("vault"); } if (string.IsNullOrEmpty(keyName)) { throw new ArgumentNullException("keyName"); } var identifier = new KeyIdentifier(vault, keyName); using (var httpResponse = await this.SendAsync <HttpRequestMessage>("DELETE", CreateKeyUrl(identifier.BaseIdentifier)).ConfigureAwait(false)) { await EnsureSuccessStatusCode(httpResponse).ConfigureAwait(false); var response = await DeserializeAsync <GetKeyResponseMessage>(httpResponse).ConfigureAwait(false); return(new KeyBundle { Attributes = response.Attributes, Key = response.Key, }); } }
/// <summary> /// Retrieves the public portion of a key plus its attributes /// </summary> /// <param name="vault">The vault name, e.g. https://myvault.vault.azure.net</param> /// <param name="keyName">The key name</param> /// <param name="keyVersion">The key version</param> /// <returns>A KeyBundle of the key and its attributes</returns> public async Task <KeyBundle> GetKeyAsync(string vault, string keyName, string keyVersion = null) { if (string.IsNullOrEmpty(vault)) { throw new ArgumentNullException("vault"); } if (string.IsNullOrEmpty(keyName)) { throw new ArgumentNullException("keyName"); } var keyIdentifier = new KeyIdentifier(vault, keyName, keyVersion); return(await GetKeyAsync(keyIdentifier.Identifier).ConfigureAwait(false)); }
/// <summary> /// Creates a new, named, key in the specified vault. /// </summary> /// <param name="vault">The URL for the vault in which the key is to be created.</param> /// <param name="keyName">The name for the key</param> /// <param name="keyType">The type of key to create (one of the valid WebKeyTypes)</param> /// <param name="keyAttributes">The attributes of the key</param> /// <returns>A key bundle containing the result of the create request</returns> public async Task <KeyBundle> CreateKeyAsync(string vault, string keyName, string keyType, int?keySize = null, string[] key_ops = null, KeyAttributes keyAttributes = null) { if (string.IsNullOrEmpty(vault)) { throw new ArgumentNullException("vault"); } if (string.IsNullOrEmpty(keyName)) { throw new ArgumentNullException("keyName"); } if (string.IsNullOrEmpty(keyType)) { throw new ArgumentNullException("keyType"); } if (!JsonWebKeyType.AllTypes.Contains(keyType)) { throw new ArgumentOutOfRangeException("keyType"); } var keyIdentifier = new KeyIdentifier(vault, keyName); var request = new CreateKeyRequestMessage { Kty = keyType, KeySize = keySize, KeyOps = key_ops, Attributes = keyAttributes }; using (var httpResponse = await this.SendAsync <CreateKeyRequestMessage>("POST", CreateKeyUrl(keyIdentifier.BaseIdentifier, "create"), request).ConfigureAwait(false)) { await EnsureSuccessStatusCode(httpResponse).ConfigureAwait(false); var response = await DeserializeAsync <GetKeyResponseMessage>(httpResponse).ConfigureAwait(false); return(new KeyBundle { Attributes = response.Attributes, Key = response.Key, }); } }
/// <summary> /// Updates the Key Attributes associated with the specified key /// </summary> /// <param name="vault">The vault name, e.g. https://myvault.vault.azure.net</param> /// <param name="keyName">The key name</param> /// <param name="keyOps">Json web key operations</param> /// <param name="attributes">The new attributes for the key</param> /// <returns> The updated key </returns> public async Task <KeyBundle> UpdateKeyAsync(string vault, string keyName, string[] keyOps = null, KeyAttributes attributes = null) { if (string.IsNullOrEmpty(vault)) { throw new ArgumentNullException("vault"); } if (string.IsNullOrEmpty(keyName)) { throw new ArgumentNullException("keyName"); } if (attributes == null && keyOps == null) { throw new ArgumentException("Must provide one of keyOps or attributes"); } var keyIdentifier = new KeyIdentifier(vault, keyName); return(await UpdateKeyAsync(keyIdentifier.Identifier, keyOps, attributes).ConfigureAwait(false)); }
/// <summary> /// Imports a key into the specified vault /// </summary> /// <param name="vault">The vault name, e.g. https://myvault.vault.azure.net</param> /// <param name="keyName">The key name</param> /// <param name="keyBundle"> Key bundle </param> /// <param name="importToHardware">Whether to import as a hardware key (HSM) or software key </param> /// <returns> Imported key bundle to the vault </returns> public async Task <KeyBundle> ImportKeyAsync(string vault, string keyName, KeyBundle keyBundle, bool?importToHardware = null) { if (string.IsNullOrEmpty(vault)) { throw new ArgumentNullException("vault"); } if (string.IsNullOrEmpty(keyName)) { throw new ArgumentNullException("keyName"); } if (keyBundle == null) { throw new ArgumentNullException("keyBundle"); } var identifier = new KeyIdentifier(vault, keyName); var request = new ImportKeyRequestMessage { Hsm = importToHardware, Key = keyBundle.Key, Attributes = keyBundle.Attributes }; using (var httpResponse = await this.SendAsync <ImportKeyRequestMessage>("PUT", CreateKeyUrl(identifier.BaseIdentifier, "import"), request).ConfigureAwait(false)) { await EnsureSuccessStatusCode(httpResponse).ConfigureAwait(false); var response = await DeserializeAsync <GetKeyResponseMessage>(httpResponse).ConfigureAwait(false); return(new KeyBundle { Attributes = response.Attributes, Key = response.Key, }); } }
/// <summary> /// Requests that a backup of the specified key be downloaded to the client. /// </summary> /// <param name="vault">The vault name, e.g. https://myvault.vault.azure.net</param> /// <param name="keyName">The key name</param> /// <returns>The backup blob containing the backed up key</returns> public async Task <byte[]> BackupKeyAsync(string vault, string keyName) { if (string.IsNullOrEmpty(vault)) { throw new ArgumentNullException("vault"); } if (string.IsNullOrEmpty(keyName)) { throw new ArgumentNullException("keyName"); } var keyIdentifier = new KeyIdentifier(vault, keyName); using (var httpResponse = await this.SendAsync <HttpRequestMessage>("POST", CreateKeyUrl(keyIdentifier.BaseIdentifier, "backup")).ConfigureAwait(false)) { await EnsureSuccessStatusCode(httpResponse).ConfigureAwait(false); var backupResponse = await DeserializeAsync <BackupKeyResponseMessage>(httpResponse).ConfigureAwait(false); return(backupResponse.Value); } }