Commit 8bf6e09f authored by Adam Langley's avatar Adam Langley

Revert "crypto/x509: implement crypto.Signer"

This reverts commit cef15faa.

Change-Id: I6df3e9ea48cd58893892587dd5cd28c1eb759c48
Reviewed-on: https://go-review.googlesource.com/3090Reviewed-by: default avatarAdam Langley <agl@golang.org>
parent 4e7f0651
...@@ -12,7 +12,7 @@ import ( ...@@ -12,7 +12,7 @@ import (
"crypto/ecdsa" "crypto/ecdsa"
"crypto/elliptic" "crypto/elliptic"
"crypto/rsa" "crypto/rsa"
_ "crypto/sha1" "crypto/sha1"
_ "crypto/sha256" _ "crypto/sha256"
_ "crypto/sha512" _ "crypto/sha512"
"crypto/x509/pkix" "crypto/x509/pkix"
...@@ -1389,14 +1389,14 @@ func subjectBytes(cert *Certificate) ([]byte, error) { ...@@ -1389,14 +1389,14 @@ func subjectBytes(cert *Certificate) ([]byte, error) {
return asn1.Marshal(cert.Subject.ToRDNSequence()) return asn1.Marshal(cert.Subject.ToRDNSequence())
} }
// signingParamsForPublicKey returns the parameters to use for signing with // signingParamsForPrivateKey returns the parameters to use for signing with
// priv. If requestedSigAlgo is not zero then it overrides the default // priv. If requestedSigAlgo is not zero then it overrides the default
// signature algorithm. // signature algorithm.
func signingParamsForPublicKey(pub interface{}, requestedSigAlgo SignatureAlgorithm) (hashFunc crypto.Hash, sigAlgo pkix.AlgorithmIdentifier, err error) { func signingParamsForPrivateKey(priv interface{}, requestedSigAlgo SignatureAlgorithm) (hashFunc crypto.Hash, sigAlgo pkix.AlgorithmIdentifier, err error) {
var pubType PublicKeyAlgorithm var pubType PublicKeyAlgorithm
switch pub := pub.(type) { switch priv := priv.(type) {
case *rsa.PublicKey: case *rsa.PrivateKey:
pubType = RSA pubType = RSA
hashFunc = crypto.SHA256 hashFunc = crypto.SHA256
sigAlgo.Algorithm = oidSignatureSHA256WithRSA sigAlgo.Algorithm = oidSignatureSHA256WithRSA
...@@ -1404,10 +1404,10 @@ func signingParamsForPublicKey(pub interface{}, requestedSigAlgo SignatureAlgori ...@@ -1404,10 +1404,10 @@ func signingParamsForPublicKey(pub interface{}, requestedSigAlgo SignatureAlgori
Tag: 5, Tag: 5,
} }
case *ecdsa.PublicKey: case *ecdsa.PrivateKey:
pubType = ECDSA pubType = ECDSA
switch pub.Curve { switch priv.Curve {
case elliptic.P224(), elliptic.P256(): case elliptic.P224(), elliptic.P256():
hashFunc = crypto.SHA256 hashFunc = crypto.SHA256
sigAlgo.Algorithm = oidSignatureECDSAWithSHA256 sigAlgo.Algorithm = oidSignatureECDSAWithSHA256
...@@ -1422,7 +1422,7 @@ func signingParamsForPublicKey(pub interface{}, requestedSigAlgo SignatureAlgori ...@@ -1422,7 +1422,7 @@ func signingParamsForPublicKey(pub interface{}, requestedSigAlgo SignatureAlgori
} }
default: default:
err = errors.New("x509: only RSA and ECDSA keys supported") err = errors.New("x509: only RSA and ECDSA private keys supported")
} }
if err != nil { if err != nil {
...@@ -1469,10 +1469,10 @@ func signingParamsForPublicKey(pub interface{}, requestedSigAlgo SignatureAlgori ...@@ -1469,10 +1469,10 @@ func signingParamsForPublicKey(pub interface{}, requestedSigAlgo SignatureAlgori
// //
// The returned slice is the certificate in DER encoding. // The returned slice is the certificate in DER encoding.
// //
// All keys types that are implemented via crypto.Signer are supported (This // The only supported key types are RSA and ECDSA (*rsa.PublicKey or
// includes *rsa.PublicKey and *ecdsa.PublicKey.) // *ecdsa.PublicKey for pub, *rsa.PrivateKey or *ecdsa.PrivateKey for priv).
func CreateCertificate(rand io.Reader, template, parent *Certificate, pub interface{}, priv crypto.Signer) (cert []byte, err error) { func CreateCertificate(rand io.Reader, template, parent *Certificate, pub interface{}, priv interface{}) (cert []byte, err error) {
hashFunc, signatureAlgorithm, err := signingParamsForPublicKey(priv.Public(), template.SignatureAlgorithm) hashFunc, signatureAlgorithm, err := signingParamsForPrivateKey(priv, template.SignatureAlgorithm)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -1482,6 +1482,10 @@ func CreateCertificate(rand io.Reader, template, parent *Certificate, pub interf ...@@ -1482,6 +1482,10 @@ func CreateCertificate(rand io.Reader, template, parent *Certificate, pub interf
return nil, err return nil, err
} }
if err != nil {
return
}
if len(parent.SubjectKeyId) > 0 { if len(parent.SubjectKeyId) > 0 {
template.AuthorityKeyId = parent.SubjectKeyId template.AuthorityKeyId = parent.SubjectKeyId
} }
...@@ -1525,7 +1529,19 @@ func CreateCertificate(rand io.Reader, template, parent *Certificate, pub interf ...@@ -1525,7 +1529,19 @@ func CreateCertificate(rand io.Reader, template, parent *Certificate, pub interf
digest := h.Sum(nil) digest := h.Sum(nil)
var signature []byte var signature []byte
signature, err = priv.Sign(rand, digest, hashFunc)
switch priv := priv.(type) {
case *rsa.PrivateKey:
signature, err = rsa.SignPKCS1v15(rand, priv, hashFunc, digest)
case *ecdsa.PrivateKey:
var r, s *big.Int
if r, s, err = ecdsa.Sign(rand, priv, digest); err == nil {
signature, err = asn1.Marshal(ecdsaSignature{r, s})
}
default:
panic("internal error")
}
if err != nil { if err != nil {
return return
} }
...@@ -1572,15 +1588,18 @@ func ParseDERCRL(derBytes []byte) (certList *pkix.CertificateList, err error) { ...@@ -1572,15 +1588,18 @@ func ParseDERCRL(derBytes []byte) (certList *pkix.CertificateList, err error) {
// CreateCRL returns a DER encoded CRL, signed by this Certificate, that // CreateCRL returns a DER encoded CRL, signed by this Certificate, that
// contains the given list of revoked certificates. // contains the given list of revoked certificates.
func (c *Certificate) CreateCRL(rand io.Reader, priv crypto.Signer, revokedCerts []pkix.RevokedCertificate, now, expiry time.Time) (crlBytes []byte, err error) { //
hashFunc, signatureAlgorithm, err := signingParamsForPublicKey(priv.Public(), 0) // The only supported key type is RSA (*rsa.PrivateKey for priv).
if err != nil { func (c *Certificate) CreateCRL(rand io.Reader, priv interface{}, revokedCerts []pkix.RevokedCertificate, now, expiry time.Time) (crlBytes []byte, err error) {
return nil, err rsaPriv, ok := priv.(*rsa.PrivateKey)
if !ok {
return nil, errors.New("x509: non-RSA private keys not supported")
} }
tbsCertList := pkix.TBSCertificateList{ tbsCertList := pkix.TBSCertificateList{
Version: 1, Version: 1,
Signature: signatureAlgorithm, Signature: pkix.AlgorithmIdentifier{
Algorithm: oidSignatureSHA1WithRSA,
},
Issuer: c.Subject.ToRDNSequence(), Issuer: c.Subject.ToRDNSequence(),
ThisUpdate: now.UTC(), ThisUpdate: now.UTC(),
NextUpdate: expiry.UTC(), NextUpdate: expiry.UTC(),
...@@ -1603,20 +1622,21 @@ func (c *Certificate) CreateCRL(rand io.Reader, priv crypto.Signer, revokedCerts ...@@ -1603,20 +1622,21 @@ func (c *Certificate) CreateCRL(rand io.Reader, priv crypto.Signer, revokedCerts
return return
} }
h := hashFunc.New() h := sha1.New()
h.Write(tbsCertListContents) h.Write(tbsCertListContents)
digest := h.Sum(nil) digest := h.Sum(nil)
var signature []byte signature, err := rsa.SignPKCS1v15(rand, rsaPriv, crypto.SHA1, digest)
signature, err = priv.Sign(rand, digest, hashFunc)
if err != nil { if err != nil {
return return
} }
return asn1.Marshal(pkix.CertificateList{ return asn1.Marshal(pkix.CertificateList{
TBSCertList: tbsCertList, TBSCertList: tbsCertList,
SignatureAlgorithm: signatureAlgorithm, SignatureAlgorithm: pkix.AlgorithmIdentifier{
SignatureValue: asn1.BitString{Bytes: signature, BitLength: len(signature) * 8}, Algorithm: oidSignatureSHA1WithRSA,
},
SignatureValue: asn1.BitString{Bytes: signature, BitLength: len(signature) * 8},
}) })
} }
...@@ -1690,19 +1710,26 @@ var oidExtensionRequest = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 14} ...@@ -1690,19 +1710,26 @@ var oidExtensionRequest = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 14}
// //
// The returned slice is the certificate request in DER encoding. // The returned slice is the certificate request in DER encoding.
// //
// All keys types that are implemented via crypto.Signer are supported (This // The only supported key types are RSA (*rsa.PrivateKey) and ECDSA
// includes *rsa.PublicKey and *ecdsa.PublicKey.) // (*ecdsa.PrivateKey).
func CreateCertificateRequest(rand io.Reader, template *CertificateRequest, priv crypto.Signer) (csr []byte, err error) { func CreateCertificateRequest(rand io.Reader, template *CertificateRequest, priv interface{}) (csr []byte, err error) {
var hashFunc crypto.Hash hashFunc, sigAlgo, err := signingParamsForPrivateKey(priv, template.SignatureAlgorithm)
var sigAlgo pkix.AlgorithmIdentifier
hashFunc, sigAlgo, err = signingParamsForPublicKey(priv.Public(), template.SignatureAlgorithm)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var publicKeyBytes []byte var publicKeyBytes []byte
var publicKeyAlgorithm pkix.AlgorithmIdentifier var publicKeyAlgorithm pkix.AlgorithmIdentifier
publicKeyBytes, publicKeyAlgorithm, err = marshalPublicKey(priv.Public())
switch priv := priv.(type) {
case *rsa.PrivateKey:
publicKeyBytes, publicKeyAlgorithm, err = marshalPublicKey(&priv.PublicKey)
case *ecdsa.PrivateKey:
publicKeyBytes, publicKeyAlgorithm, err = marshalPublicKey(&priv.PublicKey)
default:
panic("internal error")
}
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -1814,7 +1841,18 @@ func CreateCertificateRequest(rand io.Reader, template *CertificateRequest, priv ...@@ -1814,7 +1841,18 @@ func CreateCertificateRequest(rand io.Reader, template *CertificateRequest, priv
digest := h.Sum(nil) digest := h.Sum(nil)
var signature []byte var signature []byte
signature, err = priv.Sign(rand, digest, hashFunc) switch priv := priv.(type) {
case *rsa.PrivateKey:
signature, err = rsa.SignPKCS1v15(rand, priv, hashFunc, digest)
case *ecdsa.PrivateKey:
var r, s *big.Int
if r, s, err = ecdsa.Sign(rand, priv, digest); err == nil {
signature, err = asn1.Marshal(ecdsaSignature{r, s})
}
default:
panic("internal error")
}
if err != nil { if err != nil {
return return
} }
......
...@@ -6,7 +6,6 @@ package x509 ...@@ -6,7 +6,6 @@ package x509
import ( import (
"bytes" "bytes"
"crypto"
"crypto/dsa" "crypto/dsa"
"crypto/ecdsa" "crypto/ecdsa"
"crypto/elliptic" "crypto/elliptic"
...@@ -305,11 +304,10 @@ func TestCreateSelfSignedCertificate(t *testing.T) { ...@@ -305,11 +304,10 @@ func TestCreateSelfSignedCertificate(t *testing.T) {
} }
tests := []struct { tests := []struct {
name string name string
pub interface{} pub, priv interface{}
priv crypto.Signer checkSig bool
checkSig bool sigAlgo SignatureAlgorithm
sigAlgo SignatureAlgorithm
}{ }{
{"RSA/RSA", &rsaPriv.PublicKey, rsaPriv, true, SHA1WithRSA}, {"RSA/RSA", &rsaPriv.PublicKey, rsaPriv, true, SHA1WithRSA},
{"RSA/ECDSA", &rsaPriv.PublicKey, ecdsaPriv, false, ECDSAWithSHA384}, {"RSA/ECDSA", &rsaPriv.PublicKey, ecdsaPriv, false, ECDSAWithSHA384},
...@@ -781,7 +779,7 @@ func TestCreateCertificateRequest(t *testing.T) { ...@@ -781,7 +779,7 @@ func TestCreateCertificateRequest(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
priv crypto.Signer priv interface{}
sigAlgo SignatureAlgorithm sigAlgo SignatureAlgorithm
}{ }{
{"RSA", rsaPriv, SHA1WithRSA}, {"RSA", rsaPriv, SHA1WithRSA},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment