Repository navigation
Expand file tree
/
Copy pathclass-mock-keyring.php
More file actions
128 lines (104 loc) · 2.97 KB
/
Copy pathclass-mock-keyring.php
File metadata and controls
128 lines (104 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/**
* Configurable WP_Secrets_Keyring test double. Not real cryptography – a
* non-deterministic transform with an integrity tag, just enough to stand in for
* a real keyring under the conformance suite without needing that code to also
* exercise libsodium.
*/
class Mock_Keyring extends WP_Secrets_Keyring {
const MARKER = 'mock-wrapped:';
/**
* Whether wrap() should return WP_Error.
*
* @var bool
*/
private $fail_wrap = false;
/**
* Whether unwrap() should return WP_Error.
*
* @var bool
*/
private $fail_unwrap = false;
/**
* Number of times wrap() has been called.
*
* @var int
*
* @phpstan-var int<0, max>
*/
private $wrap_calls = 0;
/**
* Number of times unwrap() has been called.
*
* @var int
*
* @phpstan-var int<0, max>
*/
private $unwrap_calls = 0;
public function wrap( string $key_material ) {
++$this->wrap_calls;
if ( $this->fail_wrap ) {
return new WP_Error( WP_SECRETS_ERROR_KEY_UNAVAILABLE, 'Mock_Keyring: wrap() configured to fail.' );
}
$nonce = random_bytes( 8 );
$tag = hash( 'sha256', $nonce . $key_material, true );
return self::MARKER . base64_encode( $nonce . $key_material . $tag );
}
public function unwrap( string $wrapped ) {
++$this->unwrap_calls;
if ( $this->fail_unwrap ) {
return new WP_Error( WP_SECRETS_ERROR_KEY_UNAVAILABLE, 'Mock_Keyring: unwrap() configured to fail.' );
}
if ( ! str_starts_with( $wrapped, self::MARKER ) ) {
return new WP_Error( WP_SECRETS_ERROR_KEY_UNAVAILABLE, 'Mock_Keyring: not a value this keyring wrapped.' );
}
$decoded = base64_decode( substr( $wrapped, strlen( self::MARKER ) ), true );
if ( false === $decoded || strlen( $decoded ) < 41 ) {
return new WP_Error( WP_SECRETS_ERROR_KEY_UNAVAILABLE, 'Mock_Keyring: wrapped value is malformed.' );
}
$nonce = substr( $decoded, 0, 8 );
$key_material = substr( $decoded, 8, -32 );
$tag = substr( $decoded, -32 );
if ( ! hash_equals( hash( 'sha256', $nonce . $key_material, true ), $tag ) ) {
return new WP_Error( WP_SECRETS_ERROR_KEY_UNAVAILABLE, 'Mock_Keyring: integrity tag mismatch.' );
}
return $key_material;
}
public function get_key_source(): string {
return 'mock keyring';
}
/**
* @param bool $fail Whether wrap() should return WP_Error.
*
* @return $this
*/
public function configure_fail_wrap( bool $fail = true ): self {
$this->fail_wrap = $fail;
return $this;
}
/**
* @param bool $fail Whether unwrap() should return WP_Error.
*
* @return $this
*/
public function configure_fail_unwrap( bool $fail = true ): self {
$this->fail_unwrap = $fail;
return $this;
}
/**
* @return int Number of times wrap() has been called.
*
* @phpstan-return int<0, max>
*/
public function wrap_call_count(): int {
return $this->wrap_calls;
}
/**
* @return int Number of times unwrap() has been called.
*
* @phpstan-return int<0, max>
*/
public function unwrap_call_count(): int {
return $this->unwrap_calls;
}
}