2018-03-19 20:53:57 +00:00
|
|
|
package connect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
2018-03-21 22:54:51 +00:00
|
|
|
"strings"
|
2018-03-19 20:53:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ParseCert parses the x509 certificate from a PEM-encoded value.
|
|
|
|
func ParseCert(pemValue string) (*x509.Certificate, error) {
|
2018-03-24 18:27:44 +00:00
|
|
|
// The _ result below is not an error but the remaining PEM bytes.
|
2018-03-19 20:53:57 +00:00
|
|
|
block, _ := pem.Decode([]byte(pemValue))
|
|
|
|
if block == nil {
|
|
|
|
return nil, fmt.Errorf("no PEM-encoded data found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if block.Type != "CERTIFICATE" {
|
|
|
|
return nil, fmt.Errorf("first PEM-block should be CERTIFICATE type")
|
|
|
|
}
|
|
|
|
|
|
|
|
return x509.ParseCertificate(block.Bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseSigner parses a crypto.Signer from a PEM-encoded key. The private key
|
|
|
|
// is expected to be the first block in the PEM value.
|
|
|
|
func ParseSigner(pemValue string) (crypto.Signer, error) {
|
2018-03-24 18:27:44 +00:00
|
|
|
// The _ result below is not an error but the remaining PEM bytes.
|
2018-03-19 20:53:57 +00:00
|
|
|
block, _ := pem.Decode([]byte(pemValue))
|
|
|
|
if block == nil {
|
|
|
|
return nil, fmt.Errorf("no PEM-encoded data found")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch block.Type {
|
|
|
|
case "EC PRIVATE KEY":
|
|
|
|
return x509.ParseECPrivateKey(block.Bytes)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown PEM block type for signing key: %s", block.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-19 21:36:17 +00:00
|
|
|
// ParseCSR parses a CSR from a PEM-encoded value. The certificate request
|
|
|
|
// must be the the first block in the PEM value.
|
|
|
|
func ParseCSR(pemValue string) (*x509.CertificateRequest, error) {
|
2018-03-24 18:27:44 +00:00
|
|
|
// The _ result below is not an error but the remaining PEM bytes.
|
2018-03-19 21:36:17 +00:00
|
|
|
block, _ := pem.Decode([]byte(pemValue))
|
|
|
|
if block == nil {
|
|
|
|
return nil, fmt.Errorf("no PEM-encoded data found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if block.Type != "CERTIFICATE REQUEST" {
|
|
|
|
return nil, fmt.Errorf("first PEM-block should be CERTIFICATE REQUEST type")
|
|
|
|
}
|
|
|
|
|
|
|
|
return x509.ParseCertificateRequest(block.Bytes)
|
|
|
|
}
|
|
|
|
|
2018-03-19 20:53:57 +00:00
|
|
|
// KeyId returns a x509 KeyId from the given signing key. The key must be
|
2018-03-24 18:27:44 +00:00
|
|
|
// an *ecdsa.PublicKey currently, but may support more types in the future.
|
2018-03-19 20:53:57 +00:00
|
|
|
func KeyId(raw interface{}) ([]byte, error) {
|
|
|
|
pub, ok := raw.(*ecdsa.PublicKey)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("invalid key type: %T", raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is not standard; RFC allows any unique identifier as long as they
|
|
|
|
// match in subject/authority chains but suggests specific hashing of DER
|
2018-03-24 18:27:44 +00:00
|
|
|
// bytes of public key including DER tags.
|
|
|
|
bs, err := x509.MarshalPKIXPublicKey(pub)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// String formatted
|
|
|
|
kID := sha256.Sum256(bs)
|
|
|
|
return []byte(strings.Replace(fmt.Sprintf("% x", kID), " ", ":", -1)), nil
|
2018-03-19 20:53:57 +00:00
|
|
|
}
|
2018-03-21 22:54:51 +00:00
|
|
|
|
|
|
|
// HexString returns a standard colon-separated hex value for the input
|
|
|
|
// byte slice. This should be used with cert serial numbers and so on.
|
|
|
|
func HexString(input []byte) string {
|
|
|
|
return strings.Replace(fmt.Sprintf("% x", input), " ", ":", -1)
|
|
|
|
}
|