|
@@ -0,0 +1,87 @@
|
|
|
|
|
+package scep
|
|
|
|
|
+
|
|
|
|
|
+import (
|
|
|
|
|
+ "crypto/rand"
|
|
|
|
|
+ "crypto/x509"
|
|
|
|
|
+ "crypto/x509/pkix"
|
|
|
|
|
+ "encoding/asn1"
|
|
|
|
|
+ "testing"
|
|
|
|
|
+
|
|
|
|
|
+ "github.com/stretchr/testify/assert"
|
|
|
|
|
+ "github.com/stretchr/testify/require"
|
|
|
|
|
+ "go.step.sm/crypto/keyutil"
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+// otherNameGN encodes an otherName GeneralName with the given type and an
|
|
|
|
|
+// already DER-encoded value.
|
|
|
|
|
+func otherNameGN(t *testing.T, typeID asn1.ObjectIdentifier, value []byte) asn1.RawValue {
|
|
|
|
|
+ t.Helper()
|
|
|
|
|
+ explicit, err := asn1.Marshal(asn1.RawValue{Class: asn1.ClassContextSpecific, Tag: 0, IsCompound: true, Bytes: value})
|
|
|
|
|
+ require.NoError(t, err)
|
|
|
|
|
+ oid, err := asn1.Marshal(typeID)
|
|
|
|
|
+ require.NoError(t, err)
|
|
|
|
|
+ return asn1.RawValue{Class: asn1.ClassContextSpecific, Tag: 0, IsCompound: true, Bytes: append(oid, explicit...)}
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func upnGN(t *testing.T, upn string) asn1.RawValue {
|
|
|
|
|
+ t.Helper()
|
|
|
|
|
+ v, err := asn1.MarshalWithParams(upn, "utf8")
|
|
|
|
|
+ require.NoError(t, err)
|
|
|
|
|
+ return otherNameGN(t, oidUserPrincipalName, v)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func emailGN(email string) asn1.RawValue {
|
|
|
|
|
+ return asn1.RawValue{Class: asn1.ClassContextSpecific, Tag: 1, Bytes: []byte(email)}
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// csrWithSANs returns a parsed CSR whose subjectAltName extension holds the
|
|
|
|
|
+// given GeneralNames, or no extension when there are none.
|
|
|
|
|
+func csrWithSANs(t *testing.T, gns ...asn1.RawValue) *x509.CertificateRequest {
|
|
|
|
|
+ t.Helper()
|
|
|
|
|
+ signer, err := keyutil.GenerateDefaultSigner()
|
|
|
|
|
+ require.NoError(t, err)
|
|
|
|
|
+ tmpl := &x509.CertificateRequest{Subject: pkix.Name{CommonName: "test-device"}}
|
|
|
|
|
+ if len(gns) > 0 {
|
|
|
|
|
+ value, err := asn1.Marshal(gns)
|
|
|
|
|
+ require.NoError(t, err)
|
|
|
|
|
+ tmpl.ExtraExtensions = []pkix.Extension{{Id: oidExtensionSubjectAltName, Value: value}}
|
|
|
|
|
+ }
|
|
|
|
|
+ der, err := x509.CreateCertificateRequest(rand.Reader, tmpl, signer)
|
|
|
|
|
+ require.NoError(t, err)
|
|
|
|
|
+ csr, err := x509.ParseCertificateRequest(der)
|
|
|
|
|
+ require.NoError(t, err)
|
|
|
|
|
+ return csr
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func Test_userPrincipalNames(t *testing.T) {
|
|
|
|
|
+ ia5, err := asn1.MarshalWithParams("john@corp.example.com", "ia5")
|
|
|
|
|
+ require.NoError(t, err)
|
|
|
|
|
+ emptyUTF8, err := asn1.MarshalWithParams("", "utf8")
|
|
|
|
|
+ require.NoError(t, err)
|
|
|
|
|
+ otherUTF8, err := asn1.MarshalWithParams("something", "utf8")
|
|
|
|
|
+ require.NoError(t, err)
|
|
|
|
|
+
|
|
|
|
|
+ tests := []struct {
|
|
|
|
|
+ name string
|
|
|
|
|
+ csr *x509.CertificateRequest
|
|
|
|
|
+ want []string
|
|
|
|
|
+ }{
|
|
|
|
|
+ {"no SAN extension", csrWithSANs(t), nil},
|
|
|
|
|
+ {"email only", csrWithSANs(t, emailGN("john@example.com")), nil},
|
|
|
|
|
+ {"one UPN", csrWithSANs(t, emailGN("john@example.com"), upnGN(t, "john@corp.example.com")),
|
|
|
|
|
+ []string{"john@corp.example.com"}},
|
|
|
|
|
+ {"several UPNs keep their order", csrWithSANs(t, upnGN(t, "a@corp.example.com"), emailGN("john@example.com"), upnGN(t, "b@corp.example.com")),
|
|
|
|
|
+ []string{"a@corp.example.com", "b@corp.example.com"}},
|
|
|
|
|
+ {"other otherName types are ignored", csrWithSANs(t,
|
|
|
|
|
+ otherNameGN(t, asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 8, 3}, otherUTF8), // permanentIdentifier OID
|
|
|
|
|
+ upnGN(t, "john@corp.example.com")),
|
|
|
|
|
+ []string{"john@corp.example.com"}},
|
|
|
|
|
+ {"UPN not a UTF8String is skipped", csrWithSANs(t, otherNameGN(t, oidUserPrincipalName, ia5)), nil},
|
|
|
|
|
+ {"empty UPN is skipped", csrWithSANs(t, otherNameGN(t, oidUserPrincipalName, emptyUTF8)), nil},
|
|
|
|
|
+ }
|
|
|
|
|
+ for _, tt := range tests {
|
|
|
|
|
+ t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
+ assert.Equal(t, tt.want, userPrincipalNames(tt.csr))
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+}
|