| Conditions | 17 |
| Paths | 200 |
| Total Lines | 75 |
| Code Lines | 50 |
| 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 |
||
| 97 | public function processCertificate($cadata) { |
||
| 98 | if ($cadata === FALSE) { // we are expecting a string anyway |
||
|
1 ignored issue
–
show
|
|||
| 99 | return FALSE; |
||
| 100 | } |
||
| 101 | $pemBegin = strpos($cadata, "-----BEGIN CERTIFICATE-----"); |
||
| 102 | if ($pemBegin !== FALSE) { |
||
|
1 ignored issue
–
show
|
|||
| 103 | $pemEnd = strpos($cadata, "-----END CERTIFICATE-----") + 25; |
||
| 104 | if ($pemEnd !== FALSE) { |
||
|
1 ignored issue
–
show
|
|||
| 105 | $cadata = substr($cadata, $pemBegin, $pemEnd - $pemBegin); |
||
| 106 | if ($cadata === FALSE) { |
||
|
1 ignored issue
–
show
|
|||
| 107 | throw new Exception("Impossible: despite having found BEGIN and END markers, unable to cut out substring!"); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | $authorityDer = $this->pem2der($cadata); |
||
| 111 | $authorityPem = $this->der2pem($authorityDer); |
||
| 112 | } else { |
||
| 113 | $authorityDer = $cadata; |
||
| 114 | $authorityPem = $this->der2pem($cadata); |
||
| 115 | } |
||
| 116 | |||
| 117 | // check that the certificate is OK |
||
| 118 | $myca = openssl_x509_read($authorityPem); |
||
| 119 | if ($myca == FALSE) { |
||
|
1 ignored issue
–
show
|
|||
| 120 | return FALSE; |
||
| 121 | } |
||
| 122 | $mydetails = openssl_x509_parse($myca); |
||
| 123 | if (!isset($mydetails['subject'])) { |
||
| 124 | return FALSE; |
||
| 125 | } |
||
| 126 | $md5 = openssl_digest($authorityDer, 'MD5'); |
||
| 127 | $sha1 = openssl_digest($authorityDer, 'SHA1'); |
||
| 128 | $out = ["pem" => $authorityPem, "der" => $authorityDer, "md5" => $md5, "sha1" => $sha1, "name" => $mydetails['name']]; |
||
| 129 | |||
| 130 | $out['root'] = 0; // default, unless concinved otherwise below |
||
| 131 | if ($mydetails['issuer'] === $mydetails['subject']) { |
||
| 132 | $out['root'] = 1; |
||
| 133 | $mydetails['type'] = 'root'; |
||
| 134 | } |
||
| 135 | // again default: not a CA unless convinced otherwise |
||
| 136 | $out['ca'] = 0; // we need to resolve this ambiguity |
||
| 137 | $out['basicconstraints_set'] = 0; |
||
| 138 | // if no basicContraints are set at all, this is a problem in itself |
||
| 139 | // is this a CA? or not? Treat as server, but add a warning... |
||
| 140 | if (isset($mydetails['extensions']['basicConstraints'])) { |
||
| 141 | $out['ca'] = preg_match('/^CA:TRUE/', $mydetails['extensions']['basicConstraints']); |
||
| 142 | $out['basicconstraints_set'] = 1; |
||
| 143 | } |
||
| 144 | |||
| 145 | if ($out['ca'] > 0 && $out['root'] == 0) { |
||
| 146 | $mydetails['type'] = 'interm_ca'; |
||
| 147 | } |
||
| 148 | if ($out['ca'] == 0 && $out['root'] == 0) { |
||
| 149 | $mydetails['type'] = 'server'; |
||
| 150 | } |
||
| 151 | $mydetails['sha1'] = $sha1; |
||
| 152 | // the signature algorithm is available in PHP7 with the property "signatureTypeSN", example "RSA-SHA512" |
||
| 153 | $out['full_details'] = $mydetails; |
||
| 154 | |||
| 155 | $algoMatch = []; |
||
| 156 | $keyLengthMatch = []; |
||
| 157 | // we are also interested in the type and length of public key, |
||
| 158 | // which ..._parse doesn't tell us :-( |
||
| 159 | openssl_x509_export($myca, $output, FALSE); |
||
| 160 | if (preg_match('/^\s+Public Key Algorithm:\s*(.*)\s*$/m', $output, $algoMatch) && in_array($algoMatch[1], X509::KNOWN_PUBLIC_KEY_ALGORITHMS)) { |
||
| 161 | $out['full_details']['public_key_algorithm'] = $algoMatch[1]; |
||
| 162 | } else { |
||
| 163 | $out['full_details']['public_key_algorithm'] = "UNKNOWN"; |
||
| 164 | } |
||
| 165 | |||
| 166 | if ((preg_match('/^\s+Public-Key:\s*\((.*) bit\)\s*$/m', $output, $keyLengthMatch)) && is_numeric($keyLengthMatch[1])) { |
||
| 167 | $out['full_details']['public_key_length'] = $keyLengthMatch[1]; |
||
| 168 | } else { |
||
| 169 | $out['full_details']['public_key_length'] = 0; // if we don't know, assume an unsafe key length -> will trigger warning |
||
| 170 | } |
||
| 171 | return $out; |
||
| 172 | } |
||
| 215 |