Android SafetyNet JWT签名验证 [英] Android SafetyNet JWT signature verification

查看:230
本文介绍了Android SafetyNet JWT签名验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解jwt签名验证的工作原理.

I'm trying to understand how the jwt signature validation works.

这是我目前的做法:

1) My app calls the attest api
2) My app sends the jwt to my server
3) My server verify the signature (third field of the jwt) using the certificate provided in the header of the jwt.

我了解到,签名是通过对jwt的标头和有效负载进行哈希处理,然后使用Google的私钥对其进行签名(加密)来创建的.

I understand that the signature is created by hashing the header and the payload of the jwt and then signing it (encrypting it) with Google's private key.

在第3步中,我要做的是获取标头+有效负载,并使用 证书的公钥,并验证它是否与签名匹配. (当我说我"时,我是说一个库就这样做了)

What I do in my step 3 is that I take the header + payload and decrypt it with certificate's public key and verify that it matches the signature. (when I say 'I' do I mean a lib does it)

我的问题是,如果用户设备上存在恶意软件并即时修改发送到我的服务器的JWT,会发生什么情况?该恶意软件会在标头中添加自己的证书(由受信任的CA颁发),根据需要修改有效负载并创建签名.

My issue is that, what happens if there is a malware on the user device and amend on the fly the JWT that is sent to my server? The malware would add his own certificate (issued by a trusted CA) in the header, modify the payload as it wishes and created the signature.

我在服务器端...好吧,我将使用证书中提供的有效的公共密钥,它将与之匹配的签名有效.

Me server side... well I'm going to take the public key provided in the cert valid the signature with it which will matches.

这是正确的吗?还是我在某个地方感到困惑?因为在那种情况下,这将使所有流程变得毫无用处,不是吗?您如何确保自己100%的JWT来自Google?

Is this correct? Or I'am confused somewhere? Because in that case it would render this all flow a bit useless no? How doe insure myself 100% that the JWT comes from google?

推荐答案

关键点是验证签名证书是由受信任的证书颁发机构颁发给attest.android.com

The key point is verify that the signing certificate is issued to attest.android.com by a trusted Certificate Authority

任何受信任的CA都会向attest.android.com颁发伪造的证书.看看他们从事不良行为会发生什么 https ://security.googleblog.com/2016/10/distrusting-wosign-and-startcom.html?m = 1

Any trusted CA will issue a fake certificate to attest.android.com. See what happens if they engage in bad practices https://security.googleblog.com/2016/10/distrusting-wosign-and-startcom.html?m=1

请参阅Google的文档

See Google's doc

验证兼容性检查响应

您应采取措施确保兼容性检查响应实际上来自SafetyNet服务,并包括与您的请求数据匹配的数据.

You should take steps to make sure that the compatibility check response actually came from the SafetyNet service and includes data that matches your request data.

警告:您应该使用安全连接将整个JWS响应发送到您自己的服务器,以进行验证.我们不建议您直接在您的应用中执行验证,因为在这种情况下,不能保证验证逻辑本身没有被修改.

请遵循以下步骤来验证JWS消息的来源:

Follow these steps to verify the origin of the JWS message:

  1. 从JWS消息中提取SSL证书链.

  1. Extract the SSL certificate chain from the JWS message.

验证SSL证书链,并使用SSL主机名匹配来验证叶证书已颁发给主机名attest.android.com.

使用证书来验证JWS消息的签名.

Use the certificate to verify the signature of the JWS message.

检查JWS消息的数据,以确保它与原始请求中的数据匹配.特别是,请确保随机数,时间戳,程序包名称和SHA-256哈希值匹配.

Check the data of the JWS message to make sure it matches the data within your original request. In particular, make sure that the nonce, timestamp, package name, and the SHA-256 hashes match.

第二个点要求验证证书链.假定使用的是包含证书颁发机构根证书的信任管理器

The second dot requires to validate the certificate chain. It is assumed that it is used a Trust Manager containing the root certificate of the Certificate Authority

我已经在 OfflineVerify 以确保TrustManager的存在,因为它没有明确表示,并且在JWS验证期间有效地使用了它.它使用默认系统TrustManager,但是您可以使用自定义系统

I have inspected Google's sample code in OfflineVerify to ensure the existence of a TrustManager because it is not explictly said, and it is effectively used during JWS verification. It uses the default system TrustManager, but you can use a custom one

请注意,使用的是JWS(Json Web签名),而不是JWT. JWT通常是使用JWS签名的身份验证令牌

Note that is used JWS (Json Web Signature), not JWT. A JWT is usually an authentication token signed with JWS

这篇关于Android SafetyNet JWT签名验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