S/MIME > Encrypting Mail > PHP5 > openssl_pkcs7_encrypt()

View: New views
6 Messages — Rating Filter:   Alert me  

S/MIME > Encrypting Mail > PHP5 > openssl_pkcs7_encrypt()

by Geoff Millikan-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

So I know PHPMAILER can sign email using S/MIME but how cool would it be if
it could encrypt?!  Has anyone started this?  Any hints I can use?

Here's how far I got:

1. You can get a free S/MIME certificate easily at:
http://www.thawte.com/secure-email/personal-email-certificates/

2. Export the certificate (from a Windows XP machine certificates can be
exported easily as my_cert.pb7).

3. Upload the my_cert.pb7 to any Linux machine (or I guess Windows would
work too) and convert it to my_cert.pem using the following OpenSSL command
(tested on RedHat EL5)
openssl pkcs7 -inform DER -in my_cert.p7b -print_certs -text -out
my_cert.pem

Now I can confirm I can encrypt a HTML file in the S/MIME format using
OpenSSL with this command:
openssl smime -encrypt -des3  -in my_email.html -out my_encrypted_email
my_cert.pem

Which outputs a file that starts like this:

MIME-Version: 1.0
Content-Disposition: attachment; filename="smime.p7m"
Content-Type: application/x-pkcs7-mime; smime-type=enveloped-data;
name="smime.p7m"
Content-Transfer-Encoding: base64

MIMDPlgGCSqGSIb3DQEHA6CDAz5IMIMDPkMCAQAxggERMIIBDQIBADB2MGIxCzAJ
BgNVBAYTAlpBMSUwIwYDVQQKExxUaGF3dGUgQ29uc3VsdGluZyAoUHR5KSBMdGQu
MSwwKgYDVQQDEyNUaGF3dGUgUGVyc29uYWwgRnJlZW1haWwgSXNzdWluZyBDQQIQ
JTq4S4cikXYVV3lIoIcDODANBgkqhkiG9w0BAQEFAASBgE/vlEkYTgDc2Nm5Z6JJ
KlS3zCKIZ5ocjWmdjZMIjCbn5SS+Juy+wE0Vnrg6Ab4shItdRttRtFgtBlHI0Qaq
<snip>

I know this can be sent out, I'm just lost on how the headers work.  It
looks like the OpenSSL functionality can be interfaced via the
openssl_pkcs7_encrypt() PHP function so I think this could be a pretty easy
addition to PHPMAILER.  I'm happy to donate USD $100 to the cause.  Would
love to see this functionality added to the PHPMAILER codebase.

Thanks,

http://www.t1shopper.com/



-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Phpmailer-general mailing list
Phpmailer-general@...
https://lists.sourceforge.net/lists/listinfo/phpmailer-general

smime.p7s (4K) Download Attachment

Re: S/MIME > Encrypting Mail > PHP5 >openssl_pkcs7_encrypt()

by Geoff Millikan-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hum, look like it might be easier than I thought to add?!

How about inserting something like the following code starting on line 1036
of class.phpmailer.php?  The only changes are in Line 1036 and adding
openssl_pkcs7_encrypt() in Line 1043.  Not sure if this would work - just
throwing it out there.


    } elseif ($this->encrypt_key_file) { //Line 1036
                   $file = tempnam("", "mail"); //Line 1037
      $fp = fopen($file, "w");
      fwrite($fp, $result);
      fclose($fp);
      $signed = tempnam("", "signed");

      if (@openssl_pkcs7_encrypt($file, $signed,
"file://".$this->encrypt_cert_file, null)) { //Line 1043
        $fp = fopen($signed, "r");
        $result = '';
        while(!feof($fp)){
          $result = $result . fread($fp, 1024);
        }
        fclose($fp);
      } else {
        $this->SetError($this->Lang("signing").openssl_error_string());
        $result = '';
      }

      unlink($file);
      unlink($signed);
    }


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Phpmailer-general mailing list
Phpmailer-general@...
https://lists.sourceforge.net/lists/listinfo/phpmailer-general

smime.p7s (4K) Download Attachment

Can send emails digitally signed with S/MIME encryption!

by Geoff Millikan-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

At least on RedHat5 and CentOS5 you'll need to make the following
modifications to PHPMailer Version: 2.2.1 to get the signing to work:

