Exemple #1
0
        /// <summary>
        /// Helper to make issuer match System.Security conventions
        /// </summary>
        /// <param name="issuerDN"></param>
        /// <returns></returns>
        private static string FixUpIssuer(string issuerDN)
        {
            // replace state ST= with S=
            issuerDN = issuerDN.Replace("ST=", "S=");
            // reverse DN order
            var issuerList = CertUtils.ParseDistinguishedName(issuerDN);

            issuerList.Reverse();
            return(string.Join(", ", issuerList));
        }
Exemple #2
0
 /// <summary>
 /// Sets the parameters to suitable defaults.
 /// </summary>
 public static X500DistinguishedName Create(string subjectName)
 {
     // parse the subject name if specified.
     if (!string.IsNullOrEmpty(subjectName))
     {
         var subjectNameEntries = CertUtils.ParseDistinguishedName(subjectName)
                                  .Select(e => e.Contains("=") ? e : "CN=" + e);
         // enforce proper formatting for the subject name string
         subjectName = string.Join(", ", subjectNameEntries);
     }
     return(new X500DistinguishedName(subjectName));
 }
Exemple #3
0
        /// <summary>
        /// Validate entity
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static EntityInfoModel Validate(this EntityInfoModel model)
        {
            var entity = model.Clone();
            // parse the subject name if specified.
            List <string> subjectNameEntries = null;

            if (!string.IsNullOrEmpty(entity.SubjectName))
            {
                subjectNameEntries = CertUtils.ParseDistinguishedName(entity.SubjectName);
                // enforce proper formatting for the subject name string
                entity.SubjectName = string.Join(", ", subjectNameEntries);
            }

            // check the application name.
            if (string.IsNullOrEmpty(entity.Name))
            {
                if (subjectNameEntries == null)
                {
                    throw new ArgumentNullException(nameof(entity.Name),
                                                    "Must specify a name or a subjectName.");
                }
                // use the common name as the application name.
                foreach (var entry in subjectNameEntries)
                {
                    if (entry.StartsWith("CN=", StringComparison.InvariantCulture))
                    {
                        entity.Name = entry.Substring(3).Trim();
                        break;
                    }
                }
            }

            if (string.IsNullOrEmpty(entity.Name))
            {
                throw new ArgumentNullException(nameof(entity.Name),
                                                "Must specify a applicationName or a subjectName.");
            }

            // remove special characters from name.
            var buffer = new StringBuilder();

            foreach (var ch in entity.Name)
            {
                if (char.IsControl(ch) || ch == '/' || ch == ',' || ch == ';')
                {
                    buffer.Append('+');
                }
                else
                {
                    buffer.Append(ch);
                }
            }
            entity.Name = buffer.ToString();

            // create the subject name,
            if (string.IsNullOrEmpty(entity.SubjectName))
            {
                entity.SubjectName = "CN=" + entity.Name;
            }

            entity.SubjectName = CertUtils.ValidateSubjectName(entity.SubjectName);

            if (entity.Type != EntityType.User)
            {
                // ensure at least one uri
                if (entity.Uris == null || entity.Uris.Count == 0)
                {
                    if (entity.Addresses.Count > 0)
                    {
                        entity.Uris = new List <string> {
                            $"urn:{entity.Addresses[0]}:{entity.Name}"
                        };
                    }
                    else
                    {
                        throw new ArgumentNullException(nameof(entity.Uris),
                                                        "Must specify valid URLs.");
                    }
                }

                // Set dc if not exists
                if (entity.Addresses != null && entity.Addresses.Count > 0)
                {
                    if (!entity.SubjectName.Contains("DC=") && !entity.SubjectName.Contains("="))
                    {
                        entity.SubjectName += ", DC=" + entity.Addresses[0];
                    }
                    else
                    {
                        entity.SubjectName = CertUtils.ReplaceDCLocalhost(
                            entity.SubjectName, entity.Addresses[0]);
                    }
                }
            }
            return(entity);
        }