Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- Added checking of SubjectAltName entries to ssl remote certificate … #239

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions src/ModernHttpClient/Utility.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,46 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Security.Cryptography.X509Certificates;
using Mono.Security.X509.Extensions;

namespace ModernHttpClient
{
internal static class Utility
{
public static bool MatchHostnameToPattern(string hostname, string pattern)
{
const string subjectAltNameOid = "2.5.29.17";

internal static List<string> GetSans(X509Certificate2 certificate) {
Mono.Security.X509.X509Certificate mono_cert;
Mono.Security.X509.X509Extension ext;
SubjectAltNameExtension san;
List<string> result;

result = new List<string>();
try {
mono_cert = new Mono.Security.X509.X509Certificate(certificate.RawData);
ext = mono_cert.Extensions[subjectAltNameOid];
if (ext == null) {
return result;
}

san = new SubjectAltNameExtension(ext);
if (san != null) {
result.AddRange(san.DNSNames);
result.AddRange(san.IPAddresses);
}

return result;
} catch (Exception ex) {
return result;
}
}

public static bool MatchHostnameToPattern(string hostname, string pattern) {
if (string.IsNullOrWhiteSpace(hostname)) {
return false;
}

// check if this is a pattern
int index = pattern.IndexOf('*');
if (index == -1) {
Expand Down
6 changes: 6 additions & 0 deletions src/ModernHttpClient/iOS/NSUrlSessionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,13 @@ public override void DidReceiveChallenge(NSUrlSession session, NSUrlSessionTask

var subject = root.Subject;
var subjectCn = cnRegex.Match(subject).Groups[1].Value;
bool match;
List<string> subjects;

match = false;
subjects = Utility.GetSans(root);
subjects.Insert(0, subjectCn);
for (int i = 0; i < subjects.Count; i++) {
if (String.IsNullOrWhiteSpace(subjectCn) || !Utility.MatchHostnameToPattern(task.CurrentRequest.Url.Host, subjectCn)) {
errors = SslPolicyErrors.RemoteCertificateNameMismatch;
goto sslErrorVerify;
Expand Down