Documentation
¶
Overview ¶
Package certkit 统一提供 X.509 证书、私钥和密钥库的解析、转换、签发、 吊销验证、信任根管理及 TLS 服务探测能力。
Open 根据文件内容识别 PEM、DER、PKCS7、PKCS12 和 JKS,不依赖文件扩展名。 Store 保存规范化后的证书、私钥及密钥库条目关系,并可继续增加对象或合并其他文件:
store, err := certkit.Open(data, certkit.OpenOptions{Password: password})
if err != nil && !errx.Is(err, certkit.ErrPassword) {
return err
}
err = store.AddKeyPair("server", key, leaf, intermediate)
密码错误时,Open 会尽量返回已经安全解析的证书数据,并同时返回 ErrPassword。 调用方应使用 errx.Is 判断该错误;结构损坏和不支持格式使用各自的哨兵错误。 JCEKS 和 BKS 只进行格式识别,不提供解析和输出。
Store.Encode 输出 PEM、DER、PKCS7、PKCS12 或 JKS。PKCS12 和 JKS 不设置默认 输出密码,调用方必须通过 EncodeOptions 显式传入。可能丢弃证书或私钥的转换默认 返回 ErrLossyConversion,只有明确设置 AllowLossy 后才会执行。 PKCS12Profile 显式选择固定算法配置;PKCS12Iterations 为 0 时保留配置默认值, 正数同时覆盖加密和 MAC 的派生次数,负数返回 ErrInvalidData。
CreateRootCA、IssueCertificate、CreateCertificateRequest 和 SignCertificateRequest 支持 RSA、ECDSA 和 SM2,可构造根 CA、中间 CA、终端证书 及 PKCS#10 CSR。CreateCRL、CheckCRL 和 CheckOCSP 提供本地吊销数据处理;仅 FetchAndCheckCRL 与 FetchAndCheckOCSP 会主动访问证书扩展中的网络地址。
SystemRoots、MozillaRoots、SystemAndMozillaRoots 和 CustomRoots 构造独立信任库。 MozillaRoots 使用内嵌的 Mozilla 根集合及其约束,不修改进程级根证书配置。
InspectTLS 按指定版本分别建立连接,支持显式探测 TLCP 1.1 以及 TLS 1.0、1.1、 1.2 和 1.3,报告 SNI、协商参数、服务端证书、主机名、信任链、链完整性及 stapled OCSP。TLCP 会收集并验证服务端签名证书和加密证书。
安装:
go get github.com/go-sdk/certkit
Index ¶
- Variables
- func CreateCRL(issuer *Entry, options CreateCRLOptions) ([]byte, error)
- func CreateCertificateRequest(key crypto.Signer, options CSROptions) ([]byte, error)
- func GeneratePrivateKey(options KeyOptions) (crypto.Signer, error)
- type CRLOptions
- type CRLResult
- type CSROptions
- type Certificate
- type CertificateOptions
- type CertificateRole
- type ChainValidation
- type Confidence
- type ConversionReport
- type CreateCRLOptions
- type Detection
- type Diagnostic
- type DiagnosticLevel
- type EncodeOptions
- type Encoding
- type Entry
- func CreateRootCA(options RootCAOptions) (*Entry, error)
- func IssueCertificate(issuer *Entry, options CertificateOptions) (*Entry, error)
- func OpenEntry(certificateData, privateKeyData []byte, options OpenOptions) (*Entry, error)
- func SignCertificateRequest(issuer *Entry, requestData []byte, options CertificateOptions) (*Entry, error)
- type Format
- type KeyAlgorithm
- type KeyOptions
- type MergeReport
- type OCSPOptions
- type OCSPResult
- type OpenOptions
- type PKCS12Profile
- type PasswordProvider
- type PasswordPurpose
- type PasswordRequest
- type PrivateKey
- type PrivateKeyStatus
- type RevokedCertificate
- type RootCAOptions
- type SourceInfo
- type SourceRef
- type Store
- func (s *Store) Add(data []byte, options OpenOptions) (*MergeReport, error)
- func (s *Store) AddCertificate(cert *smx509.Certificate) error
- func (s *Store) AddEntry(entry *Entry) error
- func (s *Store) AddKeyPair(alias string, key crypto.Signer, chain ...*smx509.Certificate) error
- func (s *Store) AddPrivateKey(key crypto.Signer) error
- func (s *Store) AddTrustedCertificate(alias string, cert *smx509.Certificate) error
- func (s *Store) Encode(format Format, options EncodeOptions) ([]byte, *ConversionReport, error)
- func (s *Store) RemoveCertificate(id string) error
- func (s *Store) RemoveEntry(alias string) error
- func (s *Store) RemovePrivateKey(id string) error
- type TLSFailure
- type TLSFailureStage
- type TLSOptions
- type TLSReport
- type TLSVersion
- type TLSVersionResult
- type TrustStore
- type VerifyOptions
- type VerifyResult
Constants ¶
This section is empty.
Variables ¶
var ( // ErrPassword 表示缺少密码或密码无法解密输入。 ErrPassword = errx.New("password required or incorrect") // ErrUnsupportedFormat 表示已识别但未实现解析或输出的格式。 ErrUnsupportedFormat = errx.New("unsupported certificate format") // ErrUnknownFormat 表示无法从内容判断输入格式。 ErrUnknownFormat = errx.New("unknown certificate format") // ErrInvalidData 表示输入结构、边界或证书语义无效。 ErrInvalidData = errx.New("invalid certificate data") // ErrKeyMismatch 表示私钥与证书公钥不匹配。 ErrKeyMismatch = errx.New("private key does not match certificate") // ErrAliasConflict 表示密钥库 alias 重复。 ErrAliasConflict = errx.New("alias already exists") // ErrLossyConversion 表示转换会丢弃证书或私钥。 ErrLossyConversion = errx.New("conversion would discard data") // ErrNoCertificate 表示操作需要证书但 Store 中不存在可用证书。 ErrNoCertificate = errx.New("certificate not found") // ErrNoPrivateKey 表示操作需要私钥但 Store 中不存在可用私钥。 ErrNoPrivateKey = errx.New("private key not found") // ErrUnsupportedAlgorithm 表示请求的密码算法不受支持。 ErrUnsupportedAlgorithm = errx.New("unsupported cryptographic algorithm") )
Functions ¶
func CreateCRL ¶
func CreateCRL(issuer *Entry, options CreateCRLOptions) ([]byte, error)
CreateCRL 使用 CA 证书及私钥创建 DER 编码的 CRL。
func CreateCertificateRequest ¶
func CreateCertificateRequest(key crypto.Signer, options CSROptions) ([]byte, error)
CreateCertificateRequest 使用现有私钥创建 DER 编码的 PKCS#10 CSR。
func GeneratePrivateKey ¶
func GeneratePrivateKey(options KeyOptions) (crypto.Signer, error)
GeneratePrivateKey 生成配置指定的 RSA、ECDSA 或 SM2 私钥。
Types ¶
type CRLOptions ¶
CRLOptions 配置 CRL 验证时间、时钟偏差和网络响应大小限制。
type CRLResult ¶
type CRLResult struct {
Revoked bool
ReasonCode int
RevocationTime time.Time
ThisUpdate time.Time
NextUpdate time.Time
SignatureValid bool
Fresh bool
}
CRLResult 保存 CRL 签名、时效和证书吊销状态。
func CheckCRL ¶
func CheckCRL(cert, issuer *smx509.Certificate, data []byte, options CRLOptions) (*CRLResult, error)
CheckCRL 在本地解析并验证 CRL,不访问网络。
func FetchAndCheckCRL ¶
func FetchAndCheckCRL(ctx context.Context, client *http.Client, cert, issuer *smx509.Certificate, options CRLOptions) (*CRLResult, error)
FetchAndCheckCRL 获取证书首个 CRL 地址并验证响应。
type CSROptions ¶
type CSROptions struct {
Subject pkix.Name
DNSNames []string
IPAddresses []net.IP
EmailAddresses []string
URIs []*url.URL
}
CSROptions 配置 PKCS#10 CSR 的主题和 Subject Alternative Name。
type Certificate ¶
type Certificate struct {
ID string
Certificate *smx509.Certificate
Role CertificateRole
Source SourceRef
}
Certificate 保存规范化证书及其稳定标识和来源。
func NewCertificate ¶
func NewCertificate(cert *smx509.Certificate) (*Certificate, error)
NewCertificate 将已解析证书规范化为 Store 使用的模型。
type CertificateOptions ¶
type CertificateOptions struct {
Alias string
Subject pkix.Name
NotBefore time.Time
NotAfter time.Time
ValidFor time.Duration
DNSNames []string
IPAddresses []net.IP
EmailAddresses []string
URIs []*url.URL
IsCA bool
MaxPathLen int
MaxPathLenZero bool
KeyUsage smx509.KeyUsage
ExtKeyUsage []smx509.ExtKeyUsage
SerialNumber *big.Int
Key KeyOptions
}
CertificateOptions 配置 CA 或终端证书的主题、用途、有效期和密钥。
type CertificateRole ¶
type CertificateRole string
CertificateRole 表示证书在链中的角色。
const ( CertificateRoleUnknown CertificateRole = "unknown" CertificateRoleLeaf CertificateRole = "leaf" CertificateRoleIntermediate CertificateRole = "intermediate" CertificateRoleRoot CertificateRole = "root" )
type ChainValidation ¶
type ChainValidation struct {
HostnameValid bool
Trusted bool
ServedChainComplete bool
ContainsRoot bool
MissingIssuers []string
CompletableViaAIA bool
Error error
}
ChainValidation 保存主机名、信任链和服务端链完整性判断。
type Confidence ¶
type Confidence string
Confidence 表示内容格式检测结果的可信程度。
const ( ConfidenceLow Confidence = "low" ConfidenceMedium Confidence = "medium" ConfidenceHigh Confidence = "high" )
type ConversionReport ¶
type ConversionReport struct {
Format Format
Aliases []string
CertificatesWritten int
PrivateKeysWritten int
DiscardedCertificates int
DiscardedPrivateKeys int
Warnings []string
}
ConversionReport 描述编码结果和被丢弃的对象。
type CreateCRLOptions ¶
type CreateCRLOptions struct {
Number *big.Int
ThisUpdate time.Time
NextUpdate time.Time
ValidFor time.Duration
Certificates []RevokedCertificate
}
CreateCRLOptions 配置 CRL 编号、有效期和吊销条目。
type Detection ¶
type Detection struct {
Format Format
Encoding Encoding
Confidence Confidence
Alternatives []Format
}
Detection 描述基于内容识别出的格式、编码和候选格式。
type Diagnostic ¶
type Diagnostic struct {
Level DiagnosticLevel
Code string
Message string
Alias string
}
Diagnostic 保存未中断处理流程的诊断信息。
type DiagnosticLevel ¶
type DiagnosticLevel string
DiagnosticLevel 表示解析或转换诊断的严重程度。
const ( DiagnosticInfo DiagnosticLevel = "info" DiagnosticWarning DiagnosticLevel = "warning" DiagnosticError DiagnosticLevel = "error" )
type EncodeOptions ¶
type EncodeOptions struct {
Password []byte
KeyPassword []byte
KeyPasswords map[string][]byte
Alias string
Aliases map[string]string
AllowLossy bool
EncryptPEMPrivateKey bool
ExcludeRootCertificates bool
PKCS12Profile PKCS12Profile
// PKCS12Iterations 仅用于 PKCS12:0 保留 profile 默认值,正数同时覆盖加密及 MAC 的派生次数,负数无效。
PKCS12Iterations int
}
EncodeOptions 配置目标格式的密码、alias、PEM 根证书选择和有损转换策略。
type Entry ¶
type Entry struct {
Alias string
CreatedAt time.Time
PrivateKey *PrivateKey
PrivateKeyStatus PrivateKeyStatus
Chain []*Certificate
Trusted bool
}
Entry 保存 alias、私钥和有序证书链之间的关系。
func CreateRootCA ¶
func CreateRootCA(options RootCAOptions) (*Entry, error)
CreateRootCA 创建自签根 CA 证书及其私钥。
func IssueCertificate ¶
func IssueCertificate(issuer *Entry, options CertificateOptions) (*Entry, error)
IssueCertificate 使用 CA 生成新私钥并签发证书。
func OpenEntry ¶ added in v1.0.4
func OpenEntry(certificateData, privateKeyData []byte, options OpenOptions) (*Entry, error)
OpenEntry 分别解析证书链和私钥,并返回公钥匹配的条目。
func SignCertificateRequest ¶
func SignCertificateRequest(issuer *Entry, requestData []byte, options CertificateOptions) (*Entry, error)
SignCertificateRequest 验证 PKCS#10 CSR 并使用 CA 签发证书。
func (*Entry) CertificateChainPEM ¶ added in v1.0.3
func (e *Entry) CertificateChainPEM(options EncodeOptions) ([]byte, error)
CertificateChainPEM 按叶子证书、中间 CA 到根 CA 的顺序输出 PEM 证书链。 EncodeOptions.ExcludeRootCertificates 用于控制是否剔除自签根证书。
func (*Entry) PrivateKeyPEM ¶ added in v1.0.3
func (e *Entry) PrivateKeyPEM(options EncodeOptions) ([]byte, error)
PrivateKeyPEM 将当前条目的可用私钥输出为 PKCS#8 PEM。 EncodeOptions.EncryptPEMPrivateKey 和 Password 用于控制私钥加密。
type KeyAlgorithm ¶
type KeyAlgorithm string
KeyAlgorithm 表示证书或私钥使用的公钥算法。
const ( KeyAlgorithmUnknown KeyAlgorithm = "unknown" KeyAlgorithmRSA KeyAlgorithm = "rsa" KeyAlgorithmEC KeyAlgorithm = "ec" KeyAlgorithmSM2 KeyAlgorithm = "sm2" )
type KeyOptions ¶
type KeyOptions struct {
Algorithm KeyAlgorithm
RSABits int
Curve string
}
KeyOptions 配置 RSA、ECDSA 或 SM2 私钥生成参数。
type MergeReport ¶
MergeReport 记录 Store.Add 实际新增的对象数量。
type OCSPOptions ¶
type OCSPOptions struct {
CurrentTime time.Time
MaxClockSkew time.Duration
Hash crypto.Hash
MaxSize int64
}
OCSPOptions 配置 OCSP 验证时间、哈希算法和网络响应大小限制。
type OCSPResult ¶
type OCSPResult struct {
Status int
SerialNumber string
ProducedAt time.Time
ThisUpdate time.Time
NextUpdate time.Time
RevokedAt time.Time
RevocationReason int
Fresh bool
}
OCSPResult 保存 OCSP 响应中的状态和有效时间。
func CheckOCSP ¶
func CheckOCSP(cert, issuer *smx509.Certificate, data []byte, options OCSPOptions) (*OCSPResult, error)
CheckOCSP 在本地解析并验证指定证书的 OCSP 响应。
func FetchAndCheckOCSP ¶
func FetchAndCheckOCSP(ctx context.Context, client *http.Client, cert, issuer *smx509.Certificate, options OCSPOptions) (*OCSPResult, error)
FetchAndCheckOCSP 向证书首个 OCSP 地址发起请求并验证响应。
type OpenOptions ¶
type OpenOptions struct {
Format Format
Password []byte
KeyPasswords map[string][]byte
PasswordProvider PasswordProvider
DisableJKSDefaultPassword bool
MaxInputSize int
MaxCertificates int
MaxPrivateKeys int
}
OpenOptions 配置输入格式、密码来源和解析资源上限。
type PKCS12Profile ¶
type PKCS12Profile string
PKCS12Profile 选择 PKCS12 的算法兼容配置。
const ( // 现有配置固定保留对应算法,不跟随上游 Modern、Legacy 别名的未来调整。 PKCS12ProfileModern PKCS12Profile = "modern" PKCS12ProfileLegacy PKCS12Profile = "legacy" PKCS12ProfileShangMi PKCS12Profile = "shangmi" // PKCS12ProfileModern2023 使用 AES-256、PBKDF2-HMAC-SHA-256 和传统 PKCS12 SHA-256 MAC。 PKCS12ProfileModern2023 PKCS12Profile = "modern2023" // PKCS12ProfileModern2026 使用 AES-256 和 PBMAC1,接收方必须支持 PBMAC1。 PKCS12ProfileModern2026 PKCS12Profile = "modern2026" // PKCS12ProfileLegacyRC2 使用 RC2-40 加密证书、3DES 加密私钥,仅用于旧系统兼容。 PKCS12ProfileLegacyRC2 PKCS12Profile = "legacyrc2" // PKCS12ProfileLegacyDES 使用 3DES 加密证书和私钥,仅用于旧系统兼容。 PKCS12ProfileLegacyDES PKCS12Profile = "legacydes" )
type PasswordProvider ¶
type PasswordProvider func(PasswordRequest) ([]byte, error)
PasswordProvider 根据解析上下文按需返回密码。
type PasswordPurpose ¶
type PasswordPurpose string
PasswordPurpose 表示密码用于容器还是独立私钥。
const ( PasswordPurposeContainer PasswordPurpose = "container" PasswordPurposePrivateKey PasswordPurpose = "private_key" )
type PasswordRequest ¶
type PasswordRequest struct {
Format Format
Alias string
Purpose PasswordPurpose
}
PasswordRequest 描述密码提供器当前需要的格式、alias 和用途。
type PrivateKey ¶
type PrivateKey struct {
ID string
Signer crypto.Signer
Algorithm KeyAlgorithm
Exportable bool
Source SourceRef
}
PrivateKey 保存可签名私钥及其稳定公钥标识。
func NewPrivateKey ¶
func NewPrivateKey(key crypto.Signer) (*PrivateKey, error)
NewPrivateKey 将签名私钥规范化为 Store 使用的模型。
type PrivateKeyStatus ¶
type PrivateKeyStatus string
PrivateKeyStatus 表示密钥库条目的私钥是否可用。
const ( PrivateKeyStatusAvailable PrivateKeyStatus = "available" )
type RevokedCertificate ¶
RevokedCertificate 描述写入 CRL 的吊销证书条目。
type RootCAOptions ¶
type RootCAOptions struct {
Alias string
Subject pkix.Name
NotBefore time.Time
NotAfter time.Time
ValidFor time.Duration
MaxPathLen int
MaxPathLenZero bool
Key KeyOptions
}
RootCAOptions 配置自签根 CA 的主题、有效期、路径约束和密钥。
type SourceInfo ¶
SourceInfo 描述 Store 的来源格式和完整性状态。
type Store ¶
type Store struct {
Source SourceInfo
Entries []*Entry
Certificates []*Certificate
PrivateKeys []*PrivateKey
Diagnostics []Diagnostic
}
Store 统一保存解析后的条目、证书、私钥和诊断信息。
func (*Store) Add ¶
func (s *Store) Add(data []byte, options OpenOptions) (*MergeReport, error)
Add 解析输入并将对象合并到当前 Store。
func (*Store) AddCertificate ¶
func (s *Store) AddCertificate(cert *smx509.Certificate) error
AddCertificate 向 Store 添加证书并重新匹配已有条目。
func (*Store) AddKeyPair ¶
AddKeyPair 使用指定 alias 添加私钥及其有序证书链。
func (*Store) AddPrivateKey ¶
AddPrivateKey 向 Store 添加私钥并重新匹配已有条目。
func (*Store) AddTrustedCertificate ¶
func (s *Store) AddTrustedCertificate(alias string, cert *smx509.Certificate) error
AddTrustedCertificate 使用指定 alias 添加信任证书条目。
func (*Store) Encode ¶
func (s *Store) Encode(format Format, options EncodeOptions) ([]byte, *ConversionReport, error)
Encode 将 Store 编码为指定格式,并返回转换报告。
func (*Store) RemoveCertificate ¶
RemoveCertificate 按稳定标识移除证书及条目中的引用。
func (*Store) RemoveEntry ¶
RemoveEntry 按不区分大小写的 alias 移除条目。
func (*Store) RemovePrivateKey ¶
RemovePrivateKey 按稳定标识移除私钥并保留不可用状态。
type TLSFailure ¶
type TLSFailure struct {
Stage TLSFailureStage
Message string
}
TLSFailure 保存单个协议版本的失败阶段和错误信息。
type TLSFailureStage ¶
type TLSFailureStage string
TLSFailureStage 表示 TLCP 或 TLS 探测失败发生的阶段。
const ( TLSFailureDial TLSFailureStage = "dial" TLSFailureHandshake TLSFailureStage = "handshake" TLSFailureParse TLSFailureStage = "certificate_parse" )
type TLSOptions ¶
type TLSOptions struct {
ServerName string
Versions []TLSVersion
Roots *TrustStore
Dialer *net.Dialer
CipherSuites []uint16
NextProtos []string
CurrentTime time.Time
}
TLSOptions 配置 SNI、协议版本、信任根和连接参数。
type TLSReport ¶
type TLSReport struct {
Address string
ServerName string
Versions []TLSVersionResult
}
TLSReport 保存目标地址在各 TLCP 或 TLS 版本下的独立探测结果。
func InspectTLS ¶
InspectTLS 对每个指定 TLCP 或 TLS 版本建立独立连接并验证服务端证书。
type TLSVersion ¶
type TLSVersion uint16
TLSVersion 表示需要单独探测的 TLCP 或 TLS 协议版本。
const ( TLCP11 TLSVersion = tlcp.VersionTLCP TLS10 TLSVersion = tls.VersionTLS10 TLS11 TLSVersion = tls.VersionTLS11 TLS12 TLSVersion = tls.VersionTLS12 TLS13 TLSVersion = tls.VersionTLS13 )
type TLSVersionResult ¶
type TLSVersionResult struct {
Version TLSVersion
HandshakeSucceeded bool
NegotiatedVersion uint16
CipherSuite uint16
CipherSuiteName string
ALPN string
PeerCertificates *Store
Validation ChainValidation
StapledOCSP *OCSPResult
StapledOCSPError error
Failure *TLSFailure
}
TLSVersionResult 保存单个 TLCP 或 TLS 版本的握手和证书验证结果。
type TrustStore ¶
type TrustStore struct {
// contains filtered or unexported fields
}
TrustStore 保存一个或多个互相独立的根证书池及其约束。
func CustomRoots ¶
func CustomRoots(certs ...*smx509.Certificate) *TrustStore
CustomRoots 使用给定证书创建独立信任库。
func SystemAndMozillaRoots ¶
func SystemAndMozillaRoots() (*TrustStore, error)
SystemAndMozillaRoots 返回同时包含系统和 Mozilla 根的独立信任库。
type VerifyOptions ¶
type VerifyOptions struct {
Roots *TrustStore
Intermediates []*smx509.Certificate
DNSName string
CurrentTime time.Time
KeyUsages []smx509.ExtKeyUsage
}
VerifyOptions 配置证书链、主机名和验证时间。
type VerifyResult ¶
type VerifyResult struct {
Trusted bool
HostnameValid bool
Chains [][]*smx509.Certificate
Error error
}
VerifyResult 保存证书链和主机名验证结果。
func VerifyCertificate ¶
func VerifyCertificate(cert *smx509.Certificate, options VerifyOptions) VerifyResult
VerifyCertificate 使用指定信任根和中间证书验证证书。
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
pkcs12
Package pkcs12 implements some of PKCS#12 (also known as P12 or PFX).
|
Package pkcs12 implements some of PKCS#12 (also known as P12 or PFX). |
|
pkcs12/internal/rc2
Package rc2 implements the RC2 cipher
|
Package rc2 implements the RC2 cipher |