2018-08-13 09:39:45 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package peertls
|
|
|
|
|
|
|
|
// Many cryptography standards use ASN.1 to define their data structures,
|
|
|
|
// and Distinguished Encoding Rules (DER) to serialize those structures.
|
|
|
|
// Because DER produces binary output, it can be challenging to transmit
|
|
|
|
// the resulting files through systems, like electronic mail, that only
|
|
|
|
// support ASCII. The PEM format solves this problem by encoding the
|
|
|
|
// binary data using base64.
|
|
|
|
// (see https://en.wikipedia.org/wiki/Privacy-enhanced_Electronic_Mail)
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/asn1"
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
)
|
|
|
|
|
2018-11-01 15:48:43 +00:00
|
|
|
// ECDSASignature holds the `r` and `s` values in an ecdsa signature
|
|
|
|
// (see https://golang.org/pkg/crypto/ecdsa)
|
|
|
|
type ECDSASignature struct {
|
2018-08-13 09:39:45 +01:00
|
|
|
R, S *big.Int
|
|
|
|
}
|
|
|
|
|
|
|
|
var authECCurve = elliptic.P256()
|
|
|
|
|
|
|
|
func parseCertificateChains(rawCerts [][]byte) ([]*x509.Certificate, error) {
|
|
|
|
parsedCerts, err := parseCerts(rawCerts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsedCerts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseCerts(rawCerts [][]byte) ([]*x509.Certificate, error) {
|
|
|
|
certs := make([]*x509.Certificate, len(rawCerts))
|
|
|
|
for i, c := range rawCerts {
|
|
|
|
var err error
|
|
|
|
certs[i], err = x509.ParseCertificate(c)
|
|
|
|
if err != nil {
|
2018-10-26 14:52:37 +01:00
|
|
|
return nil, ErrParseCerts.New("unable to parse certificate at index %d", i)
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return certs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyChainSignatures(certs []*x509.Certificate) error {
|
|
|
|
for i, cert := range certs {
|
|
|
|
j := len(certs)
|
|
|
|
if i+1 < j {
|
2018-10-26 14:52:37 +01:00
|
|
|
err := verifyCertSignature(certs[i+1], cert)
|
2018-08-13 09:39:45 +01:00
|
|
|
if err != nil {
|
2018-10-26 14:52:37 +01:00
|
|
|
return ErrVerifyCertificateChain.Wrap(err)
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-10-26 14:52:37 +01:00
|
|
|
err := verifyCertSignature(cert, cert)
|
2018-08-13 09:39:45 +01:00
|
|
|
if err != nil {
|
2018-10-26 14:52:37 +01:00
|
|
|
return ErrVerifyCertificateChain.Wrap(err)
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-26 14:52:37 +01:00
|
|
|
func verifyCertSignature(parentCert, childCert *x509.Certificate) error {
|
2018-11-01 15:48:43 +00:00
|
|
|
return verifySignature(childCert.Signature, childCert.RawTBSCertificate, parentCert.PublicKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifySignature(signedData []byte, data []byte, pubKey crypto.PublicKey) error {
|
|
|
|
key, ok := pubKey.(*ecdsa.PublicKey)
|
2018-08-27 23:23:48 +01:00
|
|
|
if !ok {
|
2018-11-01 15:48:43 +00:00
|
|
|
return ErrUnsupportedKey.New("%T", key)
|
2018-08-27 23:23:48 +01:00
|
|
|
}
|
|
|
|
|
2018-11-01 15:48:43 +00:00
|
|
|
signature := new(ECDSASignature)
|
2018-08-13 09:39:45 +01:00
|
|
|
|
2018-11-01 15:48:43 +00:00
|
|
|
if _, err := asn1.Unmarshal(signedData, signature); err != nil {
|
2018-10-26 14:52:37 +01:00
|
|
|
return ErrVerifySignature.New("unable to unmarshal ecdsa signature: %v", err)
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
h := crypto.SHA256.New()
|
2018-11-01 15:48:43 +00:00
|
|
|
_, err := h.Write(data)
|
2018-08-13 09:39:45 +01:00
|
|
|
if err != nil {
|
2018-10-26 14:52:37 +01:00
|
|
|
return ErrVerifySignature.Wrap(err)
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
|
|
|
digest := h.Sum(nil)
|
|
|
|
|
2018-11-01 15:48:43 +00:00
|
|
|
if !ecdsa.Verify(key, digest, signature.R, signature.S) {
|
2018-10-26 14:52:37 +01:00
|
|
|
return ErrVerifySignature.New("signature is not valid")
|
|
|
|
}
|
|
|
|
return nil
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func newSerialNumber() (*big.Int, error) {
|
|
|
|
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
|
|
|
|
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.New("failed to generateServerTls serial number: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return serialNumber, nil
|
|
|
|
}
|