Completed
Pull Request — stable8 (#24168)
by Lukas
09:47
created

Certificate::__construct()   C

Complexity

Conditions 7
Paths 42

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 22
rs 6.9811
c 1
b 0
f 0
cc 7
eloc 16
nc 42
nop 2
1
<?php
2
/**
3
 * Copyright (c) 2014 Robin Appelman <[email protected]>
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later.
6
 * See the COPYING-README file.
7
 */
8
9
namespace OC\Security;
10
11
use OCP\ICertificate;
12
13
class Certificate implements ICertificate {
14
	protected $name;
15
16
	protected $commonName;
17
18
	protected $organization;
19
20
	protected $serial;
21
22
	protected $issueDate;
23
24
	protected $expireDate;
25
26
	protected $issuerName;
27
28
	protected $issuerOrganization;
29
30
	/**
31
	 * @param string $data base64 encoded certificate
32
	 * @param string $name
33
	 * @throws \Exception If the certificate could not get parsed
34
	 */
35
	public function __construct($data, $name) {
36
		$this->name = $name;
37
38
		// If string starts with "file://" ignore the certificate
39
		$query = 'file://';
40
		if(strtolower(substr($data, 0, strlen($query))) === $query) {
41
			throw new \Exception('Certificate could not get parsed.');
42
		}
43
44
		try {
45
			$gmt = new \DateTimeZone('GMT');
46
			$info = openssl_x509_parse($data);
47
			$this->commonName = isset($info['subject']['CN']) ? $info['subject']['CN'] : null;
48
			$this->organization = isset($info['subject']['O']) ? $info['subject']['O'] : null;
49
			$this->issueDate = new \DateTime('@' . $info['validFrom_time_t'], $gmt);
50
			$this->expireDate = new \DateTime('@' . $info['validTo_time_t'], $gmt);
51
			$this->issuerName = isset($info['issuer']['CN']) ? $info['issuer']['CN'] : null;
52
			$this->issuerOrganization = isset($info['issuer']['O']) ? $info['issuer']['O'] : null;
53
		} catch (\Exception $e) {
54
			throw new \Exception('Certificate could not get parsed.');
55
		}
56
	}
57
58
	/**
59
	 * @return string
60
	 */
61
	public function getName() {
62
		return $this->name;
63
	}
64
65
	/**
66
	 * @return string|null
67
	 */
68
	public function getCommonName() {
69
		return $this->commonName;
70
	}
71
72
	/**
73
	 * @return string
74
	 */
75
	public function getOrganization() {
76
		return $this->organization;
77
	}
78
79
	/**
80
	 * @return \DateTime
81
	 */
82
	public function getIssueDate() {
83
		return $this->issueDate;
84
	}
85
86
	/**
87
	 * @return \DateTime
88
	 */
89
	public function getExpireDate() {
90
		return $this->expireDate;
91
	}
92
93
	/**
94
	 * @return bool
95
	 */
96
	public function isExpired() {
97
		$now = new \DateTime();
98
		return $this->issueDate > $now or $now > $this->expireDate;
99
	}
100
101
	/**
102
	 * @return string|null
103
	 */
104
	public function getIssuerName() {
105
		return $this->issuerName;
106
	}
107
108
	/**
109
	 * @return string|null
110
	 */
111
	public function getIssuerOrganization() {
112
		return $this->issuerOrganization;
113
	}
114
}
115