Esempio n. 1
0
        /// <summary>
        /// Parse certificate for alternate name extension.
        /// </summary>
        private void Parse(byte[] data)
        {
            if (Oid.Value != Oids.SubjectAltName &&
                Oid.Value != Oids.SubjectAltName2)
            {
                throw new FormatException("Extension has unknown oid.");
            }

            Uris.Clear();
            DomainNames.Clear();
            IPAddresses.Clear();

            var altNames        = new DerOctetString(data);
            var altNamesObjects = X509ExtensionUtilities.FromExtensionValue(altNames);
            var generalNames    =
                Org.BouncyCastle.Asn1.X509.GeneralNames.GetInstance(altNamesObjects);

            foreach (var generalName in generalNames.GetNames())
            {
                switch (generalName.TagNo)
                {
                case Org.BouncyCastle.Asn1.X509.GeneralName.UniformResourceIdentifier:
                    Uris.Add(generalName.Name.ToString());
                    break;

                case Org.BouncyCastle.Asn1.X509.GeneralName.DnsName:
                    DomainNames.Add(generalName.Name.ToString());
                    break;

                case Org.BouncyCastle.Asn1.X509.GeneralName.IPAddress:
                    try {
                        var addr = Asn1OctetString
                                   .GetInstance(generalName.Name)
                                   .GetOctets();
                        IPAddresses.Add(new IPAddress(addr).ToString());
                    }
                    catch {
                        throw new FormatException(
                                  "Certificate contains invalid IP address.");
                    }
                    break;

                default:
                    break;
                }
            }
        }