Example #1
0
        /// <inheritdoc/>
        public async Task <Key> GetPublicKeyAsync(KeyHandle handle, CancellationToken ct)
        {
            var document = await _keys.GetAsync <KeyDocument>(
                KeyId.GetId(handle), ct);

            return(document.Value.KeyJson.ToKey().GetPublicKey());
        }
Example #2
0
        /// <inheritdoc/>
        public async Task DisableKeyAsync(KeyHandle handle, CancellationToken ct)
        {
            var keyId = KeyId.GetId(handle);

            while (true)
            {
                try {
                    var document = await _keys.FindAsync <KeyDocument>(keyId, ct);

                    if (document == null)
                    {
                        throw new ResourceNotFoundException($"{keyId} not found");
                    }
                    if (!document.Value.IsDisabled)
                    {
                        await _keys.ReplaceAsync(document, new KeyDocument {
                            Id         = document.Value.Id,
                            IsDisabled = true,
                            KeyJson    = document.Value.KeyJson
                        }, ct);
                    }
                    return;
                }
                catch (ResourceOutOfDateException) {
                    continue;
                }
            }
        }
Example #3
0
        /// <inheritdoc/>
        public async Task <bool> VerifyAsync(KeyHandle handle, byte[] hash,
                                             SignatureType algorithm, byte[] signature, CancellationToken ct)
        {
            var document = await _keys.GetAsync <KeyDocument>(KeyId.GetId(handle), ct);

            if (document.Value.IsDisabled)
            {
                throw new InvalidOperationException("Key is disabled");
            }
            var key = document.Value.KeyJson.ToKey();

            if (key == null)
            {
                throw new ResourceNotFoundException("Key not found");
            }
            switch (key.Type)
            {
            case KeyType.RSA:
                using (var rsa = key.ToRSA()) {
                    return(rsa.VerifyHash(hash, signature, algorithm.ToHashAlgorithmName(),
                                          algorithm.ToRSASignaturePadding()));
                }

            case KeyType.ECC:
                using (var ecc = key.ToECDsa()) {
                    return(ecc.VerifyHash(hash, signature));
                }

            default:
                throw new ArgumentException("Bad key type passed for signing");
            }
        }
Example #4
0
        /// <inheritdoc/>
        public async Task <Key> ExportKeyAsync(KeyHandle handle, CancellationToken ct)
        {
            var document = await _keys.GetAsync <KeyDocument>(
                KeyId.GetId(handle), ct);

            if (!document.Value.IsExportable)
            {
                throw new InvalidOperationException("Key is not exportable");
            }
            if (document.Value.IsDisabled)
            {
                throw new InvalidOperationException("Key is disabled");
            }
            return(document.Value.KeyJson.ToKey());
        }
Example #5
0
 /// <inheritdoc/>
 public Task DeleteKeyAsync(KeyHandle handle, CancellationToken ct)
 {
     return(_keys.DeleteAsync(KeyId.GetId(handle), ct));
 }