Failed Conditions
Push — feature/webhook-status ( e8032c )
by Reüel
05:37
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
	/**
19
	 * Settings constructor.
20
	 */
21
	public function __construct() {
22
		// Filters.
23
		add_filter( 'pronamic_pay_gateway_fields', array( $this, 'fields' ) );
24
25
		// Actions.
26
		add_action( 'current_screen', array( $this, 'maybe_download_private_certificate' ) );
27
		add_action( 'current_screen', array( $this, 'maybe_download_private_key' ) );
28
	}
29
30
	/**
31
	 * Fields.
32
	 *
33
	 * @param array $fields Settings fields.
34
	 *
35
	 * @return array
36
	 */
37
	public function fields( array $fields ) {
38
		/*
39
		 * Private Key and Certificate
40
		 */
41
42
		// Private key and certificate information.
43
		$fields[] = array(
44
			'section'  => 'ideal',
45
			'methods'  => array( 'ideal-advanced-v3' ),
46
			'title'    => __( 'Private key and certificate', 'pronamic_ideal' ),
47
			'type'     => 'description',
48
			'callback' => array( $this, 'field_security' ),
49
		);
50
51
		// Organization.
52
		$fields[] = array(
53
			'filter'   => FILTER_SANITIZE_STRING,
54
			'section'  => 'ideal',
55
			'methods'  => array( 'ideal-advanced-v3' ),
56
			'group'    => 'pk-cert',
57
			'meta_key' => '_pronamic_gateway_organization',
58
			'title'    => __( 'Organization', 'pronamic_ideal' ),
59
			'type'     => 'text',
60
			'tooltip'  => __( 'Organization name, e.g. Pronamic', 'pronamic_ideal' ),
61
		);
62
63
		// Organization Unit.
64
		$fields[] = array(
65
			'filter'   => FILTER_SANITIZE_STRING,
66
			'section'  => 'ideal',
67
			'methods'  => array( 'ideal-advanced-v3' ),
68
			'group'    => 'pk-cert',
69
			'meta_key' => '_pronamic_gateway_organization_unit',
70
			'title'    => __( 'Organization Unit', 'pronamic_ideal' ),
71
			'type'     => 'text',
72
			'tooltip'  => __( 'Organization unit, e.g. Administration', 'pronamic_ideal' ),
73
		);
74
75
		// Locality.
76
		$fields[] = array(
77
			'filter'   => FILTER_SANITIZE_STRING,
78
			'section'  => 'ideal',
79
			'methods'  => array( 'ideal-advanced-v3' ),
80
			'group'    => 'pk-cert',
81
			'meta_key' => '_pronamic_gateway_locality',
82
			'title'    => __( 'City', 'pronamic_ideal' ),
83
			'type'     => 'text',
84
			'tooltip'  => __( 'City, e.g. Amsterdam', 'pronamic_ideal' ),
85
		);
86
87
		// State or Province.
88
		$fields[] = array(
89
			'filter'   => FILTER_SANITIZE_STRING,
90
			'section'  => 'ideal',
91
			'methods'  => array( 'ideal-advanced-v3' ),
92
			'group'    => 'pk-cert',
93
			'meta_key' => '_pronamic_gateway_state_or_province',
94
			'title'    => __( 'State / province', 'pronamic_ideal' ),
95
			'type'     => 'text',
96
			'tooltip'  => __( 'State or province, e.g. Friesland', 'pronamic_ideal' ),
97
		);
98
99
		// Country.
100
		$locale = explode( '_', get_locale() );
101
102
		$locale = array_pop( $locale );
103
104
		$fields[] = array(
105
			'filter'      => FILTER_SANITIZE_STRING,
106
			'section'     => 'ideal',
107
			'methods'     => array( 'ideal-advanced-v3' ),
108
			'group'       => 'pk-cert',
109
			'meta_key'    => '_pronamic_gateway_country',
110
			'title'       => __( 'Country', 'pronamic_ideal' ),
111
			'type'        => 'text',
112
			'tooltip'     => sprintf(
113
				'%s %s (ISO-3166-1 alpha-2)',
114
				__( '2 letter country code, e.g.', 'pronamic_ideal' ),
115
				strtoupper( $locale )
116
			),
117
			'size'        => 2,
118
			'description' => sprintf(
119
				'%s %s',
120
				__( '2 letter country code, e.g.', 'pronamic_ideal' ),
121
				strtoupper( $locale )
122
			),
123
		);
124
125
		// Email Address.
126
		$fields[] = array(
127
			'filter'   => FILTER_SANITIZE_STRING,
128
			'section'  => 'ideal',
129
			'methods'  => array( 'ideal-advanced-v3' ),
130
			'group'    => 'pk-cert',
131
			'meta_key' => '_pronamic_gateway_email',
132
			'title'    => __( 'E-mail address', 'pronamic_ideal' ),
133
			'tooltip'  => sprintf(
134
				/* translators: %s: admin email */
135
				__( 'E-mail address, e.g. %s', 'pronamic_ideal' ),
136
				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

136
				/** @scrutinizer ignore-type */ get_option( 'admin_email' )
Loading history...
137
			),
138
			'type'     => 'text',
139
		);
140
141
		// Number Days Valid.
142
		$fields[] = array(
143
			'filter'   => FILTER_SANITIZE_NUMBER_INT,
144
			'section'  => 'ideal',
145
			'methods'  => array( 'ideal-advanced-v3' ),
146
			'group'    => 'pk-cert',
147
			'meta_key' => '_pronamic_gateway_number_days_valid',
148
			'title'    => __( 'Number Days Valid', 'pronamic_ideal' ),
149
			'type'     => 'text',
150
			'default'  => 1825,
151
			'tooltip'  => __( 'Number of days the generated certificate will be valid for, e.g. 1825 days for the maximum duration of 5 years.', 'pronamic_ideal' ),
152
		);
153
154
		// Private Key Password.
155
		$fields[] = array(
156
			'filter'   => FILTER_SANITIZE_STRING,
157
			'section'  => 'ideal',
158
			'methods'  => array( 'ideal-advanced-v3' ),
159
			'group'    => 'pk-cert',
160
			'meta_key' => '_pronamic_gateway_ideal_private_key_password',
161
			'title'    => __( 'Private Key Password', 'pronamic_ideal' ),
162
			'type'     => 'text',
163
			'classes'  => array( 'regular-text', 'code' ),
164
			'default'  => wp_generate_password(),
165
			'tooltip'  => __( 'A random password which will be used for the generation of the private key and certificate.', 'pronamic_ideal' ),
166
		);
167
168
		// Private Key.
169
		$fields[] = array(
170
			'filter'   => FILTER_SANITIZE_STRING,
171
			'section'  => 'ideal',
172
			'methods'  => array( 'ideal-advanced-v3' ),
173
			'group'    => 'pk-cert',
174
			'meta_key' => '_pronamic_gateway_ideal_private_key',
175
			'title'    => __( 'Private Key', 'pronamic_ideal' ),
176
			'type'     => 'textarea',
177
			'callback' => array( $this, 'field_private_key' ),
178
			'classes'  => array( 'code' ),
179
			'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' ),
180
		);
