Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]IMAP odbieranie podpisanych cyfrowo emaili
Forum PHP.pl > Forum > PHP
marcinja2
Witajcie jestem tu nowy więc proszę o wyrozumiałość jeśli coś zrobię bądź wytłumaczę źle. Aktualnie piszę część programu odpowiadającą za pobieranie poczty email (IMAP) sortowanie, filtrowanie i zapisywanie treści email w bazie MYSQL. Wszystko mi się piknie łączy z serwerem IMAP i pobiera wszystkie wiadomości jednak odczytanie niektórych jest dla mnie problematyczne. Chodzi o wiadomości podpisane cyfrowo. Pobierają się one jako jeden załącznik z rozszeżeniem "p7s" np faktura z popularnego operatora komórkowego. Nijak nie mogę tego otworzyć. Może ktoś z Was streści mi pokrótce logikę prawidłowego podejścia do sprawy lub może ma ktoś gotową funkcję do dekodowania takich plików? Próbowałem zaadoptować jakieś gotowe rozwiązania z Github jednak bezskutecznie, nie rozumiem tego mechanizmu do końca i być może dlatego próby jakiejś adaptacji spełzły na niczym. Proszę o pomoc i z góry dziękuję.
Pyton_000
Pokaż kod.

To sprawdzałeś? https://stackoverflow.com/questions/2186228...gned-attachment
marcinja2
Rano przejrzę wątek co zaproponowaleś
  1. define ("CA", "");
  2. define ("TMP_FILE", "/tmp/1");
  3.  
  4. define ("EMAIL", "info@antykwariat-lublin.pl");
  5. define ("PASS", "haslo");
  6.  
  7.  
  8. // following PATHS must be created
  9. // recipients/certs
  10. // users/keys
  11. // users/certs
  12. // mails/crypt
  13. // mails/decrypt
  14.  
  15. /*
  16. * sender and recipient
  17. */
  18. function senRez($mBox, $mId){
  19. $hInfo = imap_headerinfo ($mBox, $mId);
  20. $to = $hInfo->to[0]->mailbox . "@" . $hInfo->to[0]->host;
  21. $from = $hInfo->from[0]->mailbox . "@" . $hInfo->from[0]->host;
  22. return array($from, $to);
  23. }
  24. /*
  25. * shit main test func
  26. */
  27. function getMsgStrct($mBox, $mId){
  28.  
  29. $struc = imap_fetchstructure($mBox, $mId);
  30. $header = imap_fetchheader($mBox, $mId);
  31. $signtxt = "Content-Type: application/pkcs7-signature; name=\"smime.p7s\"\n";
  32. $signtxt .= "Content-Transfer-Encoding: base64\n";
  33. $signtxt .= "Content-Disposition: attachment; filename=\"smime.p7s\"\n";
  34. $signtxt .= "Content-Description: S/MIME Cryptographic Signature\n\n";
  35.  
  36. $senRez = senRez($mBox, $mId);
  37.  
  38. if ($struc->subtype == "SIGNED") {
  39. print " message is singed ";
  40. $hdrsign = "This is a cryptographically signed message in MIME format.\n\n";
  41. $hdrsign .= "--" . $struc->parameters[0]->value. "\n";
  42. $hdrsign .= "Content-Type: multipart/alternative;\n";
  43. $hdrsign .= " " .$struc->parts[0]->parameters[0]->attribute . "=\"" . $struc->parts[0]->parameters[0]->value . "\"\n\n";
  44. $signtxt = "\n--" . $struc->parameters[0]->value . "\n" . $signtxt;
  45. var_dump($signtxt);
  46. $body = imap_fetchbody($mBox, $mId, 1);
  47. $body .= $signtxt;
  48. $body .= imap_fetchbody($mBox, $mId, 2);
  49. $text = $header . $hdrsign . $body . "\n--" . $struc->parameters[0]->value . "--\n";
  50.  
  51. stoM("decrypt", $mId, $text); //zapisuje plik na dysku
  52. verM($mId, $senRez[0]);
  53. }
  54. elseif ( $struc->subtype == "S/MIME Encrypted Message" || $struc->subtype == "PKCS7-MIME" || $struc->subtype == "X-PKCS7-MIME") {
  55. print " message is encrypted ";
  56. $body = imap_fetchbody($mBox, $mId, 1);
  57. $text = $header . "\n" . $body;
  58. stoM("crypt", $mId, $text);
  59. if (decM($mBox, $mId, $senRez[1]) == "signet") {
  60. verM($mId, $senRez[0]);
  61. }
  62. }
  63. }
  64. /*
  65. * store mail
  66. */
  67. function stoM($type, $mId, $text){
  68.  
  69. $mailDir = "mails/";
  70. $infile = $mailDir . $type . "/" . $mId;
  71. $fh = fopen($infile, "w");
  72. //$text = strtr($text, array("\r" => ""));
  73. var_dump($infile);
  74. fwrite ($fh , $text);
  75. // fwrite ($fh, "\n");
  76. // fwrite ($fh , $body);
  77. fclose($fh);
  78. }
  79. /*
  80. * verify email
  81. */
  82.  
  83. // this function knows etrycerts and outfile where the cert will be storted !!!
  84.  
  85. function verM($mId, $from){
  86.  
  87. $reciDir ="recipients/certs/";
  88. $mailDir ="mails/";
  89. $file = $mailDir . "decrypt/" . $mId;
  90. $outfile = $reciDir ."cert" . $from;
  91. if (file_exists($outfile) ) {
  92. $outfile = TMP_FILE;
  93. }
  94. //der2smime($file);
  95. //$test=openssl_pkcs7_verify($file, PKCS7_NOVERIFY | PKCS7_NOSIGS, $from);
  96. //$test=openssl_pkcs7_verify($file, PKCS7_NOVERIFY | PKCS7_NOSIGS, $from, [], $from, $outfile);
  97. //$test = openssl_pkcs7_verify($file, PKCS7_BINARY | PKCS7_NOSIGS | PKCS7_NOVERIFY, $outfile, array(CA));
  98. $test = openssl_pkcs7_verify($file, PKCS7_BINARY | PKCS7_NOSIGS , $outfile, array(CA));
  99. print "signature is " . $test . "\n";
  100. print openssl_error_string();
  101. }
  102.  
  103. /*
  104. * decryt mail
  105. */
  106.  
  107. function decM($mBox, $mId, $to) {
  108. $mailDir = "mails/";
  109. $infile = $mailDir . "crypt/" . $mId;
  110. $outfile = $mailDir . "decrypt/" . $mId;
  111. $keyDir = "users/keys/";
  112. $certDir = "users/certs/";
  113. $cert = file_get_contents($certDir . "cert-" . $to);
  114. $key = file_get_contents($keyDir . "key-" . $to);
  115. openssl_pkcs7_decrypt($infile, $outfile, $cert, $key);
  116. // print openssl_error_string();
  117. if (stripos(file_get_contents($outfile), "Content-Type: multipart/signed; protocol=\"application/pkcs7-signature\";") === 0 ) {
  118. return "signet";
  119. }
  120.  
  121. if (stripos(file_get_contents($outfile), "Content-Type: application/x-pkcs7-mime; name=smime.p7m; smime-type=signed-data") === 0 ) {
  122. return "signet";
  123. }
  124. }
  125.  
  126. /*
  127. * testrun
  128. */
  129. require_once('poczta.php');
  130.  
  131. $P = new Poczta('info@antykwariat-lublin.pl');
  132. $hdr = imap_mailboxmsginfo($P->polaczenie->connection);
  133.  
  134. print "messages: " . $hdr->Nmsgs . "\n";
  135.  
  136. $mId = "233";
  137. while ($mId <= $hdr->Nmsgs){
  138. print "<li>message: " . $mId . "\n";
  139. getMsgStrct($P->polaczenie->connection, $mId);
  140. $mId++;
  141. }
  142.  




