mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 17:05:18 +00:00
f429c6db49
This PR adds support to parse Azure JWT tokens, and it also adds the STS endpoint ([Section 3.2](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-mde2/27ed8c2c-0140-41ce-b2fa-c3d1a793ab4a) on the MS-MDE2 spec) This relates to #12614 and #12613 # Checklist for submitter If some of the following don't apply, delete the relevant line. - [X] Changes file added for user-visible changes in `changes/` or `orbit/changes/`. See [Changes files](https://fleetdm.com/docs/contributing/committing-changes#changes-files) for more information. - [X] Added/updated tests - [X] Manual QA for all new/changed functionality
197 lines
6.7 KiB
Go
197 lines
6.7 KiB
Go
package microsoft_mdm
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha1" //nolint:gosec
|
|
"crypto/x509"
|
|
"encoding/hex"
|
|
"errors"
|
|
"math/big"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/fleetdm/fleet/v4/server"
|
|
"github.com/micromdm/nanomdm/cryptoutil"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type mockStore struct{}
|
|
|
|
func (m *mockStore) WSTEPStoreCertificate(ctx context.Context, name string, crt *x509.Certificate) error {
|
|
return nil
|
|
}
|
|
|
|
func (m *mockStore) WSTEPNewSerial(ctx context.Context) (*big.Int, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (m *mockStore) WSTEPAssociateCertHash(ctx context.Context, deviceUUID string, hash string) error {
|
|
return nil
|
|
}
|
|
|
|
var _ CertStore = (*mockStore)(nil)
|
|
|
|
func TestNewCertManager(t *testing.T) {
|
|
var store CertStore
|
|
|
|
wantCert, err := cryptoutil.DecodePEMCertificate(testCert)
|
|
require.NoError(t, err)
|
|
wantKey, err := server.DecodePrivateKeyPEM(testKey)
|
|
require.NoError(t, err)
|
|
wantIdentityFingerprint := CertFingerprintHexStr(wantCert)
|
|
|
|
// Test that NewCertManager returns an error if the cert PEM is invalid.
|
|
_, err = NewCertManager(store, []byte("invalid"), testKey)
|
|
require.Error(t, err)
|
|
require.ErrorContains(t, err, "failed to decode PEM certificate")
|
|
|
|
// Test that NewCertManager returns an error if the key PEM is invalid.
|
|
_, err = NewCertManager(store, testCert, []byte("invalid"))
|
|
require.Error(t, err)
|
|
require.ErrorContains(t, err, "decode private key: no PEM-encoded data found")
|
|
|
|
// Test that NewCertManager returns an error if the cert PEM is not a certificate.
|
|
_, err = NewCertManager(store, testKey, testKey)
|
|
require.Error(t, err)
|
|
require.ErrorContains(t, err, "failed to decode PEM certificate")
|
|
|
|
// Test that NewCertManager returns an error if the key PEM is not a private key.
|
|
_, err = NewCertManager(store, testCert, testCert)
|
|
require.Error(t, err)
|
|
require.ErrorContains(t, err, "decode private key: unexpected block type")
|
|
|
|
// Test that NewCertManager returns a *WSTEPDepot if the cert and key PEMs are valid.
|
|
cm, err := NewCertManager(store, testCert, testKey)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, cm)
|
|
require.Equal(t, wantIdentityFingerprint, cm.IdentityFingerprint())
|
|
|
|
// Test that newManager sets the correct fields.
|
|
m := cm.(*manager)
|
|
require.NoError(t, err)
|
|
require.Equal(t, *wantCert, *m.identityCert)
|
|
require.NoError(t, err)
|
|
require.Equal(t, *wantKey, *m.identityPrivateKey)
|
|
require.Equal(t, wantIdentityFingerprint, m.identityFingerprint)
|
|
}
|
|
|
|
func TestSTSTokenSigningAndVerification(t *testing.T) {
|
|
var store CertStore
|
|
|
|
cm, err := NewCertManager(store, testCert, testKey)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, cm)
|
|
|
|
// Get a New STS Auth token
|
|
upnEmail := "test@email.com"
|
|
stsToken, err := cm.NewSTSAuthToken(upnEmail)
|
|
require.NoError(t, err)
|
|
require.NotEmpty(t, stsToken)
|
|
|
|
// Verify the STS Auth token
|
|
upnToken, err := cm.GetSTSAuthTokenUPNClaim(stsToken)
|
|
require.NoError(t, err)
|
|
require.NotEmpty(t, upnToken)
|
|
require.Equal(t, upnEmail, upnToken)
|
|
|
|
// New invalid STS Auth token
|
|
_, err = cm.NewSTSAuthToken("")
|
|
require.ErrorContains(t, err, "invalid upn field")
|
|
}
|
|
|
|
func TestCertFingerprintHexStr(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
cert []byte
|
|
err error
|
|
}{
|
|
{
|
|
name: "valid cert",
|
|
cert: testCert,
|
|
err: nil,
|
|
},
|
|
{
|
|
name: "invalid cert",
|
|
cert: []byte("invalid"),
|
|
err: errors.New("failed to decode PEM certificate"),
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
cert, err := cryptoutil.DecodePEMCertificate(tc.cert)
|
|
if tc.err != nil {
|
|
require.Error(t, err)
|
|
require.ErrorContains(t, err, tc.err.Error())
|
|
return
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
csum := sha1.Sum(cert.Raw) // nolint:gosec
|
|
want := strings.ToUpper(hex.EncodeToString(csum[:]))
|
|
fp := CertFingerprintHexStr(cert)
|
|
require.Equal(t, want, fp)
|
|
})
|
|
}
|
|
}
|
|
|
|
var (
|
|
testCert = []byte(`-----BEGIN CERTIFICATE-----
|
|
MIIDGzCCAgOgAwIBAgIBATANBgkqhkiG9w0BAQsFADAvMQkwBwYD
|
|
VQQGEwAxEDAOBgNVBAoTB3NjZXAtY2ExEDAOBgNVBAsTB1NDRVAg
|
|
Q0EwHhcNMjIxMjIyMTM0NDMzWhcNMzIxMjIyMTM0NDMzWjAvMQkw
|
|
BwYDVQQGEwAxEDAOBgNVBAoTB3NjZXAtY2ExEDAOBgNVBAsTB1ND
|
|
RVAgQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDV
|
|
u9YVfl7gu0UgUkOJoES/XrN0WZdIjgvS2upKfvP4LSJOq1Mnp3bH
|
|
wWOA2NkHem/kjOVeotOk1aEYIzxbic6VlvNOz9huOhbJyoV4TO5v
|
|
tp/GFFcJ4IXh+f1Q4vm/NeH/XxEWn9S20B9Ok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TTk797YzQ+5lj51wPVVf8s+NZe
|
|
EgSTSKpbCJEgejkt6IzpxT3qU9LnxRhGQQIKuF+Nw+lSqrbN9D7RjsWL19sFN7Di
|
|
VyaSd3OINyk5EImOkz9AHuEvukoI5o3+B38+EJO+6QnMkaBlxo0UTjVrz12As0Se
|
|
cEnJAoGBAOUXjez9oUSzLzqG/WJFrIfHyjDA1vBS1j39XuhDuJGqMdNLlCE8Yr7h
|
|
d3gpZeuV3ZC33QAuwAXfRBNnKIDtDGpcrozM1NndcBVDs9GYvobaTiUaODGjsH44
|
|
oHwpyQbv9Qs+3bjPOQ7DkwekT+w1cptEKudBCC3WQKui1P0NNL0R
|
|
-----END RSA PRIVATE KEY-----
|
|
`))
|
|
)
|
|
|
|
// prevent static analysis tools from raising issues due to detection of private key
|
|
// in code.
|
|
func testingKey(s string) string { return strings.ReplaceAll(s, "TESTING KEY", "PRIVATE KEY") }
|