Complex classes like U2F often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use U2F, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
44 | class U2F implements U2F_interface |
||
45 | { |
||
46 | /** @var string */ |
||
47 | private $appId; |
||
48 | |||
49 | /** @var null|string */ |
||
50 | private $attestDir; |
||
51 | |||
52 | /** @internal */ |
||
53 | private $FIXCERTS = array( |
||
54 | '349bca1031f8c82c4ceca38b9cebf1a69df9fb3b94eed99eb3fb9aa3822d26e8', |
||
55 | 'dd574527df608e47ae45fbba75a2afdd5c20fd94a02419381813cd55a2a3398f', |
||
56 | '1d8764f0f7cd1352df6150045c8f638e517270e8b5dda1c63ade9c2280240cae', |
||
57 | 'd0edc9a91a1677435a953390865d208c55b3183c6759c9b5a7ff494c322558eb', |
||
58 | '6073c436dcd064a48127ddbf6032ac1a66fd59a0c24434f070d4e564c124c897', |
||
59 | 'ca993121846c464d666096d35f13bf44c1b05af205f9b4a1e00cf6cc10c5e511' |
||
60 | ); |
||
61 | |||
62 | /** |
||
63 | * @param string $appId Application id for the running application |
||
64 | * @param string|null $attestDir Directory where trusted attestation roots may be found |
||
65 | * @throws U2fError If OpenSSL older than 1.0.0 is used |
||
66 | */ |
||
67 | public function __construct($appId, $attestDir = null) |
||
68 | { |
||
69 | if (OPENSSL_VERSION_NUMBER < 0x10000000) { |
||
70 | throw new U2fError('OpenSSL has to be at least version 1.0.0, this is ' . OPENSSL_VERSION_TEXT, U2fError::ERR_OLD_OPENSSL); |
||
71 | } |
||
72 | $this->appId = $appId; |
||
73 | $this->attestDir = $attestDir; |
||
74 | } |
||
75 | |||
76 | public function getRegisterData(array $registrations = array()) |
||
77 | { |
||
78 | $challenge = $this->createChallenge(); |
||
79 | $request = new RegisterRequest($challenge, $this->appId); |
||
80 | $signs = $this->getAuthenticateData($registrations); |
||
81 | return array($request, $signs); |
||
82 | } |
||
83 | |||
84 | public function doRegister(RegisterRequestInterface $request, RegisterResponseInterface $response, $includeCert = true) |
||
85 | { |
||
86 | if ($response->getErrorCode() !== null && $response->getErrorCode() !== 0) { |
||
87 | throw new U2fError('User-agent returned error. Error code: ' . $response->getErrorCode(), U2fError::ERR_BAD_UA_RETURNING); |
||
88 | } |
||
89 | |||
90 | if (!is_bool($includeCert)) { |
||
91 | throw new \InvalidArgumentException('$include_cert of doRegister() method only accepts boolean.'); |
||
92 | } |
||
93 | |||
94 | $rawReg = $this->base64u_decode($response->getRegistrationData()); |
||
95 | $regData = array_values(unpack('C*', $rawReg)); |
||
96 | $clientData = $this->base64u_decode($response->getClientData()); |
||
97 | $cli = json_decode($clientData); |
||
98 | |||
99 | if ($cli->challenge !== $request->getChallenge()) { |
||
100 | throw new U2fError('Registration challenge does not match', U2fError::ERR_UNMATCHED_CHALLENGE); |
||
101 | } |
||
102 | |||
103 | $registration = new Registration(); |
||
104 | $offs = 1; |
||
105 | $pubKey = substr($rawReg, $offs, self::PUBKEY_LEN); |
||
106 | $offs += self::PUBKEY_LEN; |
||
107 | // decode the pubKey to make sure it's good |
||
108 | $tmpKey = $this->pubkey_to_pem($pubKey); |
||
109 | if ($tmpKey === null) { |
||
110 | throw new U2fError('Decoding of public key failed', U2fError::ERR_PUBKEY_DECODE); |
||
111 | } |
||
112 | $registration->setPublicKey(base64_encode($pubKey)); |
||
113 | $khLen = $regData[$offs++]; |
||
114 | $kh = substr($rawReg, $offs, $khLen); |
||
115 | $offs += $khLen; |
||
116 | $registration->setKeyHandle($this->base64u_encode($kh)); |
||
117 | |||
118 | // length of certificate is stored in byte 3 and 4 (excluding the first 4 bytes) |
||
119 | $certLen = 4; |
||
120 | $certLen += ($regData[$offs + 2] << 8); |
||
121 | $certLen += $regData[$offs + 3]; |
||
122 | |||
123 | $rawCert = $this->fixSignatureUnusedBits(substr($rawReg, $offs, $certLen)); |
||
124 | $offs += $certLen; |
||
125 | $pemCert = "-----BEGIN CERTIFICATE-----\r\n"; |
||
126 | $pemCert .= chunk_split(base64_encode($rawCert), 64); |
||
127 | $pemCert .= "-----END CERTIFICATE-----"; |
||
128 | if ($includeCert) { |
||
129 | $registration->setCertificate(base64_encode($rawCert)); |
||
130 | } |
||
131 | if ($this->attestDir !== null) { |
||
132 | if (openssl_x509_checkpurpose($pemCert, -1, $this->get_certs()) !== true) { |
||
133 | throw new U2fError('Attestation certificate can not be validated', U2fError::ERR_ATTESTATION_VERIFICATION); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | if (!openssl_pkey_get_public($pemCert)) { |
||
138 | throw new U2fError('Decoding of public key failed', U2fError::ERR_PUBKEY_DECODE); |
||
139 | } |
||
140 | $signature = substr($rawReg, $offs); |
||
141 | |||
142 | $dataToVerify = chr(0); |
||
143 | $dataToVerify .= hash('sha256', $request->getAppId(), true); |
||
144 | $dataToVerify .= hash('sha256', $clientData, true); |
||
145 | $dataToVerify .= $kh; |
||
146 | $dataToVerify .= $pubKey; |
||
147 | |||
148 | if (openssl_verify($dataToVerify, $signature, $pemCert, 'sha256') === 1) { |
||
149 | return $registration; |
||
150 | } else { |
||
151 | throw new U2fError('Attestation signature does not match', U2fError::ERR_ATTESTATION_SIGNATURE); |
||
152 | } |
||
153 | } |
||
154 | |||
155 | public function getAuthenticateData(array $registrations) |
||
156 | { |
||
157 | $sigs = array(); |
||
158 | foreach ($registrations as $reg) { |
||
159 | if (!($reg instanceof RegistrationInterface)) { |
||
160 | throw new \InvalidArgumentException('$registrations of getAuthenticateData() method only accepts array of object.'); |
||
161 | } |
||
162 | $sigs[] = new SignRequest($this->createChallenge(), $reg->getKeyHandle(), $this->appId); |
||
163 | } |
||
164 | return $sigs; |
||
165 | } |
||
166 | |||
167 | public function doAuthenticate(array $requests, array $registrations, AuthenticationResponseInterface $response) |
||
168 | { |
||
169 | if ($response->getErrorCode() != null) { |
||
170 | throw new U2fError('User-agent returned error. Error code: ' . $response->getErrorCode(), U2fError::ERR_BAD_UA_RETURNING); |
||
171 | } |
||
172 | |||
173 | $clientData = $this->base64u_decode($response->getClientData()); |
||
174 | $decodedClient = json_decode($clientData); |
||
175 | /** |
||
176 | * @var SignRequestInterface $req |
||
177 | */ |
||
178 | foreach ($requests as $row) { |
||
179 | if (!($row instanceof SignRequestInterface)) { |
||
180 | throw new \InvalidArgumentException('$requests of doAuthenticate() method only accepts array of SignRequest.'); |
||
181 | } |
||
182 | |||
183 | if ($row->getKeyHandle() === $response->getKeyHandle() && $row->getChallenge() === $decodedClient->challenge) { |
||
184 | $req = $row; |
||
185 | break; |
||
186 | } |
||
187 | } |
||
188 | if (!isset($req)) { |
||
189 | throw new U2fError('No matching request found', U2fError::ERR_NO_MATCHING_REQUEST); |
||
190 | } |
||
191 | /** |
||
192 | * @var RegistrationInterface reg |
||
193 | */ |
||
194 | foreach ($registrations as $row) { |
||
195 | if (!($row instanceof RegistrationInterface)) { |
||
196 | throw new \InvalidArgumentException('$registrations of doAuthenticate() method only accepts array of Registration.'); |
||
197 | } |
||
198 | |||
199 | if ($row->getKeyHandle() === $response->getKeyHandle()) { |
||
200 | $reg = $row; |
||
201 | break; |
||
202 | } |
||
203 | } |
||
204 | if (!isset($reg)) { |
||
205 | throw new U2fError('No matching registration found', U2fError::ERR_NO_MATCHING_REGISTRATION); |
||
206 | } |
||
207 | $pemKey = $this->pubkey_to_pem($this->base64u_decode($reg->getPublicKey())); |
||
208 | if ($pemKey === null) { |
||
209 | throw new U2fError('Decoding of public key failed', U2fError::ERR_PUBKEY_DECODE); |
||
210 | } |
||
211 | |||
212 | $signData = $this->base64u_decode($response->getSignatureData()); |
||
213 | $dataToVerify = hash('sha256', $req->getAppId(), true); |
||
214 | $dataToVerify .= substr($signData, 0, 5); |
||
215 | $dataToVerify .= hash('sha256', $clientData, true); |
||
216 | $signature = substr($signData, 5); |
||
217 | |||
218 | if (openssl_verify($dataToVerify, $signature, $pemKey, 'sha256') === 1) { |
||
219 | $ctr = unpack("Nctr", substr($signData, 1, 4)); |
||
220 | $counter = $ctr['ctr']; |
||
221 | /* TODO: wrap-around should be handled somehow.. */ |
||
222 | if ($counter > $reg->getCounter()) { |
||
223 | $reg->setCounter($counter); |
||
224 | return $reg; |
||
225 | } else { |
||
226 | throw new U2fError('Counter too low.', U2fError::ERR_COUNTER_TOO_LOW); |
||
227 | } |
||
228 | } else { |
||
229 | throw new U2fError('Authentication failed', U2fError::ERR_AUTHENTICATION_FAILURE); |
||
230 | } |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @return array |
||
235 | */ |
||
236 | private function get_certs() |
||
237 | { |
||
238 | $files = array(); |
||
239 | if ($this->attestDir !== null && $handle = opendir($this->attestDir)) { |
||
240 | while (false !== ($entry = @readdir($handle))) { |
||
241 | if (is_file("{$this->attestDir}/$entry")) { |
||
242 | $files[] = "{$this->attestDir}/$entry"; |
||
243 | } |
||
244 | } |
||
245 | closedir($handle); |
||
246 | } |
||
247 | return $files; |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * @param string $data |
||
252 | * @return string |
||
253 | */ |
||
254 | private function base64u_encode($data) |
||
258 | |||
259 | /** |
||
260 | * @param string $data |
||
261 | * @return string |
||
262 | */ |
||
263 | private function base64u_decode($data) |
||
267 | |||
268 | /** |
||
269 | * @param string $key |
||
270 | * @return null|string |
||
271 | */ |
||
272 | private function pubkey_to_pem($key) |
||
273 | { |
||
298 | |||
299 | /** |
||
300 | * @return string |
||
301 | * @throws U2fError |
||
302 | */ |
||
303 | private function createChallenge() |
||
314 | |||
315 | /** |
||
316 | * Fixes a certificate where the signature contains unused bits. |
||
317 | * |
||
318 | * @param string $cert |
||
319 | * @return string |
||
320 | */ |
||
321 | private function fixSignatureUnusedBits($cert) |
||
328 | } |
||
329 |