-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
274 lines (230 loc) · 7.4 KB
/
server.go
File metadata and controls
274 lines (230 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package attestation
import (
"context"
"errors"
"sync"
"time"
"github.com/kacy/device-attestation/challenge"
"github.com/kacy/device-attestation/ios"
)
// Server provides a batteries-included attestation server that handles
// challenge generation, validation, and key storage automatically.
//
// This is the recommended way to use the library for most use cases.
// For advanced customization, use NewVerifier directly with your own
// challenge store and key store implementations.
type Server struct {
verifier Verifier
challenges challenge.Store
keyStore ios.KeyStore
mu sync.RWMutex
closed bool
}
// ServerConfig holds configuration for the attestation server.
type ServerConfig struct {
// iOS configuration (optional - omit to disable iOS support)
IOS *IOSConfig
// Android configuration (optional - omit to disable Android support)
Android *AndroidConfig
// ChallengeTimeout is how long challenges remain valid (default: 5 minutes).
ChallengeTimeout time.Duration
}
// IOSConfig holds iOS-specific configuration.
type IOSConfig struct {
// BundleIDs is the list of allowed app bundle identifiers (required).
BundleIDs []string
// TeamID is your Apple Developer Team ID (required).
TeamID string
}
// AndroidConfig holds Android-specific configuration.
type AndroidConfig struct {
// PackageNames is the list of allowed app package names (required).
PackageNames []string
// GCPProjectID is your Google Cloud project ID (required).
GCPProjectID string
// GCPCredentialsFile is the path to service account credentials (optional).
// If empty, uses Application Default Credentials.
GCPCredentialsFile string
// APKCertDigests is the list of allowed APK signing certificate SHA-256 digests (optional).
APKCertDigests []string
// RequireStrongIntegrity requires hardware-backed attestation (default: false).
RequireStrongIntegrity bool
}
// NewServer creates a new attestation server with sensible defaults.
//
// Example:
//
// server, err := attestation.NewServer(attestation.ServerConfig{
// IOS: &attestation.IOSConfig{
// BundleIDs: []string{"com.example.app"},
// TeamID: "ABCD123456",
// },
// Android: &attestation.AndroidConfig{
// PackageNames: []string{"com.example.app"},
// GCPProjectID: "my-project",
// },
// })
func NewServer(cfg ServerConfig) (*Server, error) {
if cfg.IOS == nil && cfg.Android == nil {
return nil, errors.New("at least one platform (iOS or Android) must be configured")
}
timeout := cfg.ChallengeTimeout
if timeout == 0 {
timeout = 5 * time.Minute
}
// Create internal stores
challenges := challenge.NewMemoryStore(challenge.Config{
Timeout: timeout,
})
keyStore := ios.NewMemoryKeyStore()
// Build verifier config
verifierCfg := Config{
ChallengeTimeout: timeout,
KeyStore: keyStore,
}
if cfg.IOS != nil {
if len(cfg.IOS.BundleIDs) == 0 {
return nil, errors.New("iOS: at least one bundle ID is required")
}
if cfg.IOS.TeamID == "" {
return nil, errors.New("iOS: team ID is required")
}
verifierCfg.IOSBundleIDs = cfg.IOS.BundleIDs
verifierCfg.IOSTeamID = cfg.IOS.TeamID
}
if cfg.Android != nil {
if len(cfg.Android.PackageNames) == 0 {
return nil, errors.New("Android: at least one package name is required")
}
if cfg.Android.GCPProjectID == "" {
return nil, errors.New("Android: GCP project ID is required")
}
verifierCfg.AndroidPackageNames = cfg.Android.PackageNames
verifierCfg.GCPProjectID = cfg.Android.GCPProjectID
verifierCfg.GCPCredentialsFile = cfg.Android.GCPCredentialsFile
verifierCfg.AndroidAPKCertDigests = cfg.Android.APKCertDigests
verifierCfg.RequireStrongIntegrity = cfg.Android.RequireStrongIntegrity
}
verifier, err := NewVerifier(verifierCfg)
if err != nil {
challenges.Close()
return nil, err
}
return &Server{
verifier: verifier,
challenges: challenges,
keyStore: keyStore,
}, nil
}
// GenerateChallenge creates a new challenge for the given identifier.
// The identifier should be unique per attestation flow (e.g., user ID, session ID).
//
// Returns the challenge string that should be sent to the client.
func (s *Server) GenerateChallenge(identifier string) (string, error) {
s.mu.RLock()
if s.closed {
s.mu.RUnlock()
return "", errors.New("server is closed")
}
s.mu.RUnlock()
return s.challenges.Generate(identifier)
}
// VerifyAttestation verifies a device attestation.
//
// The identifier must match the one used in GenerateChallenge.
// On success, the challenge is consumed and cannot be reused.
//
// Example:
//
// result, err := server.VerifyAttestation(ctx, "user-123", attestation.VerifyRequest{
// Platform: attestation.PlatformIOS,
// Attestation: attestationFromClient,
// Challenge: challengeFromClient,
// KeyID: keyIDFromClient,
// BundleID: "com.example.app",
// })
func (s *Server) VerifyAttestation(ctx context.Context, identifier string, req VerifyRequest) (*Result, error) {
s.mu.RLock()
if s.closed {
s.mu.RUnlock()
return nil, errors.New("server is closed")
}
s.mu.RUnlock()
// Validate challenge first
if !s.challenges.Validate(identifier, req.Challenge) {
return nil, ErrInvalidChallenge
}
// Verify attestation
return s.verifier.Verify(ctx, &Request{
Platform: req.Platform,
Attestation: req.Attestation,
Challenge: req.Challenge,
KeyID: req.KeyID,
BundleID: req.BundleID,
})
}
// VerifyRequest contains the attestation data from the client.
type VerifyRequest struct {
// Platform is the device platform (PlatformIOS or PlatformAndroid).
Platform Platform
// Attestation is the base64-encoded attestation data from the device.
Attestation string
// Challenge is the challenge that was sent to the client.
Challenge string
// KeyID is the key identifier (iOS only).
KeyID string
// BundleID is the app bundle identifier (iOS only).
BundleID string
}
// VerifyAssertion verifies an iOS assertion for subsequent requests.
//
// This requires a previous successful attestation for the given KeyID.
// The assertion counter is automatically tracked to prevent replay attacks.
func (s *Server) VerifyAssertion(ctx context.Context, req AssertionRequest) (*Result, error) {
s.mu.RLock()
if s.closed {
s.mu.RUnlock()
return nil, errors.New("server is closed")
}
s.mu.RUnlock()
return s.verifier.VerifyAssertion(ctx, &ios.AssertionRequest{
Assertion: req.Assertion,
ClientData: req.ClientData,
KeyID: req.KeyID,
BundleID: req.BundleID,
})
}
// AssertionRequest contains the assertion data from the client.
type AssertionRequest struct {
// Assertion is the base64-encoded assertion from the device.
Assertion string
// ClientData is the request-specific data that was signed.
ClientData []byte
// KeyID is the key identifier from the original attestation.
KeyID string
// BundleID is the app bundle identifier.
BundleID string
}
// Close releases resources used by the server.
func (s *Server) Close() error {
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
return nil
}
s.closed = true
s.challenges.Close()
return nil
}
// Challenges returns the underlying challenge store for advanced use cases.
func (s *Server) Challenges() challenge.Store {
return s.challenges
}
// KeyStore returns the underlying key store for advanced use cases.
func (s *Server) KeyStore() ios.KeyStore {
return s.keyStore
}
// Verifier returns the underlying verifier for advanced use cases.
func (s *Server) Verifier() Verifier {
return s.verifier
}