| Conditions | 17 |
| Paths | 131 |
| Total Lines | 152 |
| Code Lines | 106 |
| 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 |
||
| 70 | public function signRequest($csr, $expiryDays): array |
||
| 71 | { |
||
| 72 | if ($csr["CSR_STRING"] === NULL) { |
||
| 73 | throw new Exception("This CA needs the CSR in a string (PEM)!"); |
||
| 74 | } |
||
| 75 | // initialise connection to eduPKI CA / eduroam RA and send the request to them |
||
| 76 | try { |
||
| 77 | $altArray = [# Array mit den Subject Alternative Names |
||
| 78 | "email:" . $csr["USERNAME"] |
||
| 79 | ]; |
||
| 80 | $soapPub = $this->initEduPKISoapSession("PUBLIC"); |
||
| 81 | $this->loggerInstance->debug(5, "FIRST ACTUAL SOAP REQUEST (Public, newRequest)!\n"); |
||
| 82 | $this->loggerInstance->debug(5, "PARAM_1: " . CertificationAuthorityEduPki::EDUPKI_RA_ID . "\n"); |
||
| 83 | $this->loggerInstance->debug(5, "PARAM_2: " . $csr["CSR_STRING"] . "\n"); |
||
| 84 | $this->loggerInstance->debug(5, "PARAM_3: "); |
||
| 85 | $this->loggerInstance->debug(5, $altArray); |
||
| 86 | $this->loggerInstance->debug(5, "PARAM_4: " . CertificationAuthorityEduPki::EDUPKI_CERT_PROFILE . "\n"); |
||
| 87 | $this->loggerInstance->debug(5, "PARAM_5: " . sha1("notused") . "\n"); |
||
| 88 | $this->loggerInstance->debug(5, "PARAM_6: " . $csr["USERNAME"] . "\n"); |
||
| 89 | $this->loggerInstance->debug(5, "PARAM_7: " . $csr["USERNAME"] . "\n"); |
||
| 90 | $this->loggerInstance->debug(5, "PARAM_8: " . \config\ConfAssistant::SILVERBULLET['product_name'] . "\n"); |
||
| 91 | $this->loggerInstance->debug(5, "PARAM_9: false\n"); |
||
| 92 | $soapNewRequest = $soapPub->newRequest( |
||
| 93 | CertificationAuthorityEduPki::EDUPKI_RA_ID, # RA-ID |
||
| 94 | $csr["CSR_STRING"], # Request im PEM-Format |
||
| 95 | $altArray, # altNames |
||
| 96 | CertificationAuthorityEduPki::EDUPKI_CERT_PROFILE, # Zertifikatprofil |
||
| 97 | sha1("notused"), # PIN |
||
| 98 | $csr["USERNAME"], # Name des Antragstellers |
||
| 99 | $csr["USERNAME"], # Kontakt-E-Mail |
||
| 100 | \config\ConfAssistant::SILVERBULLET['product_name'], # Organisationseinheit des Antragstellers |
||
| 101 | false # Veröffentlichen des Zertifikats? |
||
| 102 | ); |
||
| 103 | $this->loggerInstance->debug(5, $soapPub->__getLastRequest()); |
||
| 104 | $this->loggerInstance->debug(5, $soapPub->__getLastResponse()); |
||
| 105 | if ($soapNewRequest == 0) { |
||
| 106 | throw new Exception("Error when sending SOAP request (request serial number was zero). No further details available."); |
||
| 107 | } |
||
| 108 | $soapReqnum = intval($soapNewRequest); |
||
| 109 | } catch (Exception $e) { |
||
| 110 | // PHP 7.1 can do this much better |
||
| 111 | if (is_soap_fault($e)) { |
||
| 112 | throw new Exception("Error when sending SOAP request: " . "{$e->faultcode}: { |
||
| 113 | $e->faultstring |
||
| 114 | }\n"); |
||
| 115 | } |
||
| 116 | throw new Exception("Something odd happened while doing the SOAP request:" . $e->getMessage()); |
||
| 117 | } |
||
| 118 | try { |
||
| 119 | $soap = $this->initEduPKISoapSession("RA"); |
||
| 120 | // tell the CA the desired expiry date of the new certificate |
||
| 121 | $expiry = new \DateTime(); |
||
| 122 | $expiry->modify("+$expiryDays day"); |
||
| 123 | $expiry->setTimezone(new \DateTimeZone("UTC")); |
||
| 124 | $soapExpiryChange = $soap->setRequestParameters( |
||
| 125 | $soapReqnum, [ |
||
| 126 | "RaID" => CertificationAuthorityEduPki::EDUPKI_RA_ID, |
||
| 127 | "Role" => CertificationAuthorityEduPki::EDUPKI_CERT_PROFILE, |
||
| 128 | "Subject" => "DC=eduroam,DC=test,DC=test,C=" . $csr["FED"] . ",O=" . \config\ConfAssistant::CONSORTIUM['name'] . ",OU=" . $csr["FED"] . ",CN=" . $csr['USERNAME'] . ",emailAddress=" . $csr['USERNAME'], |
||
| 129 | "SubjectAltNames" => ["email:" . $csr["USERNAME"]], |
||
| 130 | "NotBefore" => (new \DateTime())->format('c'), |
||
| 131 | "NotAfter" => $expiry->format('c'), |
||
| 132 | ] |
||
| 133 | ); |
||
| 134 | if ($soapExpiryChange === FALSE) { |
||
| 135 | throw new Exception("Error when sending SOAP request (unable to change expiry date)."); |
||
| 136 | } |
||
| 137 | // retrieve the raw request to prepare for signature and approval |
||
| 138 | // this seems to come out base64-decoded already; maybe PHP |
||
| 139 | // considers this "convenience"? But we need it as sent on |
||
| 140 | // the wire, so re-encode it! |
||
| 141 | $soapCleartext = $soap->getRawRequest($soapReqnum); |
||
| 142 | |||
| 143 | $this->loggerInstance->debug(5, "Actual received SOAP response for getRawRequest was:\n\n"); |
||
| 144 | $this->loggerInstance->debug(5, $soap->__getLastResponse()); |
||
| 145 | // for obnoxious reasons, we have to dump the request into a file and let pkcs7_sign read from the file |
||
| 146 | // rather than just using the string. Grr. |
||
| 147 | $tempdir = \core\common\Entity::createTemporaryDirectory("test"); |
||
| 148 | file_put_contents($tempdir['dir'] . "/content.txt", $soapCleartext); |
||
| 149 | // retrieve our RA cert from filesystem |
||
| 150 | // the RA certificates are not needed right now because we |
||
| 151 | // have resorted to S/MIME signatures with openssl command-line |
||
| 152 | // rather than the built-in functions. But that may change in |
||
| 153 | // the future, so let's park these two lines for future use. |
||
| 154 | // $raCertFile = file_get_contents(ROOT . "/config/SilverbulletClientCerts/edupki-test-ra.pem"); |
||
| 155 | // $raCert = openssl_x509_read($raCertFile); |
||
| 156 | // $raKey = openssl_pkey_get_private("file://" . ROOT . "/config/SilverbulletClientCerts/edupki-test-ra.clearkey"); |
||
| 157 | // sign the data, using cmdline because openssl_pkcs7_sign produces strange results |
||
| 158 | // -binary didn't help, nor switch -md to sha1 sha256 or sha512 |
||
| 159 | $this->loggerInstance->debug(5, "Actual content to be signed is this:\n $soapCleartext\n"); |
||
| 160 | $execCmd = \config\Master::PATHS['openssl'] . " smime -sign -binary -in " . $tempdir['dir'] . "/content.txt -out " . $tempdir['dir'] . "/signature.txt -outform pem -inkey " . ROOT . "/config/SilverbulletClientCerts/edupki-test-ra.clearkey -signer " . ROOT . "/config/SilverbulletClientCerts/edupki-test-ra.pem"; |
||
| 161 | $this->loggerInstance->debug(2, "Calling openssl smime with following cmdline: $execCmd\n"); |
||
| 162 | $output = []; |
||
| 163 | $return = 999; |
||
| 164 | exec($execCmd, $output, $return); |
||
| 165 | if ($return !== 0) { |
||
| 166 | throw new Exception("Non-zero return value from openssl smime!"); |
||
| 167 | } |
||
| 168 | // and get the signature blob back from the filesystem |
||
| 169 | $detachedSig = trim(file_get_contents($tempdir['dir'] . "/signature.txt")); |
||
| 170 | $this->loggerInstance->debug(5, "Request for server approveRequest has parameters:\n"); |
||
| 171 | $this->loggerInstance->debug(5, $soapReqnum . "\n"); |
||
| 172 | $this->loggerInstance->debug(5, $soapCleartext . "\n"); // PHP magically encodes this as base64 while sending! |
||
| 173 | $this->loggerInstance->debug(5, $detachedSig . "\n"); |
||
| 174 | $soapIssueCert = $soap->approveRequest($soapReqnum, $soapCleartext, $detachedSig); |
||
| 175 | $this->loggerInstance->debug(5, "approveRequest Request was: \n" . $soap->__getLastRequest()); |
||
| 176 | $this->loggerInstance->debug(5, "approveRequest Response was: \n" . $soap->__getLastResponse()); |
||
| 177 | if ($soapIssueCert === FALSE) { |
||
| 178 | throw new Exception("The locally approved request was NOT processed by the CA."); |
||
| 179 | } |
||
| 180 | // now, get the actual cert from the CA |
||
| 181 | sleep(55); |
||
| 182 | $counter = 55; |
||
| 183 | $parsedCert = FALSE; |
||
| 184 | do { |
||
| 185 | $counter += 5; |
||
| 186 | sleep(5); // always start with a wait. Signature round-trip on the server side is at least one minute. |
||
| 187 | $soapCert = $soap->getCertificateByRequestSerial($soapReqnum); |
||
| 188 | $x509 = new common\X509(); |
||
| 189 | if (strlen($soapCert) > 10) { |
||
| 190 | $parsedCert = $x509->processCertificate($soapCert); |
||
| 191 | } |
||
| 192 | } while ($parsedCert === FALSE && $counter < 500); |
||
| 193 | // we should now have an array |
||
| 194 | if ($parsedCert === FALSE) { |
||
| 195 | throw new Exception("We did not actually get a certificate after waiting for 5 minutes."); |
||
| 196 | } |
||
| 197 | // let's get the CA certificate chain |
||
| 198 | |||
| 199 | $caInfo = $soap->getCAInfo(); |
||
| 200 | $certList = $x509->splitCertificate($caInfo->CAChain[0]); |
||
| 201 | // find the root |
||
| 202 | $theRoot = ""; |
||
| 203 | foreach ($certList as $oneCert) { |
||
| 204 | $content = $x509->processCertificate($oneCert); |
||
| 205 | if ($content['root'] == 1) { |
||
| 206 | $theRoot = $content; |
||
| 207 | } |
||
| 208 | } |
||
| 209 | if ($theRoot == "") { |
||
| 210 | throw new Exception("CAInfo has no root certificate for us!"); |
||
| 211 | } |
||
| 212 | } catch (SoapFault $e) { |
||
| 213 | throw new Exception("SoapFault: Error when sending or receiving SOAP message: " . "{$e->faultcode}: {$e->faultname}: {$e->faultstring}: {$e->faultactor}: {$e->detail}: {$e->headerfault}\n"); |
||
| 214 | } catch (Exception $e) { |
||
| 215 | throw new Exception("Exception: Something odd happened between the SOAP requests:" . $e->getMessage()); |
||
| 216 | } |
||
| 217 | return [ |
||
| 218 | "CERT" => openssl_x509_read($parsedCert['pem']), |
||
| 219 | "SERIAL" => $parsedCert['full_details']['serialNumber'], |
||
| 220 | "ISSUER" => $theRoot, |
||
| 221 | "ROOT" => $theRoot, |
||
| 222 | ]; |
||
| 443 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths