1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sop\CryptoEncoding; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Implements PEM file encoding and decoding. |
7
|
|
|
* |
8
|
|
|
* @link https://tools.ietf.org/html/rfc7468 |
9
|
|
|
*/ |
10
|
|
|
class PEM |
11
|
|
|
{ |
12
|
|
|
// well-known PEM types |
13
|
|
|
const TYPE_CERTIFICATE = "CERTIFICATE"; |
14
|
|
|
const TYPE_CRL = "X509 CRL"; |
15
|
|
|
const TYPE_CERTIFICATE_REQUEST = "CERTIFICATE REQUEST"; |
16
|
|
|
const TYPE_ATTRIBUTE_CERTIFICATE = "ATTRIBUTE CERTIFICATE"; |
17
|
|
|
const TYPE_PRIVATE_KEY = "PRIVATE KEY"; |
18
|
|
|
const TYPE_PUBLIC_KEY = "PUBLIC KEY"; |
19
|
|
|
const TYPE_ENCRYPTED_PRIVATE_KEY = "ENCRYPTED PRIVATE KEY"; |
20
|
|
|
const TYPE_RSA_PRIVATE_KEY = "RSA PRIVATE KEY"; |
21
|
|
|
const TYPE_RSA_PUBLIC_KEY = "RSA PUBLIC KEY"; |
22
|
|
|
const TYPE_EC_PRIVATE_KEY = "EC PRIVATE KEY"; |
23
|
|
|
const TYPE_PKCS7 = "PKCS7"; |
24
|
|
|
const TYPE_CMS = "CMS"; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Regular expression to match PEM block. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
const PEM_REGEX = /* @formatter:off */ '/' . |
32
|
|
|
/* line start */ '(?:^|[\r\n])' . |
33
|
|
|
/* header */ '-----BEGIN (.+?)-----[\r\n]+' . |
34
|
|
|
/* payload */ '(.+?)' . |
35
|
|
|
/* trailer */ '[\r\n]+-----END \\1-----' . |
36
|
|
|
'/ms'; /* @formatter:on */ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Content type. |
40
|
|
|
* |
41
|
|
|
* @var string $_type |
42
|
|
|
*/ |
43
|
|
|
protected $_type; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Payload. |
47
|
|
|
* |
48
|
|
|
* @var string $_data |
49
|
|
|
*/ |
50
|
|
|
protected $_data; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Constructor. |
54
|
|
|
* |
55
|
|
|
* @param string $type Content type |
56
|
|
|
* @param string $data Payload |
57
|
|
|
*/ |
58
|
5 |
|
public function __construct($type, $data) |
59
|
|
|
{ |
60
|
5 |
|
$this->_type = $type; |
61
|
5 |
|
$this->_data = $data; |
62
|
5 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Initialize from a PEM-formatted string. |
66
|
|
|
* |
67
|
|
|
* @param string $str |
68
|
|
|
* @throws \UnexpectedValueException If string is not valid PEM |
69
|
|
|
* @return self |
70
|
|
|
*/ |
71
|
5 |
|
public static function fromString($str) |
72
|
|
|
{ |
73
|
5 |
|
if (!preg_match(self::PEM_REGEX, $str, $match)) { |
74
|
1 |
|
throw new \UnexpectedValueException("Not a PEM formatted string."); |
75
|
|
|
} |
76
|
4 |
|
$payload = preg_replace('/\s+/', "", $match[2]); |
77
|
4 |
|
$data = base64_decode($payload, true); |
78
|
4 |
|
if (false === $data) { |
79
|
1 |
|
throw new \UnexpectedValueException("Failed to decode PEM data."); |
80
|
|
|
} |
81
|
3 |
|
return new self($match[1], $data); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Initialize from a file. |
86
|
|
|
* |
87
|
|
|
* @param string $filename Path to file |
88
|
|
|
* @throws \RuntimeException If file reading fails |
89
|
|
|
* @return self |
90
|
|
|
*/ |
91
|
2 |
View Code Duplication |
public static function fromFile($filename) |
|
|
|
|
92
|
|
|
{ |
93
|
2 |
|
if (!is_readable($filename) || |
94
|
2 |
|
false === ($str = file_get_contents($filename))) { |
95
|
1 |
|
throw new \RuntimeException("Failed to read $filename."); |
96
|
|
|
} |
97
|
1 |
|
return self::fromString($str); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get content type. |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
1 |
|
public function type() |
106
|
|
|
{ |
107
|
1 |
|
return $this->_type; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get payload. |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
1 |
|
public function data() |
116
|
|
|
{ |
117
|
1 |
|
return $this->_data; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Encode to PEM string. |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
4 |
|
public function string() |
126
|
|
|
{ |
127
|
4 |
|
return "-----BEGIN {$this->_type}-----\n" . |
128
|
4 |
|
trim(chunk_split(base64_encode($this->_data), 64, "\n")) . "\n" . |
129
|
4 |
|
"-----END {$this->_type}-----"; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
1 |
|
public function __toString() |
137
|
|
|
{ |
138
|
1 |
|
return $this->string(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.