181
182
		// Private Certificate.
183
		$fields[] = array(
184
			'filter'   => FILTER_SANITIZE_STRING,
185
			'section'  => 'ideal',
186
			'methods'  => array( 'ideal-advanced-v3' ),
187
			'group'    => 'pk-cert',
188
			'meta_key' => '_pronamic_gateway_ideal_private_certificate',
189
			'title'    => __( 'Private Certificate', 'pronamic_ideal' ),
190
			'type'     => 'textarea',
191
			'callback' => array( $this, 'field_private_certificate' ),
192
			'classes'  => array( 'code' ),
193
			'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' ),
194
		);
195
196
		// Transaction feedback.
197
		$fields[] = array(
198
			'section' => 'ideal',
199
			'methods' => array( 'ideal-advanced-v3' ),
200
			'title'   => __( 'Transaction feedback', 'pronamic_ideal' ),
201
			'type'    => 'description',
202
			'html'    => __( 'Payment status updates will be processed without any additional configuration.', 'pronamic_ideal' ),
203
		);
204
205
		// Return.
206
		return $fields;
207
	}
208
209
	/**
210
	 * Field security
211
	 *
212
	 * @param array $field Field.
213
	 */
214
	public function field_security( $field ) {
215
		$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

215
		$certificate = get_post_meta( /** @scrutinizer ignore-type */ get_the_ID(), '_pronamic_gateway_ideal_private_certificate', true );
Loading history...
216
217
		?>
218
		<p>
219
			<?php if ( empty( $certificate ) ) : ?>
220
221
				<span
222
					class="dashicons dashicons-no"></span> <?php esc_html_e( 'The private key and certificate have not yet been configured.', 'pronamic_ideal' ); ?>
223
				<br/>
224
225
				<br/>
226
227
				<?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' ); ?>
228
229
			<?php else : ?>
230
231
				<span
232
					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' ); ?>
233
				<br/>
234
235
				<br/>
236
237
				<?php
238
239
				submit_button(
240
					__( 'Download certificate', 'pronamic_ideal' ),
241
					'secondary',
242
					'download_private_certificate',
243
					false
244
				);
245
246
				?>
247
248
				<a class="pronamic-pay-btn-link" href="#" id="pk-cert-fields-toggle"><?php esc_html_e( 'Show details…', 'pronamic_ideal' ); ?></a>
249
250
			<?php endif; ?>
251
		</p>
252
		<?php
253
	}
