// // OpenVPN Administrator // // Author(s): Everaldo Canuto // // (C) 2006 Everaldo Canuto // (C) 2006 The Gang // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // using System; using System.IO; using System.Collections; using System.Collections.Specialized; using System.Text.RegularExpressions; namespace OpenVPN.Admin { public class CertificationInfo { #region Fields private bool empty; private string name; private string mail; private string city; private string state; private string country; private string organization; private string organizationUnit; private string expiration; private string serial; #endregion #region Constructors and destructors public CertificationInfo(string filename) { string line; string sub = ""; string exp = ""; string ser = ""; this.empty = true; this.name = ""; this.mail = ""; this.city = ""; this.state = ""; this.country = ""; this.organization = ""; this.organizationUnit = ""; this.expiration = ""; this.serial = ""; if (File.Exists(filename)) { StreamReader SR = File.OpenText(filename); try { line = SR.ReadLine(); while (line != null) { line = line.Trim(); if (line.StartsWith("Subject:")) { sub = line; } else if (line.StartsWith("Not After :")) { exp = line; } else if (line.StartsWith("serial:")) { ser = line; } line = SR.ReadLine(); } } finally { SR.Close(); } if (sub != "") { try { this.empty = false; this.name = Regex.Match(sub, @"CN=(.*)\/").Groups[1].Value; this.mail = GetParamByName(sub, "emailAddress"); this.city = GetParamByName(sub, "L"); this.state = GetParamByName(sub, "ST"); this.country = GetParamByName(sub, "C"); this.organization = GetParamByName(sub, "O"); this.organizationUnit = GetParamByName(sub, "OU"); } catch {} } if (exp != "") { this.expiration = Regex.Match(exp, @"Not After :(.*)").Groups[1].Value.Trim(); } if (ser != "") { this.serial = Regex.Match(ser, @"serial:(.*)").Groups[1].Value.Trim(); } } } // To fix problem inregular expression, must be change it. // Need to chech how get char only unitl found "," or " " or end of line. private string GetParamByName(string text, string search) { // Subject: C=XX, ST=XX, O=Secure-Server-OpenVPN, OU=Consulting, // CN=John Doe/emailAddress=john@doe.com string result = Regex.Match(text, search+@"=(.*)").Groups[1].Value; int index = result.IndexOf(","); result = (index > 0) ? result.Substring(0, index) : result; return result; } #endregion #region Properties public bool Empty { get { return this.empty; } } public string Name { get { return this.name; } } public string Mail { get { return this.mail; } } public string City { get { return this.city; } } public string State { get { return this.state; } } public string Country { get { return this.country; } } public string Organization { get { return this.organization; } } public string OrganizationUnit { get { return this.organizationUnit; } } public string Expiration { get { return this.expiration; } } public string Serial { get { return this.serial; } } #endregion } }