Failed Conditions
Push — develop ( 4093b1...fb0156 )
by Reüel
02:38
created

src/Settings.php (10 issues)

1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\IDealAdvancedV3;
4
5
use Pronamic\WordPress\Pay\Core\GatewaySettings;
6
7
/**
8
 * Title: iDEAL Advanced v3 gateway settings
9
 * Description:
10
 * Copyright: 2005-2019 Pronamic
11
 * Company: Pronamic
12
 *
13
 * @author  Remco Tolsma
14
 * @version 2.0.0
15
 * @since   1.1.2
16
 */
17
class Settings extends GatewaySettings {
18
	public function __construct() {
19
		// Filters
20
		add_filter( 'pronamic_pay_gateway_fields', array( $this, 'fields' ) );
21
22
		// Actions
23
		add_action( 'current_screen', array( $this, 'maybe_download_private_certificate' ) );
24
		add_action( 'current_screen', array( $this, 'maybe_download_private_key' ) );
25
	}
26
27
	public function fields( array $fields ) {
28
		/*
29
		 * Private Key and Certificate
30
		 */
31
32
		// Private key and certificate information
33
		$fields[] = array(
34
			'section'  => 'ideal',
35
			'methods'  => array( 'ideal-advanced-v3' ),
36
			'title'    => __( 'Private key and certificate', 'pronamic_ideal' ),
37
			'type'     => 'description',
38
			'callback' => array( $this, 'field_security' ),
39
		);
40
41
		// Organization
42
		$fields[] = array(
43
			'filter'   => FILTER_SANITIZE_STRING,
44
			'section'  => 'ideal',
45
			'methods'  => array( 'ideal-advanced-v3' ),
46
			'group'    => 'pk-cert',
47
			'meta_key' => '_pronamic_gateway_organization',
48
			'title'    => __( 'Organization', 'pronamic_ideal' ),
49
			'type'     => 'text',
50
			'tooltip'  => __( 'Organization name, e.g. Pronamic', 'pronamic_ideal' ),
51
		);
52
53
		// Organization Unit
54
		$fields[] = array(
55
			'filter'   => FILTER_SANITIZE_STRING,
56
			'section'  => 'ideal',
57
			'methods'  => array( 'ideal-advanced-v3' ),
58
			'group'    => 'pk-cert',
59
			'meta_key' => '_pronamic_gateway_organization_unit',
60
			'title'    => __( 'Organization Unit', 'pronamic_ideal' ),
61
			'type'     => 'text',
62
			'tooltip'  => __( 'Organization unit, e.g. Administration', 'pronamic_ideal' ),
63
		);
64
65
		// Locality
66
		$fields[] = array(
67
			'filter'   => FILTER_SANITIZE_STRING,
68
			'section'  => 'ideal',
69
			'methods'  => array( 'ideal-advanced-v3' ),
70
			'group'    => 'pk-cert',
71
			'meta_key' => '_pronamic_gateway_locality',
72
			'title'    => __( 'City', 'pronamic_ideal' ),
73
			'type'     => 'text',
74
			'tooltip'  => __( 'City, e.g. Amsterdam', 'pronamic_ideal' ),
75
		);
76
77
		// State or Province
78
		$fields[] = array(
79
			'filter'   => FILTER_SANITIZE_STRING,
80
			'section'  => 'ideal',
81
			'methods'  => array( 'ideal-advanced-v3' ),
82
			'group'    => 'pk-cert',
83
			'meta_key' => '_pronamic_gateway_state_or_province',
84
			'title'    => __( 'State / province', 'pronamic_ideal' ),
85
			'type'     => 'text',
86
			'tooltip'  => __( 'State or province, e.g. Friesland', 'pronamic_ideal' ),
87
		);
88
89
		// Country
90
		$locale = explode( '_', get_locale() );
91
92
		$locale = array_pop( $locale );
93
94
		$fields[] = array(
95
			'filter'      => FILTER_SANITIZE_STRING,
96
			'section'     => 'ideal',
97
			'methods'     => array( 'ideal-advanced-v3' ),
98
			'group'       => 'pk-cert',
99
			'meta_key'    => '_pronamic_gateway_country',
100
			'title'       => __( 'Country', 'pronamic_ideal' ),
101
			'type'        => 'text',
102
			'tooltip'     => sprintf(
103
				'%s %s (ISO-3166-1 alpha-2)',
104
				__( '2 letter country code, e.g.', 'pronamic_ideal' ),
105
				strtoupper( $locale )
106
			),
107
			'size'        => 2,
108
			'description' => sprintf(
109
				'%s %s',
110
				__( '2 letter country code, e.g.', 'pronamic_ideal' ),
111
				strtoupper( $locale )
112
			),
113
		);
114
115
		// Email Address
116
		$fields[] = array(
117
			'filter'   => FILTER_SANITIZE_STRING,
118
			'section'  => 'ideal',
119
			'methods'  => array( 'ideal-advanced-v3' ),
120
			'group'    => 'pk-cert',
121
			'meta_key' => '_pronamic_gateway_email',
122
			'title'    => __( 'E-mail address', 'pronamic_ideal' ),
123
			'tooltip'  => sprintf(
124
				/* translators: %s: admin email */
125
				__( 'E-mail address, e.g. %s', 'pronamic_ideal' ),
126
				get_option( 'admin_email' )
0 ignored issues
show
It seems like get_option('admin_email') can also be of type false; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

126
				/** @scrutinizer ignore-type */ get_option( 'admin_email' )
Loading history...
127
			),
128
			'type'     => 'text',
129
		);
130
131
		// Number Days Valid
132
		$fields[] = array(
133
			'filter'   => FILTER_SANITIZE_NUMBER_INT,
134
			'section'  => 'ideal',
135
			'methods'  => array( 'ideal-advanced-v3' ),
136
			'group'    => 'pk-cert',
137
			'meta_key' => '_pronamic_gateway_number_days_valid',
138
			'title'    => __( 'Number Days Valid', 'pronamic_ideal' ),
139
			'type'     => 'text',
140
			'default'  => 1825,
141
			'tooltip'  => __( 'Number of days the generated certificate will be valid for, e.g. 1825 days for the maximum duration of 5 years.', 'pronamic_ideal' ),
142
		);
143
144
		// Private Key Password
145
		$fields[] = array(
146
			'filter'   => FILTER_SANITIZE_STRING,
147
			'section'  => 'ideal',
148
			'methods'  => array( 'ideal-advanced-v3' ),
149
			'group'    => 'pk-cert',
150
			'meta_key' => '_pronamic_gateway_ideal_private_key_password',
151
			'title'    => __( 'Private Key Password', 'pronamic_ideal' ),
152
			'type'     => 'text',
153
			'classes'  => array( 'regular-text', 'code' ),
154
			'default'  => wp_generate_password(),
155
			'tooltip'  => __( 'A random password which will be used for the generation of the private key and certificate.', 'pronamic_ideal' ),
156
		);
157
158
		// Private Key
159
		$fields[] = array(
160
			'filter'   => FILTER_SANITIZE_STRING,
161
			'section'  => 'ideal',
162
			'methods'  => array( 'ideal-advanced-v3' ),
163
			'group'    => 'pk-cert',
164
			'meta_key' => '_pronamic_gateway_ideal_private_key',
165
			'title'    => __( 'Private Key', 'pronamic_ideal' ),
166
			'type'     => 'textarea',
167
			'callback' => array( $this, 'field_private_key' ),
168
			'classes'  => array( 'code' ),
169
			'tooltip'  => __( 'The private key is used for secure communication with the payment provider. If left empty, the private key will be generated using the given private key password.', 'pronamic_ideal' ),
170
		);
171
172
		// Private Certificate
173
		$fields[] = array(
174
			'filter'   => FILTER_SANITIZE_STRING,
175
			'section'  => 'ideal',
176
			'methods'  => array( 'ideal-advanced-v3' ),
177
			'group'    => 'pk-cert',
178
			'meta_key' => '_pronamic_gateway_ideal_private_certificate',
179
			'title'    => __( 'Private Certificate', 'pronamic_ideal' ),
180
			'type'     => 'textarea',
181
			'callback' => array( $this, 'field_private_certificate' ),
182
			'classes'  => array( 'code' ),
183
			'tooltip'  => __( 'The certificate is used for secure communication with the payment provider. If left empty, the certificate will be generated using the private key and given organization details.', 'pronamic_ideal' ),
184
		);
185
186
		// Transaction feedback
187
		$fields[] = array(
188
			'section' => 'ideal',
189
			'methods' => array( 'ideal-advanced-v3' ),
190
			'title'   => __( 'Transaction feedback', 'pronamic_ideal' ),
191
			'type'    => 'description',
192
			'html'    => sprintf(
193
				'<span class="dashicons dashicons-yes"></span> %s',
194
				__( 'Payment status updates will be processed without any additional configuration.', 'pronamic_ideal' )
195
			),
196
		);
197
198
		// Return
199
		return $fields;
200
	}
201
202
	/**
203
	 * Field security
204
	 *
205
	 * @param array $field
206
	 */
207
	public function field_security( $field ) {
208
		$certificate = get_post_meta( get_the_ID(), '_pronamic_gateway_ideal_private_certificate', true );
0 ignored issues
show
It seems like get_the_ID() can also be of type false; however, parameter $post_id of get_post_meta() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

208
		$certificate = get_post_meta( /** @scrutinizer ignore-type */ get_the_ID(), '_pronamic_gateway_ideal_private_certificate', true );
Loading history...
209
210
		?>
211
		<p>
212
			<?php if ( empty( $certificate ) ) : ?>
213
214
				<span
215
					class="dashicons dashicons-no"></span> <?php esc_html_e( 'The private key and certificate have not yet been configured.', 'pronamic_ideal' ); ?>
216
				<br/>
217
218
				<br/>
219
220
				<?php esc_html_e( 'A private key and certificate are required for communication with the payment provider. Enter the organization details from the iDEAL account below to generate these required files.', 'pronamic_ideal' ); ?>
221
222
			<?php else : ?>
223
224
				<span
225
					class="dashicons dashicons-yes"></span> <?php esc_html_e( 'A private key and certificate have been configured. The certificate must be uploaded to the payment provider dashboard to complete configuration.', 'pronamic_ideal' ); ?>
226
				<br/>
227
228
				<br/>
229
230
				<?php
231
232
				submit_button(
233
					__( 'Download certificate', 'pronamic_ideal' ),
234
					'secondary',
235
					'download_private_certificate',
236
					false
237
				);
238
239
				?>
240
241
				<a class="pronamic-pay-btn-link" href="#" id="pk-cert-fields-toggle"><?php esc_html_e( 'Show details…', 'pronamic_ideal' ); ?></a>
242
243
			<?php endif; ?>
244
		</p>
245
		<?php
246
	}
247
248
	public function field_private_key( $field ) {
249
		$private_key          = get_post_meta( get_the_ID(), '_pronamic_gateway_ideal_private_key', true );
0 ignored issues
show
It seems like get_the_ID() can also be of type false; however, parameter $post_id of get_post_meta() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

249
		$private_key          = get_post_meta( /** @scrutinizer ignore-type */ get_the_ID(), '_pronamic_gateway_ideal_private_key', true );
Loading history...
250
		$private_key_password = get_post_meta( get_the_ID(), '_pronamic_gateway_ideal_private_key_password', true );
251
		$number_days_valid    = get_post_meta( get_the_ID(), '_pronamic_gateway_number_days_valid', true );
252
253
		$filename = __( 'ideal.key', 'pronamic_ideal' );
254
255
		if ( ! empty( $private_key_password ) && ! empty( $number_days_valid ) ) {
256
			$command = sprintf(
257
				'openssl genrsa -aes128 -out %s -passout pass:%s 2048',
258
				escapeshellarg( $filename ),
259
				escapeshellarg( $private_key_password )
260
			);
261
262
			?>
263
264
			<p><?php esc_html_e( 'OpenSSL command', 'pronamic_ideal' ); ?></p>
265
			<input id="pronamic_ideal_openssl_command_key" name="pronamic_ideal_openssl_command_key" value="<?php echo esc_attr( $command ); ?>" type="text" class="large-text code" readonly="readonly"/>
266
267
			<?php
268
		} else {
269
			printf(
270
				'<p class="pronamic-pay-description description">%s</p>',
271
				esc_html__( 'Leave empty and save the configuration to generate the private key or view the OpenSSL command.', 'pronamic_ideal' )
272
			);
273
		}
274
275
		?>
276
		<p>
277
			<?php
278
279
			if ( ! empty( $private_key ) ) {
280
				submit_button(
281
					__( 'Download', 'pronamic_ideal' ),
282
					'secondary',
283
					'download_private_key',
284
					false
285
				);
286
287
				echo ' ';
288
			}
289
290
			printf(
291
				'<label class="pronamic-pay-form-control-file-button button">%s <input type="file" name="%s" /></label>',
292
				esc_html__( 'Upload', 'pronamic_ideal' ),
293
				'_pronamic_gateway_ideal_private_key_file'
294
			);
295
296
			?>
297
		</p>
298
		<?php
299
	}
300
301
	public function field_private_certificate( $field ) {
302
		$certificate = get_post_meta( get_the_ID(), '_pronamic_gateway_ideal_private_certificate', true );
0 ignored issues
show
It seems like get_the_ID() can also be of type false; however, parameter $post_id of get_post_meta() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

302
		$certificate = get_post_meta( /** @scrutinizer ignore-type */ get_the_ID(), '_pronamic_gateway_ideal_private_certificate', true );
Loading history...
303
304
		$private_key_password = get_post_meta( get_the_ID(), '_pronamic_gateway_ideal_private_key_password', true );
305
		$number_days_valid    = get_post_meta( get_the_ID(), '_pronamic_gateway_number_days_valid', true );
306
307
		$filename_key = __( 'ideal.key', 'pronamic_ideal' );
308
		$filename_cer = __( 'ideal.cer', 'pronamic_ideal' );
309
310
		// @link http://www.openssl.org/docs/apps/req.html
311
		$subj_args = array(
312
			'C'            => get_post_meta( get_the_ID(), '_pronamic_gateway_country', true ),
313
			'ST'           => get_post_meta( get_the_ID(), '_pronamic_gateway_state_or_province', true ),
314
			'L'            => get_post_meta( get_the_ID(), '_pronamic_gateway_locality', true ),
315
			'O'            => get_post_meta( get_the_ID(), '_pronamic_gateway_organization', true ),
316
			'OU'           => get_post_meta( get_the_ID(), '_pronamic_gateway_organization_unit', true ),
317
			'CN'           => get_post_meta( get_the_ID(), '_pronamic_gateway_organization', true ),
318
			'emailAddress' => get_post_meta( get_the_ID(), '_pronamic_gateway_email', true ),
319
		);
320
321
		$subj_args = array_filter( $subj_args );
322
323
		$subj = '';
324
		foreach ( $subj_args as $type => $value ) {
325
			$subj .= '/' . $type . '=' . addslashes( $value );
326
		}
327
328
		if ( ! empty( $subj ) ) {
329
			$command = trim( sprintf(
330
				'openssl req -x509 -sha256 -new -key %s -passin pass:%s -days %s -out %s %s',
331
				escapeshellarg( $filename_key ),
332
				escapeshellarg( $private_key_password ),
0 ignored issues
show
It seems like $private_key_password can also be of type false; however, parameter $arg of escapeshellarg() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

332
				escapeshellarg( /** @scrutinizer ignore-type */ $private_key_password ),
Loading history...
333
				escapeshellarg( $number_days_valid ),
334
				escapeshellarg( $filename_cer ),
335
				empty( $subj ) ? '' : sprintf( '-subj %s', escapeshellarg( $subj ) )
336
			) );
337
338
			?>
339
340
			<p><?php esc_html_e( 'OpenSSL command', 'pronamic_ideal' ); ?></p>
341
			<input id="pronamic_ideal_openssl_command_certificate" name="pronamic_ideal_openssl_command_certificate" value="<?php echo esc_attr( $command ); ?>" type="text" class="large-text code" readonly="readonly"/>
342
343
			<?php
344
		} else {
345
			printf(
346
				'<p class="pronamic-pay-description description">%s</p>',
347
				esc_html__( 'Leave empty and save the configuration to generate the certificate or view the OpenSSL command.', 'pronamic_ideal' )
348
			);
349
		}
350
351
		if ( ! empty( $certificate ) ) {
352
			$fingerprint = Security::get_sha_fingerprint( $certificate );
353
			$fingerprint = str_split( $fingerprint, 2 );
354
			$fingerprint = implode( ':', $fingerprint );
355
356
			echo '<dl>';
357
358
			echo '<dt>', esc_html__( 'SHA Fingerprint', 'pronamic_ideal' ), '</dt>';
359
			echo '<dd>', esc_html( $fingerprint ), '</dd>';
360
361
			$info = openssl_x509_parse( $certificate );
362
363
			if ( $info ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $info of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
364
				$date_format = __( 'M j, Y @ G:i', 'pronamic_ideal' );
365
366
				if ( isset( $info['validFrom_time_t'] ) ) {
367
					echo '<dt>', esc_html__( 'Valid From', 'pronamic_ideal' ), '</dt>';
368
					echo '<dd>', esc_html( date_i18n( $date_format, $info['validFrom_time_t'] ) ), '</dd>';
369
				}
370
371
				if ( isset( $info['validTo_time_t'] ) ) {
372
					echo '<dt>', esc_html__( 'Valid To', 'pronamic_ideal' ), '</dt>';
373
					echo '<dd>', esc_html( date_i18n( $date_format, $info['validTo_time_t'] ) ), '</dd>';
374
				}
375
			}
376
377
			echo '</dl>';
378
		}
379
380
		?>
381
		<p>
382
			<?php
383
384
			if ( ! empty( $certificate ) ) {
385
				submit_button(
386
					__( 'Download', 'pronamic_ideal' ),
387
					'secondary',
388
					'download_private_certificate',
389
					false
390
				);
391
392
				echo ' ';
393
			}
394
395
			printf(
396
				'<label class="pronamic-pay-form-control-file-button button">%s <input type="file" name="%s" /></label>',
397
				esc_html__( 'Upload', 'pronamic_ideal' ),
398
				'_pronamic_gateway_ideal_private_certificate_file'
399
			);
400
401
			?>
402
		</p>
403
		<?php
404
	}
405
406
	/**
407
	 * Download private certificate
408
	 */
409
	public function maybe_download_private_certificate() {
410
		if ( filter_has_var( INPUT_POST, 'download_private_certificate' ) ) {
411
			$post_id = filter_input( INPUT_POST, 'post_ID', FILTER_SANITIZE_STRING );
412
413
			$filename = sprintf( 'ideal-private-certificate-%s.cer', $post_id );
414
415
			header( 'Content-Description: File Transfer' );
416
			header( 'Content-Disposition: attachment; filename=' . $filename );
417
			header( 'Content-Type: application/x-x509-ca-cert; charset=' . get_option( 'blog_charset' ), true );
0 ignored issues
show
Are you sure get_option('blog_charset') of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

417
			header( 'Content-Type: application/x-x509-ca-cert; charset=' . /** @scrutinizer ignore-type */ get_option( 'blog_charset' ), true );
Loading history...
418
419
			echo get_post_meta( $post_id, '_pronamic_gateway_ideal_private_certificate', true ); //xss ok
0 ignored issues
show
Are you sure get_post_meta($post_id, ...ate_certificate', true) of type false|mixed|string can be used in echo? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

419
			echo /** @scrutinizer ignore-type */ get_post_meta( $post_id, '_pronamic_gateway_ideal_private_certificate', true ); //xss ok
Loading history...
420
421
			exit;
422
		}
423
	}
424
425
	/**
426
	 * Download private key
427
	 */
428
	public function maybe_download_private_key() {
429
		if ( filter_has_var( INPUT_POST, 'download_private_key' ) ) {
430
			$post_id = filter_input( INPUT_POST, 'post_ID', FILTER_SANITIZE_STRING );
431
432
			$filename = sprintf( 'ideal-private-key-%s.key', $post_id );
433
434
			header( 'Content-Description: File Transfer' );
435
			header( 'Content-Disposition: attachment; filename=' . $filename );
436
			header( 'Content-Type: application/pgp-keys; charset=' . get_option( 'blog_charset' ), true );
0 ignored issues
show
Are you sure get_option('blog_charset') of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

436
			header( 'Content-Type: application/pgp-keys; charset=' . /** @scrutinizer ignore-type */ get_option( 'blog_charset' ), true );
Loading history...
437
438
			echo get_post_meta( $post_id, '_pronamic_gateway_ideal_private_key', true ); //xss ok
0 ignored issues
show
Are you sure get_post_meta($post_id, ...eal_private_key', true) of type false|mixed|string can be used in echo? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

438
			echo /** @scrutinizer ignore-type */ get_post_meta( $post_id, '_pronamic_gateway_ideal_private_key', true ); //xss ok
Loading history...
439
440
			exit;
441
		}
442
	}
443
444
	/***
445
	 * Save post.
446
	 */
447
	public function save_post( $data ) {
448
		// Files
449
		$files = array(
450
			'_pronamic_gateway_ideal_private_key_file' => '_pronamic_gateway_ideal_private_key',
451
			'_pronamic_gateway_ideal_private_certificate_file' => '_pronamic_gateway_ideal_private_certificate',
452
		);
453
454
		foreach ( $files as $name => $meta_key ) {
455
			if ( isset( $_FILES[ $name ] ) && UPLOAD_ERR_OK === $_FILES[ $name ]['error'] ) { // WPCS: input var okay.
456
				$value = file_get_contents( $_FILES[ $name ]['tmp_name'] ); // WPCS: input var okay. // WPCS: sanitization ok.
457
458
				$data[ $meta_key ] = $value;
459
			}
460
		}
461
462
		// Generate private key and certificate
463
		if ( empty( $data['_pronamic_gateway_ideal_private_key_password'] ) ) {
464
			// Without private key password we can't create private key and certifiate.
465
			return $data;
466
		}
467
468
		if ( ! in_array( 'aes-128-cbc', openssl_get_cipher_methods(), true ) ) {
469
			// Without AES-128-CBC ciphter method we can't create private key and certificate.
470
			return $data;
471
		}
472
473
		// Private key
474
		$pkey = openssl_pkey_get_private( $data['_pronamic_gateway_ideal_private_key'], $data['_pronamic_gateway_ideal_private_key_password'] );
475
476
		if ( false === $pkey ) {
477
			// If we can't open the private key we will create a new private key and certificate.
478
479
			if ( defined( 'OPENSSL_CIPHER_AES_128_CBC' ) ) {
480
				$cipher = OPENSSL_CIPHER_AES_128_CBC;
481
			} elseif ( defined( 'OPENSSL_CIPHER_3DES' ) ) {
482
				// @link https://www.pronamic.nl/wp-content/uploads/2011/12/iDEAL_Advanced_PHP_EN_V2.2.pdf
483
				$cipher = OPENSSL_CIPHER_3DES;
484
			} else {
485
				// Unable to create private key without cipher.
486
				return $data;
487
			}
488
489
			$args = array(
490
				'digest_alg'             => 'SHA256',
491
				'private_key_bits'       => 2048,
492
				'private_key_type'       => OPENSSL_KEYTYPE_RSA,
493
				'encrypt_key'            => true,
494
				'encrypt_key_cipher'     => $cipher,
495
				'subjectKeyIdentifier'   => 'hash',
496
				'authorityKeyIdentifier' => 'keyid:always,issuer:always',
497
				'basicConstraints'       => 'CA:true',
498
			);
499
500
			$pkey = openssl_pkey_new( $args );
501
502
			if ( false === $pkey ) {
503
				return $data;
504
			}
505
506
			// Export key
507
			$result = openssl_pkey_export( $pkey, $private_key, $data['_pronamic_gateway_ideal_private_key_password'], $args );
508
509
			if ( false === $result ) {
510
				return $data;
511
			}
512
513
			$data['_pronamic_gateway_ideal_private_key'] = $private_key;
514
			// Delete private certificate since this is no longer valid.
515
			$data['_pronamic_gateway_ideal_private_certificate'] = null;
516
		}
517
518
		// Certificate
519
		if ( empty( $data['_pronamic_gateway_ideal_private_certificate'] ) ) {
520
			$required_keys = array(
521
				'countryName',
522
				'stateOrProvinceName',
523
				'localityName',
524
				'organizationName',
525
				'commonName',
526
				'emailAddress',
527
			);
528
529
			$distinguished_name = array(
530
				'countryName'            => $data['_pronamic_gateway_country'],
531
				'stateOrProvinceName'    => $data['_pronamic_gateway_state_or_province'],
532
				'localityName'           => $data['_pronamic_gateway_locality'],
533
				'organizationName'       => $data['_pronamic_gateway_organization'],
534
				'organizationalUnitName' => $data['_pronamic_gateway_organization_unit'],
535
				'commonName'             => $data['_pronamic_gateway_organization'],
536
				'emailAddress'           => $data['_pronamic_gateway_email'],
537
			);
538
539
			$distinguished_name = array_filter( $distinguished_name );
540
541
			// Create certificate only if distinguished name contains all required elements
542
			// @link http://stackoverflow.com/questions/13169588/how-to-check-if-multiple-array-keys-exists
543
			if ( count( array_intersect_key( array_flip( $required_keys ), $distinguished_name ) ) === count( $required_keys ) ) {
544
				$csr = openssl_csr_new( $distinguished_name, $pkey );
545
546
				$cert = openssl_csr_sign( $csr, null, $pkey, $data['_pronamic_gateway_number_days_valid'], $args, time() );
547
548
				openssl_x509_export( $cert, $certificate );
549
550
				$data['_pronamic_gateway_ideal_private_certificate'] = $certificate;
551
			}
552
		}
553
554
		return $data;
555
	}
556
}
557