W linku co wskazałeś jest inny problem. Ja jakby nie wiem jak się dobrać do załącznika kodowanego, podpisanego cyfrowo. Part 0 u mnie wygląda tak: Play - e-faktura do pobrania','X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on spamassassin.itsaas.pl X-Spam-Level: X-Spam-Status: No, score=-0.6 required=7.0 tests=BAYES_00,HTML_MESSAGE, INVALID_MSGID,MIME_HTML_ONLY,T_DKIM_INVALID autolearn=no version=3.3.2 Received: (qmail 9880 invoked from network); 27 May 2020 11:42:35 -0000 Received: from unknown (HELO mx.dmtec.eu) ([83.143.130.6]) by 192.168.133.41 with SMTP; 27 May 2020 11:42:35 -0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by mx.dmtec.eu (Postfix) with ESMTP id 01ABF1605F1 for ; Wed, 27 May 2020 13:42:34 +0200 (CEST) X-Virus-Scanned: by amavisd-new-2.5.3 (20071212) at dmtec.eu Received: from mx.dmtec.eu ([127.0.0.1]) by localhost (mx.dmtec.eu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id K4UZZjbozU94 for ; Wed, 27 May 2020 13:42:28 +0200 (CEST) X-policyd-weight: using cached result; rate: -7.6 Received: from mojefinanseplay.pl (mx-soe.mojefinanseplay.pl [213.222.202.2]) by mx.dmtec.eu (Postfix) with ESMTPS id E81861605B8 for ; Wed, 27 May 2020 13:42:24 +0200 (CEST) Received: from soe-server (unknown [10.170.5.43]) by mojefinanseplay.pl (Postfix) with ESMTP id 49X87x58Xfz7QW9Q for ; Wed, 27 May 2020 13:42:21 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mojefinanseplay.pl; s=default; t=1590579741; bh=kWy5Im4mVXa3vrkhUto8jB1CZJFt+HB+wb7kV+r2A+I=; h=From:To:Subject:Date; b=JkHbtX/JeAwVaWBw79Am2CpQ5O3voD9Hpy4Tra4i8h/MyCF5Etj+qgVIhUva/4n6r LywzFlrnSyoy9uy9yOyfo88YSGvSjzo6V1fGLWp2g+e3umAZ7yN9h0kBe70KLNYWbK OUUdKMiq+LI/K+yMk1d3Msg0fkEZeHJMGRSTlnRU= From: awizo@mojefinanseplay.pl To: INFO@ANTYKWARIAT-LUBLIN.PL Subject: Play - e-faktura do pobrania Message-Id: X-Mailer: ADSMailer-169999441@eDocument MIME-Version: 1.0 Content-Type: multipart/signed; charset="utf-8"; protocol="application/pkcs7-signature"; micalg=sha-256; boundary=assecods7unizeto1smime2boundary Date: Wed, 27 May 2020 13:42:21 +0200

W żadnym part nie widać treści email ani załącznika PDF który widoczny jest przez np pocztę webową. Nie jestem profesjonalistą, bardzo mi to potrzebne (obsługa emaili, mam fajny własny program pisany latami i ważne by emaile same podpinały się pod zamówienia), a tracę już głowę 2 dni zmarnowane. W Part 0jest: "micalg=sha-256" co to? skąd wziąć klucz? nie powinien być jakiś publiczny bądź przesyłany z wiadomością?
Pyton_000
Zerknąłem u siebie na wiadomośc od play w Gmail, i content wiadomości jest w html zakodowanym do base64

imap_fetchstructure () powinno wywalić ci strukturę mejla i ten content powinien być gdzieś w dalej.

Gdyby wiadomość była zaszyfrowana (np. gpg) to wtedy bez klucza nie jesteś w stanie jej zdekodować, ale wiadomości od play są zwykłymi wiadomościami. (no chyba że Gmmail po drodze robi jakąś magię ale nie sądzę)
Pokaż struct tego mejla (maskując dane oczywiście)
marcinja2
wygląda to mniej wiecej tak:
  1. object(stdClass) (11) {
  2. ["type"]=> int(1)
  3. ["encoding"]=> int(0)
  4. ["ifsubtype"]=> int(1)
  5. ["subtype"]=> string(6) "SIGNED"
  6. ["ifdescription"]=> int(0)
  7. ["ifid"]=> int(0)
  8. ["ifdisposition"]=> int(0)
  9. ["ifdparameters"]=> int(0)
  10. ["ifparameters"]=> int(1)
  11. ["parameters"]=> array(4) {
  12. [0]=> object(stdClass) (2) {
  13. ["attribute"]=> string(7) "charset"
  14. ["value"]=> string(5) "utf-8" }
  15. [1]=> object(stdClass) (2) {
  16. ["attribute"]=> string(8) "protocol"
  17. ["value"]=> string(27) "application/pkcs7-signature" }
  18. [2]=> object(stdClass) (2) {
  19. ["attribute"]=> string(6) "micalg"
  20. ["value"]=> string(7) "sha-256" }
  21. [3]=> object(stdClass) (2) {
  22. ["attribute"]=> string(8) "boundary"
  23. ["value"]=> string(31) "assecods7unizeto1smime2boundary" } }
  24. ["parts"]=> array(2) {
  25. [0]=> object(stdClass) (11) {
  26. ["type"]=> int(1)
  27. ["encoding"]=> int(0)
  28. ["ifsubtype"]=> int(1)
  29. ["subtype"]=> string(5) "MIXED"
  30. ["ifdescription"]=> int(0)
  31. ["ifid"]=> int(0)
  32. ["ifdisposition"]=> int(0)
  33. ["ifdparameters"]=> int(0)
  34. ["ifparameters"]=> int(1)
  35. ["parameters"]=> array(1) {
  36. [0]=> object(stdClass) (2) {
  37. ["attribute"]=> string(8) "boundary"
  38. ["value"]=> string(25) "unizetoboundarymainentity" } }
  39. ["parts"]=> array(2) {
  40. [0]=> object(stdClass) (11) {
  41. ["type"]=> int(1)
  42. ["encoding"]=> int(0)
  43. ["ifsubtype"]=> int(1)
  44. ["subtype"]=> string(7) "RELATED"
  45. ["ifdescription"]=> int(0)
  46. ["ifid"]=> int(0)
  47. ["ifdisposition"]=> int(0)
  48. ["ifdparameters"]=> int(0)
  49. ["ifparameters"]=> int(1)
  50. ["parameters"]=> array(1) {
  51. [0]=> object(stdClass) (2) {
  52. ["attribute"]=> string(8) "boundary"
  53. ["value"]=> string(22) "unizetoboundaryrelated" } }
  54. ["parts"]=> array(10) {
  55. [0]=> object(stdClass) (12) {
  56. ["type"]=> int(0)
  57. ["encoding"]=> int(3)
  58. ["ifsubtype"]=> int(1)
  59. ["subtype"]=> string(4) "HTML"
  60. ["ifdescription"]=> int(0)
  61. ["ifid"]=> int(0)
  62. ["lines"]=> int(375)
  63. ["bytes"]=> int(29270)
  64. ["ifdisposition"]=> int(0)
  65. ["ifdparameters"]=> int(0)
  66. ["ifparameters"]=> int(1)
  67. ["parameters"]=> array(1) {
  68. [0]=> object(stdClass) (2) {
  69. ["attribute"]=> string(7) "charset"
  70. ["value"]=> string(5) "utf-8" } } }
  71. [1]=> object(stdClass) (14) {
  72. ["type"]=> int(5) ["encoding"]=> int(3)
  73. ["ifsubtype"]=> int(1)
  74. ["subtype"]=> string(3) "PNG"
  75. ["ifdescription"]=> int(0)
  76. ["ifid"]=> int(1)
  77. ["id"]=> string(12) ""
  78. ["bytes"]=> int(1202)
  79. ["ifdisposition"]=> int(1)
  80. ["disposition"]=> string(6) "inline"
  81. ["ifdparameters"]=> int(1)
  82. ["dparameters"]=> array(1) {
  83. [0]=> object(stdClass) (2) {
  84. ["attribute"]=> string(8) "filename"
  85. ["value"]=> string(10) "Play24.png" } }
  86. ["ifparameters"]=> int(1)
  87. ["parameters"]=> array(1) {
  88. [0]=> object(stdClass) (2) {
  89. ["attribute"]=> string(4) "name"
  90. ["value"]=> string(10) "Play24.png" } } }
  91. [2]=> object(stdClass)(14) {
  92. ["type"]=> int(5)
  93. ["encoding"]=> int(3)
  94. ["ifsubtype"]=> int(1)
  95. ["subtype"]=> string(4) "JPEG"
  96. ["ifdescription"]=> int(0)
  97. ["ifid"]=> int(1)
  98. ["id"]=> string(16) ""
  99. ["bytes"]=> int(9970)
  100. ["ifdisposition"]=> int(1)
  101. ["disposition"]=> string(6) "inline"
  102. ["ifdparameters"]=> int(1)
  103. ["dparameters"]=> array(1) {
  104. [0]=> object(stdClass) (2) {
  105. ["attribute"]=> string(8) "filename"
  106. ["value"]=> string(14) "Arch032016.jpg" } }
  107. ["ifparameters"]=> int(1) ["parameters"]=> array(1) { [0]=> object(stdClass)...itd
Pyton_000
W jednym z `parts` będzie base64, zdekoduj to i będziesz miał html z wiadomości.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.