Ejemplo n.º 1
0
        /// <summary>
        /// Constructs the <see cref="AccessDescriptionList"/> from <see cref="Uri"/>.
        /// </summary>
        /// <param name="descriptions"></param>
        public AccessDescriptionList(AccessDescription[] descriptions) : base(new DerAsnType[0])
        {
            var list = new List <DerAsnSequence>();

            foreach (var description in descriptions)
            {
                var id = new DerAsnObjectIdentifier(DerAsnIdentifiers.Primitive.ObjectIdentifier, Oid2Array(Oid_AccessDescription + "." + (int)description.AccessMethod));
                var alternativeName   = new ContextSpecificString(description.AccessLocation);
                var accessDescription = new DerAsnSequence(new DerAsnType[] { id, alternativeName });
                list.Add(accessDescription);
            }
            Value = list.ToArray();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Constructs the <see cref="CertificatePolicies"/> from <see cref="List{PolicyInformation}"/>.
        /// </summary>
        /// <param name="policies"></param>
        public CertificatePolicies(PolicyInformation[] policies) : base(new DerAsnType[0])
        {
            var list = new List <DerAsnSequence>();

            foreach (var policy in policies)
            {
                var definition = new List <DerAsnType>();
                if (policy.PolicyIdentifier != null)
                {
                    var id = new DerAsnObjectIdentifier(DerAsnIdentifiers.Primitive.ObjectIdentifier, policy.PolicyIdentifier.OidToArray());
                    definition.Add(id);
                }
                if (policy.PolicyQualifiers?.Count > 0)
                {
                    var definitionQualifiers = new List <DerAsnType>();
                    foreach (var qualifier in policy.PolicyQualifiers)
                    {
                        var qualifierId    = new DerAsnObjectIdentifier(DerAsnIdentifiers.Primitive.ObjectIdentifier, qualifier.Identifier.OidToArray());
                        var qualifierValue = default(DerAsnType);
                        if (qualifier.Type == PolicyQualifierType.UserNotice)
                        {
                            var noticeSequence = new List <DerAsnType>();
                            if (qualifier.UserNotice?.Reference != null)
                            {
                                noticeSequence.Add(new DerAsnSequence(new DerAsnType[] {
                                    new DerAsnUtf8String(qualifier.UserNotice.Reference.Organization ?? string.Empty),
                                    new DerAsnSequence(qualifier.UserNotice.Reference.NoticeNumbers.Select(c => new DerAsnInteger(new BigInteger(c))).ToArray())
                                }));
                            }
                            if (qualifier.UserNotice?.ExplicitText != null)
                            {
                                noticeSequence.Add(new DerAsnUtf8String(qualifier.UserNotice.ExplicitText));
                            }
                            qualifierValue = new DerAsnSequence(noticeSequence.ToArray());
                        }
                        else
                        {
                            qualifierValue = new DerAsnIa5String(qualifier.CPS_Uri ?? string.Empty);
                        }
                        definitionQualifiers.Add(new DerAsnSequence(new DerAsnType[] {
                            qualifierId,
                            qualifierValue
                        }));
                    }
                    definition.Add(new DerAsnSequence(definitionQualifiers.ToArray()));
                }
                list.Add(new DerAsnSequence(definition.ToArray()));
            }
            Value = list.ToArray();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Used to create the extension from typed model
        /// </summary>
        /// <param name="organizationIdentifier"></param>
        /// <param name="critical"></param>
        public CABForumOrganizationIdentifierExtension(CABForumOrganizationIdentifier organizationIdentifier, bool critical)
        {
            Oid      = new Oid(Oid_CabForumOrganizationIdentifier, "CRL Distribution Points");
            Critical = critical;
            var container = new DerAsnSequence(new DerAsnType[] {
                new DerAsnPrintableString(organizationIdentifier.SchemeIdentifier),
                new DerAsnPrintableString(organizationIdentifier.Country),
                new DerAsnUtf8String(organizationIdentifier.Reference),
            });

            RawData = DerConvert.Encode(container).ToArray();
            _OrganizationIdentifier = organizationIdentifier;
            _decoded = true;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates the CRL envelope and includes the RSA signature with it.
        /// </summary>
        /// <param name="signingKey"></param>
        /// <param name="encoder"></param>
        /// <returns></returns>
        public byte[] SignAndSerialize(RSA signingKey, IDerAsnEncoder encoder = null)
        {
            var data      = DerConverter.DerConvert.Encode(this);
            var signature = signingKey.SignData(data, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
            var container = new DerAsnSequence(new DerAsnType[] {
                this,
                new DerAsnSequence(new DerAsnType[] {
                    new DerAsnObjectIdentifier(DerAsnIdentifiers.Primitive.ObjectIdentifier, Oid_sha256RSA.OidToArray()),
                    new DerAsnNull()
                }),
                new DerAsnBitString(new BitArray(signature))
            });

            return(DerConverter.DerConvert.Encode(container));
        }
Ejemplo n.º 5
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.º 6
0
        public void Encode_ShouldEncodeCorrectly()
        {
            var encoderMock = new Mock <IDerAsnEncoder>();
            var sequence    = new DerAsnSequence(new DerAsnType[]
            {
                new DerAsnNull(),
                new DerAsnObjectIdentifier(1, 2, 840, 113549, 1, 1, 1),
                new DerAsnNull()
            });

            sequence.Encode(encoderMock.Object);

            encoderMock.Verify(x => x.Encode(It.IsAny <DerAsnType>()), Times.Exactly(3));
            encoderMock.Verify(x => x.Encode(It.IsAny <DerAsnNull>()), Times.Exactly(2));
            encoderMock.Verify(x => x.Encode(It.IsAny <DerAsnObjectIdentifier>()), Times.Once);
        }
Ejemplo n.º 7
0
        public void WritePrivateKey(RSAParameters parameters)
        {
            var sequence = new DerAsnSequence(new DerAsnType[]
            {
                new DerAsnInteger(ToBigInteger(new byte[] { 0x00 })),   // Version
                new DerAsnInteger(ToBigInteger(parameters.Modulus)),
                new DerAsnInteger(ToBigInteger(parameters.Exponent)),
                new DerAsnInteger(ToBigInteger(parameters.D)),
                new DerAsnInteger(ToBigInteger(parameters.P)),
                new DerAsnInteger(ToBigInteger(parameters.Q)),
                new DerAsnInteger(ToBigInteger(parameters.DP)),
                new DerAsnInteger(ToBigInteger(parameters.DQ)),
                new DerAsnInteger(ToBigInteger(parameters.InverseQ))
            });

            Write(sequence, PemFormat.Rsa);
        }
Ejemplo n.º 8
0
        public void WritePrivateKey(RSAParameters parameters)
        {
            var sequence = new DerAsnSequence(new DerAsnType[]
            {
                new DerAsnInteger(new byte[] { 0x00 }, true),   // Version
                new DerAsnInteger(parameters.Modulus, true),
                new DerAsnInteger(parameters.Exponent, true),
                new DerAsnInteger(parameters.D, true),
                new DerAsnInteger(parameters.P, true),
                new DerAsnInteger(parameters.Q, true),
                new DerAsnInteger(parameters.DP, true),
                new DerAsnInteger(parameters.DQ, true),
                new DerAsnInteger(parameters.InverseQ, true)
            });

            Write(sequence, PemFormat.Rsa);
        }
Ejemplo n.º 9
0
        public void DerAsnSequence_GetBytes_ShouldEncodeCorrectly()
        {
            var type = new DerAsnSequence(new DerAsnType[]
            {
                new DerAsnNull(),
                new DerAsnObjectIdentifier("1.2.840.113549.1.1.1"),
                new DerAsnNull()
            });

            var data = type.GetBytes();

            Assert.That((DerAsnTypeTag)data[0], Is.EqualTo(DerAsnTypeTag.Sequence));
            Assert.That(data[1], Is.EqualTo(0x0F));
            Assert.That(data.Skip(2).ToArray(), Is.EqualTo(new byte[]
            {
                0x05, 0x00,
                0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01,
                0x05, 0x00
            }));
        }
Ejemplo n.º 10
0
        public void WritePublicKey(RSAParameters parameters)
        {
            var innerSequence = new DerAsnSequence(new DerAsnType[]
            {
                new DerAsnInteger(parameters.Modulus, true),
                new DerAsnInteger(parameters.Exponent, true)
            });

            var outerSequence = new DerAsnSequence(new DerAsnType[]
            {
                new DerAsnSequence(new DerAsnType[]
                {
                    new DerAsnObjectIdentifier("1.2.840.113549.1.1.1"), // rsaEncryption http://www.oid-info.com/get/1.2.840.113549.1.1.1
                    new DerAsnNull()
                }),
                new DerAsnBitString(DerConvert.Encode(innerSequence))
            });

            Write(outerSequence, PemFormat.Public);
        }
Ejemplo n.º 11
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.º 12
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.º 13
0
        public void WritePublicKey(RSAParameters parameters)
        {
            var innerSequence = new DerAsnSequence(new DerAsnType[]
            {
                new DerAsnInteger(ToBigInteger(parameters.Modulus)),
                new DerAsnInteger(ToBigInteger(parameters.Exponent))
            });

            var innerSequenceData = DerConvert.Encode(innerSequence);

            var outerSequence = new DerAsnSequence(new DerAsnType[]
            {
                new DerAsnSequence(new DerAsnType[]
                {
                    new DerAsnObjectIdentifier(1, 2, 840, 113549, 1, 1, 1), // rsaEncryption http://www.oid-info.com/get/1.2.840.113549.1.1.1
                    new DerAsnNull()
                }),
                new DerAsnBitString(ToBitArray(innerSequenceData))
            });

            Write(outerSequence, PemFormat.Public);
        }
Ejemplo n.º 14
0
        public void ValueConstructor_ShouldSetIdentifier()
        {
            var sequence = new DerAsnSequence(Array.Empty <DerAsnType>());

            Assert.That(sequence.Identifier, Is.EqualTo(DerAsnIdentifiers.Constructed.Sequence));
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Constructs the <see cref="CertificateRevocationListSequence"/> from <see cref="RevokedCertificate"/>.
        /// </summary>
        /// <param name="crl">The data use in order to load the sequence</param>
        public CertificateRevocationListSequence(CertificateRevocationList crl) : base(new DerAsnType[0])
        {
            //var container = new List<DerAsnType>();
            var details = new List <DerAsnType>();
            var list    = new List <DerAsnSequence>();

            foreach (var cert in crl.Items)
            {
                var definition     = new List <DerAsnType>();
                var serialNumber   = new DerAsnInteger(BigInteger.Parse(cert.SerialNumber.ToUpper(), NumberStyles.AllowHexSpecifier));
                var revocationDate = new DerAsnUtcTime(cert.RevocationDate);
                var reason         = new DerAsnSequence(new DerAsnType[] {
                    new DerAsnSequence(new DerAsnType [] {
                        new DerAsnObjectIdentifier(DerAsnIdentifiers.Primitive.ObjectIdentifier, Oid_CRL_Reason.OidToArray()),
                        new OctetStringSequence(new [] { new DerAsnEnumerated((byte)cert.ReasonCode) })
                    })
                });
                definition.Add(serialNumber);
                definition.Add(revocationDate);
                definition.Add(reason);
                list.Add(new DerAsnSequence(definition.ToArray()));
            }
            details.Add(new DerAsnInteger(new BigInteger(1)));
            details.Add(new DerAsnSequence(new DerAsnType[] {
                new DerAsnObjectIdentifier(DerAsnIdentifiers.Primitive.ObjectIdentifier, Oid_sha256RSA.OidToArray()),
                new DerAsnNull()
            }));
            details.Add(new DerAsnSequence(new DerAsnType[] {
                new DerAsnSet(new DerAsnType[] {
                    new DerAsnSequence(new DerAsnType[] {
                        new DerAsnObjectIdentifier(DerAsnIdentifiers.Primitive.ObjectIdentifier, Oid_Issuer_C.OidToArray()),
                        new DerAsnPrintableString(crl.Country)
                    })
                }),
                new DerAsnSet(new DerAsnType[] {
                    new DerAsnSequence(new DerAsnType[] {
                        new DerAsnObjectIdentifier(DerAsnIdentifiers.Primitive.ObjectIdentifier, Oid_Issuer_O.OidToArray()),
                        new DerAsnPrintableString(crl.Organization)
                    })
                }),
                new DerAsnSet(new DerAsnType[] {
                    new DerAsnSequence(new DerAsnType[] {
                        new DerAsnObjectIdentifier(DerAsnIdentifiers.Primitive.ObjectIdentifier, Oid_Issuer_CN.OidToArray()),
                        new DerAsnPrintableString(crl.IssuerCommonName)
                    })
                })
            }));
            details.Add(new DerAsnUtcTime(crl.EffectiveDate));
            details.Add(new DerAsnUtcTime(crl.NextUpdate));
            details.Add(new DerAsnSequence(list.ToArray()));
            details.Add(new ContextSpecificSequence(new DerAsnType[] {
                new DerAsnSequence(new DerAsnType[] {
                    new DerAsnSequence(new DerAsnType[] {
                        new DerAsnObjectIdentifier(DerAsnIdentifiers.Primitive.ObjectIdentifier, Oid_AuthorityKey.OidToArray()),
                        new OctetStringSequence(new DerAsnType[] {
                            new DerAsnSequence(new DerAsnType[] {
                                new DerAsnOctetString(new DerAsnIdentifier(DerAsnTagClass.ContextSpecific, DerAsnEncodingType.Primitive, 0x0), crl.AuthorizationKeyId.HexToBytes())
                            })
                        })
                    })
                }),
                new DerAsnSequence(new DerAsnType[] {
                    new DerAsnSequence(new DerAsnType[] {
                        new DerAsnObjectIdentifier(DerAsnIdentifiers.Primitive.ObjectIdentifier, Oid_CRLNumber.OidToArray()),
                        new OctetStringSequence(new DerAsnType[] {
                            new DerAsnInteger(new BigInteger(crl.CrlNumber))
                        })
                    })
                })
            }));
            Value = details.ToArray();
            //container.Add(new DerAsnSequence(details.ToArray()));
            //Value = container.ToArray();
        }