summaryrefslogtreecommitdiffstats
path: root/library/X509/CertificateDetails.php
blob: f28e4232aabe4c52bddc8e973b583227c160eda9 (plain)
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
<?php

// Icinga Web 2 X.509 Module | (c) 2018 Icinga GmbH | GPLv2

namespace Icinga\Module\X509;

use DateTime;
use Icinga\Module\X509\Model\X509Certificate;
use ipl\Html\BaseHtmlElement;
use ipl\Html\Html;
use ipl\Web\Widget\IcingaIcon;

/**
 * Widget to display X.509 certificate details
 */
class CertificateDetails extends BaseHtmlElement
{
    protected $tag = 'div';

    protected $defaultAttributes = ['class' => 'cert-details'];

    /**
     * @var X509Certificate
     */
    protected $cert;

    public function setCert(X509Certificate $cert)
    {
        $this->cert = $cert;

        return $this;
    }

    protected function assemble()
    {
        $pem = $this->cert->certificate;
        $cert = openssl_x509_parse($pem);
//        $pubkey = openssl_pkey_get_details(openssl_get_publickey($pem));

        $subject = Html::tag('dl');
        $sans = CertificateUtils::splitSANs($cert['extensions']['subjectAltName'] ?? null);
        if (! isset($cert['subject']['CN']) && ! empty($sans)) {
            foreach ($sans as $type => $values) {
                foreach ($values as $value) {
                    $subject->addHtml(Html::tag('dt', $type), Html::tag('dd', $value));
                }
            }
        } else {
            foreach ($cert['subject'] as $key => $value) {
                $subject->add([
                    Html::tag('dt', $key),
                    Html::tag('dd', $value)
                ]);
            }
        }

        $issuer = Html::tag('dl');
        foreach ($cert['issuer'] as $key => $value) {
            $issuer->add([
                Html::tag('dt', $key),
                Html::tag('dd', $value)
            ]);
        }

        $certInfo = Html::tag('dl');
        $certInfo->add([
            Html::tag('dt', mt('x509', 'Serial Number')),
            Html::tag('dd', bin2hex($this->cert->serial)),
            Html::tag('dt', mt('x509', 'Version')),
            Html::tag('dd', $this->cert->version),
            Html::tag('dt', mt('x509', 'Signature Algorithm')),
            Html::tag('dd', $this->cert->signature_algo . ' with ' . $this->cert->signature_hash_algo),
            Html::tag('dt', mt('x509', 'Not Valid Before')),
            Html::tag('dd', $this->cert->valid_from->format('l F jS, Y H:i:s e')),
            Html::tag('dt', mt('x509', 'Not Valid After')),
            Html::tag('dd', $this->cert->valid_to->format('l F jS, Y H:i:s e')),
        ]);

        $pubkeyInfo = Html::tag('dl');
        $pubkeyInfo->add([
            Html::tag('dt', mt('x509', 'Algorithm')),
            Html::tag('dd', $this->cert->pubkey_algo),
            Html::tag('dt', mt('x509', 'Key Size')),
            Html::tag('dd', $this->cert->pubkey_bits)
        ]);

        $extensions = Html::tag('dl');
        foreach ($cert['extensions'] as $key => $value) {
            $extensions->add([
                Html::tag('dt', ucwords(implode(' ', preg_split('/(?=[A-Z])/', $key)))),
                Html::tag('dd', $value)
            ]);
        }

        $fingerprints = Html::tag('dl');
        $fingerprints->add([
            Html::tag('dt', 'SHA-256'),
            Html::tag(
                'dd',
                wordwrap(strtoupper(bin2hex($this->cert->fingerprint)), 2, ' ', true)
            )
        ]);

        $this->add([
            Html::tag('h2', [new IcingaIcon('certificate'), $this->cert->subject]),
            Html::tag('h3', mt('x509', 'Subject Name')),
            $subject,
            Html::tag('h3', mt('x509', 'Issuer Name')),
            $issuer,
            Html::tag('h3', mt('x509', 'Certificate Info')),
            $certInfo,
            Html::tag('h3', mt('x509', 'Public Key Info')),
            $pubkeyInfo,
            Html::tag('h3', mt('x509', 'Extensions')),
            $extensions,
            Html::tag('h3', mt('x509', 'Fingerprints')),
            $fingerprints
        ]);
    }
}