Documentation
¶
Overview ¶
Package sign 提供 OFD 外部命令签署、SM2/SM3 SES 生成和完整签章验证。
签名在包内容最终确定后追加:本包为每个文档体计算被引用文件的摘要并生成 Signature.xml(SignedInfo),把该文件字节通过标准输入交给外部命令,命令在 标准输出返回 SignedValue.dat 字节,本包再写回 Signatures.xml 与 OFD.xml。 签名算法与私钥由外部命令负责,本包不接触密钥。
Index ¶
- Constants
- func CreateSignedValue(signatureXML []byte, seal *Seal, certificate []byte, key *sm2.PrivateKey, ...) ([]byte, error)
- func Sign(input any, w io.Writer, options Options) error
- type CertificateDigest
- type CertificateRevocationOptions
- type CertificateTrustOptions
- type Extension
- type Options
- type ReferenceOptions
- type SM2SignatureFormat
- type Seal
- type SealParams
- type SignatureValidation
- type SignatureVerificationResult
- type SigningOptions
- type StampOptions
- type StampSeamOptions
- type Verification
- type VerificationCheck
- type VerificationOptions
Examples ¶
Constants ¶
const ( // DefaultSignatureMethod 是 SM2 + SM3 的签名算法 OID。 DefaultSignatureMethod = "1.2.156.10197.1.501" // DefaultCheckMethod 是默认摘要算法。 DefaultCheckMethod = "SM3" )
const ( SignatureFormatAuto = parser.SM2SignatureFormatAuto SignatureFormatDER = parser.SM2SignatureFormatDER SignatureFormatRaw = parser.SM2SignatureFormatRaw )
Variables ¶
This section is empty.
Functions ¶
func CreateSignedValue ¶ added in v0.1.5
func CreateSignedValue(signatureXML []byte, seal *Seal, certificate []byte, key *sm2.PrivateKey, propertyInfo string, at time.Time, options SigningOptions) ([]byte, error)
CreateSignedValue 对 Signature.xml 字节生成 SM2/SM3 SES V4 签章值。 propertyInfo 指定受保护的 Signature.xml 包内路径。签署前检查证书与密钥匹配、 印章签名及授权、有效期和用途;证书信任和状态校验由 options.Validate 接入。 返回值可交给 creator.Signature.SignedValue,或由外部签名命令输出。
Types ¶
type CertificateDigest ¶ added in v0.1.5
type CertificateDigest = ses.CertificateDigest
type CertificateRevocationOptions ¶ added in v0.1.5
type CertificateRevocationOptions = parser.CertificateRevocationOptions
type CertificateTrustOptions ¶ added in v0.1.5
type CertificateTrustOptions = parser.CertificateTrustOptions
type Options ¶
type Options struct {
// Command 是外部签名命令,按空白拆分参数,不支持引号或 shell 语法。
// 命令从标准输入读取 Signature.xml 字节,向标准输出写 SignedValue.dat 字节。
Command string
// ID 是签名标识,空值时使用 "sign-1"。
ID string
// ProviderName、ProviderVersion、Company 写入 SignedInfo 的 Provider。
ProviderName string
ProviderVersion string
Company string
// SignatureMethod 是签名算法 OID,空值时使用 DefaultSignatureMethod。
SignatureMethod string
// CheckMethod 是摘要算法,空值时使用 DefaultCheckMethod。
CheckMethod string
// Date 是签名时间,零值时使用当前时间。
Date time.Time
// Deterministic 使用固定 ZIP 时间,生成可复现的签名结果。
// 需要 Date 固定时配合设置,否则 Signature.xml 中的签名时间仍会变化。
Deterministic bool
// Stamp 控制是否在 Signature.xml 写入 StampAnnot,让阅读器把印章图片绘制到页面上。
// 为 nil 时不写入 StampAnnot(只验签,不显示印章)。
Stamp *StampOptions
// StampSeam 在同一文档体的每个页面右边缘写入一条同一印章的裁片。
// 所有标注引用同一份 Seal,按页面顺序拼回完整骑缝章。
StampSeam *StampSeamOptions
// Seal 指定独立的电子印章文件(DER 编码的 SES_Seal,如 .esl)。提供时
// Signature.xml 会写入 <Seal BaseLoc="Seal.esl"/> 并把该文件一起写入包,
// 阅读器优先从独立印章文件提取印章图。SignedValue.dat 仍内嵌印章,
// 作为阅读器未识别 Seal 元素时的回退路径。
Seal string
// References 控制 SignedInfo/References 覆盖的文件集合;为 nil 时签名文档体目录下的全部文件(不含 OFD.xml)。
References *ReferenceOptions
// Environment 是传递给外部命令的额外环境变量,格式为 KEY=VALUE。
Environment []string
}
Options 控制外部命令签名。
type ReferenceOptions ¶
ReferenceOptions 选择 SignedInfo/References 覆盖的文件。 Include/Exclude 是不区分大小写的 glob(相对文档体目录,如 "Pages/**"、"*.xml"), 为空表示包含全部文件。RootDocument 为真时把 OFD.xml 纳入引用。
type SM2SignatureFormat ¶ added in v0.1.5
type SM2SignatureFormat = parser.SM2SignatureFormat
type Seal ¶ added in v0.1.5
func CreateSeal ¶ added in v0.1.5
func CreateSeal(params SealParams, certificate []byte, key *sm2.PrivateKey, at time.Time) (*Seal, error)
CreateSeal 使用调用方提供的制章者证书和私钥生成 SES V4 印章。 SignerCertificates/SignerDigests 可授权与制章者不同的签章者。
type SealParams ¶ added in v0.1.5
type SealParams = ses.SealParams
SealParams 描述显示尺寸以毫米计的 SES 电子印章,以及获授权签章者列表。
type SignatureValidation ¶ added in v0.1.5
type SignatureValidation = parser.SignatureValidation
type SignatureVerificationResult ¶ added in v0.1.5
type SignatureVerificationResult = parser.SignatureVerificationResult
type SigningOptions ¶ added in v0.1.5
type SigningOptions = ses.SigningOptions
type StampOptions ¶
type StampOptions struct {
// PageRef 是 StampAnnot@PageRef 引用的页面 ID;为空时使用首个文档体的首页。
PageRef string
// Boundary 是 StampAnnot@Boundary,格式为 "x y width height";为空时在首页右下角放置默认大小印章。
Boundary string
}
StampOptions 描述签章在页面上的位置。
type StampSeamOptions ¶ added in v0.1.5
type StampSeamOptions struct {
// Edge 是骑缝章贴靠的页面边缘,支持 "left"、"right"、"top" 与 "bottom";
// 空值默认为 "right"。
Edge string
// Pages 选择参与分片的页码,支持 "all"、"odd"、"even";空值默认为 "all"。
Pages string
// Pieces 是印章拆分份数;零值按参与分片的页数计算,非零时必须与参与页数相等。
Pieces int
// GroupPages 是每组骑缝章覆盖的页数;零值表示所有参与页面共用一枚章。
GroupPages int
// Size 是印章边长,单位为毫米;零值使用默认值。
Size float64
// MinStrip 是每页裁片的最小宽度,单位为毫米;零值使用 2mm。
MinStrip float64
// X 是上下边缘时印章左边坐标,单位为毫米;负值按页面水平居中。
X float64
// Y 是印章底边坐标,单位为毫米;负值按页面垂直居中,零值可贴页面底部。
Y float64
}
StampSeamOptions 描述跨整个文档体页面序列的骑缝章。 每页按自身 Area/PhysicalBox 定位,缺失时回退文档公共 PageArea。
type Verification ¶ added in v0.1.5
type Verification struct {
DocumentIndex int `json:"document_index"`
ID string `json:"id"`
// DigestValid 仅表示引用摘要与 SES DataHash 匹配;完整结论使用 Result.FullyValid()。
DigestValid bool `json:"digest_valid"`
Result *SignatureVerificationResult `json:"result,omitempty"`
Error string `json:"error,omitempty"`
}
Verification 是一个文档体中的一个签名的完整验证结果。
func Verify ¶ added in v0.1.5
func Verify(ctx context.Context, input any, options VerificationOptions) ([]Verification, error)
Verify 校验 OFD 的文件摘要及 GB/T 38540—2020 SES 签章。 input 接受文件路径、[]byte 或 io.Reader。缺少信任、吊销或印章状态证据时, Result.Validation.Status 为 incomplete;两层数学签名结果仍独立保留。 业务接受签章前应检查 Result.FullyValid()。
Example ¶
package main
import (
"context"
"fmt"
"github.com/zc310/ofd/pkg/sign"
)
func main() {
results, err := sign.Verify(context.Background(), "../../testdata/ofdrw/999.ofd", sign.VerificationOptions{})
if err != nil {
panic(err)
}
fmt.Println(len(results))
// 省略证书与状态证据时不能据两层签名结果认定完整验证通过。
fmt.Println(results[0].Result.FullyValid())
}
Output: 1 false
type VerificationCheck ¶ added in v0.1.5
type VerificationCheck = parser.VerificationCheck
type VerificationOptions ¶ added in v0.1.5
type VerificationOptions = parser.SignatureVerificationOptions