Completed
Pull Request — stable8.2 (#24197)
by Joas
11:58
created

Certificate::__construct()   C

Complexity

Conditions 7
Paths 18

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
ccs 15
cts 15
cp 1
rs 6.9811
cc 7
eloc 15
nc 18
nop 2
crap 7
1
<?php
2
/**
3
 * @author Lukas Reschke <[email protected]>
4
 * @author Morris Jobke <[email protected]>
5
 * @author Robin Appelman <[email protected]>
6
 *
7
 * @copyright Copyright (c) 2015, ownCloud, Inc.
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OC\Security;
25
26
use OCP\ICertificate;
27
28
class Certificate implements ICertificate {
29
	protected $name;
30
31
	protected $commonName;
32
33
	protected $organization;
34
35
	protected $serial;
36
37
	protected $issueDate;
38
39
	protected $expireDate;
40
41
	protected $issuerName;
42
43
	protected $issuerOrganization;
44
45
	/**
46
	 * @param string $data base64 encoded certificate
47
	 * @param string $name
48
	 * @throws \Exception If the certificate could not get parsed
49
	 */
50 12
	public function __construct($data, $name) {
51 12
		$this->name = $name;
52 12
		$gmt = new \DateTimeZone('GMT');
53 12
54 12
		// If string starts with "file://" ignore the certificate
55 2
		$query = 'file://';
56
		if(strtolower(substr($data, 0, strlen($query))) === $query) {
57
			throw new \Exception('Certificate could not get parsed.');
58 11
		}
59 11
60 11
		$info = openssl_x509_parse($data);
61 11
		if(!is_array($info)) {
62 11
			throw new \Exception('Certificate could not get parsed.');
63 11
		}
64 11
65
		$this->commonName = isset($info['subject']['CN']) ? $info['subject']['CN'] : null;
66
		$this->organization = isset($info['subject']['O']) ? $info['subject']['O'] : null;
67
		$this->issueDate = new \DateTime('@' . $info['validFrom_time_t'], $gmt);
68
		$this->expireDate = new \DateTime('@' . $info['validTo_time_t'], $gmt);
69 3
		$this->issuerName = isset($info['issuer']['CN']) ? $info['issuer']['CN'] : null;
70 3
		$this->issuerOrganization = isset($info['issuer']['O']) ? $info['issuer']['O'] : null;
71
	}
72
73
	/**
74
	 * @return string
75
	 */
76 1
	public function getName() {
77 1
		return $this->name;
78
	}
79
80
	/**
81
	 * @return string|null
82
	 */
83 1
	public function getCommonName() {
84 1
		return $this->commonName;
85
	}
86
87
	/**
88
	 * @return string
89
	 */
90 1
	public function getOrganization() {
91 1
		return $this->organization;
92
	}
93
94
	/**
95
	 * @return \DateTime
96
	 */
97 1
	public function getIssueDate() {
98 1
		return $this->issueDate;
99
	}
100
101
	/**
102
	 * @return \DateTime
103
	 */
104 1
	public function getExpireDate() {
105 1
		return $this->expireDate;
106 1
	}
107
108
	/**
109
	 * @return bool
110
	 */
111
	public function isExpired() {
112 1
		$now = new \DateTime();
113 1
		return $this->issueDate > $now or $now > $this->expireDate;
114
	}
115
116
	/**
117
	 * @return string|null
118
	 */
119 1
	public function getIssuerName() {
120 1
		return $this->issuerName;
121
	}
122
123
	/**
124
	 * @return string|null
125
	 */
126
	public function getIssuerOrganization() {
127
		return $this->issuerOrganization;
128
	}
129
}
130