@@ -902,7 +902,7 @@ public static function deleteLocalSession()
902902 /**
903903 * Calculates the fingerprint of a x509cert.
904904 *
905- * @param string $x509cert x509 cert
905+ * @param string $x509cert x509 cert formatted
906906 * @param string $alg Algorithm to be used in order to calculate the fingerprint
907907 *
908908 * @return null|string Formatted fingerprint
@@ -911,27 +911,30 @@ public static function calculateX509Fingerprint($x509cert, $alg = 'sha1')
911911 {
912912 assert (is_string ($ x509cert ));
913913
914- $ lines = explode ("\n" , $ x509cert );
915-
914+ $ arCert = explode ("\n" , $ x509cert );
916915 $ data = '' ;
917-
918- foreach ($ lines as $ line ) {
919- /* Remove '\r' from end of line if present. */
920- $ line = rtrim ($ line );
921- if ($ line === '-----BEGIN CERTIFICATE----- ' ) {
922- /* Delete junk from before the certificate. */
923- $ data = '' ;
924- } elseif ($ line === '-----END CERTIFICATE----- ' ) {
925- /* Ignore data after the certificate. */
926- break ;
927- } elseif ($ line === '-----BEGIN PUBLIC KEY----- ' || $ line === '-----BEGIN RSA PRIVATE KEY----- ' ) {
928- /* This isn't an X509 certificate. */
929- return null ;
916+ $ inData = false ;
917+
918+ foreach ($ arCert as $ curData ) {
919+ if (! $ inData ) {
920+ if (strncmp ($ curData , '-----BEGIN CERTIFICATE ' , 22 ) == 0 ) {
921+ $ inData = true ;
922+ } elseif ((strncmp ($ curData , '-----BEGIN PUBLIC KEY ' , 21 ) == 0 ) || (strncmp ($ curData , '-----BEGIN RSA PRIVATE KEY ' , 26 ) == 0 )) {
923+ /* This isn't an X509 certificate. */
924+ return null ;
925+ }
930926 } else {
931- /* Append the current line to the certificate data. */
932- $ data .= $ line ;
927+ if (strncmp ($ curData , '-----END CERTIFICATE ' , 20 ) == 0 ) {
928+ break ;
929+ }
930+ $ data .= trim ($ curData );
933931 }
934932 }
933+
934+ if (empty ($ data )) {
935+ return null ;
936+ }
937+
935938 $ decodedData = base64_decode ($ data );
936939
937940 switch ($ alg ) {
0 commit comments