Documentation ¶
Overview ¶
crypto/x509 add sm2 support
Index ¶
- Variables
- func CreateCertificate(template, parent *Certificate, publicKey *sm2.PublicKey, signer crypto.Signer) ([]byte, error)
- func CreateCertificateRequest(rand io.Reader, template *CertificateRequest, signer crypto.Signer) (csr []byte, err error)
- func CreateCertificateRequestToPem(template *CertificateRequest, signer crypto.Signer) ([]byte, error)
- func CreateCertificateToPem(template, parent *Certificate, pubKey *sm2.PublicKey, signer crypto.Signer) ([]byte, error)
- func MarshalPKCS1PrivateKey(key *rsa.PrivateKey) []byte
- func MarshalPKIXPublicKey(pub interface{}) ([]byte, error)
- func MarshalSm2EcryptedPrivateKey(PrivKey *sm2.PrivateKey, pwd []byte) ([]byte, error)
- func MarshalSm2PrivateKey(key *sm2.PrivateKey, pwd []byte) ([]byte, error)
- func MarshalSm2PublicKey(key *sm2.PublicKey) ([]byte, error)
- func MarshalSm2UnecryptedPrivateKey(key *sm2.PrivateKey) ([]byte, error)
- func ParseCRL(crlBytes []byte) (*pkix.CertificateList, error)
- func ParseDERCRL(derBytes []byte) (*pkix.CertificateList, error)
- func ParsePKCS1PrivateKey(der []byte) (*rsa.PrivateKey, error)
- func ParsePKCS8EcryptedPrivateKey(der, pwd []byte) (*sm2.PrivateKey, error)
- func ParsePKCS8PrivateKey(der, pwd []byte) (*sm2.PrivateKey, error)
- func ParsePKCS8UnecryptedPrivateKey(der []byte) (*sm2.PrivateKey, error)
- func ParsePKIXPublicKey(derBytes []byte) (pub interface{}, err error)
- func ParseSm2CertifateToX509(asn1data []byte) (*x509.Certificate, error)
- func ParseSm2PrivateKey(der []byte) (*sm2.PrivateKey, error)
- func ParseSm2PublicKey(der []byte) (*sm2.PublicKey, error)
- func ReadPrivateKeyFromHex(Dhex string) (*sm2.PrivateKey, error)
- func ReadPrivateKeyFromPem(privateKeyPem []byte, pwd []byte) (*sm2.PrivateKey, error)
- func ReadPublicKeyFromHex(Qhex string) (*sm2.PublicKey, error)
- func ReadPublicKeyFromPem(publicKeyPem []byte) (*sm2.PublicKey, error)
- func RegisterHash(h Hash, f func() hash.Hash)
- func WritePrivateKeyToHex(key *sm2.PrivateKey) string
- func WritePrivateKeyToPem(key *sm2.PrivateKey, pwd []byte) ([]byte, error)
- func WritePublicKeyToHex(key *sm2.PublicKey) string
- func WritePublicKeyToPem(key *sm2.PublicKey) ([]byte, error)
- type Certificate
- func (c *Certificate) CheckCRLSignature(crl *pkix.CertificateList) error
- func (c *Certificate) CheckSignature(algo SignatureAlgorithm, signed, signature []byte) error
- func (c *Certificate) CheckSignatureFrom(parent *Certificate) error
- func (c *Certificate) CreateCRL(rand io.Reader, priv interface{}, revokedCerts []pkix.RevokedCertificate, ...) (crlBytes []byte, err error)
- func (c *Certificate) Equal(other *Certificate) bool
- func (c *Certificate) FromX509Certificate(x509Cert *x509.Certificate)
- func (c *Certificate) ToX509Certificate() *x509.Certificate
- type CertificateRequest
- type ConstraintViolationError
- type EncryptedPrivateKeyInfo
- type ExtKeyUsage
- type Hash
- type InsecureAlgorithmError
- type KeyUsage
- type Pbes2Algorithms
- type Pbes2Encs
- type Pbes2KDfs
- type Pbes2Params
- type Pkdf2Params
- type PrivateKeyInfo
- type PublicKeyAlgorithm
- type SignatureAlgorithm
- type UnhandledCriticalExtension
Constants ¶
This section is empty.
Variables ¶
var ErrUnsupportedAlgorithm = errors.New("x509: cannot verify signature: algorithm unimplemented")
ErrUnsupportedAlgorithm results from attempting to perform an operation that involves algorithms that are not currently implemented.
Functions ¶
func CreateCertificate ¶
func CreateCertificate(template, parent *Certificate, publicKey *sm2.PublicKey, signer crypto.Signer) ([]byte, error)
CreateCertificate creates a new certificate based on a template. The following members of template are used: SerialNumber, Subject, NotBefore, NotAfter, KeyUsage, ExtKeyUsage, UnknownExtKeyUsage, BasicConstraintsValid, IsCA, MaxPathLen, SubjectKeyId, DNSNames, PermittedDNSDomainsCritical, PermittedDNSDomains, SignatureAlgorithm.
The certificate is signed by parent. If parent is equal to template then the certificate is self-signed. The parameter pub is the public key of the signee and priv is the private key of the signer.
The returned slice is the certificate in DER encoding.
All keys types that are implemented via crypto.Signer are supported (This includes *rsa.PublicKey and *ecdsa.PublicKey.)
func CreateCertificateRequest ¶
func CreateCertificateRequest(rand io.Reader, template *CertificateRequest, signer crypto.Signer) (csr []byte, err error)
CreateCertificateRequest creates a new certificate request based on a template. The following members of template are used: Subject, Attributes, SignatureAlgorithm, Extensions, DNSNames, EmailAddresses, and IPAddresses. The private key is the private key of the signer.
The returned slice is the certificate request in DER encoding.
All keys types that are implemented via crypto.Signer are supported (This includes *rsa.PublicKey and *ecdsa.PublicKey.)
func CreateCertificateRequestToPem ¶
func CreateCertificateRequestToPem(template *CertificateRequest, signer crypto.Signer) ([]byte, error)
func CreateCertificateToPem ¶
func CreateCertificateToPem(template, parent *Certificate, pubKey *sm2.PublicKey, signer crypto.Signer) ([]byte, error)
CreateCertificateToPem creates a new certificate based on a template and encodes it to PEM format. It uses CreateCertificate to create certificate and returns its PEM format.
func MarshalPKCS1PrivateKey ¶
func MarshalPKCS1PrivateKey(key *rsa.PrivateKey) []byte
MarshalPKCS1PrivateKey converts a private key to ASN.1 DER encoded form.
func MarshalPKIXPublicKey ¶
MarshalPKIXPublicKey serialises a public key to DER-encoded PKIX format.
func MarshalSm2EcryptedPrivateKey ¶
func MarshalSm2EcryptedPrivateKey(PrivKey *sm2.PrivateKey, pwd []byte) ([]byte, error)
func MarshalSm2PrivateKey ¶
func MarshalSm2PrivateKey(key *sm2.PrivateKey, pwd []byte) ([]byte, error)
func MarshalSm2UnecryptedPrivateKey ¶
func MarshalSm2UnecryptedPrivateKey(key *sm2.PrivateKey) ([]byte, error)
func ParseCRL ¶
func ParseCRL(crlBytes []byte) (*pkix.CertificateList, error)
ParseCRL parses a CRL from the given bytes. It's often the case that PEM encoded CRLs will appear where they should be DER encoded, so this function will transparently handle PEM encoding as long as there isn't any leading garbage.
func ParseDERCRL ¶
func ParseDERCRL(derBytes []byte) (*pkix.CertificateList, error)
ParseDERCRL parses a DER encoded CRL from the given bytes.
func ParsePKCS1PrivateKey ¶
func ParsePKCS1PrivateKey(der []byte) (*rsa.PrivateKey, error)
ParsePKCS1PrivateKey returns an RSA private key from its ASN.1 PKCS#1 DER encoded form.
func ParsePKCS8EcryptedPrivateKey ¶
func ParsePKCS8EcryptedPrivateKey(der, pwd []byte) (*sm2.PrivateKey, error)
func ParsePKCS8PrivateKey ¶
func ParsePKCS8PrivateKey(der, pwd []byte) (*sm2.PrivateKey, error)
func ParsePKCS8UnecryptedPrivateKey ¶
func ParsePKCS8UnecryptedPrivateKey(der []byte) (*sm2.PrivateKey, error)
func ParsePKIXPublicKey ¶
ParsePKIXPublicKey parses a DER encoded public key. These values are typically found in PEM blocks with "BEGIN PUBLIC KEY".
Supported key types include RSA, DSA, and ECDSA. Unknown key types result in an error.
On success, pub will be of type *rsa.PublicKey, *dsa.PublicKey, or *ecdsa.PublicKey.
func ParseSm2CertifateToX509 ¶
func ParseSm2CertifateToX509(asn1data []byte) (*x509.Certificate, error)
func ParseSm2PrivateKey ¶
func ParseSm2PrivateKey(der []byte) (*sm2.PrivateKey, error)
func ReadPrivateKeyFromHex ¶
func ReadPrivateKeyFromHex(Dhex string) (*sm2.PrivateKey, error)
DHex是sm2私钥的真正关键数值
func ReadPrivateKeyFromPem ¶
func ReadPrivateKeyFromPem(privateKeyPem []byte, pwd []byte) (*sm2.PrivateKey, error)
func RegisterHash ¶
RegisterHash registers a function that returns a new instance of the given hash function. This is intended to be called from the init function in packages that implement hash functions.
func WritePrivateKeyToHex ¶
func WritePrivateKeyToHex(key *sm2.PrivateKey) string
func WritePrivateKeyToPem ¶
func WritePrivateKeyToPem(key *sm2.PrivateKey, pwd []byte) ([]byte, error)
func WritePublicKeyToHex ¶
Types ¶
type Certificate ¶
type Certificate struct { Raw []byte // Complete ASN.1 DER content (certificate, signature algorithm and signature). RawTBSCertificate []byte // Certificate part of raw ASN.1 DER content. RawSubjectPublicKeyInfo []byte // DER encoded SubjectPublicKeyInfo. RawSubject []byte // DER encoded Subject RawIssuer []byte // DER encoded Issuer Signature []byte SignatureAlgorithm SignatureAlgorithm PublicKeyAlgorithm PublicKeyAlgorithm PublicKey interface{} Version int SerialNumber *big.Int Issuer pkix.Name Subject pkix.Name NotBefore, NotAfter time.Time // Validity bounds. KeyUsage KeyUsage // Extensions contains raw X.509 extensions. When parsing certificates, // this can be used to extract non-critical extensions that are not // parsed by this package. When marshaling certificates, the Extensions // field is ignored, see ExtraExtensions. Extensions []pkix.Extension // ExtraExtensions contains extensions to be copied, raw, into any // marshaled certificates. Values override any extensions that would // otherwise be produced based on the other fields. The ExtraExtensions // field is not populated when parsing certificates, see Extensions. ExtraExtensions []pkix.Extension // UnhandledCriticalExtensions contains a list of extension IDs that // were not (fully) processed when parsing. Verify will fail if this // slice is non-empty, unless verification is delegated to an OS // library which understands all the critical extensions. // // Users can access these extensions using Extensions and can remove // elements from this slice if they believe that they have been // handled. UnhandledCriticalExtensions []asn1.ObjectIdentifier ExtKeyUsage []ExtKeyUsage // Sequence of extended key usages. UnknownExtKeyUsage []asn1.ObjectIdentifier // Encountered extended key usages unknown to this package. BasicConstraintsValid bool // if true then the next two fields are valid. IsCA bool MaxPathLen int // MaxPathLenZero indicates that BasicConstraintsValid==true and // MaxPathLen==0 should be interpreted as an actual maximum path length // of zero. Otherwise, that combination is interpreted as MaxPathLen // not being set. MaxPathLenZero bool SubjectKeyId []byte AuthorityKeyId []byte // RFC 5280, 4.2.2.1 (Authority Information Access) OCSPServer []string IssuingCertificateURL []string // Subject Alternate Name values DNSNames []string EmailAddresses []string IPAddresses []net.IP // Name constraints PermittedDNSDomainsCritical bool // if true then the name constraints are marked critical. PermittedDNSDomains []string // CRL Distribution Points CRLDistributionPoints []string PolicyIdentifiers []asn1.ObjectIdentifier }
A Certificate represents an X.509 certificate.
func ParseCertificate ¶
func ParseCertificate(asn1Data []byte) (*Certificate, error)
ParseCertificate parses a single certificate from the given ASN.1 DER data.
func ParseCertificates ¶
func ParseCertificates(asn1Data []byte) ([]*Certificate, error)
ParseCertificates parses one or more certificates from the given ASN.1 DER data. The certificates must be concatenated with no intermediate padding.
func ReadCertificateFromPem ¶
func ReadCertificateFromPem(certPem []byte) (*Certificate, error)
func (*Certificate) CheckCRLSignature ¶
func (c *Certificate) CheckCRLSignature(crl *pkix.CertificateList) error
CheckCRLSignature checks that the signature in crl is from c.
func (*Certificate) CheckSignature ¶
func (c *Certificate) CheckSignature(algo SignatureAlgorithm, signed, signature []byte) error
CheckSignature verifies that signature is a valid signature over signed from c's public key.
func (*Certificate) CheckSignatureFrom ¶
func (c *Certificate) CheckSignatureFrom(parent *Certificate) error
CheckSignatureFrom verifies that the signature on c is a valid signature from parent.
func (*Certificate) CreateCRL ¶
func (c *Certificate) CreateCRL(rand io.Reader, priv interface{}, revokedCerts []pkix.RevokedCertificate, now, expiry time.Time) (crlBytes []byte, err error)
CreateCRL returns a DER encoded CRL, signed by this Certificate, that contains the given list of revoked certificates.
func (*Certificate) Equal ¶
func (c *Certificate) Equal(other *Certificate) bool
func (*Certificate) FromX509Certificate ¶
func (c *Certificate) FromX509Certificate(x509Cert *x509.Certificate)
func (*Certificate) ToX509Certificate ¶
func (c *Certificate) ToX509Certificate() *x509.Certificate
type CertificateRequest ¶
type CertificateRequest struct { Raw []byte // Complete ASN.1 DER content (CSR, signature algorithm and signature). RawTBSCertificateRequest []byte // Certificate request info part of raw ASN.1 DER content. RawSubjectPublicKeyInfo []byte // DER encoded SubjectPublicKeyInfo. RawSubject []byte // DER encoded Subject. Version int Signature []byte SignatureAlgorithm SignatureAlgorithm PublicKeyAlgorithm PublicKeyAlgorithm PublicKey interface{} Subject pkix.Name // Attributes is the dried husk of a bug and shouldn't be used. Attributes []pkix.AttributeTypeAndValueSET // Extensions contains raw X.509 extensions. When parsing CSRs, this // can be used to extract extensions that are not parsed by this // package. Extensions []pkix.Extension // ExtraExtensions contains extensions to be copied, raw, into any // marshaled CSR. Values override any extensions that would otherwise // be produced based on the other fields but are overridden by any // extensions specified in Attributes. // // The ExtraExtensions field is not populated when parsing CSRs, see // Extensions. ExtraExtensions []pkix.Extension // Subject Alternate Name values. DNSNames []string EmailAddresses []string IPAddresses []net.IP }
CertificateRequest represents a PKCS #10, certificate signature request.
func ParseCertificateRequest ¶
func ParseCertificateRequest(asn1Data []byte) (*CertificateRequest, error)
ParseCertificateRequest parses a single certificate request from the given ASN.1 DER data.
func ReadCertificateRequestFromPem ¶
func ReadCertificateRequestFromPem(certPem []byte) (*CertificateRequest, error)
func (*CertificateRequest) CheckSignature ¶
func (c *CertificateRequest) CheckSignature() error
CheckSignature reports whether the signature on c is valid.
type ConstraintViolationError ¶
type ConstraintViolationError struct{}
ConstraintViolationError results when a requested usage is not permitted by a certificate. For example: checking a signature when the public key isn't a certificate signing key.
func (ConstraintViolationError) Error ¶
func (ConstraintViolationError) Error() string
type EncryptedPrivateKeyInfo ¶
type EncryptedPrivateKeyInfo struct { EncryptionAlgorithm Pbes2Algorithms EncryptedData []byte }
reference to https://www.rfc-editor.org/rfc/rfc5958.txt
type ExtKeyUsage ¶
type ExtKeyUsage int
ExtKeyUsage represents an extended set of actions that are valid for a given key. Each of the ExtKeyUsage* constants define a unique action.
const ( ExtKeyUsageAny ExtKeyUsage = iota ExtKeyUsageServerAuth ExtKeyUsageClientAuth ExtKeyUsageCodeSigning ExtKeyUsageEmailProtection ExtKeyUsageIPSECEndSystem ExtKeyUsageIPSECTunnel ExtKeyUsageIPSECUser ExtKeyUsageTimeStamping ExtKeyUsageOCSPSigning ExtKeyUsageMicrosoftServerGatedCrypto ExtKeyUsageNetscapeServerGatedCrypto )
type Hash ¶
type Hash uint
const ( MD4 Hash = 1 + iota // import golang.org/x/crypto/md4 MD5 // import crypto/md5 SHA1 // import crypto/sha1 SHA224 // import crypto/sha256 SHA256 // import crypto/sha256 SHA384 // import crypto/sha512 SHA512 // import crypto/sha512 MD5SHA1 // no implementation; MD5+SHA1 used for TLS RSA RIPEMD160 // import golang.org/x/crypto/ripemd160 SHA3_224 // import golang.org/x/crypto/sha3 SHA3_256 // import golang.org/x/crypto/sha3 SHA3_384 // import golang.org/x/crypto/sha3 SHA3_512 // import golang.org/x/crypto/sha3 SHA512_224 // import crypto/sha512 SHA512_256 // import crypto/sha512 SM3 )
func (Hash) Available ¶
Available reports whether the given hash function is linked into the binary.
type InsecureAlgorithmError ¶
type InsecureAlgorithmError SignatureAlgorithm
An InsecureAlgorithmError
func (InsecureAlgorithmError) Error ¶
func (e InsecureAlgorithmError) Error() string
type KeyUsage ¶
type KeyUsage int
KeyUsage represents the set of actions that are valid for a given key. It's a bitmap of the KeyUsage* constants.
type Pbes2Algorithms ¶
type Pbes2Algorithms struct { IdPBES2 asn1.ObjectIdentifier Pbes2Params Pbes2Params }
reference to https://www.ietf.org/rfc/rfc2898.txt
type Pbes2Encs ¶
type Pbes2Encs struct { EncryAlgo asn1.ObjectIdentifier IV []byte }
type Pbes2KDfs ¶
type Pbes2KDfs struct { IdPBKDF2 asn1.ObjectIdentifier Pkdf2Params Pkdf2Params }
reference to https://www.ietf.org/rfc/rfc2898.txt
type Pbes2Params ¶
type Pbes2Params struct { KeyDerivationFunc Pbes2KDfs // PBES2-KDFs EncryptionScheme Pbes2Encs // PBES2-Encs }
reference to https://www.ietf.org/rfc/rfc2898.txt
type Pkdf2Params ¶
type Pkdf2Params struct { Salt []byte IterationCount int Prf pkix.AlgorithmIdentifier }
reference to https://www.ietf.org/rfc/rfc2898.txt
type PrivateKeyInfo ¶
type PrivateKeyInfo struct { Version int // v1 or v2 PrivateKeyAlgorithm []asn1.ObjectIdentifier PrivateKey []byte }
reference to https://www.rfc-editor.org/rfc/rfc5958.txt
type PublicKeyAlgorithm ¶
type PublicKeyAlgorithm int
const ( UnknownPublicKeyAlgorithm PublicKeyAlgorithm = iota RSA DSA ECDSA SM2 )
type SignatureAlgorithm ¶
type SignatureAlgorithm int
const ( UnknownSignatureAlgorithm SignatureAlgorithm = iota MD2WithRSA MD5WithRSA // SM3WithRSA reserve SHA1WithRSA SHA256WithRSA SHA384WithRSA SHA512WithRSA DSAWithSHA1 DSAWithSHA256 ECDSAWithSHA1 ECDSAWithSHA256 ECDSAWithSHA384 ECDSAWithSHA512 SHA256WithRSAPSS SHA384WithRSAPSS SHA512WithRSAPSS SM2WithSM3 SM2WithSHA1 SM2WithSHA256 )
func (SignatureAlgorithm) String ¶
func (algo SignatureAlgorithm) String() string
type UnhandledCriticalExtension ¶
type UnhandledCriticalExtension struct{}
func (UnhandledCriticalExtension) Error ¶
func (h UnhandledCriticalExtension) Error() string