Security   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 87
ccs 0
cts 25
cp 0
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get_md5_fingerprint() 0 2 1
A get_sha_fingerprint() 0 2 1
A get_fingerprint() 0 45 4
1
<?php
2
/**
3
 * Security.
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2021 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\IDealAdvancedV3;
12
13
/**
14
 * Title: Security
15
 * Description:
16
 * Copyright: 2005-2021 Pronamic
17
 * Company: Pronamic
18
 *
19
 * @author  Remco Tolsma
20
 * @version 2.0.0
21
 * @since   1.0.0
22
 */
23
class Security {
24
	/**
25
	 * Indicator for the begin of an certificate
26
	 *
27
	 * @var string
28
	 */
29
	const CERTIFICATE_BEGIN = '-----BEGIN CERTIFICATE-----';
30
31
	/**
32
	 * Indicator for the end of an certificate
33
	 *
34
	 * @var string
35
	 */
36
	const CERTIFICATE_END = '-----END CERTIFICATE-----';
37
38
	/**
39
	 * Get the sha1 fingerprint from the specified certificate
40
	 *
41
	 * @param string $certificate Certificate.
42
	 * @return string|null Fingerprint or `null` on failure.
43
	 */
44
	public static function get_sha_fingerprint( $certificate ) {
45
		return self::get_fingerprint( $certificate, 'sha1' );
46
	}
47
48
	/**
49
	 * Get the md5 fingerprint from the specified certificate
50
	 *
51
	 * @param string $certificate Certificate.
52
	 * @return string|null Fingerprint or `null` on failure.
53
	 */
54
	public static function get_md5_fingerprint( $certificate ) {
55
		return self::get_fingerprint( $certificate, 'md5' );
56
	}
57
58
	/**
59
	 * Get the fingerprint from the specified certificate
60
	 *
61
	 * @param string      $certificate Certificate.
62
	 * @param string|null $hash        Hashing algorithm.
63
	 * @return string|null Fingerprint or `null` on failure.
64
	 */
65
	public static function get_fingerprint( $certificate, $hash = null ) {
66
		$fingerprint = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $fingerprint is dead and can be removed.
Loading history...
67
68
		// The openssl_x509_read() function will throw an warning if the supplied
69
		// parameter cannot be coerced into an X509 certificate
70
		// @codingStandardsIgnoreStart
71
		$resource = @openssl_x509_read( $certificate );
72
		// @codingStandardsIgnoreEnd
73
74
		if ( false === $resource ) {
75
			return null;
76
		}
77
78
		$output = null;
79
80
		$result = openssl_x509_export( $resource, $output );
81
82
		if ( false === $result ) {
83
			return null;
84
		}
85
86
		$output = str_replace( self::CERTIFICATE_BEGIN, '', (string) $output );
87
		$output = str_replace( self::CERTIFICATE_END, '', $output );
88
89
		// Base64 decode.
90
		// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
91
		$fingerprint = base64_decode( $output );
92
93
		// Hash.
94
		if ( null !== $hash ) {
95
			$fingerprint = hash( $hash, $fingerprint );
96
		}
97
98
		/*
99
		 * Uppercase
100
		 *
101
		 * Cannot find private certificate file with fingerprint: b4845cb5cbcee3e1e0afef2662552a2365960e72
102
		 * (Note: Some acquirers only accept fingerprints in uppercase. Make the value of "KeyName" in your XML data uppercase.).
103
		 * https://www.ideal-checkout.nl/simulator/
104
		 *
105
		 * @since 1.1.11
106
		 */
107
		$fingerprint = strtoupper( $fingerprint );
108
109
		return $fingerprint;
110
	}
111
}
112