Exemplo n.º 1
0
        private void DecodeExtension()
        {
            var sequence = DerConvert.Decode(RawData) as DerAsnSequence;

            _Policies = new CertificatePolicies(sequence.Value).Extract();
            _decoded  = true;
        }
Exemplo n.º 2
0
        private void DecodeExtension()
        {
            var sequence = DerConvert.Decode(RawData) as DerAsnSequence;

            _Psd2Type = new Psd2QcStatement(sequence.Value).ExtractAttributes();
            _decoded  = true;
        }
Exemplo n.º 3
0
        public void Decode_NoDefaultDecoder_ShouldThrowArgumentNullException()
        {
            DerConvert.DefaultDecoder = null;
            var ex = Assert.Throws <ArgumentNullException>(() => DerConvert.Decode(new byte[] { 0x00 }));

            Assert.That(ex.ParamName, Is.EqualTo("DefaultDecoder"));
        }
Exemplo n.º 4
0
        public void Decode_EmptyRawDataArray_ShouldThrowArgumentException()
        {
            var ex = Assert.Throws <ArgumentException>(() => DerConvert.Decode(new byte[] { }));

            Assert.That(ex.Message, Does.StartWith("Data cannot be empty"));
            Assert.That(ex.ParamName, Is.EqualTo("rawData"));
        }
Exemplo n.º 5
0
        public void Decode_ShouldForwardCallToDecoder_ShouldDisposeDecoder()
        {
            var data        = new byte[] { 1, 2, 3 };
            var decoderMock = new Mock <IDerAsnDecoder>();

            DerConvert.DefaultDecoder = () => decoderMock.Object;
            DerConvert.Decode(data);
            decoderMock.Verify(x => x.Decode(data), Times.Once);
            decoderMock.Verify(x => x.Dispose(), Times.Once);
        }
Exemplo n.º 6
0
        private void DecodeExtension()
        {
            _OrganizationIdentifier = new CABForumOrganizationIdentifier();
            var root = DerConvert.Decode(RawData) as DerAsnSequence;

            _OrganizationIdentifier.SchemeIdentifier = ((DerAsnPrintableString)root.Value[0]).Value;
            _OrganizationIdentifier.Country          = ((DerAsnPrintableString)root.Value[1]).Value;
            _OrganizationIdentifier.Reference        = ((DerAsnUtf8String)root.Value[2]).Value;
            _decoded = true;
        }
Exemplo n.º 7
0
        private void DecodeExtension()
        {
            _Statements = new QualifiedCertificateStatements();
            var root = DerConvert.Decode(RawData) as DerAsnSequence;

            if (root.Value[0] is DerAsnSequence)
            {
                foreach (var sequence in root.Value.OfType <DerAsnSequence>())
                {
                    if (sequence.Value[0] is DerAsnObjectIdentifier oid)
                    {
                        switch (oid.Value.ToOidString())
                        {
                        case QcComplianceStatement.Oid_QcCompliance:
                            _Statements.IsCompliant = new QcComplianceStatement(sequence.Value).Extract(); break;

                        case QcLimitValueStatement.Oid_QcLimitValue:
                            _Statements.LimitValue = new QcLimitValueStatement(sequence.Value).Extract(); break;

                        case QcRetentionPeriodStatement.Oid_QcRetentionPeriod:
                            _Statements.RetentionPeriod = new QcRetentionPeriodStatement(sequence.Value).Extract(); break;

                        case QcSSCDStatement.Oid_QcSSCD:
                            _Statements.IsQSCD = new QcSSCDStatement(sequence.Value).Extract(); break;

                        case QcPdsStatement.Oid_QcPds:
                            _Statements.PdsLocations = new QcPdsStatement(sequence.Value).Extract(); break;

                        case QcTypeStatement.Oid_QcType:
                            _Statements.Type = new QcTypeStatement(sequence.Value).Extract(); break;

                        case Psd2QcStatement.Oid_PSD2_QcStatement:
                            _Statements.Psd2Type = new Psd2QcStatement(sequence.Value).Extract(); break;

                        default: break;
                        }
                    }
                }
            }
            else if (root.Value[0] is DerAsnObjectIdentifier oid &&
                     Psd2QcStatement.Oid_PSD2_QcStatement.Equals(oid.Value.ToOidString()))
            {
                _Statements.Psd2Type = new Psd2QcStatement(root.Value).Extract();
            }
            _decoded = true;
        }
Exemplo n.º 8
0
        public RSAParameters ReadRsaKey()
        {
            var parts        = ReadPemParts();
            var headerFormat = ExtractFormat(parts.Header, isFooter: false);
            var footerFormat = ExtractFormat(parts.Footer, isFooter: true);

            if (!headerFormat.Equals(footerFormat))
            {
                throw new InvalidOperationException($"Header/footer format mismatch: {headerFormat}/{footerFormat}");
            }

            var derData = Convert.FromBase64String(parts.Body);
            var der     = DerConvert.Decode(derData);

            if (headerFormat.Equals(PemFormat.Public))
            {
                return(ReadPublicKey(der));
            }
            if (headerFormat.Equals(PemFormat.Rsa))
            {
                return(ReadPrivateKey(der));
            }
            throw new NotImplementedException($"The format {headerFormat} is not yet implemented");
        }
Exemplo n.º 9
0
        public void Decode_NullAsRawData_ShouldThrowArgumentNullException()
        {
            var ex = Assert.Throws <ArgumentNullException>(() => DerConvert.Decode(null));

            Assert.That(ex.ParamName, Is.EqualTo("rawData"));
        }
Exemplo n.º 10
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])
            });
        }
Exemplo n.º 11
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])
            });
        }