Ejemplo n.º 1
0
        public void DerAsnInteger_Parse_ShouldDecodeCorrectly()
        {
            var data = new byte[] { 0x02, 0x81, 0x80,
                                    0x47, 0xeb, 0x99, 0x5a, 0xdf, 0x9e, 0x70, 0x0d, 0xfb, 0xa7, 0x31, 0x32, 0xc1, 0x5f, 0x5c, 0x24,
                                    0xc2, 0xe0, 0xbf, 0xc6, 0x24, 0xaf, 0x15, 0x66, 0x0e, 0xb8, 0x6a, 0x2e, 0xab, 0x2b, 0xc4, 0x97,
                                    0x1f, 0xe3, 0xcb, 0xdc, 0x63, 0xa5, 0x25, 0xec, 0xc7, 0xb4, 0x28, 0x61, 0x66, 0x36, 0xa1, 0x31,
                                    0x1b, 0xbf, 0xdd, 0xd0, 0xfc, 0xbf, 0x17, 0x94, 0x90, 0x1d, 0xe5, 0x5e, 0xc7, 0x11, 0x5e, 0xc9,
                                    0x55, 0x9f, 0xeb, 0xa3, 0x3e, 0x14, 0xc7, 0x99, 0xa6, 0xcb, 0xba, 0xa1, 0x46, 0x0f, 0x39, 0xd4,
                                    0x44, 0xc4, 0xc8, 0x4b, 0x76, 0x0e, 0x20, 0x5d, 0x6d, 0xa9, 0x34, 0x9e, 0xd4, 0xd5, 0x87, 0x42,
                                    0xeb, 0x24, 0x26, 0x51, 0x14, 0x90, 0xb4, 0x0f, 0x06, 0x5e, 0x52, 0x88, 0x32, 0x7a, 0x95, 0x20,
                                    0xa0, 0xfd, 0xf7, 0xe5, 0x7d, 0x60, 0xdd, 0x72, 0x68, 0x9b, 0xf5, 0x7b, 0x05, 0x8f, 0x6d, 0x1e };

            var type = DerAsnType.Parse(data);

            Assert.That(type is DerAsnInteger, Is.True);

            var integerType = type as DerAsnInteger;

            Assert.That(integerType.Value as byte[], Is.EqualTo(data.Skip(3).ToArray()));
            Assert.That(integerType.Unsigned, Is.False);

            integerType = DerAsnType.Parse(new byte[] { 0x02, 0x02, 0x00, 0x83 }) as DerAsnInteger;
            Assert.That(integerType.Value as byte[], Is.EqualTo(new byte[] { 0x83 }));
            Assert.That(integerType.Unsigned, Is.True);

            integerType = DerAsnType.Parse(new byte[] { 0x02, 0x02, 0x83, 0x40 }) as DerAsnInteger;
            Assert.That(integerType.Value as byte[], Is.EqualTo(new byte[] { 0x83, 0x40 }));
            Assert.That(integerType.Unsigned, Is.False);

            integerType = DerAsnType.Parse(new byte[] { 0x02, 0x01, 0x45 }) as DerAsnInteger;
            Assert.That(integerType.Value as byte[], Is.EqualTo(new byte[] { 0x45 }));
            Assert.That(integerType.Unsigned, Is.False);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Constructs the QcStatement so it can be added to a certificate.
        /// </summary>
        public QcTypeStatement(QcTypeIdentifiers type) : base(Array.Empty <DerAsnType>())
        {
            var oid     = new DerAsnObjectIdentifier(DerAsnIdentifiers.Primitive.ObjectIdentifier, Oid_QcType.OidToArray());
            var oidType = new DerAsnObjectIdentifier(DerAsnIdentifiers.Primitive.ObjectIdentifier, (Oid_QcType + "." + (int)type).OidToArray());

            Value = new DerAsnType[] { oid, new DerAsnSequence(new DerAsnType[] { oidType }) };
        }
Ejemplo n.º 3
0
        private static RSAParameters ReadPrivateKey(DerAsnType der)
        {
            if (der == null)
            {
                throw new ArgumentNullException(nameof(der));
            }
            var sequence = der as DerAsnSequence;

            if (sequence == null)
            {
                throw new ArgumentException($"{nameof(der)} is not a sequence");
            }
            if (sequence.Value.Length != 9)
            {
                throw new InvalidOperationException("Sequence must contain 9 parts");
            }
            return(new RSAParameters
            {
                Modulus = GetIntegerData(sequence.Value[1]),
                Exponent = GetIntegerData(sequence.Value[2]),
                D = GetIntegerData(sequence.Value[3]),
                P = GetIntegerData(sequence.Value[4]),
                Q = GetIntegerData(sequence.Value[5]),
                DP = GetIntegerData(sequence.Value[6]),
                DQ = GetIntegerData(sequence.Value[7]),
                InverseQ = GetIntegerData(sequence.Value[8]),
            });
        }
Ejemplo n.º 4
0
        private static byte[] GetIntegerData(DerAsnType der)
        {
            var data = (der as DerAsnInteger)?.Value as byte[];

            if (data == null)
            {
                throw new InvalidOperationException("Part does not contain integer data");
            }
            return(data);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Constructs the QcStatement so it can be added to a certificate.
        /// </summary>
        public QcLimitValueStatement(decimal limitValue, string currenyCode) : base(Array.Empty <DerAsnType>())
        {
            var parts  = decimal.GetBits(limitValue);
            var scale  = (byte)((parts[3] >> 16) & 0x7F);
            var amount = ((BigInteger)limitValue) * (int)Math.Pow(10.0, scale);
            var oid    = new DerAsnObjectIdentifier(DerAsnIdentifiers.Primitive.ObjectIdentifier, Oid_QcLimitValue.OidToArray());

            Value = new DerAsnType[] { oid, new DerAsnSequence(new DerAsnType[] {
                    new DerAsnPrintableString(currenyCode.Substring(0, 3)),
                    new DerAsnInteger(amount),
                    new DerAsnInteger(new BigInteger(-scale)),
                }) };
        }
Ejemplo n.º 6
0
        private static byte[] GetIntegerData(DerAsnType der)
        {
            var data = (der as DerAsnInteger)?.Encode(null);

            if (data == null)
            {
                throw new InvalidOperationException("Part does not contain integer data");
            }
            if (data[0] == 0x00)
            {
                data = data.Skip(1).ToArray();
            }
            return(data);
        }
Ejemplo n.º 7
0
        public void DerAsnBoolean_Parse_ShouldDecodeCorrectly()
        {
            var dataFalse = new byte[] { 0x01, 0x01, 0x00 };
            var dataTrue  = new byte[] { 0x01, 0x01, 0x07 };

            var type = DerAsnType.Parse(dataFalse);

            Assert.That(type is DerAsnBoolean, Is.True);
            Assert.That((bool)type.Value, Is.False);

            type = DerAsnType.Parse(dataTrue);
            Assert.That(type is DerAsnBoolean, Is.True);
            Assert.That((bool)type.Value, Is.True);
        }
        public void DerAsnOctetString_Parse_ShouldDecodeCorrectly()
        {
            var data = new byte[]
            {
                0x04, 0x0A,
                0x1E, 0x08, 0x00, 0x55, 0x00, 0x73, 0x00, 0x65, 0x00, 0x72
            };

            var type = DerAsnType.Parse(data);

            Assert.That(type is DerAsnOctetString, Is.True);

            Assert.That(type.Value, Is.EqualTo(data.Skip(2).ToArray()));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Constructs the QcStatement so it can be added to a certificate.
        /// </summary>
        public QcPdsStatement(IEnumerable <PdsLocation> pdsLocations) : base(Array.Empty <DerAsnType>())
        {
            var oid      = new DerAsnObjectIdentifier(DerAsnIdentifiers.Primitive.ObjectIdentifier, Oid_QcPds.OidToArray());
            var sequence = new List <DerAsnType>();

            foreach (var item in pdsLocations)
            {
                var pdsSequense = new DerAsnSequence(new DerAsnType[] {
                    new DerAsnIa5String(item.Url),
                    new DerAsnPrintableString(item.Language)
                });
                sequence.Add(pdsSequense);
            }
            Value = new DerAsnType[] { oid, new DerAsnSequence(sequence.ToArray()) };
        }
Ejemplo n.º 10
0
        public void DerAsnObjectIdentifier_Parse_ShouldDecodeCorrectly()
        {
            var data = new byte[]
            {
                0x06, 0x09,
                0x2B, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x15, 0x14
            };

            var type = DerAsnType.Parse(data);

            Assert.That(type is DerAsnObjectIdentifier, Is.True);

            var objectIdentifier = type as DerAsnObjectIdentifier;

            Assert.That(objectIdentifier.Value, Is.EqualTo("1.3.6.1.4.1.311.21.20"));
        }
Ejemplo n.º 11
0
        public static byte[] Encode(DerAsnType asnType)
        {
            if (asnType == null)
            {
                throw new ArgumentNullException(nameof(asnType));
            }
            if (DefaultEncoder == null)
            {
                throw new ArgumentNullException(nameof(DefaultEncoder));
            }

            using (var encoder = DefaultEncoder())
            {
                return(encoder.Encode(asnType).ToArray());
            }
        }
Ejemplo n.º 12
0
        private void Write(DerAsnType der, PemFormat format)
        {
            var derBytes = DerConvert.Encode(der);

            var derBase64 = Convert.ToBase64String(derBytes);

            var pem = new StringBuilder();

            pem.Append(format.Header + "\n");
            for (var i = 0; i < derBase64.Length; i += _maximumLineLength)
            {
                pem.Append(derBase64.Substring(i, Math.Min(_maximumLineLength, derBase64.Length - i)));
                pem.Append("\n");
            }
            pem.Append(format.Footer + "\n");

            using (var writer = new StreamWriter(_stream, _encoding, 4096, true))
                writer.Write(pem.ToString());
        }
Ejemplo n.º 13
0
        public void DerAsnSequence_Parse_ShouldDecodeCorrectly()
        {
            var data = new byte[]
            {
                0x30, 0x0D,
                0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01,
                0x05, 0x00
            };

            var type = DerAsnType.Parse(data);

            Assert.That(type is DerAsnSequence, Is.True);

            var items = type.Value as DerAsnType[];

            Assert.That(items, Is.Not.Null);
            Assert.That(items[0] is DerAsnObjectIdentifier, Is.True);
            Assert.That(items[0].Value, Is.EqualTo("1.2.840.113549.1.1.1"));
            Assert.That(items[1] is DerAsnNull, Is.True);
        }
Ejemplo n.º 14
0
        public void DerAsnBitString_Parse_ShouldDecodeCorrectly()
        {
            var data = new byte[] { 0x03, 0x81, 0x81, 0x05,
                                    0x47, 0xeb, 0x99, 0x5a, 0xdf, 0x9e, 0x70, 0x0d, 0xfb, 0xa7, 0x31, 0x32, 0xc1, 0x5f, 0x5c, 0x24,
                                    0xc2, 0xe0, 0xbf, 0xc6, 0x24, 0xaf, 0x15, 0x66, 0x0e, 0xb8, 0x6a, 0x2e, 0xab, 0x2b, 0xc4, 0x97,
                                    0x1f, 0xe3, 0xcb, 0xdc, 0x63, 0xa5, 0x25, 0xec, 0xc7, 0xb4, 0x28, 0x61, 0x66, 0x36, 0xa1, 0x31,
                                    0x1b, 0xbf, 0xdd, 0xd0, 0xfc, 0xbf, 0x17, 0x94, 0x90, 0x1d, 0xe5, 0x5e, 0xc7, 0x11, 0x5e, 0xc9,
                                    0x55, 0x9f, 0xeb, 0xa3, 0x3e, 0x14, 0xc7, 0x99, 0xa6, 0xcb, 0xba, 0xa1, 0x46, 0x0f, 0x39, 0xd4,
                                    0x44, 0xc4, 0xc8, 0x4b, 0x76, 0x0e, 0x20, 0x5d, 0x6d, 0xa9, 0x34, 0x9e, 0xd4, 0xd5, 0x87, 0x42,
                                    0xeb, 0x24, 0x26, 0x51, 0x14, 0x90, 0xb4, 0x0f, 0x06, 0x5e, 0x52, 0x88, 0x32, 0x7a, 0x95, 0x20,
                                    0xa0, 0xfd, 0xf7, 0xe5, 0x7d, 0x60, 0xdd, 0x72, 0x68, 0x9b, 0xf5, 0x7b, 0x05, 0x8f, 0x6d, 0x1e };

            var type = DerAsnType.Parse(data);

            Assert.That(type is DerAsnBitString, Is.True);

            var bitString = type as DerAsnBitString;

            Assert.That(bitString.UnusedLowerBitsInLastByte, Is.EqualTo(0x05));
            Assert.That((bitString.Value as byte[]), Is.EqualTo(data.Skip(4).ToArray()));
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Constructs the QcStatement from <see cref="Psd2Attributes "/>.
        /// </summary>
        /// <param name="psd2"></param>
        public Psd2QcStatement(Psd2Attributes psd2) : base(Array.Empty <DerAsnType>())
        {
            var rolesList = new List <DerAsnSequence>();

            foreach (var roleName in psd2.Roles)
            {
                var id   = new DerAsnObjectIdentifier(DerAsnIdentifiers.Primitive.ObjectIdentifier, GetPsd2Oid(roleName).OidToArray());
                var name = new DerAsnUtf8String(roleName);
                var role = new DerAsnSequence(new DerAsnType[] { id, name });
                rolesList.Add(role);
            }
            var rolesOfPSP = new DerAsnSequence(rolesList.ToArray()); //RolesOfPSP ::= SEQUENCE OF RoleOfPSP
            var ncaName    = new DerAsnUtf8String(psd2.AuthorityName);
            var ncaId      = new DerAsnUtf8String(psd2.AuthorizationId.ToString());

            var typeSequence = new DerAsnSequence(new DerAsnType[] { rolesOfPSP, ncaName, ncaId });

            var psd2QstatementOid = new DerAsnObjectIdentifier(DerAsnIdentifiers.Primitive.ObjectIdentifier, Oid_PSD2_QcStatement.OidToArray());

            Value = new DerAsnType[] { psd2QstatementOid, typeSequence };
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Constructs the QcStatement from <see cref="Psd2CertificateAttributes "/>.
        /// </summary>
        /// <param name="type"></param>
        public Psd2QcStatement(Psd2CertificateAttributes type) : base(new DerAsnType[0])
        {
            var rolesList = new List <DerAsnSequence>();

            foreach (var roleName in type.Roles)
            {
                var id   = new DerAsnObjectIdentifier(DerAsnIdentifiers.Primitive.ObjectIdentifier, Oid2Array(GetPsd2Oid(roleName)));
                var name = new DerAsnUtf8String(roleName);
                var role = new DerAsnSequence(new DerAsnType[] { id, name });
                rolesList.Add(role);
            }
            var rolesOfPSP = new DerAsnSequence(rolesList.ToArray()); //RolesOfPSP ::= SEQUENCE OF RoleOfPSP
            var ncaName    = new DerAsnUtf8String(type.AuthorityName);
            var ncaId      = new DerAsnUtf8String(type.AuthorizationNumber.ToString());

            var typeSequence = new DerAsnSequence(new DerAsnType[] { rolesOfPSP, ncaName, ncaId });

            var psd2QstatementOid = new DerAsnObjectIdentifier(DerAsnIdentifiers.Primitive.ObjectIdentifier, Oid2Array(Oid_PSD2_QcStatement));

            Value = new DerAsnType[] { psd2QstatementOid, typeSequence };
        }
Ejemplo n.º 17
0
 public static DerAsnType Decode(byte[] data) => DerAsnType.Parse(data);
Ejemplo n.º 18
0
        /// <summary>
        /// Constructs the QcStatement so it can be added to a certificate.
        /// </summary>
        public QcRetentionPeriodStatement(int retentionPeriod) : base(Array.Empty <DerAsnType>())
        {
            var oid = new DerAsnObjectIdentifier(DerAsnIdentifiers.Primitive.ObjectIdentifier, Oid_QcRetentionPeriod.OidToArray());

            Value = new DerAsnType[] { oid, new DerAsnInteger(new BigInteger(retentionPeriod)) };
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Constructs the QcStatement so it can be added to a certificate.
        /// </summary>
        public QcSSCDStatement() : base(Array.Empty <DerAsnType>())
        {
            var oid = new DerAsnObjectIdentifier(DerAsnIdentifiers.Primitive.ObjectIdentifier, Oid_QcSSCD.OidToArray());

            Value = new DerAsnType[] { oid };
        }
Ejemplo n.º 20
0
        private static RSAParameters ReadPublicKey(DerAsnType der)
        {
            if (der == null)
            {
                throw new ArgumentNullException(nameof(der));
            }
            var outerSequence = der as DerAsnSequence;

            if (outerSequence == null)
            {
                throw new ArgumentException($"{nameof(der)} is not a sequence");
            }
            if (outerSequence.Value.Length != 2)
            {
                throw new InvalidOperationException("Outer sequence must contain 2 parts");
            }

            var headerSequence = outerSequence.Value[0] as DerAsnSequence;

            if (headerSequence == null)
            {
                throw new InvalidOperationException("First part of outer sequence must be another sequence (the header sequence)");
            }
            if (headerSequence.Value.Length != 2)
            {
                throw new InvalidOperationException("The header sequence must contain 2 parts");
            }
            var objectIdentifier = headerSequence.Value[0] as DerAsnObjectIdentifier;

            if (objectIdentifier == null)
            {
                throw new InvalidOperationException("First part of header sequence must be an object-identifier");
            }
            if (!Enumerable.SequenceEqual(objectIdentifier.Value, RsaIdentifier))
            {
                throw new InvalidOperationException($"RSA object-identifier expected 1.2.840.113549.1.1.1, got: {string.Join(".", objectIdentifier.Value.Select(x => x.ToString()))}");
            }
            if (!(headerSequence.Value[1] is DerAsnNull))
            {
                throw new InvalidOperationException("Second part of header sequence must be a null");
            }

            var innerSequenceBitString = outerSequence.Value[1] as DerAsnBitString;

            if (innerSequenceBitString == null)
            {
                throw new InvalidOperationException("Second part of outer sequence must be a bit-string");
            }

            var innerSequenceData = innerSequenceBitString.ToByteArray();
            var innerSequence     = DerConvert.Decode(innerSequenceData) as DerAsnSequence;

            if (innerSequence == null)
            {
                throw new InvalidOperationException("Could not decode the bit-string as a sequence");
            }
            if (innerSequence.Value.Length < 2)
            {
                throw new InvalidOperationException("Inner sequence must at least contain 2 parts (modulus and exponent)");
            }

            return(new RSAParameters
            {
                Modulus = GetIntegerData(innerSequence.Value[0]),
                Exponent = GetIntegerData(innerSequence.Value[1])
            });
        }
Ejemplo n.º 21
0
        private static RSAParameters ReadPublicKey(DerAsnType der)
        {
            if (der == null)
            {
                throw new ArgumentNullException(nameof(der));
            }
            var outerSequence = der as DerAsnSequence;

            if (outerSequence == null)
            {
                throw new ArgumentException($"{nameof(der)} is not a sequence");
            }
            if (outerSequence.Items.Count != 2)
            {
                throw new InvalidOperationException("Outer sequence must contain 2 parts");
            }

            var headerSequence = outerSequence.Items[0] as DerAsnSequence;

            if (headerSequence == null)
            {
                throw new InvalidOperationException("First part of outer sequence must be another sequence (the header sequence)");
            }
            if (headerSequence.Items.Count != 2)
            {
                throw new InvalidOperationException("The header sequence must contain 2 parts");
            }
            var objectIdentifier = headerSequence.Items[0] as DerAsnObjectIdentifier;

            if (objectIdentifier == null)
            {
                throw new InvalidOperationException("First part of header sequence must be an object-identifier");
            }
            if (!objectIdentifier.Value.Equals("1.2.840.113549.1.1.1"))
            {
                throw new InvalidOperationException($"RSA object-identifier expected 1.2.840.113549.1.1.1, got: {objectIdentifier.Value}");
            }
            if (!(headerSequence.Items[1] is DerAsnNull))
            {
                throw new InvalidOperationException("Second part of header sequence must be a null");
            }

            var innerSequenceData = outerSequence.Items[1] as DerAsnBitString;

            if (innerSequenceData == null)
            {
                throw new InvalidOperationException("Second part of outer sequence must be a bit-string");
            }

            var innerSequence = DerConvert.Decode(innerSequenceData.Value as byte[]) as DerAsnSequence;

            if (innerSequence == null)
            {
                throw new InvalidOperationException("Could not decode the bit-string as a sequence");
            }
            if (innerSequence.Items.Count < 2)
            {
                throw new InvalidOperationException("Inner sequence must at least contain 2 parts (modulus and exponent)");
            }

            return(new RSAParameters
            {
                Modulus = GetIntegerData(innerSequence.Items[0]),
                Exponent = GetIntegerData(innerSequence.Items[1])
            });
        }
Ejemplo n.º 22
0
 public static byte[] Encode(DerAsnType data) => data.GetBytes();
Ejemplo n.º 23
0
        public void DerAsnNull_Parse_ShouldDecodeCorrectly()
        {
            var type = DerAsnType.Parse(new byte[] { 0x05, 0x00 });

            Assert.That(type is DerAsnNull, Is.True);
        }