Security   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 19
c 2
b 0
f 0
dl 0
loc 78
ccs 0
cts 22
cp 0
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
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-2020 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\Adyen
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Adyen;
12
13
/**
14
 * Title: Security
15
 * Description:
16
 * Copyright: 2005-2020 Pronamic
17
 * Company: Pronamic
18
 *
19
 * @author  Remco Tolsma
20
 * @version 1.0.7
21
 * @since   1.0.7
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
	 *
43
	 * @return null|string Fingerprint or null on failure
44
	 */
45
	public static function get_sha_fingerprint( $certificate ) {
46
		return self::get_fingerprint( $certificate, 'sha1' );
47
	}
48
49
	/**
50
	 * Get the fingerprint from the specified certificate
51
	 *
52
	 * @param string      $certificate Certificate.
53
	 * @param null|string $hash        Hash.
54
	 * @return null|string Fingerprint or null on failure.
55
	 */
56
	public static function get_fingerprint( $certificate, $hash = null ) {
57
		// The openssl_x509_read() function will throw an warning if the supplied
58
		// parameter cannot be coerced into an X509 certificate
59
		// @codingStandardsIgnoreStart
60
		$resource = @openssl_x509_read( $certificate );
61
		// @codingStandardsIgnoreEnd
62
63
		if ( false === $resource ) {
64
			return null;
65
		}
66
67
		$output = '';
68
69
		$result = \openssl_x509_export( $resource, $output );
70
71
		if ( false === $result ) {
72
			return null;
73
		}
74
75
		$output = \str_replace( self::CERTIFICATE_BEGIN, '', $output );
76
		$output = \str_replace( self::CERTIFICATE_END, '', $output );
77
78
		$output = \strval( $output );
79
80
		// Base64 decode.
81
		// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
82
		$fingerprint = \base64_decode( $output );
83
84
		// Hash.
85
		if ( null !== $hash ) {
86
			$fingerprint = \hash( $hash, $fingerprint );
87
		}
88
89
		/*
90
		 * Uppercase
91
		 *
92
		 * Cannot find private certificate file with fingerprint: b4845cb5cbcee3e1e0afef2662552a2365960e72
93
		 * (Note: Some acquirers only accept fingerprints in uppercase. Make the value of "KeyName" in your XML data uppercase.).
94
		 * https://www.ideal-checkout.nl/simulator/
95
		 *
96
		 * @since 1.1.11
97
		 */
98
		$fingerprint = \strtoupper( $fingerprint );
99
100
		return $fingerprint;
101
	}
102
}
103