]> Cypherpunks repositories - gostls13.git/commitdiff
crypto/x509, crypto/tls: improve root matching and observe CA flag.
authorAdam Langley <agl@golang.org>
Mon, 20 Sep 2010 16:17:31 +0000 (12:17 -0400)
committerAdam Langley <agl@golang.org>
Mon, 20 Sep 2010 16:17:31 +0000 (12:17 -0400)
The key/value format of X.500 names means that it's possible to encode
a name with multiple values for, say, organisation. RFC5280
doesn't seem to consider this, but there are Verisign root
certificates which do this and, in order to find the correct
root certificate in some cases, we need to handle it.

Also, CA certificates should set the CA flag and we now check
this. After looking at the other X.509 extensions it appears
that they are universally ignored/bit rotted away so we ignore
them.

R=rsc
CC=golang-dev
https://golang.org/cl/2249042

src/pkg/crypto/tls/ca_set.go
src/pkg/crypto/tls/handshake_client.go
src/pkg/crypto/x509/x509.go
src/pkg/crypto/x509/x509_test.go

index 7f7566e4609a46ee382dc9234dc448a424eb13e0..d2fb69ced8126fe27ace478d2948a48e6c2698aa 100644 (file)
@@ -7,6 +7,7 @@ package tls
 import (
        "crypto/x509"
        "encoding/pem"
+       "strings"
 )
 
 // A CASet is a set of certificates.
@@ -23,7 +24,7 @@ func NewCASet() *CASet {
 }
 
 func nameToKey(name *x509.Name) string {
-       return name.Country + "/" + name.Organization + "/" + name.OrganizationalUnit + "/" + name.CommonName
+       return strings.Join(name.Country, ",") + "/" + strings.Join(name.Organization, ",") + "/" + strings.Join(name.OrganizationalUnit, ",") + "/" + name.CommonName
 }
 
 // FindParent attempts to find the certificate in s which signs the given
index c629920648e99c71219c4b7d440758079e4f1878..a37fc78ccaf1620fba1da93c56903967906f8431 100644 (file)
@@ -84,8 +84,30 @@ func (c *Conn) clientHandshake() os.Error {
                certs[i] = cert
        }
 
-       // TODO(agl): do better validation of certs: max path length, name restrictions etc.
        for i := 1; i < len(certs); i++ {
+               if !certs[i].BasicConstraintsValid || !certs[i].IsCA {
+                       return c.sendAlert(alertBadCertificate)
+               }
+               // KeyUsage status flags are ignored. From Engineering
+               // Security, Peter Gutmann:
+               // A European government CA marked its signing certificates as
+               // being valid for encryption only, but no-one noticed. Another
+               // European CA marked its signature keys as not being valid for
+               // signatures. A different CA marked its own trusted root
+               // certificate as being invalid for certificate signing.
+               // Another national CA distributed a certificate to be used to
+               // encrypt data for the country’s tax authority that was marked
+               // as only being usable for digital signatures but not for
+               // encryption. Yet another CA reversed the order of the bit
+               // flags in the keyUsage due to confusion over encoding
+               // endianness, essentially setting a random keyUsage in
+               // certificates that it issued. Another CA created a
+               // self-invalidating certificate by adding a certificate policy
+               // statement stipulating that the certificate had to be used
+               // strictly as specified in the keyUsage, and a keyUsage
+               // containing a flag indicating that the RSA encryption key
+               // could only be used for Diffie-Hellman key agreement.
+
                if err := certs[i-1].CheckSignatureFrom(certs[i]); err != nil {
                        return c.sendAlert(alertBadCertificate)
                }
index 3d940e585b4e85c8365abeb2991ab7e38330106e..1fd5f6ca0fec8732250665626d65db242d2b3597 100644 (file)
@@ -162,9 +162,10 @@ const (
 // Name represents an X.509 distinguished name. This only includes the common
 // elements of a DN.  Additional elements in the name are ignored.
 type Name struct {
-       Country, Organization, OrganizationalUnit string
-       CommonName, SerialNumber, Locality        string
-       Province, StreetAddress, PostalCode       string
+       Country, Organization, OrganizationalUnit []string
+       Locality, Province                        []string
+       StreetAddress, PostalCode                 []string
+       SerialNumber, CommonName                  string
 }
 
 func (n *Name) fillFromRDNSequence(rdns *rdnSequence) {
@@ -186,19 +187,19 @@ func (n *Name) fillFromRDNSequence(rdns *rdnSequence) {
                        case 5:
                                n.SerialNumber = value
                        case 6:
-                               n.Country = value
+                               n.Country = appendString(n.Country, value)
                        case 7:
-                               n.Locality = value
+                               n.Locality = appendString(n.Locality, value)
                        case 8:
-                               n.Province = value
+                               n.Province = appendString(n.Province, value)
                        case 9:
-                               n.StreetAddress = value
+                               n.StreetAddress = appendString(n.StreetAddress, value)
                        case 10:
-                               n.Organization = value
+                               n.Organization = appendString(n.Organization, value)
                        case 11:
-                               n.OrganizationalUnit = value
+                               n.OrganizationalUnit = appendString(n.OrganizationalUnit, value)
                        case 17:
-                               n.PostalCode = value
+                               n.PostalCode = appendString(n.PostalCode, value)
                        }
                }
        }
index 12292c1b2e86249efb6b3c395350c899ed22232b..8a024528bd50bbb80190161f53b2e49a02bccf7e 100644 (file)
@@ -158,7 +158,7 @@ func TestCreateSelfSignedCertificate(t *testing.T) {
                SerialNumber: []byte{1},
                Subject: Name{
                        CommonName:   "test.example.com",
-                       Organization: "Acme Co",
+                       Organization: []string{"Acme Co"},
                },
                NotBefore: time.SecondsToUTC(1000),
                NotAfter:  time.SecondsToUTC(100000),