254
255
	/**
256
	 * Field private key.
257
	 *
258
	 * @param array $field Field.
259
	 */
260
	public function field_private_key( $field ) {
261
		$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

261
		$private_key          = get_post_meta( /** @scrutinizer ignore-type */ get_the_ID(), '_pronamic_gateway_ideal_private_key', true );
Loading history...
262
		$private_key_password = get_post_meta( get_the_ID(), '_pronamic_gateway_ideal_private_key_password', true );
263
		$number_days_valid    = get_post_meta( get_the_ID(), '_pronamic_gateway_number_days_valid', true );
264
265
		$filename = __( 'ideal.key', 'pronamic_ideal' );
266
267
		if ( ! empty( $private_key_password ) && ! empty( $number_days_valid ) ) {
268
			$command = sprintf(
269
				'openssl genrsa -aes128 -out %s -passout pass:%s 2048',
270
				escapeshellarg( $filename ),
271
				escapeshellarg( $private_key_password )
272
			);
273
274
			?>
275
276
			<p><?php esc_html_e( 'OpenSSL command', 'pronamic_ideal' ); ?></p>
277
			<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"/>
278
279
			<?php
280
		} else {
281
			printf(
282
				'<p class="pronamic-pay-description description">%s</p>',
283
				esc_html__( 'Leave empty and save the configuration to generate the private key or view the OpenSSL command.', 'pronamic_ideal' )
284
			);
285
		}
286
287
		?>
288
		<p>
289
			<?php
290
291
			if ( ! empty( $private_key ) ) {
292
				submit_button(
293
					__( 'Download', 'pronamic_ideal' ),
294
					'secondary',
295
					'download_private_key',
296
					false
297
				);
298
299
				echo ' ';
300
			}
301
302
			printf(
303
				'<label class="pronamic-pay-form-control-file-button button">%s <input type="file" name="%s" /></label>',
304
				esc_html__( 'Upload', 'pronamic_ideal' ),
305
				'_pronamic_gateway_ideal_private_key_file'
306
			);
307
308
			?>
309
		</p>
310
		<?php
311
	}
312
313
	/**
314
	 * Field private certificate.
315
	 *
316
	 * @param array $field Field.
317
	 */
