|
@@ -73,334 +73,346 @@ const ERR_OLD_OPENSSL = 11;
|
73
|
73
|
|
74
|
74
|
const PUBKEY_LEN = 65;
|
75
|
75
|
|
76
|
|
-class U2F {
|
77
|
|
- private $appId;
|
|
76
|
+class U2F
|
|
77
|
+{
|
|
78
|
+ private $appId;
|
78
|
79
|
|
79
|
|
- private $attestDir;
|
|
80
|
+ private $attestDir;
|
80
|
81
|
|
81
|
|
- private $FIXCERTS = array(
|
|
82
|
+ private $FIXCERTS = array(
|
82
|
83
|
'349bca1031f8c82c4ceca38b9cebf1a69df9fb3b94eed99eb3fb9aa3822d26e8',
|
83
|
84
|
'dd574527df608e47ae45fbba75a2afdd5c20fd94a02419381813cd55a2a3398f',
|
84
|
85
|
'1d8764f0f7cd1352df6150045c8f638e517270e8b5dda1c63ade9c2280240cae',
|
85
|
86
|
'd0edc9a91a1677435a953390865d208c55b3183c6759c9b5a7ff494c322558eb',
|
86
|
87
|
'6073c436dcd064a48127ddbf6032ac1a66fd59a0c24434f070d4e564c124c897',
|
87
|
88
|
'ca993121846c464d666096d35f13bf44c1b05af205f9b4a1e00cf6cc10c5e511'
|
88
|
|
- );
|
89
|
|
-
|
90
|
|
- /**
|
91
|
|
- * @param string $appId Application id for the running application
|
92
|
|
- * @param string|null $attestDir Directory where trusted attestation roots may be found
|
93
|
|
- * @throws Error If OpenSSL older than 1.0.0 is used
|
94
|
|
- */
|
95
|
|
- public function __construct($appId, $attestDir = null) {
|
96
|
|
- if (OPENSSL_VERSION_NUMBER < 0x10000000) {
|
97
|
|
- throw new Error('OpenSSL has to be at least version 1.0.0, this is ' . OPENSSL_VERSION_TEXT, ERR_OLD_OPENSSL);
|
98
|
|
- }
|
99
|
|
- $this->appId = $appId;
|
100
|
|
- $this->attestDir = $attestDir;
|
101
|
|
- }
|
102
|
|
-
|
103
|
|
- /**
|
104
|
|
- * Called to get a registration request to send to a user.
|
105
|
|
- * Returns an array of one registration request and a array of sign requests.
|
106
|
|
- *
|
107
|
|
- * @param array $registrations List of current registrations for this
|
108
|
|
- * user, to prevent the user from registering the same authenticator several
|
109
|
|
- * times.
|
110
|
|
- * @return array An array of two elements, the first containing a
|
111
|
|
- * RegisterRequest the second being an array of SignRequest
|
112
|
|
- * @throws Error
|
113
|
|
- */
|
114
|
|
- public function getRegisterData(array $registrations = []) {
|
115
|
|
- $challenge = $this->createChallenge();
|
116
|
|
- $request = new RegisterRequest($challenge, $this->appId);
|
117
|
|
- $signs = $this->getAuthenticateData($registrations);
|
118
|
|
- return [$request, $signs];
|
119
|
|
- }
|
120
|
|
-
|
121
|
|
- /**
|
122
|
|
- * Called to verify and unpack a registration message.
|
123
|
|
- *
|
124
|
|
- * @param RegisterRequest $request this is a reply to
|
125
|
|
- * @param object $response response from a user
|
126
|
|
- * @param bool $includeCert set to true if the attestation certificate should be
|
127
|
|
- * included in the returned Registration object
|
128
|
|
- * @return Registration
|
129
|
|
- * @throws Error
|
130
|
|
- */
|
131
|
|
- public function doRegister($request, $response, $includeCert = true) {
|
132
|
|
- if (!is_object($request)) {
|
133
|
|
- throw new \InvalidArgumentException('$request of doRegister() method only accepts object.');
|
134
|
|
- }
|
|
89
|
+ );
|
135
|
90
|
|
136
|
|
- if (!is_object($response)) {
|
137
|
|
- throw new \InvalidArgumentException('$response of doRegister() method only accepts object.');
|
|
91
|
+ /**
|
|
92
|
+ * @param string $appId Application id for the running application
|
|
93
|
+ * @param string|null $attestDir Directory where trusted attestation roots may be found
|
|
94
|
+ * @throws Error If OpenSSL older than 1.0.0 is used
|
|
95
|
+ */
|
|
96
|
+ public function __construct($appId, $attestDir = null)
|
|
97
|
+ {
|
|
98
|
+ if (OPENSSL_VERSION_NUMBER < 0x10000000) {
|
|
99
|
+ throw new Error('OpenSSL has to be at least version 1.0.0, this is ' . OPENSSL_VERSION_TEXT, ERR_OLD_OPENSSL);
|
|
100
|
+ }
|
|
101
|
+ $this->appId = $appId;
|
|
102
|
+ $this->attestDir = $attestDir;
|
138
|
103
|
}
|
139
|
104
|
|
140
|
|
- if (property_exists($response, 'errorCode') && $response->errorCode !== 0) {
|
141
|
|
- throw new Error('User-agent returned error. Error code: ' . $response->errorCode, ERR_BAD_UA_RETURNING );
|
|
105
|
+ /**
|
|
106
|
+ * Called to get a registration request to send to a user.
|
|
107
|
+ * Returns an array of one registration request and a array of sign requests.
|
|
108
|
+ *
|
|
109
|
+ * @param array $registrations List of current registrations for this
|
|
110
|
+ * user, to prevent the user from registering the same authenticator several
|
|
111
|
+ * times.
|
|
112
|
+ * @return array An array of two elements, the first containing a
|
|
113
|
+ * RegisterRequest the second being an array of SignRequest
|
|
114
|
+ * @throws Error
|
|
115
|
+ */
|
|
116
|
+ public function getRegisterData(array $registrations = [])
|
|
117
|
+ {
|
|
118
|
+ $challenge = $this->createChallenge();
|
|
119
|
+ $request = new RegisterRequest($challenge, $this->appId);
|
|
120
|
+ $signs = $this->getAuthenticateData($registrations);
|
|
121
|
+ return [$request, $signs];
|
142
|
122
|
}
|
143
|
123
|
|
144
|
|
- if (!is_bool($includeCert)) {
|
145
|
|
- throw new \InvalidArgumentException('$include_cert of doRegister() method only accepts boolean.');
|
146
|
|
- }
|
|
124
|
+ /**
|
|
125
|
+ * Called to verify and unpack a registration message.
|
|
126
|
+ *
|
|
127
|
+ * @param RegisterRequest $request this is a reply to
|
|
128
|
+ * @param object $response response from a user
|
|
129
|
+ * @param bool $includeCert set to true if the attestation certificate should be
|
|
130
|
+ * included in the returned Registration object
|
|
131
|
+ * @return Registration
|
|
132
|
+ * @throws Error
|
|
133
|
+ */
|
|
134
|
+ public function doRegister($request, $response, $includeCert = true)
|
|
135
|
+ {
|
|
136
|
+ if (!is_object($request)) {
|
|
137
|
+ throw new \InvalidArgumentException('$request of doRegister() method only accepts object.');
|
|
138
|
+ }
|
147
|
139
|
|
148
|
|
- $rawReg = $this->base64u_decode($response->registrationData);
|
149
|
|
- $regData = array_values(unpack('C*', $rawReg));
|
150
|
|
- $clientData = $this->base64u_decode($response->clientData);
|
151
|
|
- $cli = json_decode($clientData);
|
|
140
|
+ if (!is_object($response)) {
|
|
141
|
+ throw new \InvalidArgumentException('$response of doRegister() method only accepts object.');
|
|
142
|
+ }
|
152
|
143
|
|
153
|
|
- if ($cli->challenge !== $request->challenge) {
|
154
|
|
- throw new Error('Registration challenge does not match', ERR_UNMATCHED_CHALLENGE );
|
155
|
|
- }
|
|
144
|
+ if (property_exists($response, 'errorCode') && $response->errorCode !== 0) {
|
|
145
|
+ throw new Error('User-agent returned error. Error code: ' . $response->errorCode, ERR_BAD_UA_RETURNING);
|
|
146
|
+ }
|
156
|
147
|
|
157
|
|
- $registration = new Registration();
|
158
|
|
- $offs = 1;
|
159
|
|
- $pubKey = substr($rawReg, $offs, PUBKEY_LEN);
|
160
|
|
- $offs += PUBKEY_LEN;
|
161
|
|
- // decode the pubKey to make sure it's good
|
162
|
|
- $tmpKey = $this->pubkey_to_pem($pubKey);
|
163
|
|
- if ($tmpKey === null) {
|
164
|
|
- throw new Error('Decoding of public key failed', ERR_PUBKEY_DECODE );
|
165
|
|
- }
|
166
|
|
- $registration->publicKey = base64_encode($pubKey);
|
167
|
|
- $khLen = $regData[$offs++];
|
168
|
|
- $kh = substr($rawReg, $offs, $khLen);
|
169
|
|
- $offs += $khLen;
|
170
|
|
- $registration->keyHandle = $this->base64u_encode($kh);
|
171
|
|
-
|
172
|
|
- // length of certificate is stored in byte 3 and 4 (excluding the first 4 bytes)
|
173
|
|
- $certLen = 4;
|
174
|
|
- $certLen += ($regData[$offs + 2] << 8);
|
175
|
|
- $certLen += $regData[$offs + 3];
|
176
|
|
-
|
177
|
|
- $rawCert = $this->fixSignatureUnusedBits(substr($rawReg, $offs, $certLen));
|
178
|
|
- $offs += $certLen;
|
179
|
|
- $pemCert = "-----BEGIN CERTIFICATE-----\r\n";
|
180
|
|
- $pemCert .= chunk_split(base64_encode($rawCert), 64);
|
181
|
|
- $pemCert .= "-----END CERTIFICATE-----";
|
182
|
|
- if ($includeCert) {
|
183
|
|
- $registration->certificate = base64_encode($rawCert);
|
184
|
|
- }
|
185
|
|
- if ($this->attestDir) {
|
186
|
|
- if (openssl_x509_checkpurpose($pemCert, -1, $this->get_certs()) !== true) {
|
187
|
|
- throw new Error('Attestation certificate can not be validated', ERR_ATTESTATION_VERIFICATION );
|
188
|
|
- }
|
189
|
|
- }
|
|
148
|
+ if (!is_bool($includeCert)) {
|
|
149
|
+ throw new \InvalidArgumentException('$include_cert of doRegister() method only accepts boolean.');
|
|
150
|
+ }
|
190
|
151
|
|
191
|
|
- if (!openssl_pkey_get_public($pemCert)) {
|
192
|
|
- throw new Error('Decoding of public key failed', ERR_PUBKEY_DECODE );
|
193
|
|
- }
|
194
|
|
- $signature = substr($rawReg, $offs);
|
195
|
|
-
|
196
|
|
- $dataToVerify = chr(0);
|
197
|
|
- $dataToVerify .= hash('sha256', $request->appId, true);
|
198
|
|
- $dataToVerify .= hash('sha256', $clientData, true);
|
199
|
|
- $dataToVerify .= $kh;
|
200
|
|
- $dataToVerify .= $pubKey;
|
201
|
|
-
|
202
|
|
- if (openssl_verify($dataToVerify, $signature, $pemCert, 'sha256') === 1) {
|
203
|
|
- return $registration;
|
204
|
|
- } else {
|
205
|
|
- throw new Error('Attestation signature does not match', ERR_ATTESTATION_SIGNATURE );
|
206
|
|
- }
|
207
|
|
- }
|
208
|
|
-
|
209
|
|
- /**
|
210
|
|
- * Called to get an authentication request.
|
211
|
|
- *
|
212
|
|
- * @param array $registrations An array of the registrations to create authentication requests for.
|
213
|
|
- * @return array An array of SignRequest
|
214
|
|
- * @throws Error
|
215
|
|
- */
|
216
|
|
- public function getAuthenticateData(array $registrations) {
|
217
|
|
- $sigs = array();
|
218
|
|
- $challenge = $this->createChallenge();
|
219
|
|
- foreach ($registrations as $reg) {
|
220
|
|
- if (!is_object($reg)) {
|
221
|
|
- throw new \InvalidArgumentException('$registrations of getAuthenticateData() method only accepts array of object.');
|
222
|
|
- }
|
223
|
|
-
|
224
|
|
- $sig = new SignRequest();
|
225
|
|
- $sig->appId = $this->appId;
|
226
|
|
- $sig->keyHandle = $reg->keyHandle;
|
227
|
|
- $sig->challenge = $challenge;
|
228
|
|
- $sigs[] = $sig;
|
229
|
|
- }
|
230
|
|
- return $sigs;
|
231
|
|
- }
|
232
|
|
-
|
233
|
|
- /**
|
234
|
|
- * Called to verify an authentication response
|
235
|
|
- *
|
236
|
|
- * @param array $requests An array of outstanding authentication requests
|
237
|
|
- * @param array $registrations An array of current registrations
|
238
|
|
- * @param object $response A response from the authenticator
|
239
|
|
- * @return Registration
|
240
|
|
- * @throws Error
|
241
|
|
- *
|
242
|
|
- * The Registration object returned on success contains an updated counter
|
243
|
|
- * that should be saved for future authentications.
|
244
|
|
- * If the Error returned is ERR_COUNTER_TOO_LOW this is an indication of
|
245
|
|
- * token cloning or similar and appropriate action should be taken.
|
246
|
|
- */
|
247
|
|
- public function doAuthenticate(array $requests, array $registrations, $response) {
|
248
|
|
- if (!is_object($response)) {
|
249
|
|
- throw new \InvalidArgumentException('$response of doAuthenticate() method only accepts object.');
|
|
152
|
+ $rawReg = $this->base64u_decode($response->registrationData);
|
|
153
|
+ $regData = array_values(unpack('C*', $rawReg));
|
|
154
|
+ $clientData = $this->base64u_decode($response->clientData);
|
|
155
|
+ $cli = json_decode($clientData);
|
|
156
|
+
|
|
157
|
+ if ($cli->challenge !== $request->challenge) {
|
|
158
|
+ throw new Error('Registration challenge does not match', ERR_UNMATCHED_CHALLENGE);
|
|
159
|
+ }
|
|
160
|
+
|
|
161
|
+ $registration = new Registration();
|
|
162
|
+ $offs = 1;
|
|
163
|
+ $pubKey = substr($rawReg, $offs, PUBKEY_LEN);
|
|
164
|
+ $offs += PUBKEY_LEN;
|
|
165
|
+ // decode the pubKey to make sure it's good
|
|
166
|
+ $tmpKey = $this->pubkey_to_pem($pubKey);
|
|
167
|
+ if ($tmpKey === null) {
|
|
168
|
+ throw new Error('Decoding of public key failed', ERR_PUBKEY_DECODE);
|
|
169
|
+ }
|
|
170
|
+ $registration->publicKey = base64_encode($pubKey);
|
|
171
|
+ $khLen = $regData[$offs++];
|
|
172
|
+ $kh = substr($rawReg, $offs, $khLen);
|
|
173
|
+ $offs += $khLen;
|
|
174
|
+ $registration->keyHandle = $this->base64u_encode($kh);
|
|
175
|
+
|
|
176
|
+ // length of certificate is stored in byte 3 and 4 (excluding the first 4 bytes)
|
|
177
|
+ $certLen = 4;
|
|
178
|
+ $certLen += ($regData[$offs + 2] << 8);
|
|
179
|
+ $certLen += $regData[$offs + 3];
|
|
180
|
+
|
|
181
|
+ $rawCert = $this->fixSignatureUnusedBits(substr($rawReg, $offs, $certLen));
|
|
182
|
+ $offs += $certLen;
|
|
183
|
+ $pemCert = "-----BEGIN CERTIFICATE-----\r\n";
|
|
184
|
+ $pemCert .= chunk_split(base64_encode($rawCert), 64);
|
|
185
|
+ $pemCert .= "-----END CERTIFICATE-----";
|
|
186
|
+ if ($includeCert) {
|
|
187
|
+ $registration->certificate = base64_encode($rawCert);
|
|
188
|
+ }
|
|
189
|
+ if ($this->attestDir) {
|
|
190
|
+ if (openssl_x509_checkpurpose($pemCert, -1, $this->get_certs()) !== true) {
|
|
191
|
+ throw new Error('Attestation certificate can not be validated', ERR_ATTESTATION_VERIFICATION);
|
|
192
|
+ }
|
|
193
|
+ }
|
|
194
|
+
|
|
195
|
+ if (!openssl_pkey_get_public($pemCert)) {
|
|
196
|
+ throw new Error('Decoding of public key failed', ERR_PUBKEY_DECODE);
|
|
197
|
+ }
|
|
198
|
+ $signature = substr($rawReg, $offs);
|
|
199
|
+
|
|
200
|
+ $dataToVerify = chr(0);
|
|
201
|
+ $dataToVerify .= hash('sha256', $request->appId, true);
|
|
202
|
+ $dataToVerify .= hash('sha256', $clientData, true);
|
|
203
|
+ $dataToVerify .= $kh;
|
|
204
|
+ $dataToVerify .= $pubKey;
|
|
205
|
+
|
|
206
|
+ if (openssl_verify($dataToVerify, $signature, $pemCert, 'sha256') === 1) {
|
|
207
|
+ return $registration;
|
|
208
|
+ } else {
|
|
209
|
+ throw new Error('Attestation signature does not match', ERR_ATTESTATION_SIGNATURE);
|
|
210
|
+ }
|
250
|
211
|
}
|
251
|
212
|
|
252
|
|
- if (property_exists($response, 'errorCode') && $response->errorCode !== 0) {
|
253
|
|
- throw new Error('User-agent returned error. Error code: ' . $response->errorCode, ERR_BAD_UA_RETURNING );
|
|
213
|
+ /**
|
|
214
|
+ * Called to get an authentication request.
|
|
215
|
+ *
|
|
216
|
+ * @param array $registrations An array of the registrations to create authentication requests for.
|
|
217
|
+ * @return array An array of SignRequest
|
|
218
|
+ * @throws Error
|
|
219
|
+ */
|
|
220
|
+ public function getAuthenticateData(array $registrations)
|
|
221
|
+ {
|
|
222
|
+ $sigs = array();
|
|
223
|
+ $challenge = $this->createChallenge();
|
|
224
|
+ foreach ($registrations as $reg) {
|
|
225
|
+ if (!is_object($reg)) {
|
|
226
|
+ throw new \InvalidArgumentException('$registrations of getAuthenticateData() method only accepts array of object.');
|
|
227
|
+ }
|
|
228
|
+
|
|
229
|
+ $sig = new SignRequest();
|
|
230
|
+ $sig->appId = $this->appId;
|
|
231
|
+ $sig->keyHandle = $reg->keyHandle;
|
|
232
|
+ $sig->challenge = $challenge;
|
|
233
|
+ $sigs[] = $sig;
|
|
234
|
+ }
|
|
235
|
+ return $sigs;
|
254
|
236
|
}
|
255
|
237
|
|
256
|
|
- $req = null;
|
257
|
|
- $reg = null;
|
|
238
|
+ /**
|
|
239
|
+ * Called to verify an authentication response
|
|
240
|
+ *
|
|
241
|
+ * @param array $requests An array of outstanding authentication requests
|
|
242
|
+ * @param array $registrations An array of current registrations
|
|
243
|
+ * @param object $response A response from the authenticator
|
|
244
|
+ * @return Registration
|
|
245
|
+ * @throws Error
|
|
246
|
+ *
|
|
247
|
+ * The Registration object returned on success contains an updated counter
|
|
248
|
+ * that should be saved for future authentications.
|
|
249
|
+ * If the Error returned is ERR_COUNTER_TOO_LOW this is an indication of
|
|
250
|
+ * token cloning or similar and appropriate action should be taken.
|
|
251
|
+ */
|
|
252
|
+ public function doAuthenticate(array $requests, array $registrations, $response)
|
|
253
|
+ {
|
|
254
|
+ if (!is_object($response)) {
|
|
255
|
+ throw new \InvalidArgumentException('$response of doAuthenticate() method only accepts object.');
|
|
256
|
+ }
|
|
257
|
+
|
|
258
|
+ if (property_exists($response, 'errorCode') && $response->errorCode !== 0) {
|
|
259
|
+ throw new Error('User-agent returned error. Error code: ' . $response->errorCode, ERR_BAD_UA_RETURNING);
|
|
260
|
+ }
|
258
|
261
|
|
259
|
|
- $clientData = $this->base64u_decode($response->clientData);
|
260
|
|
- $decodedClient = json_decode($clientData);
|
261
|
|
- foreach ($requests as $req) {
|
262
|
|
- if (!is_object($req)) {
|
263
|
|
- throw new \InvalidArgumentException('$requests of doAuthenticate() method only accepts array of object.');
|
264
|
|
- }
|
|
262
|
+ $req = null;
|
|
263
|
+ $reg = null;
|
265
|
264
|
|
266
|
|
- if ($req->keyHandle === $response->keyHandle && $req->challenge === $decodedClient->challenge) {
|
267
|
|
- break;
|
268
|
|
- }
|
|
265
|
+ $clientData = $this->base64u_decode($response->clientData);
|
|
266
|
+ $decodedClient = json_decode($clientData);
|
|
267
|
+ foreach ($requests as $req) {
|
|
268
|
+ if (!is_object($req)) {
|
|
269
|
+ throw new \InvalidArgumentException('$requests of doAuthenticate() method only accepts array of object.');
|
|
270
|
+ }
|
269
|
271
|
|
270
|
|
- $req = null;
|
271
|
|
- }
|
272
|
|
- if ($req === null) {
|
273
|
|
- throw new Error('No matching request found', ERR_NO_MATCHING_REQUEST );
|
274
|
|
- }
|
275
|
|
- foreach ($registrations as $reg) {
|
276
|
|
- if (!is_object($reg)) {
|
277
|
|
- throw new \InvalidArgumentException('$registrations of doAuthenticate() method only accepts array of object.');
|
278
|
|
- }
|
279
|
|
-
|
280
|
|
- if ($reg->keyHandle === $response->keyHandle) {
|
281
|
|
- break;
|
282
|
|
- }
|
283
|
|
- $reg = null;
|
|
272
|
+ if ($req->keyHandle === $response->keyHandle && $req->challenge === $decodedClient->challenge) {
|
|
273
|
+ break;
|
|
274
|
+ }
|
|
275
|
+
|
|
276
|
+ $req = null;
|
|
277
|
+ }
|
|
278
|
+ if ($req === null) {
|
|
279
|
+ throw new Error('No matching request found', ERR_NO_MATCHING_REQUEST);
|
|
280
|
+ }
|
|
281
|
+ foreach ($registrations as $reg) {
|
|
282
|
+ if (!is_object($reg)) {
|
|
283
|
+ throw new \InvalidArgumentException('$registrations of doAuthenticate() method only accepts array of object.');
|
|
284
|
+ }
|
|
285
|
+
|
|
286
|
+ if ($reg->keyHandle === $response->keyHandle) {
|
|
287
|
+ break;
|
|
288
|
+ }
|
|
289
|
+ $reg = null;
|
|
290
|
+ }
|
|
291
|
+ if ($reg === null) {
|
|
292
|
+ throw new Error('No matching registration found', ERR_NO_MATCHING_REGISTRATION);
|
|
293
|
+ }
|
|
294
|
+ $pemKey = $this->pubkey_to_pem($this->base64u_decode($reg->publicKey));
|
|
295
|
+ if ($pemKey === null) {
|
|
296
|
+ throw new Error('Decoding of public key failed', ERR_PUBKEY_DECODE);
|
|
297
|
+ }
|
|
298
|
+
|
|
299
|
+ $signData = $this->base64u_decode($response->signatureData);
|
|
300
|
+ $dataToVerify = hash('sha256', $req->appId, true);
|
|
301
|
+ $dataToVerify .= substr($signData, 0, 5);
|
|
302
|
+ $dataToVerify .= hash('sha256', $clientData, true);
|
|
303
|
+ $signature = substr($signData, 5);
|
|
304
|
+
|
|
305
|
+ if (openssl_verify($dataToVerify, $signature, $pemKey, 'sha256') === 1) {
|
|
306
|
+ $ctr = unpack("Nctr", substr($signData, 1, 4));
|
|
307
|
+ $counter = $ctr['ctr'];
|
|
308
|
+ /* TODO: wrap-around should be handled somehow.. */
|
|
309
|
+ if ($counter > $reg->counter) {
|
|
310
|
+ $reg->counter = $counter;
|
|
311
|
+ return $reg;
|
|
312
|
+ } else {
|
|
313
|
+ throw new Error('Counter too low.', ERR_COUNTER_TOO_LOW);
|
|
314
|
+ }
|
|
315
|
+ } else {
|
|
316
|
+ throw new Error('Authentication failed', ERR_AUTHENTICATION_FAILURE);
|
|
317
|
+ }
|
284
|
318
|
}
|
285
|
|
- if ($reg === null) {
|
286
|
|
- throw new Error('No matching registration found', ERR_NO_MATCHING_REGISTRATION );
|
|
319
|
+
|
|
320
|
+ /**
|
|
321
|
+ * @return array
|
|
322
|
+ */
|
|
323
|
+ private function get_certs()
|
|
324
|
+ {
|
|
325
|
+ $files = array();
|
|
326
|
+ $dir = $this->attestDir;
|
|
327
|
+ if ($dir && $handle = opendir($dir)) {
|
|
328
|
+ while (false !== ($entry = readdir($handle))) {
|
|
329
|
+ if (is_file("$dir/$entry")) {
|
|
330
|
+ $files[] = "$dir/$entry";
|
|
331
|
+ }
|
|
332
|
+ }
|
|
333
|
+ closedir($handle);
|
|
334
|
+ }
|
|
335
|
+ return $files;
|
287
|
336
|
}
|
288
|
|
- $pemKey = $this->pubkey_to_pem($this->base64u_decode($reg->publicKey));
|
289
|
|
- if ($pemKey === null) {
|
290
|
|
- throw new Error('Decoding of public key failed', ERR_PUBKEY_DECODE );
|
|
337
|
+
|
|
338
|
+ /**
|
|
339
|
+ * @param string $data
|
|
340
|
+ * @return string
|
|
341
|
+ */
|
|
342
|
+ private function base64u_encode($data)
|
|
343
|
+ {
|
|
344
|
+ return trim(strtr(base64_encode($data), '+/', '-_'), '=');
|
291
|
345
|
}
|
292
|
346
|
|
293
|
|
- $signData = $this->base64u_decode($response->signatureData);
|
294
|
|
- $dataToVerify = hash('sha256', $req->appId, true);
|
295
|
|
- $dataToVerify .= substr($signData, 0, 5);
|
296
|
|
- $dataToVerify .= hash('sha256', $clientData, true);
|
297
|
|
- $signature = substr($signData, 5);
|
298
|
|
-
|
299
|
|
- if (openssl_verify($dataToVerify, $signature, $pemKey, 'sha256') === 1) {
|
300
|
|
- $ctr = unpack("Nctr", substr($signData, 1, 4));
|
301
|
|
- $counter = $ctr['ctr'];
|
302
|
|
- /* TODO: wrap-around should be handled somehow.. */
|
303
|
|
- if ($counter > $reg->counter) {
|
304
|
|
- $reg->counter = $counter;
|
305
|
|
- return $reg;
|
306
|
|
- } else {
|
307
|
|
- throw new Error('Counter too low.', ERR_COUNTER_TOO_LOW );
|
308
|
|
- }
|
309
|
|
- } else {
|
310
|
|
- throw new Error('Authentication failed', ERR_AUTHENTICATION_FAILURE );
|
|
347
|
+ /**
|
|
348
|
+ * @param string $data
|
|
349
|
+ * @return string
|
|
350
|
+ */
|
|
351
|
+ private function base64u_decode($data)
|
|
352
|
+ {
|
|
353
|
+ return base64_decode(strtr($data, '-_', '+/'));
|
311
|
354
|
}
|
312
|
|
- }
|
313
|
|
-
|
314
|
|
- /**
|
315
|
|
- * @return array
|
316
|
|
- */
|
317
|
|
- private function get_certs() {
|
318
|
|
- $files = array();
|
319
|
|
- $dir = $this->attestDir;
|
320
|
|
- if ($dir && $handle = opendir($dir)) {
|
321
|
|
- while(false !== ($entry = readdir($handle))) {
|
322
|
|
- if (is_file("$dir/$entry")) {
|
323
|
|
- $files[] = "$dir/$entry";
|
|
355
|
+
|
|
356
|
+ /**
|
|
357
|
+ * @param string $key
|
|
358
|
+ * @return null|string
|
|
359
|
+ */
|
|
360
|
+ private function pubkey_to_pem($key)
|
|
361
|
+ {
|
|
362
|
+ if (strlen($key) !== PUBKEY_LEN || $key[0] !== "\x04") {
|
|
363
|
+ return null;
|
324
|
364
|
}
|
325
|
|
- }
|
326
|
|
- closedir($handle);
|
327
|
|
- }
|
328
|
|
- return $files;
|
329
|
|
- }
|
330
|
|
-
|
331
|
|
- /**
|
332
|
|
- * @param string $data
|
333
|
|
- * @return string
|
334
|
|
- */
|
335
|
|
- private function base64u_encode($data) {
|
336
|
|
- return trim(strtr(base64_encode($data), '+/', '-_'), '=');
|
337
|
|
- }
|
338
|
|
-
|
339
|
|
- /**
|
340
|
|
- * @param string $data
|
341
|
|
- * @return string
|
342
|
|
- */
|
343
|
|
- private function base64u_decode($data) {
|
344
|
|
- return base64_decode(strtr($data, '-_', '+/'));
|
345
|
|
- }
|
346
|
|
-
|
347
|
|
- /**
|
348
|
|
- * @param string $key
|
349
|
|
- * @return null|string
|
350
|
|
- */
|
351
|
|
- private function pubkey_to_pem($key) {
|
352
|
|
- if (strlen($key) !== PUBKEY_LEN || $key[0] !== "\x04") {
|
353
|
|
- return null;
|
|
365
|
+
|
|
366
|
+ /*
|
|
367
|
+ * Convert the public key to binary DER format first
|
|
368
|
+ * Using the ECC SubjectPublicKeyInfo OIDs from RFC 5480
|
|
369
|
+ *
|
|
370
|
+ * SEQUENCE(2 elem) 30 59
|
|
371
|
+ * SEQUENCE(2 elem) 30 13
|
|
372
|
+ * OID1.2.840.10045.2.1 (id-ecPublicKey) 06 07 2a 86 48 ce 3d 02 01
|
|
373
|
+ * OID1.2.840.10045.3.1.7 (secp256r1) 06 08 2a 86 48 ce 3d 03 01 07
|
|
374
|
+ * BIT STRING(520 bit) 03 42 ..key..
|
|
375
|
+ */
|
|
376
|
+ $der = "\x30\x59\x30\x13\x06\x07\x2a\x86\x48\xce\x3d\x02\x01";
|
|
377
|
+ $der .= "\x06\x08\x2a\x86\x48\xce\x3d\x03\x01\x07\x03\x42";
|
|
378
|
+ $der .= "\0".$key;
|
|
379
|
+
|
|
380
|
+ $pem = "-----BEGIN PUBLIC KEY-----\r\n";
|
|
381
|
+ $pem .= chunk_split(base64_encode($der), 64);
|
|
382
|
+ $pem .= "-----END PUBLIC KEY-----";
|
|
383
|
+
|
|
384
|
+ return $pem;
|
354
|
385
|
}
|
355
|
386
|
|
356
|
|
- /*
|
357
|
|
- * Convert the public key to binary DER format first
|
358
|
|
- * Using the ECC SubjectPublicKeyInfo OIDs from RFC 5480
|
359
|
|
- *
|
360
|
|
- * SEQUENCE(2 elem) 30 59
|
361
|
|
- * SEQUENCE(2 elem) 30 13
|
362
|
|
- * OID1.2.840.10045.2.1 (id-ecPublicKey) 06 07 2a 86 48 ce 3d 02 01
|
363
|
|
- * OID1.2.840.10045.3.1.7 (secp256r1) 06 08 2a 86 48 ce 3d 03 01 07
|
364
|
|
- * BIT STRING(520 bit) 03 42 ..key..
|
|
387
|
+ /**
|
|
388
|
+ * @return string
|
|
389
|
+ * @throws Error
|
365
|
390
|
*/
|
366
|
|
- $der = "\x30\x59\x30\x13\x06\x07\x2a\x86\x48\xce\x3d\x02\x01";
|
367
|
|
- $der .= "\x06\x08\x2a\x86\x48\xce\x3d\x03\x01\x07\x03\x42";
|
368
|
|
- $der .= "\0".$key;
|
369
|
|
-
|
370
|
|
- $pem = "-----BEGIN PUBLIC KEY-----\r\n";
|
371
|
|
- $pem .= chunk_split(base64_encode($der), 64);
|
372
|
|
- $pem .= "-----END PUBLIC KEY-----";
|
373
|
|
-
|
374
|
|
- return $pem;
|
375
|
|
- }
|
376
|
|
-
|
377
|
|
- /**
|
378
|
|
- * @return string
|
379
|
|
- * @throws Error
|
380
|
|
- */
|
381
|
|
- private function createChallenge() {
|
382
|
|
- $challenge = openssl_random_pseudo_bytes(32, $crypto_strong );
|
383
|
|
- if ($crypto_strong !== true) {
|
384
|
|
- throw new Error('Unable to obtain a good source of randomness', ERR_BAD_RANDOM);
|
385
|
|
- }
|
|
391
|
+ private function createChallenge()
|
|
392
|
+ {
|
|
393
|
+ $challenge = openssl_random_pseudo_bytes(32, $crypto_strong);
|
|
394
|
+ if ($crypto_strong !== true) {
|
|
395
|
+ throw new Error('Unable to obtain a good source of randomness', ERR_BAD_RANDOM);
|
|
396
|
+ }
|
386
|
397
|
|
387
|
|
- $challenge = $this->base64u_encode( $challenge );
|
|
398
|
+ $challenge = $this->base64u_encode($challenge);
|
388
|
399
|
|
389
|
|
- return $challenge;
|
390
|
|
- }
|
|
400
|
+ return $challenge;
|
|
401
|
+ }
|
391
|
402
|
|
392
|
|
- /**
|
393
|
|
- * Fixes a certificate where the signature contains unused bits.
|
394
|
|
- *
|
395
|
|
- * @param string $cert
|
396
|
|
- * @return mixed
|
397
|
|
- */
|
398
|
|
- private function fixSignatureUnusedBits($cert) {
|
399
|
|
- if (in_array(hash('sha256', $cert), $this->FIXCERTS)) {
|
400
|
|
- $cert[strlen($cert) - 257] = "\0";
|
|
403
|
+ /**
|
|
404
|
+ * Fixes a certificate where the signature contains unused bits.
|
|
405
|
+ *
|
|
406
|
+ * @param string $cert
|
|
407
|
+ * @return mixed
|
|
408
|
+ */
|
|
409
|
+ private function fixSignatureUnusedBits($cert)
|
|
410
|
+ {
|
|
411
|
+ if (in_array(hash('sha256', $cert), $this->FIXCERTS)) {
|
|
412
|
+ $cert[strlen($cert) - 257] = "\0";
|
|
413
|
+ }
|
|
414
|
+ return $cert;
|
401
|
415
|
}
|
402
|
|
- return $cert;
|
403
|
|
- }
|
404
|
416
|
}
|
405
|
417
|
|
406
|
418
|
/**
|
|
@@ -408,22 +420,24 @@ class U2F {
|
408
|
420
|
*
|
409
|
421
|
* @package u2flib_server
|
410
|
422
|
*/
|
411
|
|
-class RegisterRequest {
|
412
|
|
- public $version = U2F_VERSION;
|
413
|
|
-
|
414
|
|
- public $challenge;
|
415
|
|
-
|
416
|
|
- public $appId;
|
417
|
|
-
|
418
|
|
- /**
|
419
|
|
- * @param string $challenge
|
420
|
|
- * @param string $appId
|
421
|
|
- * @internal
|
422
|
|
- */
|
423
|
|
- public function __construct($challenge, $appId) {
|
424
|
|
- $this->challenge = $challenge;
|
425
|
|
- $this->appId = $appId;
|
426
|
|
- }
|
|
423
|
+class RegisterRequest
|
|
424
|
+{
|
|
425
|
+ public $version = U2F_VERSION;
|
|
426
|
+
|
|
427
|
+ public $challenge;
|
|
428
|
+
|
|
429
|
+ public $appId;
|
|
430
|
+
|
|
431
|
+ /**
|
|
432
|
+ * @param string $challenge
|
|
433
|
+ * @param string $appId
|
|
434
|
+ * @internal
|
|
435
|
+ */
|
|
436
|
+ public function __construct($challenge, $appId)
|
|
437
|
+ {
|
|
438
|
+ $this->challenge = $challenge;
|
|
439
|
+ $this->appId = $appId;
|
|
440
|
+ }
|
427
|
441
|
}
|
428
|
442
|
|
429
|
443
|
/**
|
|
@@ -431,14 +445,15 @@ class RegisterRequest {
|
431
|
445
|
*
|
432
|
446
|
* @package u2flib_server
|
433
|
447
|
*/
|
434
|
|
-class SignRequest {
|
435
|
|
- public $version = U2F_VERSION;
|
|
448
|
+class SignRequest
|
|
449
|
+{
|
|
450
|
+ public $version = U2F_VERSION;
|
436
|
451
|
|
437
|
|
- public $challenge;
|
|
452
|
+ public $challenge;
|
438
|
453
|
|
439
|
|
- public $keyHandle;
|
|
454
|
+ public $keyHandle;
|
440
|
455
|
|
441
|
|
- public $appId;
|
|
456
|
+ public $appId;
|
442
|
457
|
}
|
443
|
458
|
|
444
|
459
|
/**
|
|
@@ -446,14 +461,15 @@ class SignRequest {
|
446
|
461
|
*
|
447
|
462
|
* @package u2flib_server
|
448
|
463
|
*/
|
449
|
|
-class Registration {
|
450
|
|
- public $keyHandle;
|
|
464
|
+class Registration
|
|
465
|
+{
|
|
466
|
+ public $keyHandle;
|
451
|
467
|
|
452
|
|
- public $publicKey;
|
|
468
|
+ public $publicKey;
|
453
|
469
|
|
454
|
|
- public $certificate;
|
|
470
|
+ public $certificate;
|
455
|
471
|
|
456
|
|
- public $counter = -1;
|
|
472
|
+ public $counter = -1;
|
457
|
473
|
}
|
458
|
474
|
|
459
|
475
|
/**
|
|
@@ -461,14 +477,16 @@ class Registration {
|
461
|
477
|
*
|
462
|
478
|
* @package u2flib_server
|
463
|
479
|
*/
|
464
|
|
-class Error extends \Exception {
|
465
|
|
- /**
|
466
|
|
- * Override constructor and make message and code mandatory
|
467
|
|
- * @param string $message
|
468
|
|
- * @param int $code
|
469
|
|
- * @param \Exception|null $previous
|
470
|
|
- */
|
471
|
|
- public function __construct($message, $code, \Exception $previous = null) {
|
472
|
|
- parent::__construct($message, $code, $previous);
|
473
|
|
- }
|
|
480
|
+class Error extends \Exception
|
|
481
|
+{
|
|
482
|
+ /**
|
|
483
|
+ * Override constructor and make message and code mandatory
|
|
484
|
+ * @param string $message
|
|
485
|
+ * @param int $code
|
|
486
|
+ * @param \Exception|null $previous
|
|
487
|
+ */
|
|
488
|
+ public function __construct($message, $code, \Exception $previous = null)
|
|
489
|
+ {
|
|
490
|
+ parent::__construct($message, $code, $previous);
|
|
491
|
+ }
|
474
|
492
|
}
|