This repository was archived by the owner on Dec 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPKCS12.class.php
More file actions
58 lines (53 loc) · 1.79 KB
/
PKCS12.class.php
File metadata and controls
58 lines (53 loc) · 1.79 KB
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
<?php
/**
*
* A PKCS12 container, storing a certificate and public and private keys.
* @author Anders
* @property PublicKey $publicKey
* @property PrivateKey $privateKey
* @property X509Certificate $certificate
*
*/
class PKCS12 extends KeyStore {
private $X509Certificate = null;
private $privateKey = null;
/**
* Represents a PKCS12 keystore.
* @param string $contents The contents of the PKCS12 keystore.
*/
public function __construct($contents, $passphrase) {
if(!extension_loaded('openssl'))
throw new OpenSSLExtensionNotLoadedException('The openssl module is not loaded.');
if(!openssl_pkcs12_read($contents, $keystore, $passphrase))
throw new KeyStoreDecryptionFailedException(
'Could not decrypt the certificate, the passphrase is incorrect, '.
'its contents are mangled or it is not a valid PKCS #12 keystore.');
$this->X509Certificate = new X509Certificate($keystore['cert']);
$this->privateKey = new PrivateKey($keystore['pkey']);
}
/**
* Initialize the PKCS12 keystore from a file.
* @param string $keystoreLocation
* @throws FileNotFoundException
* @throws FileNotReadableException
*/
public static function initFromFile($keystoreLocation, $passphrase) {
if(!file_exists($keystoreLocation))
throw new FileNotFoundException("The keystore file '$keystoreLocation' does not exist.");
if(!is_readable($keystoreLocation))
throw new FileNotReadableException("The keystore file '$keystoreLocation' is not readable.");
return new self(file_get_contents($keystoreLocation), $passphrase);
}
public function __get($name) {
switch($name) {
case 'publicKey':
return $this->X509Certificate->publicKey;
case 'privateKey':
return $this->privateKey;
case 'certificate':
return $this->X509Certificate;
default:
return null;
}
}
}