A first version of a gpg signature checker
authorx2003bruneau <x2003bruneau@9869982d-c50d-0410-be91-f2a2ec7c7c7b>
Mon, 11 Jun 2007 23:06:56 +0000 (23:06 +0000)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Fri, 4 Jan 2008 23:35:45 +0000 (00:35 +0100)
git-svn-id: svn+ssh://murphy/home/svn/banana/trunk@264 9869982d-c50d-0410-be91-f2a2ec7c7c7b

banana/mimepart.inc.php

index 9270438..5e603b9 100644 (file)
@@ -19,6 +19,7 @@ class BananaMimePart
     private $boundary     = null;
     private $filename     = null;
     private $format       = null;
+    private $sign_protocole = null;
 
     private $body         = null;
     private $multipart    = null;
@@ -81,12 +82,13 @@ class BananaMimePart
         return true;
     }
 
-    protected function makeMultiPart($body, $content_type, $encoding, $boundary)
+    protected function makeMultiPart($body, $content_type, $encoding, $boundary, $sign_protocole)
     {
         $this->body         = $body;
         $this->content_type = $content_type;
         $this->encoding     = $encoding;
         $this->boundary     = $boundary;
+        $this->sign_protocole = $sign_protocole;
         $this->parse();
     }
 
@@ -173,6 +175,7 @@ class BananaMimePart
             $filename     = $this->getHeader('content-disposition', '/filename="?([^ "]+?)"?\s*(;|$)/i');
             $format       = strtolower($this->getHeader('content-type', '/format="?([^ "]+?)"?\s*(;|$)/i'));
             $id           = $this->getHeader('content-id', '/<(.*?)>/');
+            $sign_protocole = strtolower($this->getHeader('content-type', '/protocol="?([^ "]+?)"?\s*(;|$)/i'));
             if (empty($filename)) {
                 $filename = $this->getHeader('content-type', '/name="?([^"]+)"?/');
             }
@@ -183,7 +186,7 @@ class BananaMimePart
             $this->makeTextPart($content, $content_type, $encoding, $charset, $format);
             break;
           case 'multipart':
-            $this->makeMultiPart($content, $content_type, $encoding, $boundary);
+            $this->makeMultiPart($content, $content_type, $encoding, $boundary, $sign_protocole);
             break;
           default:
             $this->makeDataPart($content, $content_type, $encoding, $filename, $disposition, $id);
@@ -210,13 +213,24 @@ class BananaMimePart
             $this->multipart = array();
         }
         $boundary =& $this->boundary;
-        $parts = preg_split("/\n--" . preg_quote($boundary, '/') . "(--|\n)/", $this->body, -1, PREG_SPLIT_NO_EMPTY);
+        $parts = preg_split("/(^|\n)--" . preg_quote($boundary, '/') . "(--|\n)/", $this->body, -1, PREG_SPLIT_NO_EMPTY);
+        $signed = $this->isType('multipart', 'signed');
+        $signature = null;
+        $signed_message = null;
         foreach ($parts as &$part) {
             $newpart = new BananaMimePart($part);
             if (!is_null($newpart->content_type)) {
+                if ($signed && $newpart->content_type == $this->sign_protocole) { 
+                    $signature = $newpart->body; 
+                } elseif ($signed) { 
+                    $signed_message = $part; 
+                } 
                 $this->multipart[] = $newpart;
             }
         }
+        if ($signed) {
+            $this->checkSignature($signature, $signed_message);
+        }
         $this->body = null;
     }
 
@@ -600,6 +614,13 @@ class BananaMimePart
         }
         return null;
     }
+
+    private function checkSignature($signature, $message)
+    {
+        file_put_contents('machin.asc', $signature);
+        file_put_contents('message', str_replace(array("\r\n", "\n"), array("\n", "\r\n"), $message));
+        passthru('gpg --verify machin.asc message');
+    }
 }
 
 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: