Ejemplo n.º 1
0
        internal KeyBundle(Azure.KeyVault.Models.KeyBundle keyBundle, VaultUriHelper vaultUriHelper)
        {
            if (keyBundle == null)
            {
                throw new ArgumentNullException("keyBundle");
            }
            if (keyBundle.Key == null || keyBundle.Attributes == null)
            {
                throw new ArgumentException(KeyVaultProperties.Resources.InvalidKeyBundle);
            }

            SetObjectIdentifier(vaultUriHelper, keyBundle.KeyIdentifier);

            Key        = keyBundle.Key;
            Attributes = new KeyAttributes(
                keyBundle.Attributes.Enabled,
                keyBundle.Attributes.Expires,
                keyBundle.Attributes.NotBefore,
                keyBundle.Key.Kty,
                keyBundle.Key.KeyOps.ToArray(),
                keyBundle.Attributes.Created,
                keyBundle.Attributes.Updated,
                keyBundle.Attributes.RecoveryLevel,
                keyBundle.Tags);
        }
        public KeyBundle ImportKey(string vaultName, string keyName, KeyAttributes keyAttributes, JsonWebKey webKey, bool?importToHsm)
        {
            if (string.IsNullOrEmpty(vaultName))
            {
                throw new ArgumentNullException("vaultName");
            }
            if (string.IsNullOrEmpty(keyName))
            {
                throw new ArgumentNullException("keyName");
            }
            if (keyAttributes == null)
            {
                throw new ArgumentNullException("keyAttributes");
            }
            if (webKey == null)
            {
                throw new ArgumentNullException("webKey");
            }
            if (webKey.Kty == JsonWebKeyType.RsaHsm && (importToHsm.HasValue && !importToHsm.Value))
            {
                throw new ArgumentException(KeyVaultProperties.Resources.ImportByokAsSoftkeyError);
            }

            string vaultAddress = this.vaultUriHelper.CreateVaultAddress(vaultName);

            webKey.KeyOps = keyAttributes.KeyOps;
            var keyBundle = new Azure.KeyVault.Models.KeyBundle()
            {
                Attributes = (Azure.KeyVault.Models.KeyAttributes)keyAttributes,
                Key        = webKey,
                Tags       = keyAttributes.TagsDirectionary
            };

            try
            {
                keyBundle = this.keyVaultClient.ImportKeyAsync(vaultAddress, keyName, keyBundle, importToHsm).GetAwaiter().GetResult();
            }
            catch (Exception ex)
            {
                throw GetInnerException(ex);
            }

            return(new KeyBundle(keyBundle, this.vaultUriHelper));
        }
        public KeyBundle ImportKey(string vaultName, string keyName, KeyAttributes keyAttributes, JsonWebKey webKey, bool? importToHsm)
        {
            if (string.IsNullOrEmpty(vaultName))
                throw new ArgumentNullException("vaultName");
            if (string.IsNullOrEmpty(keyName))
                throw new ArgumentNullException("keyName");
            if (keyAttributes == null)
                throw new ArgumentNullException("keyAttributes");
            if (webKey == null)
                throw new ArgumentNullException("webKey");
            if (webKey.Kty == JsonWebKeyType.RsaHsm && (importToHsm.HasValue && !importToHsm.Value))
                throw new ArgumentException(KeyVaultProperties.Resources.ImportByokAsSoftkeyError);

            string vaultAddress = this.vaultUriHelper.CreateVaultAddress(vaultName);
            
            webKey.KeyOps = keyAttributes.KeyOps;            
            var keyBundle = new Azure.KeyVault.Models.KeyBundle()
            {
                Attributes = (Azure.KeyVault.Models.KeyAttributes)keyAttributes,
                Key = webKey,
                Tags = keyAttributes.TagsDirectionary
            };

            try
            {
                keyBundle = this.keyVaultClient.ImportKeyAsync(vaultAddress, keyName, keyBundle, importToHsm).GetAwaiter().GetResult();
            }
            catch (Exception ex)
            {
                throw GetInnerException(ex);
            }

            return new KeyBundle(keyBundle, this.vaultUriHelper);
        }