1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Create an end-entity certificate based on CSR and sign using CA certificate. |
4
|
|
|
* |
5
|
|
|
* php issue-cert.php <(php create-ca-cert.php) <(php create-csr.php) |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
use Sop\CryptoEncoding\PEM; |
9
|
|
|
use Sop\CryptoTypes\AlgorithmIdentifier\Hash\SHA512AlgorithmIdentifier; |
10
|
|
|
use Sop\CryptoTypes\AlgorithmIdentifier\Signature\SignatureAlgorithmIdentifierFactory; |
11
|
|
|
use Sop\CryptoTypes\Asymmetric\PrivateKeyInfo; |
12
|
|
|
use X509\Certificate\Certificate; |
13
|
|
|
use X509\Certificate\TBSCertificate; |
14
|
|
|
use X509\Certificate\Validity; |
15
|
|
|
use X509\Certificate\Extension\BasicConstraintsExtension; |
16
|
|
|
use X509\Certificate\Extension\KeyUsageExtension; |
17
|
|
|
use X509\CertificationRequest\CertificationRequest; |
18
|
|
|
|
19
|
|
|
require dirname(__DIR__) . "/vendor/autoload.php"; |
20
|
|
|
|
21
|
|
|
$argc == 3 or printf("Usage: %s <ca-path> <csr-path>\n", $argv[0]) and exit(1); |
22
|
|
|
|
23
|
|
|
// load issuer certificate from PEM |
24
|
|
|
$issuer_cert = Certificate::fromPEM(PEM::fromFile($argv[1])); |
25
|
|
|
|
26
|
|
|
// load certification request from PEM |
27
|
|
|
$csr = CertificationRequest::fromPEM(PEM::fromFile($argv[2])); |
28
|
|
|
|
29
|
|
|
// verify CSR |
30
|
|
|
if (!$csr->verify()) { |
31
|
|
|
echo "Failed to verify certification request signature.\n"; |
32
|
|
|
exit(1); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// load CA's private key from PEM |
36
|
|
|
$private_key_info = PrivateKeyInfo::fromPEM( |
37
|
|
|
PEM::fromFile(dirname(__DIR__) . "/test/assets/rsa/private_key.pem")); |
38
|
|
|
|
39
|
|
|
// initialize certificate from CSR and issuer's certificate |
40
|
|
|
$tbs_cert = TBSCertificate::fromCSR($csr)->withIssuerCertificate($issuer_cert); |
41
|
|
|
|
42
|
|
|
// set random serial number |
43
|
|
|
$tbs_cert = $tbs_cert->withRandomSerialNumber(); |
44
|
|
|
|
45
|
|
|
// set validity period |
46
|
|
|
$tbs_cert = $tbs_cert->withValidity( |
47
|
|
|
Validity::fromStrings("now", "now + 3 months")); |
48
|
|
|
|
49
|
|
|
// add extensions |
50
|
|
|
$tbs_cert = $tbs_cert->withAdditionalExtensions( |
51
|
|
|
new KeyUsageExtension(true, |
52
|
|
|
KeyUsageExtension::DIGITAL_SIGNATURE | |
53
|
|
|
KeyUsageExtension::KEY_ENCIPHERMENT), |
54
|
|
|
new BasicConstraintsExtension(true, false)); |
55
|
|
|
|
56
|
|
|
// sign certificate with issuer's private key |
57
|
|
|
$algo = SignatureAlgorithmIdentifierFactory::algoForAsymmetricCrypto( |
58
|
|
|
$private_key_info->algorithmIdentifier(), new SHA512AlgorithmIdentifier()); |
|
|
|
|
59
|
|
|
|
60
|
|
|
$cert = $tbs_cert->sign($algo, $private_key_info); |
61
|
|
|
|
62
|
|
|
echo $cert; |
63
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.