Starting on line 261 change this:

  private $sign_cert_file  = "";
  private $sign_key_file   = "";
  private $sign_key_pass   = "";

To this:

  public $sign_cert_file  = "";
  public $sign_key_file   = "";
  public $sign_key_pass   = "";

And change line 1022 from this:

if (@openssl_pkcs7_sign($file, $signed, "file://".$this->sign_cert_file,
array("file://".$this->sign_key_file, $this->sign_key_pass), null)) {

To this:

if (@openssl_pkcs7_sign($file, $signed,
file_get_contents($this->sign_cert_file),
array(file_get_contents($this->sign_key_file), $this->sign_key_pass), null))
{

Live example below for your S/MIME'ing pleasure.

Happy S/MIME'ing!

http://www.t1shopper.com/

======================
<?php
include_once 'PHPMailer/class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "mail.mycompany.com";  // specify main and backup server
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = "myusername";  // SMTP username
$mail->Password = "mypassword"; // SMTP password
$body ="Hi there!";
$mail->Body    = wordwrap($body, 70);
$mail->AltBody = $text_body;
$mail->From = "me@...";
$mail->FromName = "All of us at My Comapny";
$mail->AddAddress("myemailaddress@...", "Test");
$mail->AddReplyTo("someoneelse@...", "Someone Else Test");
$mail->Subject = "S/MIME Test";
/*
* You can get a free personal certificate from Thawte
here:http://www.thawte.com/secure-email/personal-email-certificates/
* After installing it, export both the private and public keys from Windows,
"my_key.pfx"
* Upload my_key.pfx to your Linux server.
* Now convert from pfx to pem like this:
* openssl pkcs12 -in my_key.pfx -out my_key.pem
* Now put the pem file somewhere safe on your server and you're set to go.
* Where you put the pem file impacts who can run this script.  
* Using the path in example below would mean only the root user could run
this script.
*/
$mail->sign_cert_file="/etc/pki/tls/private/my_key.pem";
$mail->sign_key_file="/etc/pki/tls/private/my_key.pem";
$mail->sign_key_pass="password_to_my_private_key";
if(!$mail->Send()) {echo "Oopsie, try again.";}
?>


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Phpmailer-general mailing list
Phpmailer-general@...
https://lists.sourceforge.net/lists/listinfo/phpmailer-general

smime.p7s (4K) Download Attachment

PHP Fatal error: Call to undefined method SMTP::getError() in class.phpmailer.5.0.2.php on line 1789

by Geoff Millikan-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Line 1789 seems to call a method "smtp" which doesn't seem to exist in
5.0.2.

1789: $lasterror = $this->smtp->getError();

Reverting back a version may help?

Thanks guys and gals,

http://www.t1shopper.com


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Phpmailer-general mailing list
Phpmailer-general@...
https://lists.sourceforge.net/lists/listinfo/phpmailer-general

smime.p7s (4K) Download Attachment

Re: PHP Fatal error: Call to undefined method SMTP::getError() in class.phpmailer.5.0.2.php on line 1789

by Geoff Millikan-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Nope, reverting to 5.0.0 doesn't help.  Same error, different line.

PHP Fatal error:  Call to undefined method SMTP::getError() in
class.phpmailer.5.0.0.php on line 1784

Backing out to version 2.3 now...


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Phpmailer-general mailing list
Phpmailer-general@...
https://lists.sourceforge.net/lists/listinfo/phpmailer-general

smime.p7s (4K) Download Attachment

Re: PHP Fatal error: Call to undefined method SMTP::getError() in class.phpmailer.5.0.2.php on line 1789

by Geoff Millikan-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ok, my bad.  It works.  I was renaming the files to include the version
number.  I checked, but missed it that class.phpmailer.php (v5.0.2) loads
'class.smtp.php' on line 610.  If you are renaming the files to support
versioning, then you have to change line 610 from this:

require_once $this->PluginDir . 'class.smtp.php';

To this:

require_once $this->PluginDir . 'class.smtp.5.0.2.php';

Sorry about that.

Thanks,

http://www.t1shopper.com


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Phpmailer-general mailing list
Phpmailer-general@...
https://lists.sourceforge.net/lists/listinfo/phpmailer-general

smime.p7s (4K) Download Attachment