-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Kevin <kevin.dinh@lissi.id>
- Loading branch information
Showing
13 changed files
with
715 additions
and
435 deletions.
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.