< Summary

Class:Microsoft.Azure.KeyVault.WebKey.EccExtension
Assembly:Microsoft.Azure.KeyVault.WebKey
File(s):C:\Git\azure-sdk-for-net\sdk\keyvault\Microsoft.Azure.KeyVault.WebKey\src\EccExtension.cs
Covered lines:16
Uncovered lines:2
Coverable lines:18
Total lines:60
Line coverage:88.8% (16 of 18)
Covered branches:2
Total branches:4
Branch coverage:50% (2 of 4)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
ExportParameters(...)-100%100%
GetKeyOperations(...)-75%50%
GetEcdsaCng(...)-75%50%
.cctor()-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\keyvault\Microsoft.Azure.KeyVault.WebKey\src\EccExtension.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License. See License.txt in the project root for
 3// license information.
 4
 5using System;
 6using System.Collections.Generic;
 7using System.Security.Cryptography;
 8
 9namespace Microsoft.Azure.KeyVault.WebKey
 10{
 11    /// <summary>
 12    /// Because the current version of ECC is not supporting some of the operations needed for WebKey,
 13    /// those operations are added as ECC extension.
 14    /// </summary>
 15    public static class EccExtension
 16    {
 17        /// <summary>
 18        /// Exports EC parameters from a CNG object.
 19        /// </summary>
 20        /// <param name="ecdsa">The CNG object initialized with desired key</param>
 21        /// <param name="includePrivateParameters">Determines whether the private key part is to be exported.</param>
 22        /// <returns></returns>
 23        public static ECParameters ExportParameters( this ECDsa ecdsa, bool includePrivateParameters )
 24        {
 4425            var ecdsaCng = GetEcdsaCng( ecdsa );
 4426            return ECParameters.FromEcdsa( ecdsaCng, includePrivateParameters );
 27        }
 28
 29        public static string[] GetKeyOperations( this ECDsa ecdsa )
 30        {
 4431            var keyUsage = GetEcdsaCng( ecdsa ).Key.KeyUsage;
 32
 4433            if ( !_cngOperations.ContainsKey( keyUsage ) )
 034                throw new CryptographicException( $"Unknown key usage {keyUsage}" );
 35
 4436            return (string[]) _cngOperations[keyUsage].Clone();
 37        }
 38
 39        private static ECDsaCng GetEcdsaCng(ECDsa ecdsa )
 40        {
 8841            var ecdsaCng = ecdsa as ECDsaCng;
 8842            if ( ecdsaCng == null )
 043                throw new NotSupportedException( $"This version requires a CNG object." );
 8844            return ecdsaCng;
 45        }
 46
 47        private static readonly Dictionary<CngKeyUsages, string[]> _cngOperations;
 48
 49        static EccExtension()
 50        {
 251            _cngOperations = new Dictionary<CngKeyUsages, string[]>
 252            {
 253                {CngKeyUsages.None, new string[0]},
 254                {CngKeyUsages.Signing, new[] {JsonWebKeyOperation.Sign, JsonWebKeyOperation.Verify}},
 255                {CngKeyUsages.Decryption, new[] {JsonWebKeyOperation.Encrypt, JsonWebKeyOperation.Decrypt, JsonWebKeyOpe
 256                {CngKeyUsages.AllUsages, JsonWebKeyOperation.AllOperations}
 257            };
 258        }
 59    }
 60}