Minor bug fixes.
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
package certlib
|
package certlib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"errors"
|
"errors"
|
||||||
@@ -13,6 +15,7 @@ import (
|
|||||||
// ReadCertificate reads a DER or PEM-encoded certificate from the
|
// ReadCertificate reads a DER or PEM-encoded certificate from the
|
||||||
// byte slice.
|
// byte slice.
|
||||||
func ReadCertificate(in []byte) (*x509.Certificate, []byte, error) {
|
func ReadCertificate(in []byte) (*x509.Certificate, []byte, error) {
|
||||||
|
in = bytes.TrimSpace(in)
|
||||||
if len(in) == 0 {
|
if len(in) == 0 {
|
||||||
return nil, nil, certerr.ParsingError(certerr.ErrorSourceCertificate, certerr.ErrEmptyCertificate)
|
return nil, nil, certerr.ParsingError(certerr.ErrorSourceCertificate, certerr.ErrEmptyCertificate)
|
||||||
}
|
}
|
||||||
@@ -24,10 +27,10 @@ func ReadCertificate(in []byte) (*x509.Certificate, []byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
rest := remaining
|
rest := remaining
|
||||||
if p.Type != "CERTIFICATE" {
|
if p.Type != pemTypeCertificate {
|
||||||
return nil, rest, certerr.ParsingError(
|
return nil, rest, certerr.ParsingError(
|
||||||
certerr.ErrorSourceCertificate,
|
certerr.ErrorSourceCertificate,
|
||||||
certerr.ErrInvalidPEMType(p.Type, "CERTIFICATE"),
|
certerr.ErrInvalidPEMType(p.Type, pemTypeCertificate),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,3 +112,12 @@ func PoolFromBytes(certBytes []byte) (*x509.CertPool, error) {
|
|||||||
|
|
||||||
return pool, nil
|
return pool, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExportPrivateKeyPEM(priv crypto.PrivateKey) ([]byte, error) {
|
||||||
|
keyDER, err := x509.MarshalPKCS8PrivateKey(priv)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return pem.EncodeToMemory(&pem.Block{Type: pemTypePrivateKey, Bytes: keyDER}), nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -75,6 +75,11 @@ var DelegationExtension = pkix.Extension{
|
|||||||
Value: []byte{0x05, 0x00}, // ASN.1 NULL
|
Value: []byte{0x05, 0x00}, // ASN.1 NULL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
pemTypeCertificate = "CERTIFICATE"
|
||||||
|
pemTypePrivateKey = "PRIVATE KEY"
|
||||||
|
)
|
||||||
|
|
||||||
// InclusiveDate returns the time.Time representation of a date - 1
|
// InclusiveDate returns the time.Time representation of a date - 1
|
||||||
// nanosecond. This allows time.After to be used inclusively.
|
// nanosecond. This allows time.After to be used inclusively.
|
||||||
func InclusiveDate(year int, month time.Month, day int) time.Time {
|
func InclusiveDate(year int, month time.Month, day int) time.Time {
|
||||||
@@ -246,7 +251,7 @@ func EncodeCertificatesPEM(certs []*x509.Certificate) []byte {
|
|||||||
var buffer bytes.Buffer
|
var buffer bytes.Buffer
|
||||||
for _, cert := range certs {
|
for _, cert := range certs {
|
||||||
if err := pem.Encode(&buffer, &pem.Block{
|
if err := pem.Encode(&buffer, &pem.Block{
|
||||||
Type: "CERTIFICATE",
|
Type: pemTypeCertificate,
|
||||||
Bytes: cert.Raw,
|
Bytes: cert.Raw,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user