Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
Fix cnf format for MTLS (#4290)
Browse files Browse the repository at this point in the history
* fix cnf generation

* update cnf validation middleware
  • Loading branch information
leastprivilege committed Apr 16, 2020
1 parent efac03b commit 6b0bf52
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Expand Up @@ -2,7 +2,9 @@
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json.Linq;
using System;
using System.Buffers.Text;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.Certificate;
using Microsoft.AspNetCore.Authentication.JwtBearer;
Expand Down Expand Up @@ -50,13 +52,14 @@ public async Task Invoke(HttpContext ctx)
return;
}

var thumbprint = certResult.Principal.FindFirst(ClaimTypes.Thumbprint).Value;
var certificate = await ctx.Connection.GetClientCertificateAsync();
var thumbprint = Base64UrlTextEncoder.Encode(certificate.GetCertHash(HashAlgorithmName.SHA256));

var cnf = JObject.Parse(cnfJson);
var sha256 = cnf.Value<string>("x5t#S256");

if (String.IsNullOrWhiteSpace(sha256) ||
!thumbprint.Equals(sha256, StringComparison.OrdinalIgnoreCase))
!thumbprint.Equals(sha256, StringComparison.Ordinal))
{
await ctx.ChallengeAsync(_options.JwtBearerSchemeName);
return;
Expand Down
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text.Json;
using IdentityModel;

namespace IdentityServer4.Extensions
{
Expand All @@ -16,9 +18,11 @@ public static class X509CertificateExtensions
/// <returns></returns>
public static string CreateThumbprintCnf(this X509Certificate2 certificate)
{
var hash = certificate.GetCertHash(HashAlgorithmName.SHA256);

var values = new Dictionary<string, string>
{
{ "x5t#S256", certificate.Thumbprint.ToLowerInvariant() }
{ "x5t#S256", Base64Url.Encode(hash) }
};

return JsonSerializer.Serialize(values);
Expand Down

0 comments on commit 6b0bf52

Please sign in to comment.