internal static PythonDictionary CertificateToPython(CodeContext context, X509Certificate2 cert) { var dict = new CommonDictionaryStorage(); dict.AddNoLock("notAfter", ToPythonDateFormat(cert.NotAfter)); dict.AddNoLock("subject", IssuerToPython(context, cert.Subject)); dict.AddNoLock("notBefore", ToPythonDateFormat(cert.NotBefore)); dict.AddNoLock("serialNumber", SerialNumberToPython(cert)); dict.AddNoLock("version", cert.Version); dict.AddNoLock("issuer", IssuerToPython(context, cert.Issuer)); AddSubjectAltNames(dict, cert); return(new PythonDictionary(dict)); string ToPythonDateFormat(DateTime date) { var dateStr = date.ToUniversalTime().ToString("MMM dd HH:mm:ss yyyy", CultureInfo.InvariantCulture) + " GMT"; if (dateStr[4] == '0') { dateStr = dateStr.Substring(0, 4) + " " + dateStr.Substring(5); // CPython uses leading space } return(dateStr); } }
private static void AddSubjectAltNames(CommonDictionaryStorage dict, X509Certificate2 cert2) { foreach (var extension in cert2.Extensions) { if (extension.Oid.Value != "2.5.29.17") // Subject Alternative Name { continue; } var altNames = new List <object>(); var sr = new StringReader(extension.Format(true)); // The string generated by format varies depending on the platform, for example: // - On Windows, one entry per line: // DNS Name=www.python.org // DNS Name=pypi.python.org // - On Mac/Linux (.NET Core), multiple entries on a single line: // DNS:www.python.org, DNS:pypi.python.org string line; while (null != (line = sr.ReadLine())) { line = line.Trim(); // On Linux and Mac (.NET Core), Format produces a string matching the OpenSSL format which may contain multiple entries: foreach (var val in line.Split(',')) { var keyValue = val.Split(new char[] { ':', '=' }); // On Windows, Format produces different results based on the locale so we can't check for a specific key if (keyValue[0].Contains("DNS") && keyValue.Length == 2) { altNames.Add(PythonTuple.MakeTuple("DNS", keyValue[1])); } } } dict.AddNoLock("subjectAltName", PythonTuple.MakeTuple(altNames.ToArray())); break; } }
private static void AddSubjectAltNames(CommonDictionaryStorage dict, X509Certificate2 cert2) { foreach (var extension in cert2.Extensions) { if (extension.Oid.Value != "2.5.29.17") // Subject Alternative Name { continue; } var altNames = new List <object>(); var sr = new StringReader(extension.Format(true)); string line; while (null != (line = sr.ReadLine())) { line = line.Trim(); var keyValue = line.Split('='); // Format produces different results based on the locale so we can't check for a specific key if (keyValue[0].Contains("DNS") && keyValue.Length == 2) { altNames.Add(PythonTuple.MakeTuple("DNS", keyValue[1])); } } dict.AddNoLock("subjectAltName", PythonTuple.MakeTuple(altNames.ToArray())); break; } }
internal static PythonDictionary CertificateToPython(CodeContext context, X509Certificate2 cert, bool complete) { var dict = new CommonDictionaryStorage(); dict.AddNoLock("notAfter", ToPythonDateFormat(cert.NotAfter.ToString())); dict.AddNoLock("subject", IssuerToPython(context, cert.Subject)); if (complete) { dict.AddNoLock("notBefore", ToPythonDateFormat(cert.NotBefore.ToString())); dict.AddNoLock("serialNumber", SerialNumberToPython(cert)); dict.AddNoLock("version", cert.GetCertHashString()); dict.AddNoLock("issuer", IssuerToPython(context, cert.Issuer)); AddSubjectAltNames(dict, cert); } return new PythonDictionary(dict); }
private static void AddSubjectAltNames(CommonDictionaryStorage dict, X509Certificate2 cert2) { foreach (var extension in cert2.Extensions) { if (extension.Oid.Value != "2.5.29.17") { // Subject Alternative Name continue; } var altNames = new List<object>(); var sr = new StringReader(extension.Format(true)); string line; while (null != (line = sr.ReadLine())) { line = line.Trim(); var keyValue = line.Split('='); if (keyValue[0] == "DNS Name" && keyValue.Length == 2) { altNames.Add(PythonTuple.MakeTuple("DNS", keyValue[1])); } } dict.AddNoLock("subjectAltName", PythonTuple.MakeTuple(altNames.ToArray())); break; } }
private MSAst.Expression ReduceConstant() { for (int index = 0; index < _items.Length; index++) { SliceExpression slice = _items[index]; if (!slice.SliceStop.IsConstant || !slice.SliceStart.IsConstant) { return(null); } } CommonDictionaryStorage storage = new CommonDictionaryStorage(); for (int index = 0; index < _items.Length; index++) { SliceExpression slice = _items[index]; storage.AddNoLock(slice.SliceStart.GetConstantValue(), slice.SliceStop.GetConstantValue()); } return(Ast.Call(AstMethods.MakeConstantDict, Ast.Constant(new ConstantDictionaryStorage(storage), typeof(object)))); }