Completed
Push — master ( abae97...fc5d0c )
by Breno
03:37 queued 01:48
created

Certificate::publicKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace XmlSigner;
4
5
use XmlSigner\Exception\CertificateException;
6
7
class Certificate implements SignerInterface, VerifierInterface
8
{
9
    /**
10
     * Read a PFX certificate and return this class
11
     * @param string $content
12
     * @param string $password
13
     * @return Certificate
14
     * @throws CertificateException
15
     */
16 5
    public static function readPfx($content, $password)
17
    {
18 5
        $certs = [];
19 5
        if (!openssl_pkcs12_read($content, $certs, $password)) {
20 1
            throw CertificateException::unableToRead();
21
        }
22 4
        $chain = '';
23 4
        if (!empty($certs['extracerts'])) {
24 4
            foreach ($certs['extracerts'] as $ec) {
25 4
                $chain .= $ec;
26
            }
27
        }
28 4
        return new Certificate(
29 4
            new PrivateKey($certs['pkey']),
30 4
            new PublicKey($certs['cert']),
31 4
            $chain
32
        );
33
    }
34
35
    private $privateKey;
36
    private $publicKey;
37
    private $chainKeysString;
38
39 4
    public function __construct(
40
        PrivateKey $privateKey,
41
        PublicKey $publicKey,
42
        $chainKeysString = null
43
    ) {
44 4
        $this->privateKey = $privateKey;
45 4
        $this->publicKey = $publicKey;
46 4
        $this->chainKeysString = $chainKeysString;
47 4
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 2
    public function sign($content, $algorithm = OPENSSL_ALGO_SHA1)
53
    {
54 2
        return $this->privateKey->sign($content, $algorithm);
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 1
    public function verify($data, $signature, $algorithm = OPENSSL_ALGO_SHA1)
61
    {
62 1
        return $this->publicKey->verify($data, $signature, $algorithm);
63
    }
64
65 1
    public function publicKey() : string
66
    {
67 1
        return $this->publicKey->unformated();
68
    }
69
}
70