@@ -85,7 +85,7 @@ class CertificateGenerator
85
85
openssl_x509_export_to_file ($ this ->ca , $ file );
86
86
}
87
87
88
- public function saveNewCertAsFileWithKey (
88
+ private function generateCertAndKey (
89
89
$ commonNameForCert , $ file , $ keyLength = null , $ subjectAltName = null
90
90
) {
91
91
$ dn = [
@@ -120,36 +120,50 @@ CONFIG;
120
120
$ configFile = $ file . '.cnf ' ;
121
121
file_put_contents ($ configFile , $ configCode );
122
122
123
- try {
124
- $ config = [
125
- 'config ' => $ configFile ,
126
- 'req_extensions ' => 'v3_req ' ,
127
- 'x509_extensions ' => 'usr_cert ' ,
128
- ];
129
-
130
- $ this ->lastKey = self ::generateKey ($ keyLength );
131
- $ csr = openssl_csr_new ($ dn , $ this ->lastKey , $ config );
132
- $ this ->lastCert = openssl_csr_sign (
133
- $ csr ,
134
- $ this ->ca ,
135
- $ this ->caKey ,
136
- /* days */ 2 ,
137
- $ config ,
138
- );
139
- if (!$ this ->lastCert ) {
140
- throw new Exception ('Failed to create certificate ' );
141
- }
123
+ $ config = [
124
+ 'config ' => $ configFile ,
125
+ 'req_extensions ' => 'v3_req ' ,
126
+ 'x509_extensions ' => 'usr_cert ' ,
127
+ ];
142
128
143
- $ certText = '' ;
144
- openssl_x509_export ($ this ->lastCert , $ certText );
129
+ $ this ->lastKey = self ::generateKey ($ keyLength );
130
+ $ csr = openssl_csr_new ($ dn , $ this ->lastKey , $ config );
131
+ $ this ->lastCert = openssl_csr_sign (
132
+ $ csr ,
133
+ $ this ->ca ,
134
+ $ this ->caKey ,
135
+ /* days */ 2 ,
136
+ $ config ,
137
+ );
145
138
146
- $ keyText = '' ;
147
- openssl_pkey_export ( $ this -> lastKey , $ keyText , null , $ config );
139
+ return $ config ;
140
+ }
148
141
149
- file_put_contents ($ file , $ certText . PHP_EOL . $ keyText );
150
- } finally {
151
- unlink ($ configFile );
152
- }
142
+ public function saveNewCertAsFileWithKey (
143
+ $ commonNameForCert , $ file , $ keyLength = null , $ subjectAltName = null
144
+ ) {
145
+ $ config = $ this ->generateCertAndKey ($ commonNameForCert , $ file , $ keyLength , $ subjectAltName );
146
+
147
+ $ certText = '' ;
148
+ openssl_x509_export ($ this ->lastCert , $ certText );
149
+
150
+ $ keyText = '' ;
151
+ openssl_pkey_export ($ this ->lastKey , $ keyText , null , $ config );
152
+
153
+ file_put_contents ($ file , $ certText . PHP_EOL . $ keyText );
154
+
155
+ unlink ($ config ['config ' ]);
156
+ }
157
+
158
+ public function saveNewCertAndKey (
159
+ $ commonNameForCert , $ certFile , $ keyFile , $ keyLength = null , $ subjectAltName = null
160
+ ) {
161
+ $ config = $ this ->generateCertAndKey ($ commonNameForCert , $ certFile , $ keyLength , $ subjectAltName );
162
+
163
+ openssl_x509_export_to_file ($ this ->lastCert , $ certFile );
164
+ openssl_pkey_export_to_file ($ this ->lastKey , $ keyFile , null , $ config );
165
+
166
+ unlink ($ config ['config ' ]);
153
167
}
154
168
155
169
public function getCertDigest ($ algo )
0 commit comments