--> -->

skimemo


skimemo - 日記/2018-08-06

_ TCPDFでSSL operation failedが出るので対処してみた

tcpdfを使用していて、特定の環境ではhttpsのimageが取得できない事例が発生しました。
相手先のSSL証明書はもちろん正しく設定されていて、SSL通信もTLS1.2で行われているのにも関わらずです。
エラーの原因はこれです。

file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed

ググると沢山出てきますが、このfile_get_contents()に限らず、ファイルから情報を取得する様々な関数でエラーになります。

エラーの出るサーバーでも別のバージョンのPHPで実行すると問題なかったりするので、PHP側の環境をごにょごにょする解決策もありそうですが、「PHPのバージョンを上げたら発生した」みたいな事例も多いようで、将来的に発生する可能性を潰すためTCPDF側に手を入れました。

なお、この方法は画像取得先のSSL証明書を無視します。意図しない画像をTCPDFに通す可能性がある場合は危険ですので適用しないでください。

変更するファイルは3つです。(行頭「-」が変更前、「+」が変更後(一応・・))
■/tcpdf/include/tcpdf_images.php
168行目付近

-		$a = getimagesize($file);
+		$a = getimagesizefromstring(self::file_get_contents_with_noverify($file));

206行目付近

-		$data = file_get_contents($file);
+		$data = self::file_get_contents_with_noverify($file);

243行目付近

-		$f = @fopen($file, 'rb');
+		$arrContextOptions=array(
+			"ssl"=>array(
+				"verify_peer"=>false,
+				"verify_peer_name"=>false,
+			),
+		);
+		$f = @fopen($file, 'rb', false, stream_context_create($arrContextOptions));

最後の方に追加

+	public static function file_get_contents_with_noverify($file) {
+		$arrContextOptions=array(
+			"ssl"=>array(
+				"verify_peer"=>false,
+				"verify_peer_name"=>false,
+			),
+		);
+		return file_get_contents($file, false, stream_context_create($arrContextOptions));
+	}


■/tcpdf/include/tcpdf_static.php
1916行目付近

-			$ret = @file_get_contents($path);
+			$ret = TCPDF_IMAGES::file_get_contents_with_noverify($path);

■/tcpdf/tcpdf.php
6853行目(!)付近

-			if (($imsize = @getimagesize($file)) === FALSE) {
+			if (($imsize = @getimagesizefromstring(TCPDF_IMAGES::file_get_contents_with_noverify($file))) === FALSE) {

6874行目付近

-			$imsize = @getimagesize($file);
+			$imsize = @getimagesizefromstring(TCPDF_IMAGES::file_get_contents_with_noverify($file));

7036行目付近

-				$img = imagecreatefrompng($file);
+				$img = imagecreatefromstring(TCPDF_IMAGES::file_get_contents_with_noverify($file));

7285行目付近

-			$img = imagecreatefrompng($file);
+			$img = imagecreatefromstring(TCPDF_IMAGES::file_get_contents_with_noverify($file));


これからSSL必須になってきますから、色々なところで色々な事がおきそうですね、これ・・・。
2018年PHP+SSL問題と言ってもいいような良くないような・・・(笑)。

Category: [Linux] - 09:25:08



 
Last-modified: 2018-08-06 (月) 09:25:08 (2083d)