318
	public function field_private_certificate( $field ) {
319
		$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

319
		$certificate = get_post_meta( /** @scrutinizer ignore-type */ get_the_ID(), '_pronamic_gateway_ideal_private_certificate', true );
Loading history...
320
321
		$private_key_password = get_post_meta( get_the_ID(), '_pronamic_gateway_ideal_private_key_password', true );
322
		$number_days_valid    = get_post_meta( get_the_ID(), '_pronamic_gateway_number_days_valid', true );
323
324
		$filename_key = __( 'ideal.key', 'pronamic_ideal' );
325
		$filename_cer = __( 'ideal.cer', 'pronamic_ideal' );
326
327
		// @link http://www.openssl.org/docs/apps/req.html
328
		$subj_args = array(
329
			'C'            => get_post_meta( get_the_ID(), '_pronamic_gateway_country', true ),
330
			'ST'           => get_post_meta( get_the_ID(), '_pronamic_gateway_state_or_province', true ),
331
			'L'            => get_post_meta( get_the_ID(), '_pronamic_gateway_locality', true ),
332
			'O'            => get_post_meta( get_the_ID(), '_pronamic_gateway_organization', true ),
333
			'OU'           => get_post_meta( get_the_ID(), '_pronamic_gateway_organization_unit', true ),
334
			'CN'           => get_post_meta( get_the_ID(), '_pronamic_gateway_organization', true ),
335
			'emailAddress' => get_post_meta( get_the_ID(), '_pronamic_gateway_email', true ),
336
		);
337
338
		$subj_args = array_filter( $subj_args );
339
340
		$subj = '';
341
		foreach ( $subj_args as $type => $value ) {
342
			$subj .= '/' . $type . '=' . addslashes( $value );
343
		}
344
345
		if ( ! empty( $subj ) ) {
346
			$command = trim(
347
				sprintf(
348
					'openssl req -x509 -sha256 -new -key %s -passin pass:%s -days %s -out %s %s',
349
					escapeshellarg( $filename_key ),
350
					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

350
					escapeshellarg( /** @scrutinizer ignore-type */ $private_key_password ),
Loading history...
351
					escapeshellarg( $number_days_valid ),
352
					escapeshellarg( $filename_cer ),
353
					empty( $subj ) ? '' : sprintf( '-subj %s', escapeshellarg( $subj ) )
354
				)
355
			);
356
357
			?>
358
359
			<p><?php esc_html_e( 'OpenSSL command', 'pronamic_ideal' ); ?></p>
360
			<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"/>
361
362
			<?php
363
		} else {
364
			printf(
365
				'<p class="pronamic-pay-description description">%s</p>',
366
				esc_html__( 'Leave empty and save the configuration to generate the certificate or view the OpenSSL command.', 'pronamic_ideal' )
367
			);
368
		}
369
370
		if ( ! empty( $certificate ) ) {
371
			$fingerprint = Security::get_sha_fingerprint( $certificate );
372
			$fingerprint = str_split( $fingerprint, 2 );
373
			$fingerprint = implode( ':', $fingerprint );
374
375
			echo '<dl>';
376
377
			echo '<dt>', esc_html__( 'SHA Fingerprint', 'pronamic_ideal' ), '</dt>';
378
			echo '<dd>', esc_html( $fingerprint ), '</dd>';
379
380
			$info = openssl_x509_parse( $certificate );
381
382
			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...
383
				$date_format = __( 'M j, Y @ G:i', 'pronamic_ideal' );
384
385
				if ( isset( $info['validFrom_time_t'] ) ) {
386
					echo '<dt>', esc_html__( 'Valid From', 'pronamic_ideal' ), '</dt>';
387
					echo '<dd>', esc_html( date_i18n( $date_format, $info['validFrom_time_t'] ) ), '</dd>';
388
				}
389
390
				if ( isset( $info['validTo_time_t'] ) ) {
391
					echo '<dt>', esc_html__( 'Valid To', 'pronamic_ideal' ), '</dt>';
392
					echo '<dd>', esc_html( date_i18n( $date_format, $info['validTo_time_t'] ) ), '</dd>';
393
				}
394
			}
395
396
			echo '</dl>';
397
		}
398
399
		?>
400
		<p>
401
			<?php
402
403
			if ( ! empty( $certificate ) ) {
404
				submit_button(
405
					__( 'Download', 'pronamic_ideal' ),
406
					'secondary',
407
					'download_private_certificate',
408
					false
409
				);
410
411
				echo ' ';
412
			}
413
414
			printf(
415
				'<label class="pronamic-pay-form-control-file-button button">%s <input type="file" name="%s" /></label>',
416
				esc_html__( 'Upload', 'pronamic_ideal' ),
417
				'_pronamic_gateway_ideal_private_certificate_file'
418
			);
419
420
			?>
421
		</p>
422
		<?php
423
	}
424
425
	/**
426
	 * Download private certificate
427
	 */
428
	public function maybe_download_private_certificate() {
429
		if ( filter_has_var( INPUT_POST, 'download_private_certificate' ) ) {
430
			$post_id = filter_input( INPUT_POST, 'post_ID', FILTER_SANITIZE_STRING );
431
432
			$filename = sprintf( 'ideal-private-certificate-%s.cer', $post_id );
433
434
			header( 'Content-Description: File Transfer' );
435
			header( 'Content-Disposition: attachment; filename=' . $filename );
436
			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

436
			header( 'Content-Type: application/x-x509-ca-cert; charset=' . /** @scrutinizer ignore-type */ get_option( 'blog_charset' ), true );
Loading history...
437
438
			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

438
			echo /** @scrutinizer ignore-type */ get_post_meta( $post_id, '_pronamic_gateway_ideal_private_certificate', true ); // xss ok.
Loading history...
439
440
			exit;
441
		}
442
	}
443
444
	/**
445
	 * Download private key
446
	 */
447
	public function maybe_download_private_key() {
448
		if ( filter_has_var( INPUT_POST, 'download_private_key' ) ) {
449
			$post_id = filter_input( INPUT_POST, 'post_ID', FILTER_SANITIZE_STRING );
450
451
			$filename = sprintf( 'ideal-private-key-%s.key', $post_id );
452
453
			header( 'Content-Description: File Transfer' );
454
			header( 'Content-Disposition: attachment; filename=' . $filename );
455
			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

455
			header( 'Content-Type: application/pgp-keys; charset=' . /** @scrutinizer ignore-type */ get_option( 'blog_charset' ), true );
Loading history...
456
457
			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

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