-
Notifications
You must be signed in to change notification settings - Fork 7
/
X509_CRL.php
204 lines (175 loc) · 7.97 KB
/
X509_CRL.php
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/**
* X.509 CRL creator
*
* @see https://www.ietf.org/rfc/rfc2459.txt
*/
namespace Ukrbublik\openssl_x509_crl;
use Ukrbublik\openssl_x509_crl\ASN1;
use Ukrbublik\openssl_x509_crl\ASN1_SIMPLE;
use Ukrbublik\openssl_x509_crl\ASN1_SEQUENCE;
use Ukrbublik\openssl_x509_crl\ASN1_SET;
use Ukrbublik\openssl_x509_crl\ASN1_NULL;
use Ukrbublik\openssl_x509_crl\ASN1_UTF8STRING;
use Ukrbublik\openssl_x509_crl\ASN1_TELETEXSTRING;
use Ukrbublik\openssl_x509_crl\ASN1_ASCIISTRING;
use Ukrbublik\openssl_x509_crl\ASN1_BITSTRING;
use Ukrbublik\openssl_x509_crl\ASN1_OCTETSTRING;
use Ukrbublik\openssl_x509_crl\ASN1_INT;
use Ukrbublik\openssl_x509_crl\ASN1_ENUM;
use Ukrbublik\openssl_x509_crl\ASN1_GENERALTIME;
use Ukrbublik\openssl_x509_crl\ASN1_UTCTIME;
use Ukrbublik\openssl_x509_crl\ASN1_BOOL;
use Ukrbublik\openssl_x509_crl\ASN1_OID;
use Ukrbublik\openssl_x509_crl\OID;
use Ukrbublik\openssl_x509_crl\X509;
use Ukrbublik\openssl_x509_crl\X509_CERT;
/**
* X.509 CRL
*/
class X509_CRL
{
/**
* Generates and signs CRL from provided data
* Returns in DER format
*
* @param array $ci data for CRL creation. <br>
* Format: array(<br>
* 'no' => number of CRL,<br>
* 'version' => CRL format version, 1 or 2,<br>
* 'days' => CRL validity in days from date of creation,<br>
* 'alg' => OPENSSL_ALGO_*,<br>
* 'revoked' => array( array( //list of revoked certificates<br>
* 'serial' => S/N of revoked cert,<br>
* 'rev_date' => date of revokation, timestamp,<br>
* 'reason' => code of revokation reason, see X509::getRevokeReasonCodeByName(),<br>
* 'compr_date' => date when certifacate became compromised, timestamp,<br>
* 'hold_instr' => code of hold instruction, see X509::getHoldInstructionCodeByName(),<br>
* ), ... )<br>
* )
* @param resource $ca_pkey key pair for CA root certificate, got from openssl_pkey_get_private()
* @param string $ca_cert CA root certificate data in DER format
* @return string CRL in DER format
*/
static function create($ci, $ca_pkey, $ca_cert) {
$ca_decoded = X509_CERT::decode($ca_cert);
//CRL version
$crl_version = ((isset($ci['version']) && ($ci['version'] == 2 || $ci['version'] == 1)) ? $ci['version'] : 2);
//Algorithm
$algs_cipher = array( OPENSSL_KEYTYPE_RSA, OPENSSL_KEYTYPE_DSA, OPENSSL_KEYTYPE_DH, OPENSSL_KEYTYPE_EC );
$algs_hash = array( /*OPENSSL_ALGO_DSS1, */OPENSSL_ALGO_SHA1, OPENSSL_ALGO_MD5, OPENSSL_ALGO_MD4 );
if(defined('OPENSSL_ALGO_MD2'))
$algs_hash[] = OPENSSL_ALGO_MD2;
$ca_pkey_details = openssl_pkey_get_details($ca_pkey);
if($ca_pkey_details === false)
return false;
$ca_pkey_type = $ca_pkey_details['type'];
if(!in_array($ca_pkey_type, $algs_cipher))
return false;
if(isset($ci['alg']) && !in_array($ci['alg'], $algs_hash))
return false;
$crl_hash_alg = (isset($ci['alg']) ? $ci['alg'] : OPENSSL_ALGO_SHA1);
$sign_alg_oid = OID::getAlgoOID($ca_pkey_type, $crl_hash_alg);
if($sign_alg_oid === false)
return false;
//Create CRL stricture
$crl = new ASN1_SEQUENCE;
$crl->content['tbsCertList'] = new ASN1_SEQUENCE;
$tbsCertList = & $crl->content['tbsCertList'];
if($crl_version == 2) {
$tbsCertList->content['version'] = new ASN1_INT( $crl_version - 1 );
}
$tbsCertList->content['signature'] = new ASN1_SEQUENCE;
$tbsCertList->content['signature']->content['algorithm'] = new ASN1_OID( $sign_alg_oid );
$tbsCertList->content['signature']->content['parameters'] = new ASN1_NULL;
$tbsCertList->content['issuer'] = X509_CERT::getExtVal_Subject($ca_decoded);
$tbsCertList->content['thisUpdate'] = new ASN1_UTCTIME( time() );
if(function_exists('add_date'))
$nextUpdateTs = add_date(time(), 0, 0, 0, $ci['days']);
else
$nextUpdateTs = time() + $ci['days'] * 24*60*60;
$tbsCertList->content['nextUpdate'] = new ASN1_UTCTIME( $nextUpdateTs );
//Revoked certs list
if(isset($ci['revoked']) && is_array($ci['revoked']) && !empty($ci['revoked'])) {
$tbsCertList->content['revokedCertificates'] = new ASN1_SEQUENCE;
$revokedCerts = & $tbsCertList->content['revokedCertificates'];
for($i = 0 ; $i < count($ci['revoked']) ; $i++) {
$ci_rev = & $ci['revoked'][$i];
$revokedCerts->content[$i] = new ASN1_SEQUENCE;
$revCert = & $revokedCerts->content[$i];
$revCert->content['userCertificate'] = new ASN1_INT( $ci_rev['serial'] );
if(!is_null( $ci_rev['rev_date'] )) {
$revCert->content['revocationDate'] = new ASN1_UTCTIME( $ci_rev['rev_date'] );
}
// Revo Extensions
if($crl_version == 2 && !is_null($ci_rev['reason'])) {
$revCert->content['crlEntryExtensions'] = new ASN1_SEQUENCE;
$crlExts = & $revCert->content['crlEntryExtensions'];
$crlExts->content['reasonCode'] = new ASN1_SEQUENCE;
$reasonCode = & $crlExts->content['reasonCode'];
$reasonCode->content['OID'] = new ASN1_OID(OID::getOIDFromName("cRLReason"));
$reasonCode->content['VAL'] = new ASN1_OCTETSTRING(false);
$reasonCode->content['VAL']->content[0] = new ASN1_ENUM( $ci_rev['reason'] );
if( $ci_rev['reason'] == X509::getRevokeReasonCodeByName('keyCompromise') && !is_null( $ci_rev['compr_date'] ) ) {
$crlExts->content['invalidityDate'] = new ASN1_SEQUENCE;
$invalidityDate = & $crlExts->content['invalidityDate'];
$invalidityDate->content['OID'] = new ASN1_OID(OID::getOIDFromName("invalidityDate"));
$invalidityDate->content['VAL'] = new ASN1_OCTETSTRING(false);
$invalidityDate->content['VAL']->content[0] = new ASN1_GENERALTIME( $ci_rev['compr_date'] );
}
if( $ci_rev['reason'] == X509::getRevokeReasonCodeByName('certificateHold') && !is_null( $ci_rev['hold_instr'] ) ) {
$crlExts->content['holdInstructionCode'] = new ASN1_SEQUENCE;
$holdInstructionCode = & $crlExts->content['holdInstructionCode'];
$holdInstructionCode->content['OID'] = new ASN1_OID(OID::getOIDFromName("instructionCode"));
$holdInstructionCode->content['VAL'] = new ASN1_OCTETSTRING(false);
$holdInstructionCode->content['VAL']->content[0] = new ASN1_OID( OID::getOIDFromName( X509::getHoldInstructionNameByCode($ci_rev['hold_instr']) ) );
}
}
}
}
//CRL Extensions
if($crl_version == 2) {
$tbsCertList->content['crlExtensions'] = new ASN1_SEQUENCE;
$crlExts = & $tbsCertList->content['crlExtensions'];
$crlExts->setType(0, true, ASN1_CLASSTYPE_CONTEXT);
$crlExts->content[0] = new ASN1_SEQUENCE;
$crlExts->content[0]->content['authorityKeyIdentifier'] = new ASN1_SEQUENCE;
$authKeyId = & $crlExts->content[0]->content['authorityKeyIdentifier'];
$authKeyId->content['OID'] = new ASN1_OID(OID::getOIDFromName("authorityKeyIdentifier"));
$authKeyId->content['VAL'] = new ASN1_OCTETSTRING(false);
$authKeyId->content['VAL']->content[0] = X509_CERT::getExtVal_SubjectKeyIdentifier($ca_decoded);
if(isset($ci['no']) && is_numeric($ci['no'])) {
$crlExts->content[0]->content['cRLNumber'] = new ASN1_SEQUENCE;
$cRLNumber = & $crlExts->content[0]->content['cRLNumber'];
$cRLNumber->content['OID'] = new ASN1_OID(OID::getOIDFromName("cRLNumber"));
$cRLNumber->content['VAL'] = new ASN1_OCTETSTRING(false);
$cRLNumber->content['VAL']->content[0] = new ASN1_INT($ci['no']);
}
}
//Sign CRL info
$crl_info = $tbsCertList->encode();
$crl_sig = "";
$crl_sign_result = openssl_sign( $crl_info, $crl_sig, $ca_pkey, $crl_hash_alg );
if(!$crl_sign_result)
return false;
//Add sign to CRL structure
$crl->content['signatureAlgorithm'] = new ASN1_SEQUENCE;
$signAlgo = & $crl->content['signatureAlgorithm'];
$signAlgo->content['algorithm'] = new ASN1_OID( $sign_alg_oid );
$signAlgo->content['parameters'] = new ASN1_NULL;
$crl->content['signature'] = new ASN1_BITSTRING($crl_sig);
//Encode CRL content to DER format
$crl_encoded = $crl->encode();
return $crl_encoded;
}
}
/**
* Generates and signs CRL from provided data
* Returns in DER format
*
* @see X509_CRL::create()
*/
function openssl_x509_crl($ci, $ca_pkey, $ca_data) {
return X509_CRL::create($ci, $ca_pkey, $ca_data);
}
?>