Exemple #1
0
        /// <summary>
        /// Determines whether the <see cref="ECDsaSecurityKey"/> can compute a JWK thumbprint.
        /// </summary>
        /// <returns><c>true</c> if JWK thumbprint can be computed; otherwise, <c>false</c>.</returns>
        /// <remarks>https://tools.ietf.org/html/rfc7638</remarks>
        public override bool CanComputeJwkThumbprint()
        {
#if NET472 || NETSTANDARD2_0
            if (ECDsaAdapter.SupportsECParameters())
            {
                return(true);
            }
#endif
            return(false);
        }
Exemple #2
0
        /// <summary>
        /// Computes a sha256 hash over the <see cref="ECDsaSecurityKey"/>.
        /// </summary>
        /// <returns>A JWK thumbprint.</returns>
        /// <remarks>https://tools.ietf.org/html/rfc7638</remarks>
        public override byte[] ComputeJwkThumbprint()
        {
#if NET472 || NETSTANDARD2_0
            if (ECDsaAdapter.SupportsECParameters())
            {
                ECParameters parameters   = ECDsa.ExportParameters(false);
                var          canonicalJwk = $@"{{""{JsonWebKeyParameterNames.Crv}"":""{ECDsaAdapter.GetCrvParameterValue(parameters.Curve)}"",""{JsonWebKeyParameterNames.Kty}"":""{JsonWebAlgorithmsKeyTypes.EllipticCurve}"",""{JsonWebKeyParameterNames.X}"":""{Base64UrlEncoder.Encode(parameters.Q.X)}"",""{JsonWebKeyParameterNames.Y}"":""{Base64UrlEncoder.Encode(parameters.Q.Y)}""}}";
                return(Utility.GenerateSha256Hash(canonicalJwk));
            }
#endif
            throw LogHelper.LogExceptionMessage(new PlatformNotSupportedException(LogMessages.IDX10695));
        }
        /// <summary>
        /// Converts a <see cref="ECDsaSecurityKey"/> into a <see cref="JsonWebKey"/>
        /// </summary>
        /// <param name="key">an <see cref="ECDsaSecurityKey"/> to convert.</param>
        /// <returns>a <see cref="JsonWebKey"/></returns>
        /// <exception cref="ArgumentNullException">if <paramref name="key"/>is null.</exception>
        public static JsonWebKey ConvertFromECDsaSecurityKey(ECDsaSecurityKey key)
        {
            if (!ECDsaAdapter.SupportsECParameters())
            {
                throw LogHelper.LogExceptionMessage(new PlatformNotSupportedException(LogMessages.IDX10695));
            }

            if (key == null)
            {
                throw LogHelper.LogArgumentNullException(nameof(key));
            }

            if (key.ECDsa == null)
            {
                throw LogHelper.LogArgumentNullException(nameof(key.ECDsa));
            }

            ECParameters parameters;

            try
            {
                parameters = key.ECDsa.ExportParameters(true);
            }
            catch
            {
                parameters = key.ECDsa.ExportParameters(false);
            }

            return(new JsonWebKey
            {
                Crv = ECDsaAdapter.GetCrvParameterValue(parameters.Curve),
                X = parameters.Q.X != null?Base64UrlEncoder.Encode(parameters.Q.X) : null,
                        Y = parameters.Q.Y != null?Base64UrlEncoder.Encode(parameters.Q.Y) : null,
                                D = parameters.D != null?Base64UrlEncoder.Encode(parameters.D) : null,
                                        Kty = JsonWebAlgorithmsKeyTypes.EllipticCurve,
                                        Kid = key.KeyId,
                                        ConvertedSecurityKey = key
            });
        }