当前位置:首页 > Web开发 > 正文

JSON Web Signature 规范解析

2024-03-31 Web开发

JWS 也就是 Json Web Signature,是构造 JWT 的基础结构(JWT 其实涵盖了 JWS 和 JWE 两类,其中 JWT 的载荷还可以是嵌套的 JWT),包括三部分 JOSE Header、JWS Payload、JWS Signature。

这里的 Signature 可以有两种生成方式,一种是标准的签名,使用非对称加密,因为私钥的保密性,能够确认签名的主体,同时能保护完整性;另一种是消息认证码 MAC(Message Authentication Code),使用对称秘钥,该秘钥需要在签发、验证的多个主体间共享,因此无法确认签发的主体,只能起到保护完整性的作用。

JWS 最终有两种序列化的表现形式,一种是 JWS Compact Serialization,为一串字符;另一种是 JWS JSON Serialization,,是一个标准的 Json 对象,允许为同样的内容生成多个签名/消息认证码。

技术图片

JWS Compact Serialization,各部分以 ‘.’ 分隔。

BASE64URL(UTF8(JWS Protected Header)) || ’.’ ||
BASE64URL(JWS Payload) || ’.’ ||
BASE64URL(JWS Signature)

JWS Json Serialization 还可以分为两种子格式:通用、扁平。

通用格式,最外层为 payload、signatures。signatures 中可以包含多个 json 对象,内层的 json 对象由 protected、header、signature 组成。不同的 protected header 生成不同的 Signature。

{ "payload": "<payload contents>", "signatures": [ { "protected": "<integrity-protected header 1 contents>", "header": "<non-integrity-protected header 1 contents>", "signature": "<signature 1 contents>" }, ... { "protected": "<integrity-protected header N contents>", "header": "<non-integrity-protected header 1 contents>", "signature": "<signature N contents>" } ] }

扁平格式,就是为只有一个 signature/mac 准备的。

{ "payload": "<payload contents>", "protected": "<integrity-protected header contents>", "header": "<non-integrity-protected header contents>", "signature": "<signature contents>" }

JOSE Header:Json Object Signing and Encryption Header。描述加密行为及其他用到的参数,是 JWS Protected/Unprotected Header 的合集。

JWS Protected Header,有完整性保护的头参数。

JWS Unprotected Header,无完整性保护头参数,仅出现在 JWS Json Serialization 格式中。

以下列出了 JOSE Header 中的规定参数,各参数详细信息请参考 rfc7515

头参数全称解释必选
alg   algorithm   指定签名算法,为 none 时,表示不使用签名来保护完整性    
jku   JWK set URL   签名所用 key 对应公匙所在的 URI    
jwk   json web key   签名所用 key 对应的公匙    
kid   key id   签名所用 key 的 id    
typ   Type   指明整个 jws 的媒体类型,JOSE 意味着为compact,JOSE+JSON意味着为json    
cty   Content Type   载荷的媒体类型    
crit   Critical   此字段列出的扩展头参数必须被接收者理解并处理,否则该 jws 无效,该字段为数组格式    
x5u   X.509 URL      
x5c   X.509 Certificate Chain      
x5t   X.509 Certificate SHA-1 Thumbprint      
x5t#S256   X.509 Certificate SHA-256 Thumbprint      
# crit 参数例子 { "alg":"ES256", "crit":["exp","iss"], "exp":1363284000, "iss":"test" }

以下为 rfc7515 中提供的一个案例,使用 HMAC_SHA256 生成 JWS Signature。

1. 头部就是一个紧凑的字符串,不换行,也无空格。

Header = {“typ”:“JWT”,“alg”:“HS256”}

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/40389.html