|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/groob/plist" |
| 9 | + "github.com/micromdm/nanomdm/mdm" |
| 10 | + "github.com/micromdm/nanomdm/service/certauth" |
| 11 | + "github.com/micromdm/nanomdm/storage" |
| 12 | + "github.com/micromdm/nanomdm/test" |
| 13 | + "github.com/micromdm/nanomdm/test/enrollment" |
| 14 | +) |
| 15 | + |
| 16 | +func certAuth(t *testing.T, ctx context.Context, store storage.CertAuthStore) { |
| 17 | + d, auth, tok, err := setupEnrollment() |
| 18 | + if err != nil { |
| 19 | + t.Fatal(err) |
| 20 | + } |
| 21 | + |
| 22 | + // init service |
| 23 | + svc := certauth.New(&test.NopService{}, store) |
| 24 | + |
| 25 | + // send a non-Authenticate message (without an initial Authenticate message) |
| 26 | + err = svc.TokenUpdate(d.NewMDMRequest(ctx), tok) |
| 27 | + expectErr(t, err, certauth.ErrNoCertAssoc) |
| 28 | + |
| 29 | + // send another one to make sure we're not accidentally allowing retroactive mode |
| 30 | + err = svc.TokenUpdate(d.NewMDMRequest(ctx), tok) |
| 31 | + expectErr(t, err, certauth.ErrNoCertAssoc) |
| 32 | + |
| 33 | + // sent an authenticate message. this should associate our cert hash. |
| 34 | + err = svc.Authenticate(d.NewMDMRequest(ctx), auth) |
| 35 | + expectErr(t, err, nil) |
| 36 | + |
| 37 | + // now send an a message that should be authenticated |
| 38 | + err = svc.TokenUpdate(d.NewMDMRequest(ctx), tok) |
| 39 | + expectErr(t, err, nil) |
| 40 | + |
| 41 | + // lets swap out the device identity. i.e. attempt to spoof the device with another cert. |
| 42 | + err = enrollment.ReplaceIdentityRandom(d) |
| 43 | + if err != nil { |
| 44 | + t.Fatal(err) |
| 45 | + } |
| 46 | + |
| 47 | + // try the spoofed request |
| 48 | + err = svc.TokenUpdate(d.NewMDMRequest(ctx), tok) |
| 49 | + expectErr(t, err, certauth.ErrNoCertAssoc) |
| 50 | +} |
| 51 | + |
| 52 | +func certAuthRetro(t *testing.T, ctx context.Context, store storage.CertAuthStore) { |
| 53 | + d, _, tok, err := setupEnrollment() |
| 54 | + if err != nil { |
| 55 | + t.Fatal(err) |
| 56 | + } |
| 57 | + |
| 58 | + // init service with retroactive |
| 59 | + svc := certauth.New(&test.NopService{}, store, certauth.WithAllowRetroactive()) |
| 60 | + |
| 61 | + // without retroactive a non-Authenticate message would generate an ErrNoCertAssoc. |
| 62 | + // however with retro on it should allow the association. |
| 63 | + err = svc.TokenUpdate(d.NewMDMRequest(ctx), tok) |
| 64 | + expectErr(t, err, nil) |
| 65 | + |
| 66 | + // send another one to make sure the reto association is still good. |
| 67 | + err = svc.TokenUpdate(d.NewMDMRequest(ctx), tok) |
| 68 | + expectErr(t, err, nil) |
| 69 | + |
| 70 | + // lets swap out the device identity. i.e. attempt to spoof the device with another cert. |
| 71 | + err = enrollment.ReplaceIdentityRandom(d) |
| 72 | + if err != nil { |
| 73 | + t.Fatal(err) |
| 74 | + } |
| 75 | + |
| 76 | + // try the spoofed request post-association |
| 77 | + err = svc.TokenUpdate(d.NewMDMRequest(ctx), tok) |
| 78 | + expectErr(t, err, certauth.ErrNoCertReuse) |
| 79 | +} |
| 80 | + |
| 81 | +func expectErr(t *testing.T, have, want error) { |
| 82 | + if !errors.Is(have, want) { |
| 83 | + t.Helper() |
| 84 | + t.Errorf("have: %v; want: %v", have, want) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func setupEnrollment() (*enrollment.Enrollment, *mdm.Authenticate, *mdm.TokenUpdate, error) { |
| 89 | + // create our test device |
| 90 | + d, err := enrollment.NewRandomDeviceEnrollment(nil, "com.apple.test-topic", "/", "") |
| 91 | + if err != nil { |
| 92 | + return d, nil, nil, err |
| 93 | + } |
| 94 | + |
| 95 | + // gen the Authenticate msg and turn into NanoMDM msg |
| 96 | + r, err := d.GenAuthenticate() |
| 97 | + if err != nil { |
| 98 | + return d, nil, nil, err |
| 99 | + } |
| 100 | + auth := new(mdm.Authenticate) |
| 101 | + err = plist.NewDecoder(r).Decode(auth) |
| 102 | + if err != nil { |
| 103 | + return d, auth, nil, err |
| 104 | + } |
| 105 | + |
| 106 | + // gen the TokenUpdate msg and turn into NanoMDM msg |
| 107 | + r, err = d.GenTokenUpdate() |
| 108 | + if err != nil { |
| 109 | + return d, auth, nil, err |
| 110 | + } |
| 111 | + tok := new(mdm.TokenUpdate) |
| 112 | + err = plist.NewDecoder(r).Decode(tok) |
| 113 | + if err != nil { |
| 114 | + return d, auth, tok, err |
| 115 | + } |
| 116 | + |
| 117 | + return d, auth, tok, err |
| 118 | +} |
0 commit comments