/// <summary> /// Estrae determinati dati del certificato dal timestamp /// </summary> /// <param name="certsColl"></param> /// <returns></returns> public DocsPaVO.documento.TSInfo getTSCertInfo(ICollection certsColl) { DocsPaVO.documento.TSInfo retval = new DocsPaVO.documento.TSInfo(); try { foreach (object obj in certsColl) { retval.dataInizioValiditaCert = (DateTime)getProps("NotBefore", obj); retval.dataFineValiditaCert = (DateTime)getProps("NotAfter", obj); string issuer = (string)getProps("IssuerDN", obj).ToString(); string subject = (string)getProps("SubjectDN", obj).ToString(); DocsPaVO.documento.SubjectInfo subjectInfo = new DocsPaVO.documento.SubjectInfo(); SignedDocument sd = new SignedDocument(); sd.ParseCNIPASubjectInfo(ref subjectInfo, issuer); retval.TSANameIssuer = subjectInfo.CommonName; sd.ParseCNIPASubjectInfo(ref subjectInfo, subject); retval.TSANameSubject = subjectInfo.CommonName; break; //solo il primo } } catch {} return(retval); }
/// <summary> /// Parse Certificate Description /// </summary> /// <param name="sd">a structure filled with results</param> /// <param name="subject">the input string</param> public void ParseCNIPASubjectInfo(ref DocsPaVO.documento.SubjectInfo subjectInfo, string subject) { Regex r = new Regex(".*?CN=(?<cognome>.*?)/(?<nome>.*?)/(?<cf>.*?)/(?<cid>.*?),.*", RegexOptions.IgnoreCase); Match match = r.Match(subject); if (match.Success) { // Formato supportato dalla normativa 2000 string commonName = match.Groups["cognome"].Value + " " + match.Groups["nome"].Value; if (commonName.Trim() != string.Empty) { subjectInfo.CommonName = commonName; } subjectInfo.CodiceFiscale = match.Groups["cf"].Value; subjectInfo.CertId = match.Groups["cid"].Value; } r = new Regex(@"Description=\""C=(?<cognome>.*?)/N=(?<nome>.*?)/D=(?<g>\d+)-(?<m>\d+)-(?<a>\d+)(/R=(?<r>.*?))?\""(.*C=(?<country>.*?)$)?", RegexOptions.IgnoreCase); match = r.Match(subject); if (match.Success) { // Formato supportato dalla normativa 2000 subjectInfo.Cognome = match.Groups["cognome"].Value; subjectInfo.Nome = match.Groups["nome"].Value; subjectInfo.DataDiNascita = new DateTime(Int32.Parse(match.Groups["a"].Value), Int32.Parse(match.Groups["m"].Value), Int32.Parse(match.Groups["g"].Value)).ToShortDateString(); subjectInfo.Ruolo = match.Groups["r"].Value; subjectInfo.Country = match.Groups["country"].Value; } else { // Formato supportato dalla normativa del 17/02/2005 Hashtable subjectItems = this.ParseCNIPASubjectItems(subject); subjectInfo.CommonName = this.GetSubjectItem(subjectItems, "CN"); subjectInfo.Cognome = this.GetSubjectItem(subjectItems, "SN"); subjectInfo.Nome = this.GetSubjectItem(subjectItems, "G"); subjectInfo.CertId = this.GetSubjectItem(subjectItems, "DNQUALIFIER"); subjectInfo.SerialNumber = this.GetSubjectItem(subjectItems, "OID.2.5.4.5"); // Se il country code è "IT", il "SerialNumber" contiene il codice fiscale del titolare del certificato if (subjectInfo.SerialNumber.StartsWith("IT:")) { subjectInfo.CodiceFiscale = subjectInfo.SerialNumber.Substring(subjectInfo.SerialNumber.IndexOf(":") + 1); } subjectInfo.Country = this.GetSubjectItem(subjectItems, "C"); if (subjectInfo.CodiceFiscale == null) { subjectInfo.CodiceFiscale = this.GetSubjectItem(subjectItems, "SERIALNUMBER"); } subjectItems.Clear(); subjectItems = null; } }