示例#1
0
        internal static KeyPair GetKeyPair(byte[] encoded)
        {
            var parser  = new Asn1StreamParser(encoded);
            var keyPair = new KeyPair();

            parser.ReadObject <DerSequenceParser>();
            var version = parser.ReadObject <DerInteger>().Value.IntValue;

            if (1 != version)
            {
                throw new ParsingException($"Expected version 1, got {version}");
            }

            var octetString = (DerOctetString)parser.ReadObject <DerOctetStringParser>().ToAsn1Object();

            keyPair.PrivateKey = new BigInteger(1, octetString.GetOctets());

            parser.ReadObject <BerTaggedObjectParser>();
            var oid = parser.ReadObject <DerObjectIdentifier>();

            if (!BrainpoolP320R1.ObjectIdentifier.Equals(oid))
            {
                throw new ParsingException($"Expected BrainpoolP320r1 ({BrainpoolP320R1.ObjectIdentifier}), got {oid}");
            }

            var obj = parser.ReadObject();

            if (obj == null)
            {
                return(keyPair);
            }

            Asn1StreamParserExtensions.CheckObject <BerTaggedObjectParser>(obj);
            try
            {
                keyPair.PublicKey = BrainpoolP320R1.Curve.DecodePoint(parser.ReadObject <DerBitString>().GetBytes()).Normalize();
            }
            catch (ArgumentException e)
            {
                throw new ParsingException("Could not decode point on curve", e);
            }

            BrainpoolP320R1.G.Multiply(keyPair.PrivateKey).Normalize();
            if (!BrainpoolP320R1.G.Multiply(keyPair.PrivateKey).Equals(keyPair.PublicKey))
            {
                throw new ParsingException("Public key does not belong to private key");
            }

            return(keyPair);
        }
示例#2
0
        public static T GetEncryptedEntity <T>(byte[] encoded, bool isPseudonym) where T : EncryptedEntity, new()
        {
            var parser = new Asn1StreamParser(encoded);
            var entity = new T();

            parser.ReadObject <DerSequenceParser>();
            parser.ReadObject <DerSequenceParser>();
            var oid = parser.ReadObject <DerObjectIdentifier>().Id;

            AssertBsnkTypeIsCorrect(oid, isPseudonym);

            entity.SchemeVersion    = parser.ReadObject <DerInteger>().Value.IntValue;
            entity.SchemeKeyVersion = parser.ReadObject <DerInteger>().Value.IntValue;
            parser.ReadObject <DerIA5String>(); //Creator, not used
            entity.Recipient = parser.ReadObject <DerIA5String>().GetString();
            entity.RecipientKeySetVersion = parser.ReadObject <DerInteger>().Value.IntValue;

            if (isPseudonym)
            {
                var obj = parser.ReadObject();
                if (obj is DerIA5String derIa5String)
                {
                    derIa5String.GetString();
                    parser.ReadObject <DerInteger>(); //Type, not used
                }
                else
                {
                    Asn1StreamParserExtensions.CheckObject <DerInteger>(obj); //Type, not used
                }
            }

            parser.ReadObject <DerSequenceParser>();

            for (var i = 0; i < entity.Points.Length; i++)
            {
                var octet =
                    (DerOctetString)parser.ReadObject <DerOctetStringParser>().ToAsn1Object();
                try
                {
                    entity.Points[i] = BrainpoolP320R1.Curve.DecodePoint(octet.GetOctets());
                }
                catch (ArgumentException e)
                {
                    throw new ParsingException("Could not decode point on curve", e);
                }
            }

            return(entity);
        }