示例#1
0
        public CngKey Build()
        {
            if (_theReader.First().Header == "PUBLIC KEY")
            {
                using (var ms = new MemoryStream(_theReader.First().Body))
                    using (var br = new BinaryReader(ms))
                    {
                        // should have a sequence
                        br.Require(Asn1Token.Sequence);
                        // ignore the length
                        br.ReadLengthField();

                        // should have an inner sequence
                        br.Require(Asn1Token.Sequence);
                        // ignore the length
                        br.ReadLengthField();

                        //should have an OID
                        br.Require(Asn1Token.Oid);
                        // get its length
                        int oidLength = br.ReadByte();
                        var oid       = Asn1Token.GetOid(br.ReadBytes(oidLength));
                        var builder   = GetBuilderFor(oid);
                        return(builder.Build(br));
                    }
            }
            throw new NotImplementedException();
        }
示例#2
0
            public override CngKey Build(BinaryReader reader)
            {
                reader.Require(Asn1Token.Oid);
                var length = reader.ReadLengthField();
                var curve  = Asn1Token.GetOid(reader.ReadBytes(length));

                if (!isSupportedCurve(curve))
                {
                    throw new UnsupportedCurveException("Unsupported curve oid");
                }

                // We need to build a key blob structured as follows:
                //     BCRYPT_ECCKEY_BLOB   header
                //     byte[cbPublicExp]    publicExponent      - Exponent
                //     byte[cbModulus]      modulus             - Modulus
                //     -- Private only --
                //     byte[cbPrime1]       prime1              - P
                //     byte[cbPrime2]       prime2              - Q
                //

                // Where
                // typedef struct _BCRYPT_ECCKEY_BLOB {
                //  ULONG Magic; //BCRYPT_ECDSA_PUBLIC_P256_MAGIC =  0x31534345
                //  ULONG cbKey; // Key length in bytes
                //} B

                reader.Require(Asn1Token.BitString);
                var keyLength = reader.ReadLengthField() - 2;

                //ignore the zero byte
                reader.Require(0x00);

                // if this isn't an uncompressed curve, then panic
                reader.Require(0x04);

                var x = reader.ReadBytes(keyLength / 2);
                var y = reader.ReadBytes(keyLength / 2);

                return(BuildEcKey(x, y, curve));
            }