Completed
Pull Request — master (#49)
by
unknown
03:19
created
src/u2flib_server/U2F.php 1 patch
Spacing   +45 added lines, -45 removed lines patch added patch discarded remove patch
@@ -97,8 +97,8 @@  discard block
 block discarded – undo
97 97
      */
98 98
     public function __construct($appId, $attestDir = null)
99 99
     {
100
-        if(OPENSSL_VERSION_NUMBER < 0x10000000) {
101
-            throw new Error('OpenSSL has to be at least version 1.0.0, this is ' . OPENSSL_VERSION_TEXT, ERR_OLD_OPENSSL);
100
+        if (OPENSSL_VERSION_NUMBER < 0x10000000) {
101
+            throw new Error('OpenSSL has to be at least version 1.0.0, this is '.OPENSSL_VERSION_TEXT, ERR_OLD_OPENSSL);
102 102
         }
103 103
         $this->appId = $appId;
104 104
         $this->attestDir = $attestDir;
@@ -135,19 +135,19 @@  discard block
 block discarded – undo
135 135
      */
136 136
     public function doRegister($request, $response, $includeCert = true)
137 137
     {
138
-        if( !is_object( $request ) ) {
138
+        if (!is_object($request)) {
139 139
             throw new \InvalidArgumentException('$request of doRegister() method only accepts object.');
140 140
         }
141 141
 
142
-        if( !is_object( $response ) ) {
142
+        if (!is_object($response)) {
143 143
             throw new \InvalidArgumentException('$response of doRegister() method only accepts object.');
144 144
         }
145 145
 
146
-        if( property_exists( $response, 'errorCode') && $response->errorCode !== 0 ) {
147
-            throw new Error('User-agent returned error. Error code: ' . $response->errorCode, ERR_BAD_UA_RETURNING );
146
+        if (property_exists($response, 'errorCode') && $response->errorCode !== 0) {
147
+            throw new Error('User-agent returned error. Error code: '.$response->errorCode, ERR_BAD_UA_RETURNING);
148 148
         }
149 149
 
150
-        if( !is_bool( $includeCert ) ) {
150
+        if (!is_bool($includeCert)) {
151 151
             throw new \InvalidArgumentException('$include_cert of doRegister() method only accepts boolean.');
152 152
         }
153 153
 
@@ -156,8 +156,8 @@  discard block
 block discarded – undo
156 156
         $clientData = $this->base64u_decode($response->clientData);
157 157
         $cli = json_decode($clientData);
158 158
 
159
-        if($cli->challenge !== $request->challenge) {
160
-            throw new Error('Registration challenge does not match', ERR_UNMATCHED_CHALLENGE );
159
+        if ($cli->challenge !== $request->challenge) {
160
+            throw new Error('Registration challenge does not match', ERR_UNMATCHED_CHALLENGE);
161 161
         }
162 162
 
163 163
         $registration = new Registration();
@@ -166,8 +166,8 @@  discard block
 block discarded – undo
166 166
         $offs += PUBKEY_LEN;
167 167
         // decode the pubKey to make sure it's good
168 168
         $tmpKey = $this->pubkey_to_pem($pubKey);
169
-        if($tmpKey === null) {
170
-            throw new Error('Decoding of public key failed', ERR_PUBKEY_DECODE );
169
+        if ($tmpKey === null) {
170
+            throw new Error('Decoding of public key failed', ERR_PUBKEY_DECODE);
171 171
         }
172 172
         $registration->publicKey = base64_encode($pubKey);
173 173
         $khLen = $regData[$offs++];
@@ -185,17 +185,17 @@  discard block
 block discarded – undo
185 185
         $pemCert  = "-----BEGIN CERTIFICATE-----\r\n";
186 186
         $pemCert .= chunk_split(base64_encode($rawCert), 64);
187 187
         $pemCert .= "-----END CERTIFICATE-----";
188
-        if($includeCert) {
188
+        if ($includeCert) {
189 189
             $registration->certificate = base64_encode($rawCert);
190 190
         }
191
-        if($this->attestDir) {
192
-            if(openssl_x509_checkpurpose($pemCert, -1, $this->get_certs()) !== true) {
193
-                throw new Error('Attestation certificate can not be validated', ERR_ATTESTATION_VERIFICATION );
191
+        if ($this->attestDir) {
192
+            if (openssl_x509_checkpurpose($pemCert, -1, $this->get_certs()) !== true) {
193
+                throw new Error('Attestation certificate can not be validated', ERR_ATTESTATION_VERIFICATION);
194 194
             }
195 195
         }
196 196
 
197
-        if(!openssl_pkey_get_public($pemCert)) {
198
-            throw new Error('Decoding of public key failed', ERR_PUBKEY_DECODE );
197
+        if (!openssl_pkey_get_public($pemCert)) {
198
+            throw new Error('Decoding of public key failed', ERR_PUBKEY_DECODE);
199 199
         }
200 200
         $signature = substr($rawReg, $offs);
201 201
 
@@ -205,10 +205,10 @@  discard block
 block discarded – undo
205 205
         $dataToVerify .= $kh;
206 206
         $dataToVerify .= $pubKey;
207 207
 
208
-        if(openssl_verify($dataToVerify, $signature, $pemCert, 'sha256') === 1) {
208
+        if (openssl_verify($dataToVerify, $signature, $pemCert, 'sha256') === 1) {
209 209
             return $registration;
210 210
         } else {
211
-            throw new Error('Attestation signature does not match', ERR_ATTESTATION_SIGNATURE );
211
+            throw new Error('Attestation signature does not match', ERR_ATTESTATION_SIGNATURE);
212 212
         }
213 213
     }
214 214
 
@@ -223,7 +223,7 @@  discard block
 block discarded – undo
223 223
     {
224 224
         $sigs = array();
225 225
         foreach ($registrations as $reg) {
226
-            if( !is_object( $reg ) ) {
226
+            if (!is_object($reg)) {
227 227
                 throw new \InvalidArgumentException('$registrations of getAuthenticateData() method only accepts array of object.');
228 228
             }
229 229
 
@@ -252,12 +252,12 @@  discard block
 block discarded – undo
252 252
      */
253 253
     public function doAuthenticate(array $requests, array $registrations, $response)
254 254
     {
255
-        if( !is_object( $response ) ) {
255
+        if (!is_object($response)) {
256 256
             throw new \InvalidArgumentException('$response of doAuthenticate() method only accepts object.');
257 257
         }
258 258
 
259
-        if( property_exists( $response, 'errorCode') && $response->errorCode !== 0 ) {
260
-            throw new Error('User-agent returned error. Error code: ' . $response->errorCode, ERR_BAD_UA_RETURNING );
259
+        if (property_exists($response, 'errorCode') && $response->errorCode !== 0) {
260
+            throw new Error('User-agent returned error. Error code: '.$response->errorCode, ERR_BAD_UA_RETURNING);
261 261
         }
262 262
 
263 263
         /** @var object|null $req */
@@ -269,35 +269,35 @@  discard block
 block discarded – undo
269 269
         $clientData = $this->base64u_decode($response->clientData);
270 270
         $decodedClient = json_decode($clientData);
271 271
         foreach ($requests as $req) {
272
-            if( !is_object( $req ) ) {
272
+            if (!is_object($req)) {
273 273
                 throw new \InvalidArgumentException('$requests of doAuthenticate() method only accepts array of object.');
274 274
             }
275 275
 
276
-            if($req->keyHandle === $response->keyHandle && $req->challenge === $decodedClient->challenge) {
276
+            if ($req->keyHandle === $response->keyHandle && $req->challenge === $decodedClient->challenge) {
277 277
                 break;
278 278
             }
279 279
 
280 280
             $req = null;
281 281
         }
282
-        if($req === null) {
283
-            throw new Error('No matching request found', ERR_NO_MATCHING_REQUEST );
282
+        if ($req === null) {
283
+            throw new Error('No matching request found', ERR_NO_MATCHING_REQUEST);
284 284
         }
285 285
         foreach ($registrations as $reg) {
286
-            if( !is_object( $reg ) ) {
286
+            if (!is_object($reg)) {
287 287
                 throw new \InvalidArgumentException('$registrations of doAuthenticate() method only accepts array of object.');
288 288
             }
289 289
 
290
-            if($reg->keyHandle === $response->keyHandle) {
290
+            if ($reg->keyHandle === $response->keyHandle) {
291 291
                 break;
292 292
             }
293 293
             $reg = null;
294 294
         }
295
-        if($reg === null) {
296
-            throw new Error('No matching registration found', ERR_NO_MATCHING_REGISTRATION );
295
+        if ($reg === null) {
296
+            throw new Error('No matching registration found', ERR_NO_MATCHING_REGISTRATION);
297 297
         }
298 298
         $pemKey = $this->pubkey_to_pem($this->base64u_decode($reg->publicKey));
299
-        if($pemKey === null) {
300
-            throw new Error('Decoding of public key failed', ERR_PUBKEY_DECODE );
299
+        if ($pemKey === null) {
300
+            throw new Error('Decoding of public key failed', ERR_PUBKEY_DECODE);
301 301
         }
302 302
 
303 303
         $signData = $this->base64u_decode($response->signatureData);
@@ -306,18 +306,18 @@  discard block
 block discarded – undo
306 306
         $dataToVerify .= hash('sha256', $clientData, true);
307 307
         $signature = substr($signData, 5);
308 308
 
309
-        if(openssl_verify($dataToVerify, $signature, $pemKey, 'sha256') === 1) {
309
+        if (openssl_verify($dataToVerify, $signature, $pemKey, 'sha256') === 1) {
310 310
             $ctr = unpack("Nctr", substr($signData, 1, 4));
311 311
             $counter = $ctr['ctr'];
312 312
             /* TODO: wrap-around should be handled somehow.. */
313
-            if($counter > $reg->counter) {
313
+            if ($counter > $reg->counter) {
314 314
                 $reg->counter = $counter;
315 315
                 return $reg;
316 316
             } else {
317
-                throw new Error('Counter too low.', ERR_COUNTER_TOO_LOW );
317
+                throw new Error('Counter too low.', ERR_COUNTER_TOO_LOW);
318 318
             }
319 319
         } else {
320
-            throw new Error('Authentication failed', ERR_AUTHENTICATION_FAILURE );
320
+            throw new Error('Authentication failed', ERR_AUTHENTICATION_FAILURE);
321 321
         }
322 322
     }
323 323
 
@@ -328,9 +328,9 @@  discard block
 block discarded – undo
328 328
     {
329 329
         $files = array();
330 330
         $dir = $this->attestDir;
331
-        if($dir && $handle = opendir($dir)) {
332
-            while(false !== ($entry = readdir($handle))) {
333
-                if(is_file("$dir/$entry")) {
331
+        if ($dir && $handle = opendir($dir)) {
332
+            while (false !== ($entry = readdir($handle))) {
333
+                if (is_file("$dir/$entry")) {
334 334
                     $files[] = "$dir/$entry";
335 335
                 }
336 336
             }
@@ -363,7 +363,7 @@  discard block
 block discarded – undo
363 363
      */
364 364
     private function pubkey_to_pem($key)
365 365
     {
366
-        if(strlen($key) !== PUBKEY_LEN || $key[0] !== "\x04") {
366
+        if (strlen($key) !== PUBKEY_LEN || $key[0] !== "\x04") {
367 367
             return null;
368 368
         }
369 369
 
@@ -394,12 +394,12 @@  discard block
 block discarded – undo
394 394
      */
395 395
     private function createChallenge()
396 396
     {
397
-        $challenge = openssl_random_pseudo_bytes(32, $crypto_strong );
398
-        if( $crypto_strong !== true ) {
397
+        $challenge = openssl_random_pseudo_bytes(32, $crypto_strong);
398
+        if ($crypto_strong !== true) {
399 399
             throw new Error('Unable to obtain a good source of randomness', ERR_BAD_RANDOM);
400 400
         }
401 401
 
402
-        $challenge = $this->base64u_encode( $challenge );
402
+        $challenge = $this->base64u_encode($challenge);
403 403
 
404 404
         return $challenge;
405 405
     }
@@ -412,7 +412,7 @@  discard block
 block discarded – undo
412 412
      */
413 413
     private function fixSignatureUnusedBits($cert)
414 414
     {
415
-        if(in_array(hash('sha256', $cert), $this->FIXCERTS)) {
415
+        if (in_array(hash('sha256', $cert), $this->FIXCERTS)) {
416 416
             $cert[strlen($cert) - 257] = "\0";
417 417
         }
418 418
         return $cert;
Please login to merge, or discard this patch.