| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\CryptoEncoding; |
| 6 |
|
| 7 |
use function is_string; |
| 8 |
use RuntimeException; |
| 9 |
use Stringable; |
| 10 |
use UnexpectedValueException; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
final class PEM implements Stringable |
| 18 |
{ |
| 19 |
|
| 20 |
final public const TYPE_CERTIFICATE = 'CERTIFICATE'; |
| 21 |
|
| 22 |
final public const TYPE_CRL = 'X509 CRL'; |
| 23 |
|
| 24 |
final public const TYPE_CERTIFICATE_REQUEST = 'CERTIFICATE REQUEST'; |
| 25 |
|
| 26 |
final public const TYPE_ATTRIBUTE_CERTIFICATE = 'ATTRIBUTE CERTIFICATE'; |
| 27 |
|
| 28 |
final public const TYPE_PRIVATE_KEY = 'PRIVATE KEY'; |
| 29 |
|
| 30 |
final public const TYPE_PUBLIC_KEY = 'PUBLIC KEY'; |
| 31 |
|
| 32 |
final public const TYPE_ENCRYPTED_PRIVATE_KEY = 'ENCRYPTED PRIVATE KEY'; |
| 33 |
|
| 34 |
final public const TYPE_RSA_PRIVATE_KEY = 'RSA PRIVATE KEY'; |
| 35 |
|
| 36 |
final public const TYPE_RSA_PUBLIC_KEY = 'RSA PUBLIC KEY'; |
| 37 |
|
| 38 |
final public const TYPE_EC_PRIVATE_KEY = 'EC PRIVATE KEY'; |
| 39 |
|
| 40 |
final public const TYPE_PKCS7 = 'PKCS7'; |
| 41 |
|
| 42 |
final public const TYPE_CMS = 'CMS'; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
final public const PEM_REGEX = '/' . |
| 50 |
|
| 51 |
'(?:^|[\r\n])' . |
| 52 |
|
| 53 |
'-----BEGIN (.+?)-----[\r\n]+' . |
| 54 |
|
| 55 |
'(.+?)' . |
| 56 |
|
| 57 |
'[\r\n]+-----END \\1-----' . |
| 58 |
'/ms'; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
private function __construct( |
| 65 |
private readonly string $type, |
| 66 |
private readonly string $data |
| 67 |
) { |
| 68 |
} |
| 69 |
|
| 70 |
public function __toString(): string |
| 71 |
{ |
| 72 |
return $this->string(); |
| 73 |
} |
| 74 |
|
| 75 |
public static function create(string $_type, string $_data): self |
| 76 |
{ |
| 77 |
return new self($_type, $_data); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public static function fromString(string $str): self |
| 84 |
{ |
| 85 |
if (preg_match(self::PEM_REGEX, $str, $match) !== 1) { |
| 86 |
throw new UnexpectedValueException('Not a PEM formatted string.'); |
| 87 |
} |
| 88 |
$payload = preg_replace('/\s+/', '', $match[2]); |
| 89 |
if (! is_string($payload)) { |
| 90 |
throw new UnexpectedValueException('Failed to decode PEM data.'); |
| 91 |
} |
| 92 |
$data = base64_decode($payload, true); |
| 93 |
if ($data === false) { |
| 94 |
throw new UnexpectedValueException('Failed to decode PEM data.'); |
| 95 |
} |
| 96 |
return self::create($match[1], $data); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public static function fromFile(string $filename): self |
| 105 |
{ |
| 106 |
if (! is_readable($filename)) { |
| 107 |
throw new RuntimeException("Failed to read {$filename}."); |
| 108 |
} |
| 109 |
$str = file_get_contents($filename); |
| 110 |
if ($str === false) { |
| 111 |
throw new RuntimeException("Failed to read {$filename}."); |
| 112 |
} |
| 113 |
return self::fromString($str); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function type(): string |
| 120 |
{ |
| 121 |
return $this->type; |
| 122 |
} |
| 123 |
|
| 124 |
public function data(): string |
| 125 |
{ |
| 126 |
return $this->data; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public function string(): string |
| 133 |
{ |
| 134 |
return "-----BEGIN {$this->type}-----\n" . |
| 135 |
trim(chunk_split(base64_encode($this->data), 64, "\n")) . "\n" . |
| 136 |
"-----END {$this->type}-----"; |
| 137 |
} |
| 138 |
} |