Documentation
¶
Overview ¶
Package crypt 实现 GM/T 0099-2020 规定的容器协议。
Index ¶
- Constants
- Variables
- func Present(pkg *container.Package) bool
- type CredentialRequest
- type CredentialResolver
- type Credentials
- type DecryptOptions
- type DecryptReport
- type Diagnostic
- type EncryptOptions
- type EncryptRecipient
- type EncryptReport
- type Info
- type InspectOptions
- type Limits
- type LockedError
- type Mapping
- type Mode
- type OpenOptions
- type Operation
- type OperationResult
- type PageSelection
- type Provider
- type Recipient
- type SeedInfo
- type SeedResult
- type View
- func (v *View) Close() error
- func (v *View) EncryptionInfo() *Info
- func (v *View) Entries() []container.Entry
- func (v *View) Export(ctx context.Context, emit func(string, []byte) error) (*DecryptReport, error)
- func (v *View) InputSize() int64
- func (v *View) OFDPackage() (*container.Package, error)
- func (v *View) OpenPhysicalPackage() (*container.Package, error)
- func (v *View) Read(name string) ([]byte, error)
- func (v *View) ReadContext(ctx context.Context, name string) ([]byte, error)
Constants ¶
const ( EntryPoint = spec.EncryptionEntryPoint Namespace = spec.Namespace PasswordScheme = "1.1.1" CertificateScheme = "1.1.2" )
Variables ¶
var ( ErrLocked = errors.New("crypt: encrypted OFD is locked") ErrInvalidMetadata = errors.New("crypt: invalid encryption metadata") ErrLimitExceeded = errors.New("crypt: resource limit exceeded") ErrNotEncrypted = errors.New("crypt: no encryption entry point") ErrAlreadyEncrypted = errors.New("crypt: input is already encrypted") ErrUnsupported = errors.New("crypt: unsupported encryption feature") ErrCredentialsRequired = errors.New("crypt: credentials required") ErrDecryptFailed = errors.New("crypt: credentials do not match or ciphertext is damaged") ErrProtectedContent = errors.New("crypt: transformation would change protected content") )
Functions ¶
Types ¶
type CredentialRequest ¶
type CredentialResolver ¶
type CredentialResolver func(context.Context, CredentialRequest) (Credentials, error)
type Credentials ¶
type DecryptOptions ¶
type DecryptOptions struct {
Mode Mode
Limits Limits
// Password 为 nil 表示未提供口令,非 nil 的空切片表示空口令。
// 字节必须为 UTF-8,由调用方持有,调用期间不得修改。
// 库不会对口令进行归一化或清零。
Password []byte
// PrivateKey 接受支持 SM2 的 crypto.Decrypter,接收 ASN.1 SM2Cipher 和
// sm2.ASN1DecrypterOpts,随机源参数为 crypto/rand.Reader;硬件适配器须遵守此约定。
// 仅检查接收者证书与公钥匹配,不验证证书信任链、有效期或吊销状态;
// 应用的证书使用策略须单独执行,解锁成功不表示接收者身份受信任。
PrivateKey crypto.Decrypter
UserName string
// MissingScheme 在 compat 模式下为缺失的 EncryptCaseId 显式选择 1.1.1 或 1.1.2。
// 它不会覆盖源文件中已声明的方案。
MissingScheme string
Resolve CredentialResolver
}
func NormalizeDecryptOptions ¶
func NormalizeDecryptOptions(opt DecryptOptions) (DecryptOptions, error)
type DecryptReport ¶
type DecryptReport struct {
Operations []OperationResult `json:"operations,omitempty"`
State string `json:"state"`
OperationID string `json:"operation_id"`
Scheme string `json:"scheme"`
Recipient Recipient `json:"recipient"`
DecryptedEntries int `json:"decrypted_entries"`
OutputEntries int `json:"output_entries"`
OutputBytes int64 `json:"output_bytes"`
Diagnostics []Diagnostic `json:"diagnostics,omitempty"`
}
DecryptReport 仅在解密成功时返回。解锁不代表 CBC 明文完整性已验证, 也不代表证书受信任或签名有效。
func DecryptPackage ¶
func DecryptPackage(ctx context.Context, pkg *container.Package, opt DecryptOptions, emit func(string, []byte) error) (*DecryptReport, error)
DecryptPackage 输出条目的原始字节,不重新序列化文档 XML。 输出回调的错误原样返回;发生任何错误时都必须丢弃输出。
type Diagnostic ¶
type EncryptOptions ¶
type EncryptOptions struct {
Limits Limits
Recipients []EncryptRecipient
// Entries 为 nil 时选择全部文件,可通过 KeepRoot 排除 OFD.xml。
// 非 nil 切片按规范 ZIP 相对路径精确选择,空列表会返回错误。
Entries []string
KeepRoot bool
// PlaintextMap 为 true 时输出明文 XML 映射表,默认加密映射表以隐藏原始路径。
PlaintextMap bool
// IndependentKeys 为每个选中条目生成独立 FEK/IV 和 seed,通用密钥仅用于映射表。
IndependentKeys bool
// Append 非 nil 时追加到已有线性加密链;其中的凭据用于保护范围预检。
// 新层选择物理 ZIP 条目,不隐式解开、改写旧层。
Append *DecryptOptions
// Pages 计算指定文档页面的模板、注释及资源范围,与 Entries、Append 互斥。
Pages *PageSelection
}
func NormalizeEncryptOptions ¶
func NormalizeEncryptOptions(opt EncryptOptions) (EncryptOptions, error)
type EncryptRecipient ¶
EncryptRecipient 描述同一加密方案中的一个接收者。 必须提供非 nil 的 UTF-8 口令或 DER 格式的 SM2 证书,二者互斥。
type EncryptReport ¶
type EncryptReport struct {
Relative string `json:"relative,omitempty"`
IndependentKeys bool `json:"independent_keys,omitempty"`
SelectedEntries []string `json:"selected_entries,omitempty"`
State string `json:"state"`
OperationID string `json:"operation_id"`
Scheme string `json:"scheme"`
Recipients []Recipient `json:"recipients"`
EncryptedEntries int `json:"encrypted_entries"`
OutputEntries int `json:"output_entries"`
OutputBytes int64 `json:"output_bytes"`
}
func EncryptPackage ¶
func EncryptPackage(ctx context.Context, pkg *container.Package, opt EncryptOptions, emit func(string, []byte) error) (*EncryptReport, error)
EncryptPackage 按附录 A、C 写入一次新加密操作,文档条目保持原始字节。
type Info ¶
type Info struct {
// Opaque 表示不可读的映射表可能遮蔽尚未知晓的逻辑路径。
Opaque bool `json:"opaque,omitempty"`
Encrypted bool `json:"encrypted"`
State string `json:"state"`
EntryPoint string `json:"entry_point,omitempty"`
Operations []Operation `json:"operations,omitempty"`
Diagnostics []Diagnostic `json:"diagnostics,omitempty"`
}
Info 仅描述可见元数据,不表示解密成功、内容完整、证书受信任或文档签名有效。
func InspectPackage ¶
InspectPackage 借用 pkg。发生错误时,非 nil 的 Info 仅包含失败前已成功检查的元数据, 不得据此允许后续处理。
type InspectOptions ¶
func NormalizeOptions ¶
func NormalizeOptions(opt InspectOptions) (InspectOptions, error)
type Limits ¶
type Limits struct {
MaxInputBytes int64
MaxEntries int
MaxMetadataBytes int64
MaxTotalMetadataBytes int64
MaxXMLNodes int
MaxXMLDepth int
MaxOperations int
MaxRecipients int
MaxMappings int
MaxEntryBytes int64
MaxTotalBytes int64
MaxCredentialAttempts int
MaxLayers int
// MaxProcessedBytes 限制构建和读取视图时累计处理的字节数,
// 包括中间层和重复读取;为零时默认使用 2 GiB。
MaxProcessedBytes int64
}
Limits 限制检查和解密的资源用量,包括 .dat 条目中的 XML。 字段为零时使用有限的默认值。
type LockedError ¶
LockedError 在使用方需要明文时保留加密元数据的检查详情。 Cause 非 nil 表示加密元数据检查失败。
func (*LockedError) Error ¶
func (e *LockedError) Error() string
func (*LockedError) Is ¶
func (e *LockedError) Is(target error) bool
func (*LockedError) Unwrap ¶
func (e *LockedError) Unwrap() error
type Mapping ¶
type Mapping struct {
Path string `json:"path"`
EPath string `json:"encrypted_path,omitempty"`
DecryptSeedLoc string `json:"decrypt_seed_loc,omitempty"`
Seed *SeedInfo `json:"seed,omitempty"`
}
Mapping 中的路径为规范 ZIP 相对路径;XML 中使用包内绝对路径。
type OpenOptions ¶
type OpenOptions struct {
DecryptOptions
// AllowPartial 仅允许因缺少凭据而保留部分解锁的视图。
// 元数据无效、凭据错误或密文损坏仍会使打开失败。
AllowPartial bool
}
type Operation ¶
type Operation struct {
State string `json:"state,omitempty"`
ID string `json:"id"`
Relative string `json:"relative,omitempty"`
Provider Provider `json:"provider"`
Scope string `json:"scope"`
EncryptDate string `json:"encrypt_date,omitempty"`
DecryptSeedLoc string `json:"decrypt_seed_loc"`
EntriesMapLoc string `json:"entries_map_loc"`
Seed SeedInfo `json:"seed"`
// Deferred 表示较早加密层的元数据需要在后续解密步骤中读取。
Deferred bool `json:"deferred,omitempty"`
// MapState 为 plaintext、encrypted 或 unknown(检查失败时)。
MapState string `json:"map_state"`
// Entries 在缺少凭据而无法读取映射表时省略。
Entries []Mapping `json:"entries,omitempty"`
}
type OperationResult ¶
type PageSelection ¶
PageSelection 按 Document.xml 的规范包内路径和页面 ID 选择范围。 共用资源文件整体纳入范围,可能影响使用这些资源的其他页面。
type Recipient ¶
type Recipient struct {
UserName string `json:"user_name"`
UserType string `json:"user_type"`
CertificateSHA256 string `json:"certificate_sha256,omitempty"`
IVDefaulted bool `json:"iv_defaulted"`
}
Recipient 不包含包装后的密钥和原始证书数据。
type SeedResult ¶
type View ¶
type View struct {
// contains filtered or unexported fields
}
View 是不可变的逻辑条目视图,仅保留密文数据源和恢复的密钥, 不保存明文 ZIP 或解密后的资源文件缓存。 打开时建立并校验索引,后续每次读取都重新解密。
func OpenView ¶
func OpenView(ctx context.Context, pkg *container.Package, opt OpenOptions, closeInput func() error) (view *View, err error)
OpenView 借用 pkg,直至最后一个租约关闭。若提供 closeInput, 仅在成功时接管输入资源;失败时仍由调用方关闭 pkg。
func (*View) EncryptionInfo ¶
func (*View) OFDPackage ¶
OFDPackage 返回供使用方持有的独立租约。部分解锁的视图不能传给文档处理器, 可通过 Read/Entries 检查其中可用的条目。
func (*View) OpenPhysicalPackage ¶
OpenPhysicalPackage 提供原始 ZIP 的租约,供审计使用。 返回物理条目的元数据,不以解密后的虚拟条目替代。