-
Notifications
You must be signed in to change notification settings - Fork 5
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
Implement verifier authentication with x509_san_dns ClientIdScheme #44
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/Hyperledger.Aries/Features/OpenID4VC/Vp/Extensions/JwtSecurityTokenExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using Org.BouncyCastle.Asn1; | ||
using Org.BouncyCastle.Crypto.Parameters; | ||
using Org.BouncyCastle.Math; | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Linq; | ||
using System; | ||
using static Microsoft.IdentityModel.Tokens.Base64UrlEncoder; | ||
using static Org.BouncyCastle.Security.SignerUtilities; | ||
using static System.Text.Encoding; | ||
|
||
namespace Hyperledger.Aries.Features.OpenID4VC.Vp.Extensions | ||
{ | ||
/// <summary> | ||
/// Extension methods for <see cref="JwtSecurityToken" />. | ||
/// </summary> | ||
public static class JwtSecurityTokenExtensions | ||
{ | ||
/// <summary> | ||
/// Validates the signature of the JWT token using the provided public key. | ||
/// </summary> | ||
/// <param name="token">The JWT token to validate.</param> | ||
/// <param name="publicKey">The public key to use for validation.</param> | ||
/// <returns>True if the signature is valid, otherwise false.</returns> | ||
public static bool IsSignatureValid(this JwtSecurityToken token, ECPublicKeyParameters publicKey) | ||
{ | ||
var signer = GetSigner("SHA-256withECDSA"); | ||
|
||
signer.Init(false, publicKey); | ||
|
||
var encodedHeaderAndPayload = UTF8.GetBytes(token.EncodedHeader + "." + token.EncodedPayload); | ||
|
||
signer.BlockUpdate(encodedHeaderAndPayload, 0, encodedHeaderAndPayload.Length); | ||
|
||
return signer.VerifySignature( | ||
ConvertRawToDerFormat( | ||
DecodeBytes(token.RawSignature) | ||
) | ||
); | ||
} | ||
|
||
private static byte[] ConvertRawToDerFormat(byte[] rawSignature) | ||
{ | ||
if (rawSignature.Length != 64) | ||
throw new ArgumentException("Raw signature should be 64 bytes long", nameof(rawSignature)); | ||
|
||
var r = new BigInteger(1, rawSignature.Take(32).ToArray()); | ||
var s = new BigInteger(1, rawSignature.Skip(32).ToArray()); | ||
|
||
var derSignature = new DerSequence( | ||
new DerInteger(r), | ||
new DerInteger(s) | ||
).GetDerEncoded(); | ||
|
||
return derSignature; | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/Hyperledger.Aries/Features/OpenID4VC/Vp/Extensions/X509CertificateExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Org.BouncyCastle.Asn1.X509; | ||
using Org.BouncyCastle.Pkix; | ||
using Org.BouncyCastle.Utilities.Collections; | ||
using Org.BouncyCastle.X509; | ||
using Org.BouncyCastle.X509.Store; | ||
|
||
namespace Hyperledger.Aries.Features.OpenID4VC.Vp.Extensions | ||
{ | ||
/// <summary> | ||
/// Extension methods for <see cref="X509Certificate" />. | ||
/// </summary> | ||
public static class X509CertificateExtensions | ||
{ | ||
/// <summary> | ||
/// Validates the trust chain of the certificate. | ||
/// </summary> | ||
/// <param name="trustChain">The trust chain to validate.</param> | ||
/// <returns>True if the trust chain is valid, otherwise false.</returns> | ||
public static bool IsTrustChainValid(this List<X509Certificate> trustChain) | ||
{ | ||
var leafCert = trustChain.First(); | ||
|
||
var rootCerts = | ||
new HashSet( | ||
trustChain | ||
.Skip(1) | ||
.Where(cert => cert.IssuerDN.Equivalent(cert.SubjectDN)) | ||
.Select(cert => new TrustAnchor(cert, null)) | ||
.ToList() | ||
); | ||
|
||
var intermediateCerts = | ||
new HashSet( | ||
trustChain | ||
.Skip(1) | ||
.Where(cert => !cert.IssuerDN.Equivalent(cert.SubjectDN)) | ||
.Append(leafCert) | ||
); | ||
|
||
var builderParams = | ||
new PkixBuilderParameters( | ||
rootCerts, | ||
new X509CertStoreSelector | ||
{ | ||
Certificate = leafCert | ||
} | ||
) | ||
{ | ||
IsRevocationEnabled = | ||
leafCert.GetExtensionValue(X509Extensions.CrlDistributionPoints) is not null | ||
}; | ||
|
||
builderParams.AddStore( | ||
X509StoreFactory.Create( | ||
"Certificate/Collection", | ||
new X509CollectionStoreParameters(intermediateCerts) | ||
) | ||
); | ||
|
||
// This throws if validation fails | ||
new PkixCertPathValidator() | ||
.Validate( | ||
new PkixCertPathBuilder() | ||
.Build(builderParams) | ||
.CertPath, | ||
builderParams | ||
); | ||
|
||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I understand, all those Properties make up the RequestObject. The question is only how it is delivered through the AuthorizationRequest (directly in uri or indirectly via request_uri)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont really understand the question. Besides that, the HAIP defines that only request_uri must be supported https://openid.net/specs/openid4vc-high-assurance-interoperability-profile-sd-jwt-vc-1_0.html#section-5-1.5