예제 #1
0
        public FakeEccKeyRepository(CngKey key)
        {
            _key = key;
            var xml = new ECDsaCng(_key).ToXmlString(ECKeyXmlFormat.Rfc4050);

            _public = new ECDsaCng();
            _public.FromXmlString(xml, ECKeyXmlFormat.Rfc4050);
        }
예제 #2
0
        public ECDsaCng LoadRemoteKey(JsonWebTokenHeader header)
        {
            var dsa    = new ECDsaCng();
            var cached = _cache.GetPublicKeyBytes(header.KeyUri.ToString(), header.KeyId);

            if (null != cached)
            {
                dsa.FromXmlString(Encoding.UTF8.GetString(cached), ECKeyXmlFormat.Rfc4050);
                return(dsa);
            }

            string data;

            using (var wc = new WebClient())
            {
                try
                {
                    data = wc.DownloadString(header.KeyUri);
                }
                catch (WebException e)
                {
                    throw new RemoteKeyInaccessibleException("Unable to download the public key from URI " + header.KeyUri, e);
                }
            }

            switch (header.KeyFormat)
            {
            case KeyFormat.Rfc4050:

                dsa.FromXmlString(data, ECKeyXmlFormat.Rfc4050);
                _cache.Cache(Encoding.UTF8.GetBytes(data), header.KeyId, header.KeyUri.ToString());
                return(dsa);

            case KeyFormat.X509:
                var ms     = new MemoryStream(Encoding.ASCII.GetBytes(data));
                var reader = new CngBuilder(new PemReader(ms));
                dsa = new ECDsaCng(reader.Build());
                _cache.Cache(Encoding.UTF8.GetBytes(dsa.ToXmlString(ECKeyXmlFormat.Rfc4050)), header.KeyId, header.KeyUri.ToString());
                return(dsa);
            }
            throw new NotSupportedException("Can not open an ECC key with the keyformat " + header.KeyFormat);
        }
예제 #3
0
        public static bool Verify(byte[] publicKey, byte[] signature, Stream stream)
        {
#if Mono
            throw new NotSupportedException();
#else
            try
            {
                using (ECDsaCng ecdsa = new ECDsaCng())
                {
                    ecdsa.FromXmlString(Encoding.ASCII.GetString(publicKey), ECKeyXmlFormat.Rfc4050);
                    ecdsa.HashAlgorithm = CngAlgorithm.Sha256;
                    return(ecdsa.VerifyData(stream, signature));
                }
            }
            catch (Exception)
            {
                return(false);
            }
#endif
        }