Passed
Push — master ( 3569aa...ab7ae0 )
by Florent
02:27
created

AttestationObjectLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace U2FAuthentication\Fido2\AttestationStatement;
15
16
use Base64Url\Base64Url;
17
use CBOR\Decoder;
18
use U2FAuthentication\Fido2\AttestedCredentialData;
19
use U2FAuthentication\Fido2\AuthenticatorData;
20
use U2FAuthentication\Fido2\StringStream;
21
22
class AttestationObjectLoader
23
{
24
    private const FLAG_AT = 0b01000000;
25
    private const FLAG_ED = 0b10000000;
26
27
    /**
28
     * @var Decoder
29
     */
30
    private $decoder;
31
32
    public function __construct(Decoder $decoder)
33
    {
34
        $this->decoder = $decoder;
35
    }
36
37
    public function load(string $data): AttestationObject
38
    {
39
        $decodedData = Base64Url::decode($data);
40
        $stream = new StringStream($decodedData);
41
        $attestationObject = $this->decoder->decode($stream)->getNormalizedData();
42
        $authData = $attestationObject['authData'];
43
44
        $authDataStream = new StringStream($authData);
45
        $rp_id_hash = $authDataStream->read(32);
46
        $flags = $authDataStream->read(1);
47
        $signCount = $authDataStream->read(4);
48
        $signCount = unpack('l', $signCount)[1];
49
50
        if (\ord($flags) & self::FLAG_AT) {
51
            $aaguid = $authDataStream->read(16);
52
            $credentialLength = $authDataStream->read(2);
53
            $credentialLength = unpack('n', $credentialLength)[1];
54
            $credentialId = $authDataStream->read($credentialLength);
55
            $credentialPublicKey = $this->decoder->decode($authDataStream);
56
            //TODO: should be converted into a COSE Key
57
            $attestedCredentialData = new AttestedCredentialData($aaguid, $credentialId, $credentialPublicKey->getNormalizedData());
0 ignored issues
show
Bug introduced by
It seems like $credentialPublicKey->getNormalizedData() can also be of type null and string; however, parameter $credentialPublicKey of U2FAuthentication\Fido2\...tialData::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
            $attestedCredentialData = new AttestedCredentialData($aaguid, $credentialId, /** @scrutinizer ignore-type */ $credentialPublicKey->getNormalizedData());
Loading history...
58
        } else {
59
            $attestedCredentialData = null;
60
        }
61
62
        if (\ord($flags) & self::FLAG_ED) {
63
            //TODO: should be correctly handled
64
            $extension = $this->decoder->decode($authDataStream);
65
        } else {
66
            $extension = null;
67
        }
68
        $authenticatorData = new AuthenticatorData(
69
            $rp_id_hash,
70
            $flags,
71
            $signCount,
72
            $attestedCredentialData,
73
            $extension
74
        );
75
76
        return new AttestationObject(
77
            $data,
78
            new AttestationStatement($attestationObject['fmt'], $attestationObject['attStmt']),
79
            $authenticatorData
80
        );
81
    }
82
}
83