reSIProcate/stack  9694
testSMIME.cxx
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include "config.h"
00003 #endif
00004 
00005 #include "resip/stack/MultipartSignedContents.hxx"
00006 #include "resip/stack/Pkcs7Contents.hxx"
00007 #include "resip/stack/PlainContents.hxx"
00008 
00009 #ifdef USE_SSL
00010 #include "resip/stack/ssl/Security.hxx"
00011 #endif
00012 
00013 #include "rutil/Log.hxx"
00014 
00015 #include <fstream>
00016 
00017 using resip::BaseSecurity;
00018 using resip::Contents;
00019 using resip::Data;
00020 using resip::Log;
00021 using resip::MultipartMixedContents;
00022 using resip::MultipartSignedContents;
00023 using resip::Pkcs7Contents;
00024 using resip::PlainContents;
00025 using resip::Security;
00026 using resip::SignatureStatus;
00027 using resip::h_ContentTransferEncoding;
00028 
00029 int main(int argc, char* argv[])
00030 {
00031 #if defined(USE_SSL)
00032    Log::initialize(Log::Cout, Log::Info, argv[0]);
00033 
00034    if(argc != 4)
00035    {
00036       std::cout << "Usage: testSMIME certDirectory sender@example.com receiver@example.com";
00037       return -1;
00038    }
00039 
00040    Security security(argv[1], BaseSecurity::StrongestSuite);
00041    security.preload();
00042 
00043    const Data sender(argv[2]);
00044    const Data receiver(argv[3]);
00045 
00046    // .bwc. Signed contents
00047    {
00048       PlainContents contents("hello");
00049       contents.header(h_ContentTransferEncoding).value()="binary";
00050 
00051       MultipartSignedContents* mps = security.sign(sender, &contents);
00052 
00053       std::ofstream file("signedContents.out");
00054       std::ofstream binFile("binaryBlob1.out");
00055       file << *mps;
00056 
00057       Pkcs7Contents* pkcs = dynamic_cast<Pkcs7Contents*>(mps->parts().back());
00058       binFile << pkcs->getBodyData();
00059 
00060       SignatureStatus status=resip::SignatureNone;
00061       Data sender2(sender);
00062       // .bwc. The return here is just a reference into mps. Don't delete it.
00063       Contents* verified = security.checkSignature(mps, &sender2, &status);
00064       assert(verified);
00065       switch(status)
00066       {
00067          case resip::SignatureTrusted:
00068          case resip::SignatureCATrusted:
00069             break;
00070          case resip::SignatureNotTrusted:
00071          case resip::SignatureSelfSigned:
00072          case resip::SignatureNone:
00073          case resip::SignatureIsBad:
00074          default:
00075             assert(0);
00076       }
00077 
00078       assert(sender2=="sip:"+sender);
00079       assert(verified->getBodyData()=="hello");
00080 
00081       delete mps;
00082    }
00083 
00084    // .bwc. Encrypted contents
00085    {
00086       PlainContents contents("hello");
00087       contents.header(h_ContentTransferEncoding).value()="binary";
00088 
00089       Pkcs7Contents* pkcs = security.encrypt(&contents,receiver);
00090 
00091       std::ofstream file("binaryBlob2.out");
00092       file << *pkcs;
00093 
00094       Contents* decrypted = security.decrypt(receiver, pkcs);
00095       assert(decrypted);
00096 
00097       assert(decrypted->getBodyData()=="hello");
00098 
00099       delete pkcs;
00100       delete decrypted;
00101    }
00102 
00103    // .bwc. Signed and Encrypted contents
00104    {
00105       PlainContents contents("hello");
00106       contents.header(h_ContentTransferEncoding).value()="binary";
00107 
00108       MultipartSignedContents* mps = security.signAndEncrypt(sender, &contents, receiver);
00109 
00110       std::ofstream file("signedAndEncryptedContents.out");
00111       file << *mps;
00112 
00113       std::ofstream blob3("binaryBlob3.out");
00114       blob3 << *mps->parts().front();
00115 
00116       std::ofstream blob4("binaryBlob4.out");
00117       blob4 << *mps->parts().front();
00118 
00119       SignatureStatus status=resip::SignatureNone;
00120       Data sender2(sender);
00121       // .bwc. The return here is just a reference into mps. Don't delete it.
00122       Contents* verified = security.checkSignature(mps, &sender2, &status);
00123       assert(verified);
00124       switch(status)
00125       {
00126          case resip::SignatureTrusted:
00127          case resip::SignatureCATrusted:
00128             break;
00129          case resip::SignatureNotTrusted:
00130          case resip::SignatureSelfSigned:
00131          case resip::SignatureNone:
00132          case resip::SignatureIsBad:
00133          default:
00134             assert(0);
00135       }
00136 
00137       assert(sender2=="sip:"+sender);
00138 
00139       Pkcs7Contents* pkcs=dynamic_cast<Pkcs7Contents*>(verified);
00140 
00141       assert(pkcs);
00142       Contents* decrypted = security.decrypt(receiver, pkcs);
00143       assert(decrypted);
00144       assert(decrypted->getBodyData()=="hello");
00145 
00146       delete mps;
00147       delete decrypted;
00148    }
00149 
00150    return 0;
00151 #else
00152 // No SSL
00153    std::cout << "Compiled without SSL support -- S/MIME Cannot be tested" << std::endl;
00154    return -1;
00155 #endif
00156 }