如何在旧框架中验证 JWS?

发布于 2025-01-17 02:34:19 字数 931 浏览 4 评论 0原文

我们有一个较旧的 CMS (Kentico 11),它基于 ASP.NET(不是 Core、不是 Blazor、不是 MVC)。由于各种原因,我们不能很快更换它。作为逐步替换我们的 Web 产品的过程的一部分,我们希望构建一个针对我们的 Azure AD B2C 租户进行身份验证的组件,以便旧的 CMS 内容和我们推出的新组件可以使用相同的身份验证机构。

显然,这个较旧的代码库不包含对更新的 Microsoft Identity 交互的任何支持。我们无法使用任何 Microsoft.Identity 库,因为整个项目是为 .NET Core 构建的。我们可以成功地将身份验证移交给 B2C 工作流程并取回 Open ID 令牌,并且我们可以读取令牌中的声明。我们还成功地读取了公钥指数和模数,以验证 b2clogin.com 站点的令牌签名。我们不知道如何做的是实际验证签名。我们发现的几乎每一篇文章都说,在这个过程中,“使用您最喜欢的库验证签名”。

我们不知道如何找到一个可以在这个级别上帮助我们的库。如上所述,用于此目的的所有 Microsoft 库都需要 .NET Core 或更新版本的 .NET Framework。我们确实遇到了 Bouncy Castle 项目 (https://www.bouncycastle.org/csharp/index .html),但其 API 的 C# 版本显然没有文档,因此我们不确定如何利用它。

所以,基本上:我们有 B2C 公钥,并且有令牌签名,如果我理解我所读到的内容,它是令牌其余部分的私钥加密摘要。我们实际上用这些做什么?是解密签名并匹配摘要的问题,还是还有更多内容?或者更好的是,是否有一个与 Framework 4.6.1 兼容的库知道如何执行此操作,并且具有对 JWT 和加密新手有意义的文档?

We have an older CMS (Kentico 11), which is ASP.NET-based (not Core, not Blazor, not MVC). For various reasons we can't just replace it any time soon. As part of the process of replacing our web offerings gradually we would like to build in a piece that will authenticate against our Azure AD B2C tenant so the old CMS content and the new pieces we roll out can be using the same authentication authority.

Obviously this older code base does not include any support for more recent Microsoft Identity interactions. We are unable to use any of the Microsoft.Identity libraries since that whole project is built for .NET Core. We can successfully hand off the authentication to the B2C workflow and get back an Open ID token, and we can read the claims in the token. We have also succeeded in reading the public key exponent and modulus for validating the token signature from the b2clogin.com site. What we don't know how to do is actually validate the signature. Pretty much every article we've found says, at this point in the process, "validate the signature using your favorite library".

We don't know how to find a library that might help us at this level. All of the Microsoft libraries for this purpose, as noted above, require .NET Core or a newer version of .NET Framework. We did happen across the Bouncy Castle project (https://www.bouncycastle.org/csharp/index.html), but the C# version of their API has apparently no documentation so we're not sure how we might take advantage of it.

So, basically: We have the B2C public key, and we have the token signature which, if I understand what I've read, is a private-key encrypted digest of the rest of the token. What do we actually do with these? Is it a matter of decrypting the signature and matching the digest, or is there more to it? Or better, is there a Framework 4.6.1-compatible library that knows how to do this and has documentation that will make sense to JWT and cryptographic novices?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巾帼英雄 2025-01-24 02:34:19

一旦您掌握了正确的信息,事情就会变得非常简单。将签名、令牌的其余部分减去签名以及公钥模数和指数转换为字节数组后,这就是全部内容:

var rsa = new RSACryptoServiceProvider();
rsa.ImportParameters(new RSAParameters { Exponent = exponent, Modulus = modulus });

bool isSignatureVerified = rsa.VerifyData(tokenMinusSignature, CryptoConfig.MapNameToOID(hashAlgorithmFromTokenHeader), signature);

如上所述,我们对如何验证的理解几乎是正确的。重要的缺失部分是加密和签名之间的区别,在这里以令人钦佩的清晰解释:https://www.cs.cornell.edu/courses/cs5430/2015sp/notes/rsa_sign_vs_dec.php

This turns out to be very simple, once you have the right information. Having translated the signature, the rest of the token minus the signature, and the public key modulus and exponent, into byte arrays, this is all there is to it:

var rsa = new RSACryptoServiceProvider();
rsa.ImportParameters(new RSAParameters { Exponent = exponent, Modulus = modulus });

bool isSignatureVerified = rsa.VerifyData(tokenMinusSignature, CryptoConfig.MapNameToOID(hashAlgorithmFromTokenHeader), signature);

Our understanding, described above, of how to verify was nearly correct. The important missing piece was the difference between encryption and signing, explained with admirable clarity here: https://www.cs.cornell.edu/courses/cs5430/2015sp/notes/rsa_sign_vs_dec.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文