Documentation
¶
Overview ¶
Package wop 是 WOP 网关商户侧官方 Go SDK:封装协议核心(套件解析、 canonicalRequest、结构化签名、content-digest、L2 数字信封、验签解密) 与 HTTP 适配层,商户无需理解线上字节格式即可安全对接网关。
协议真源:gtsp-wop-gateway/docs/crypto-strategy-spec.md(v0.3-reviewed) 与 docs/wop-sdk-spec.md(v1.0-ratified)。全部二进制线上编码为 base64url 无填充(拒收 '='),十六进制统一小写。
对外错误模糊化纪律(I7):验签与解密失败的错误信息不区分原因细节 (GCM tag 失败、密钥不符等),详细原因仅内部日志级别可见;配置类与 协议格式类错误语义明确,便于商户集成自查。
Index ¶
- Constants
- func CanonicalHeaders(headers map[string]string) string
- func CanonicalRequest(...) string
- func DecodeB64URL(s string) ([]byte, error)
- func DigestHeaderValue(s Suite, data []byte) string
- func EncodeB64URL(b []byte) string
- func LowerHex(b []byte) string
- func ParseContentDigest(value string) (tag, hexSum string, err error)
- func ParseSignHeader(header string) (signHeader, error)
- func TrimAll(s string) string
- func URLEncodeJava(s string) string
- func ValidateContentDigest(s Suite, headerValue string, wireBody []byte) error
- func ValidateContentDigestHeader(s Suite, headerValue string) error
- type Client
- func (c *Client) BuildRequest(method, path string, body []byte, level Level, opts ...RequestOption) (RequestDraft, error)
- func (c *Client) Do(method, path string, body []byte, level Level, opts ...RequestOption) (VerifyResult, TransportResponse, error)
- func (c *Client) Suite() Suite
- func (c *Client) VerifyCallback(callbackURL string, header http.Header, wireBody []byte) VerifyResult
- func (c *Client) VerifyResponse(method, path string, header http.Header, wireBody []byte) VerifyResult
- type Config
- type DefaultTransport
- type Error
- type ErrorCode
- type Family
- type Level
- type RequestDraft
- type RequestOption
- type RequestOptions
- type Suite
- func (s Suite) Digest(data []byte) []byte
- func (s Suite) DigestTag() string
- func (s Suite) Family() Family
- func (s Suite) IsSM2() bool
- func (s Suite) KeyBits() int
- func (s Suite) KeyWrapAlgorithm() string
- func (s Suite) MessageAlgorithm() string
- func (s Suite) SecurityReq() string
- func (s Suite) SignAlgorithm() string
- type Transport
- type TransportFunc
- type TransportResponse
- type VerifyResult
Constants ¶
const ( // HeaderAppKey 应用唯一标识头。 HeaderAppKey = "x-wop-appkey" // HeaderSign 结构化签名头。 HeaderSign = "x-wop-sign" // HeaderContentDigest 报文摘要头。 HeaderContentDigest = "x-wop-content-digest" // HeaderTimestamp 请求时间戳头。 HeaderTimestamp = "x-wop-timestamp" // HeaderNonce 防重放随机数头。 HeaderNonce = "x-wop-nonce" // HeaderEncrypt L2 数字信封元数据头。 HeaderEncrypt = "x-wop-encrypt" )
协议 Header 名称(x-wop- 前缀,与网关 GatewayConstants 对齐)。
const ( // SignProtocolVersion 签名协议版本。 SignProtocolVersion = "v1" // SignExpiredSecondsDefault 出站签名默认有效时长(秒)。 SignExpiredSecondsDefault = int64(1800) // SignExpiredSecondsMax expiredSeconds 允许上限(秒),防超大窗口拉长重放风险。 SignExpiredSecondsMax = int64(86400) )
签名协议常量(spec §7 / 网关 GatewayConstants)。
Variables ¶
This section is empty.
Functions ¶
func CanonicalHeaders ¶
CanonicalHeaders 构造规范标头(F2):名称 lowercase + TrimAll + urlencode, 值 TrimAll + urlencode,按名称 ASCII 升序,行间 '\n' 连接,尾行不加 '\n'。
func CanonicalRequest ¶
func CanonicalRequest(authString, method, canonicalURI, canonicalQueryString, canonicalHeaders string) string
CanonicalRequest 组装 5 段规范请求(F2):
authString\nhttpRequestMethod\ncanonicalURI\ncanonicalQueryString\ncanonicalHeaders
POST 的 canonicalQueryString 为空串,分隔空行不可省略; method 统一大写。Go string 零值即 "",天然等价网关 build 的 null→"" 合并。
func DecodeB64URL ¶
DecodeB64URL 严格解码 base64url 无填充:含 '='、'+'、'/'、空白或 长度非法(%4==1)一律拒绝(F6/F7 负向量锚点)。
func DigestHeaderValue ¶
DigestHeaderValue 组装 x-wop-content-digest 线上值: 算法标记 + 恰一空格 + 小写 hex(D2)。摘要对象 = data 原始字节 (L2 时即密文载体,不摘明文)。
func ParseContentDigest ¶
ParseContentDigest 严格解析 digest 头值,返回 (tag, hex)。 结构非法(双空格、大写、长度不符、未支持 tag 等)→ 协议类明确错误。
func ParseSignHeader ¶
ParseSignHeader 严格解析 x-wop-sign 值;结构非法为协议类明确错误。
func TrimAll ¶
TrimAll:去首尾空白,连续空白折叠为单个空格(canonicalRequest 用)。 空白类对齐 Java Character.isWhitespace 常见子集:空格、\t、\n、\x0B、\f、\r。
func URLEncodeJava ¶
URLEncodeJava 按 java.net.URLEncoder(UTF-8) 语义编码,并将输出中的 '+' 替换回 %20(canonicalRequest 的 RFC 3986 风格钉子,F2): 保留 [A-Za-z0-9.-*_],其余字符按 UTF-8 字节 %XX,空格 → %20。
func ValidateContentDigest ¶
ValidateContentDigest 复核线上报文摘要:结构(D2)→ 套件族耦合(I5)→ 值比对。 摘要不匹配返回完整性类明确错误(CodeDigestMismatch)。
func ValidateContentDigestHeader ¶
ValidateContentDigestHeader 结构 + 套件族耦合校验(D2/I5,不含值比对; 与网关 ContentDigestHeader.validate 对齐,formatRules 消费口径)。
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client 是线程安全的 WOP 协议客户端:BuildRequest 纯函数产线, VerifyResponse/VerifyCallback 消费 F6 管线,Do 一站式发送+校验。
func (*Client) BuildRequest ¶
func (c *Client) BuildRequest(method, path string, body []byte, level Level, opts ...RequestOption) (RequestDraft, error)
BuildRequest 构造已签名(L2 时已加密)的请求草稿(spec §2 buildRequest)。 纯计算、零网络 IO;除 CSPRNG 值外同输入同输出。
func (*Client) Do ¶
func (c *Client) Do(method, path string, body []byte, level Level, opts ...RequestOption) (VerifyResult, TransportResponse, error)
Do 一站式调用:BuildRequest → Transport.Send → VerifyResponse(F6)。 构建或发送失败返回错误;响应校验失败时 err 携带 wop.Error(Code 可编程处理), VerifyResult 同时返回完整判定。
func (*Client) VerifyCallback ¶
func (c *Client) VerifyCallback(callbackURL string, header http.Header, wireBody []byte) VerifyResult
VerifyCallback 校验平台回调(spec §2):canonical URI 取回调 URL 的 path (不含 query),HTTP 方法恒为 POST。
func (*Client) VerifyResponse ¶
func (c *Client) VerifyResponse(method, path string, header http.Header, wireBody []byte) VerifyResult
VerifyResponse 校验网关响应(F6 顺序钉死): 验签 → digest 复核 → DEK 解包 → alg 族比对(解包后、bulk 解密前)→ bulk 解密。 method/path 为商户原始请求的方法与路径(平台响应 canonical 复用请求 URI)。
type Config ¶
type Config struct {
// AppKey 平台分配的应用唯一标识。
AppKey string
// SecurityReq 算法套件标识(如 WOP-RSA3072-SHA256)。
SecurityReq string
// MerchantPrivateKey 商户私钥(加签 / L2 入站解包)。
MerchantPrivateKey string
// PlatformPublicKey 平台公钥(验签 / L2 出站 DEK 包装)。
PlatformPublicKey string
// GatewayBaseURL 网关基地址(DefaultTransport 使用)。
GatewayBaseURL string
// ExpiredSeconds 出站签名有效时长(秒),0 → 默认 1800,上限 86400。
ExpiredSeconds int64
// Transport 发送适配器;nil → DefaultTransport(http.Client 默认实例)。
Transport Transport
}
Config 商户接入配置。密钥材料为字符串(PEM 或 Base64 单行,D12)。
type DefaultTransport ¶
type DefaultTransport struct {
// HTTPClient 为 nil 时使用 http.DefaultClient。
HTTPClient *http.Client
// BaseURL 网关基地址;draft.Path 拼接其上。为空时 draft.Path 须为完整 URL。
BaseURL string
}
DefaultTransport 默认 net/http 适配器。
func (DefaultTransport) Send ¶
func (t DefaultTransport) Send(d RequestDraft) (TransportResponse, error)
Send 实现 Transport:构建 http.Request 并发送,读取响应体。
type ErrorCode ¶
type ErrorCode string
ErrorCode 是稳定的公共错误码契约(商户可编程处理)。 分类依据 crypto-strategy-spec §10.2:鉴权前可判定的公开协议知识 → 明确; 依赖密钥参与的判定 → 模糊(防 padding-oracle 式信息泄露)。
const ( // CodeConfig 配置类(明确):密钥缺失、密钥解析失败、密钥与套件不符。 CodeConfig ErrorCode = "CONFIG" // CodeSuiteParse 解析类(明确):securityReq 空值/格式/前缀错误。 CodeSuiteParse ErrorCode = "SUITE_PARSE" // CodeSuiteUnsupported 支持类(明确):算法不在支持列表、跨族组合、长度非法。 CodeSuiteUnsupported ErrorCode = "SUITE_UNSUPPORTED" // CodeProtocol 协议格式类(明确):x-wop-sign / digest 头 / L2 信封结构非法。 CodeProtocol ErrorCode = "PROTOCOL" // CodeDigestMismatch 完整性类(明确):摘要与线上报文字节不符(D2)。 CodeDigestMismatch ErrorCode = "DIGEST_MISMATCH" // CodeVerifyFailed 验签类(模糊):签名验证失败,对外不区分原因(I7)。 CodeVerifyFailed ErrorCode = "VERIFY_FAILED" // CodeDecryptFailed 解密类(模糊):DEK 解包或 GCM 解密失败,对外不区分原因(I7)。 CodeDecryptFailed ErrorCode = "DECRYPT_FAILED" // CodeAlgMismatch 一致性类(明确):dek alg 与套件族不符(公开映射知识,I3 允许提前拒)。 CodeAlgMismatch ErrorCode = "ALG_MISMATCH" )
type RequestDraft ¶
RequestDraft 是协议核心产出的待发送请求:商户可直接消费自带 HTTP 栈, 或交给本 SDK Transport 发送。
type RequestOption ¶
type RequestOption func(*RequestOptions)
RequestOption 单个选项。
func WithRandom ¶
func WithRandom(r io.Reader) RequestOption
WithRandom 注入确定性随机源(联调用;生产禁用——IV 复用即 I4 违规)。
type RequestOptions ¶
type RequestOptions struct {
// TimestampMs 毫秒 Unix 时间戳;0 → 当前时间。
TimestampMs int64
// Nonce 防重放随机串;空 → CSPRNG 生成 32 位 hex。
Nonce string
// Random 随机源(nonce/CEK/IV 顺序消费);nil → crypto/rand。
Random io.Reader
}
RequestOptions 是 BuildRequest 的可选项(测试确定性钩子)。
type Suite ¶
type Suite struct {
// contains filtered or unexported fields
}
Suite 是一次通信的算法套件上下文(spec §4.4 / §3.2 推导规则), 由 securityReq 一次性原子解析(I6),不可变值类型。
func ParseSuite ¶
ParseSuite 从 securityReq 解析算法套件(F1)。 错误分类(spec §2.4):格式/前缀错误 → 解析类;算法不支持/跨族 → 支持类。 两者对外语义均明确。
func (Suite) KeyWrapAlgorithm ¶
KeyWrapAlgorithm 返回 DEK 非对称包装算法名(③)。
func (Suite) MessageAlgorithm ¶
MessageAlgorithm 返回 L2 报文对称算法名(②,dek alg 段)。
type Transport ¶
type Transport interface {
Send(RequestDraft) (TransportResponse, error)
}
Transport 是可插拔 HTTP 适配层(spec §1.1 Q1:协议核心纯函数,传输可替换)。 商户自带栈时直接消费 RequestDraft,无需本接口。
func RoundTripperTransport ¶
func RoundTripperTransport(rt http.RoundTripper, baseURL string) Transport
RoundTripperTransport 把任意 http.RoundTripper 桥接为 Transport (商户复用自带栈的连接池/中间件;baseURL 语义同 DefaultTransport)。
type TransportFunc ¶
type TransportFunc func(RequestDraft) (TransportResponse, error)
TransportFunc 函数适配器(测试/自定义发送逻辑)。
func (TransportFunc) Send ¶
func (f TransportFunc) Send(d RequestDraft) (TransportResponse, error)
Send 实现 Transport。
type TransportResponse ¶
TransportResponse 是适配层归一化的响应。