-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathauthData.pas
353 lines (289 loc) · 10.6 KB
/
authData.pas
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// ###################################################################
// #### This file is part of the mathematics library project, and is
// #### offered under the licence agreement described on
// #### http://www.mrsoft.org/
// ####
// #### Copyright:(c) 2020, Michael R. . All rights reserved.
// ####
// #### Unless required by applicable law or agreed to in writing, software
// #### distributed under the License is distributed on an "AS IS" BASIS,
// #### WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// #### See the License for the specific language governing permissions and
// #### limitations under the License.
// ###################################################################
unit authData;
interface
uses SysUtils, Types, cbor;
type
TFidoRPIDHash = Array[0..31] of byte;
EAuthDataException = class(Exception);
// ###########################################
// #### Decodes the Authenticator buffer for both the enrolling process and
// assertion process (with less data)
type
TAuthData = class(TObject)
private
// minimum
frpIDHash : TFidoRPIDHash;
fflags : byte; // bit 0: user present
// bit 2: user verification
// bit 6: attested credential data included
// bit 7: extension included
fsignCnt : LongWord;
// attested credential data (optional)
faaGUID : TGuid;
fcredIDLend : word;
fCredID : TBytes;
fCred : TCborMap;
fPubType : integer;
fPubAlg : integer;
fPublicKeyData1 : TBytes; // field -1
fPublicKeyData2 : TBytes; // field -2
fPublicKeyData3 : TBytes; // field -3
fHasPublicKey : boolean;
fExtensions : TCborItem;
fRawAuthData : TBytes;
public
function UserPresent : boolean;
function UserVerified : boolean;
function HasAttestedData : boolean;
function HasExtensions : boolean;
function RPIDHash : TFidoRPIDHash;
function SigCount : LongWord;
function AAUID : TGuid;
function HasPublicKey : boolean;
function KeyType : integer;
function PublicKeyAlg : integer;
function CredId : TBytes;
function AttestData1 : TBytes;
function AttestData2 : TBytes;
function AttestData3 : TBytes;
function ToString : string; override;
function RawData : TBytes;
constructor Create( fromData : TBytes ); overload;
constructor Create( data : PByte; len : integer); overload;
destructor Destroy; override;
end;
implementation
uses SuperObject, Fido2dll;
{ TAuthData }
function TAuthData.UserPresent: boolean;
begin
Result := (fflags and $01) <> 0;
end;
function TAuthData.UserVerified: boolean;
begin
Result := (fflags and $04) <> 0;
end;
function TAuthData.HasAttestedData: boolean;
begin
Result := (fflags and $40) <> 0;
end;
function TAuthData.HasExtensions: boolean;
begin
Result := (fflags and $80) <> 0;
end;
function TAuthData.HasPublicKey: boolean;
begin
Result := fHasPublicKey;
end;
function TAuthData.KeyType: integer;
begin
Result := fPubType;
end;
function TAuthData.PublicKeyAlg: integer;
begin
// only valid if HasPublicKey is valid
if fHasPublicKey
then
Result := fPubAlg
else
Result := 0;
end;
function TAuthData.RawData: TBytes;
begin
Result := fRawAuthData;
end;
function TAuthData.RPIDHash: TFidoRPIDHash;
begin
Result := frpIDHash;
end;
function TAuthData.SigCount: LongWord;
begin
Result := fsignCnt;
end;
procedure RevertByteOrder( stream : PByte; numBytes : integer);
var i: Integer;
pEnd : PByte;
tmp : byte;
begin
pEnd := stream;
inc(pEnd, numBytes - 1);
for i := 0 to numBytes div 2 - 1 do
begin
tmp := stream^;
stream^ := pEnd^;
pEnd^ := tmp;
inc(stream);
dec(pEnd);
end;
end;
constructor TAuthData.Create(fromData: TBytes);
var idx : integer;
numDecoded : integer;
coseIdx1, coseIdx2, coseIdx3, coseIdx4, coseIdx5 : integer;
cose1 : TCborUINTItem;
cose2 : TCborNegIntItem;
begin
fRawAuthData := Copy(fromData, 0, Length(fromData));
// according to https://www.w3.org/TR/webauthn/#sctn-attestation
// the attestation object subtype authData is:
// 32 bytes of rpIDHash
// 1 byte flags:
// 4 bytess counter. -> a total of 37 bytes minimum
// if AttestedData flag is set then
// 16 bytes AAUID of the key
// 2 bytes Credential id length
// followed this number of bytes credential id
// followed by the credential public key (variable length COSE_key)
// this data is cbor encoded
// if extended flag is set the rest is
// a cbor map of extended flags
cose1 := nil;
fHasPublicKey := False;
if Length(fromData) < 37 then
raise EAuthDataException.Create('Error at least 37 bytes required');
Move( fromData[0], frpIDHash, sizeof(frpIDHash));
fflags := fromData[32];
fsignCnt := PLongWord( @fromData[33] )^;
RevertByteOrder( @fsignCnt, sizeof(fsignCnt));
fPubType := -1;
idx := 37;
if HasAttestedData then
begin
if Length(fromData) < idx + sizeof(word) + sizeof(TGuid) then
raise EAuthDataException.Create('Error no memory for attestation data');
Move( fromData[idx], faaGUID, sizeof(faaGUID));
idx := idx + sizeof(TGUID);
fcredIDLend := PWord(@fromData[idx])^;
RevertByteOrder(@fcredIDLend, sizeof(fcredIDLend));
inc(idx, sizeof(word));
if Length(fromData) < idx + fcredIDLend then
raise EAuthDataException.Create('Bad attestation data - data too short');
SetLength(fCredID, fcredIDLend);
if fcredIDLend > 0 then
Move(fromData[idx], fCredID[0], fcredIDLend);
inc(idx, fcredIDLend);
numDecoded := 0;
if idx < Length(fromData) then
begin
// decode credential public key...
fCred := TCborDecoding.DecodeData(@fromData[idx], Length(fromData) - idx, numDecoded) as TCborMap;
// for now we just check if fields 1, 3, -2 and -3 are available and the
// key type is in in the range of the ones supported from the fido dll
coseIdx1 := fCred.IndexOfName('1');
coseIdx2 := fCred.IndexOfName('3');
coseIdx3 := fCred.IndexOfName('-1');
coseIdx4 := fCred.IndexOfName('-2');
coseIdx5 := fCred.IndexOfName('-3');
if (coseIdx1 < 0) or (coseIdx2 < 0) or (coseIdx3 < 0) then
raise EAuthDataException.Create('Credential public key does not contain the fields "1", "3" or "-1"');
cose1 := fCred.Values[coseIdx1] as TCborUINTItem;
// COSE_KTY_OKP = 1;
// COSE_KTY_EC2 = 2;
// COSE_KTY_RSA = 3;
if not (cose1.Value in [COSE_KTY_OKP, COSE_KTY_EC2, COSE_KTY_RSA]) then
raise EAuthDataException.Create('Cose type not in the expected range');
fPubType := Integer(cose1.Value);
// COSE_ES256 = -7;
// COSE_EDDSA = -8;
// COSE_RS256 = -257;
cose2 := fCred.Values[coseIdx2] as TCborNegIntItem;
if (cose2.Value <> COSE_ES256) and (cose2.Value <> COSE_EDDSA) and (cose2.Value <> COSE_RS256) then
raise EAuthDataException.Create('Cose algorithm not recognized');
fPubAlg := cose2.Value;
// COSE_KTY_EC2: -1: p-256 curve, -2: x coordinate as byte string, -3: y - coordinate as byte string
// COSE_RS256: -1: rsa modulos, -2 rsa public exponent
if coseIdx3 >= 0 then
begin
if fCred.Values[coseIdx3] is TCborByteString
then
fPUblicKeyData1 := (fCred.Values[coseIdx3] as TCborByteString).ToBytes
else if fCred.Values[coseIdx3] is TCborUINTItem then
begin
SetLength(fPUblicKeyData1, sizeof(Uint64));
Move( (fCred.Values[coseIdx3] as TCborUIntItem).Value, fPUblicKeyData1[0], sizeof(UINt64));
end;
end;
if coseIdx4 >= 0 then
fPUblicKeyData2 := (fCred.Values[coseIdx4] as TCborByteString).ToBytes;
if coseIdx5 >= 0 then
fPUblicKeyData3 := (fCred.Values[coseIdx5] as TCborByteString).ToBytes;
end;
idx := idx + numDecoded;
end;
if Assigned(cose1) then
begin
// todo: verify if that is correct - check the length...
case cose1.Value of
COSE_KTY_OKP: fHasPublicKey := (fPublicKeyData1 <> nil) and (fPublicKeyData2 <> nil);
COSE_KTY_EC2: fHasPublicKey := (fPublicKeyData1 <> nil) and (fPublicKeyData2 <> nil) and (fPublicKeyData3 <> nil);
COSE_KTY_RSA: fHasPublicKey := (fPublicKeyData1 <> nil) and (fPublicKeyData2 <> nil);
else
fHasPublicKey := False;
end;
end;
if HasExtensions then
begin
// cbor encoded extensions
fExtensions := TCborDecoding.DecodeData( @fromData[idx], Length(fromData) - idx);
end;
end;
function TAuthData.AAUID: TGuid;
begin
Result := faaGUID;
end;
function TAuthData.AttestData1: TBytes;
begin
Result := fPublicKeyData1;
end;
function TAuthData.AttestData2: TBytes;
begin
Result := fPublicKeyData2;
end;
function TAuthData.AttestData3: TBytes;
begin
Result := fPublicKeyData3;
end;
constructor TAuthData.Create(data: PByte; len: integer);
var fromData : TBytes;
begin
SetLength(fromData, len);
if len > 0 then
Move( data^, fromData[0], len);
Create( fromData );
end;
function TAuthData.CredId: TBytes;
begin
Result := fCredID;
end;
destructor TAuthData.Destroy;
begin
fExtensions.Free;
fCred.Free;
inherited;
end;
function TAuthData.ToString: string;
var res : ISuperObject;
begin
res := SO;
res.S['aauid'] := GUIDToString( faaGUID );
res.B['UserPresent'] := UserPresent;
res.B['UserVerified'] := UserVerified;
res.I['sigCnt'] := fsignCnt;
res.S['hash'] := Base64URLEncode( @frpIDHash[0], Length(frpIDHash) );
if Assigned(fCred) then
res.S['cred'] := fCred.ToString;
Result := res.AsJSon;
end;
end.