LaDeak.HttpMessageSigner.Core
0.3.1
dotnet add package LaDeak.HttpMessageSigner.Core --version 0.3.1
NuGet\Install-Package LaDeak.HttpMessageSigner.Core -Version 0.3.1
<PackageReference Include="LaDeak.HttpMessageSigner.Core" Version="0.3.1" />
<PackageVersion Include="LaDeak.HttpMessageSigner.Core" Version="0.3.1" />
<PackageReference Include="LaDeak.HttpMessageSigner.Core" />
paket add LaDeak.HttpMessageSigner.Core --version 0.3.1
#r "nuget: LaDeak.HttpMessageSigner.Core, 0.3.1"
#:package LaDeak.HttpMessageSigner.Core@0.3.1
#addin nuget:?package=LaDeak.HttpMessageSigner.Core&version=0.3.1
#tool nuget:?package=LaDeak.HttpMessageSigner.Core&version=0.3.1
HttpMessageSigner
ASP.NET Core middleware for signing HTTP responses with HTTP Message Signatures (RFC 9421). It can also calculate a response Content-Digest (RFC 9530) and include it in the signature.
This is useful in the following scenarios:
- A message is saved for temporal reference. The source and integrity of the message can be validated across databases reads and writes that are separate events in time. For example, signatures in a legal archive preserves the integrity of the message.
- Protection against intermediaries in transport security. It provides end-to-end integrity. For example, protection against early TLS termination or compromised intermediaries (API gateways).
- Signed business assertions.
- Response binding to a request.
Getting started
Install
Add the NuGet package to your ASP.NET Core project:
dotnet add package LaDeak.HttpMessageSigner.AspNetCore
The package targets .NET and uses the ASP.NET Core framework.
Configure ASP.NET Core
The middleware needs a signing certificate with a private key. Configure its path and password using your normal configuration provider (for example, environment variables Signing__CertificatePath and Signing__CertificatePassword). Keep production private keys and passwords in a secure store; do not commit them to source control.
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using HttpMessageSigner.AspNetCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSignatureGenerator(x =>
{
x.Algorithm = "ecdsa-p256-sha256";
x.Components = ["@method", "@path", "@authority", "@scheme", "@status", "Content-Digest"];
x.IncludeCreated = true;
x.ContentDigestHashAlgorithm = System.Security.Cryptography.HashAlgorithmName.SHA256;
x.KeyId = "ecdsa256";
x.SigningCertificate = X509CertificateLoader.LoadPkcs12FromFile("<path to certificate>", "<cert password>", X509KeyStorageFlags.EphemeralKeySet | X509KeyStorageFlags.Exportable);
});
var app = builder.Build();
app.UseHttpsRedirection();
app.UseSignatureGeneration();
app.MapGet("/hello", () => new { Message = "Hello, signed world!" });
app.Run();
The middleware hashes the response body to produce Content-Digest and signs it together with the configured request and response components. Where the server supports response trailers, the digest and signature are written as trailers, allowing the response body to stream without buffering (for example, over HTTP/2). For HTTP/1.1 responses, the middleware buffers the response so it can calculate the digest and add the signature before sending the response.
Validate signatures
Register the signature validator and add its middleware to the ASP.NET Core pipeline. Configure SignatureName for the signature label used in Signature and Signature-Input, and add each public certificate using the keyid value declared by the signer.
using System.Security.Cryptography.X509Certificates;
using HttpMessageSigner.AspNetCore;
builder.Services.AddSignatureValidator(x =>
{
x.SignatureName = "s0";
x.Certificates.Add("ecdsa256", X509CertificateLoader.LoadPkcs12FromFile(
"<path to certificate>",
"<cert password>",
X509KeyStorageFlags.EphemeralKeySet));
});
var app = builder.Build();
app.UseHttpsRedirection();
app.UseSignatureValidation();
app.MapPost("/submit", async (HttpContext context) =>
{
// Consume the request body before validating when the signature includes
// Content-Digest, so the digest can be calculated over the complete body.
await context.Request.BodyReader.CopyToAsync(Stream.Null);
var validator = context.Features.GetRequiredFeature<ISignatureValidatiorFeature>();
if (!validator.ValidateSignature())
return Results.Unauthorized();
return Results.Ok();
});
app.Run();
For endpoint filters, add SignatureValidatingFilter to an endpoint instead:
app.MapPost("/submit", (MyRequest value) => Results.Ok())
.AddEndpointFilter<SignatureValidatingFilter>();
ValidateSignature() validates the signature parameters and cryptographic value, as well as Content-Digest when a digest is included. Unsupported or invalid signatures return false; application code is responsible for returning an appropriate response, such as 401 Unauthorized.
Unsupported component names
The library does not support the RFC 9421 derived components @query-param and @request-target. Configuring either component, including a variant such as @query-param;name="foo", throws NotSupportedException.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net11.0 is compatible. |
-
net11.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on LaDeak.HttpMessageSigner.Core:
| Package | Downloads |
|---|---|
|
LaDeak.HttpMessageSigner.AspNetCore
Signs ASP.NET Core HTTP response according to RFC 9421. |
GitHub repositories
This package is not used by any popular GitHub repositories.