| Conditions | 16 |
| Paths | 10 |
| Total Lines | 114 |
| Code Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 309 | public function verify() { |
||
| 310 | $message = []; |
||
| 311 | |||
| 312 | if (!$this->valid()) { |
||
| 313 | throw new OCSPException('Certificate expired', OCSP_CERT_EXPIRED); |
||
| 314 | } |
||
| 315 | |||
| 316 | $issuer = $this->issuer(); |
||
| 317 | if (!is_object($issuer)) { |
||
| 318 | throw new OCSPException('No issuer', OCSP_NO_ISSUER); |
||
| 319 | } |
||
| 320 | |||
| 321 | /* Set custom error handler since the nemid ocsp library uses |
||
| 322 | * trigger_error() to throw errors when it cannot parse certain |
||
| 323 | * x509 fields which are not required for the OCSP Request. |
||
| 324 | * Also when receiving the OCSP request, the OCSP library |
||
| 325 | * triggers errors when the request does not adhere to the |
||
| 326 | * standard. |
||
| 327 | */ |
||
| 328 | set_error_handler("tempErrorHandler"); |
||
| 329 | |||
| 330 | $x509 = new X509(); |
||
| 331 | $issuer = $x509->certificate($issuer->der()); |
||
| 332 | $certificate = $x509->certificate($this->der()); |
||
| 333 | |||
| 334 | $ocspclient = new OCSP(); |
||
| 335 | $certID = $ocspclient->certOcspID( |
||
| 336 | [ |
||
| 337 | 'issuerName' => $issuer['tbsCertificate']['subject_der'], |
||
| 338 | // remember to skip the first byte it is the number of |
||
| 339 | // unused bits and it is always 0 for keys and certificates |
||
| 340 | 'issuerKey' => substr((string) $issuer['tbsCertificate']['subjectPublicKeyInfo']['subjectPublicKey'], 1), |
||
| 341 | 'serialNumber_der' => $certificate['tbsCertificate']['serialNumber_der'], |
||
| 342 | ], |
||
| 343 | 'sha1' |
||
| 344 | ); |
||
| 345 | |||
| 346 | $ocspreq = $ocspclient->request([$certID]); |
||
| 347 | |||
| 348 | $stream_options = [ |
||
| 349 | 'http' => [ |
||
| 350 | 'ignore_errors' => false, |
||
| 351 | 'method' => 'POST', |
||
| 352 | 'header' => 'Content-type: application/ocsp-request' . "\r\n", |
||
| 353 | 'content' => $ocspreq, |
||
| 354 | 'timeout' => 1, |
||
| 355 | ], |
||
| 356 | ]; |
||
| 357 | |||
| 358 | $ocspUrl = $this->ocspURL(); |
||
| 359 | // The OCSP URL is empty, import certificate, but show a warning. |
||
| 360 | if (strlen($ocspUrl) == 0) { |
||
| 361 | throw new OCSPException('The OCSP URL is empty', OCSP_NO_RESPONSE); |
||
| 362 | } |
||
| 363 | // Do the OCSP request |
||
| 364 | $context = stream_context_create($stream_options); |
||
| 365 | $derresponse = file_get_contents($ocspUrl, false, $context); |
||
| 366 | // OCSP service not available, import certificate, but show a warning. |
||
| 367 | if ($derresponse === false) { |
||
| 368 | throw new OCSPException('No response', OCSP_NO_RESPONSE); |
||
| 369 | } |
||
| 370 | $ocspresponse = $ocspclient->response($derresponse); |
||
| 371 | |||
| 372 | // Restore the previous error handler |
||
| 373 | restore_error_handler(); |
||
| 374 | |||
| 375 | // responseStatuses: successful, malformedRequest, |
||
| 376 | // internalError, tryLater, sigRequired, unauthorized. |
||
| 377 | if (isset($ocspresponse['responseStatus']) && |
||
| 378 | $ocspresponse['responseStatus'] !== 'successful') { |
||
| 379 | throw new OCSPException('Response status' . $ocspresponse['responseStatus'], OCSP_RESPONSE_STATUS); |
||
| 380 | } |
||
| 381 | |||
| 382 | $resp = $ocspresponse['responseBytes']['BasicOCSPResponse']['tbsResponseData']['responses'][0]; |
||
| 383 | /* |
||
| 384 | * OCSP response status, possible values are: good, revoked, |
||
| 385 | * unknown according to the RFC |
||
| 386 | * https://www.ietf.org/rfc/rfc2560.txt |
||
| 387 | */ |
||
| 388 | if ($resp['certStatus'] !== 'good') { |
||
| 389 | // Certificate status is not good, revoked or unknown |
||
| 390 | $exception = new OCSPException('Certificate status ' . $resp['certStatus'], OCSP_CERT_STATUS); |
||
| 391 | $exception->setCertStatus($resp['certStatus']); |
||
| 392 | |||
| 393 | throw $exception; |
||
| 394 | } |
||
| 395 | |||
| 396 | /* Check if: |
||
| 397 | * - hash algorithm is equal |
||
| 398 | * - check if issuerNamehash is the same from response |
||
| 399 | * - check if issuerKeyHash is the same from response |
||
| 400 | * - check if serialNumber is the same from response |
||
| 401 | */ |
||
| 402 | if ($resp['certID']['hashAlgorithm'] !== 'sha1' && |
||
| 403 | $resp['certID']['issuerNameHash'] !== $certID['issuerNameHash'] && |
||
| 404 | $resp['certID']['issuerKeyHash'] !== $certID['issuerKeyHash'] && |
||
| 405 | $resp['certID']['serialNumber'] !== $certID['serialNumber']) { |
||
| 406 | // OCSP Revocation, mismatch between original and checked certificate |
||
| 407 | throw new OCSPException('Certificate mismatch', OCSP_CERT_MISMATCH); |
||
| 408 | } |
||
| 409 | |||
| 410 | // check if OCSP revocation update is recent |
||
| 411 | $now = new DateTime(gmdate('YmdHis\Z')); |
||
| 412 | $thisUpdate = new DateTime($resp['thisUpdate']); |
||
| 413 | |||
| 414 | // Check if update time is earlier then our own time |
||
| 415 | if (!isset($resp['nextupdate']) && $thisUpdate > $now) { |
||
| 416 | throw new OCSPException('Update time earlier then our own time', OCSP_RESPONSE_TIME_EARLY); |
||
| 417 | } |
||
| 418 | |||
| 419 | // Current time should be between thisUpdate and nextUpdate. |
||
| 420 | if ($thisUpdate > $now && $now > new DateTime($resp['nextUpdate'])) { |
||
| 421 | // OCSP Revocation status not current |
||
| 422 | throw new OCSPException('Current time not between thisUpdate and nextUpdate', OCSP_RESPONSE_TIME_INVALID); |
||
| 423 | } |
||
| 426 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.