/[resiprocate]/main/resip/stack/test/testSMIME.cxx
ViewVC logotype

Contents of /main/resip/stack/test/testSMIME.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9493 - (show annotations) (download)
Sat Apr 7 10:56:50 2012 UTC (7 years, 7 months ago) by dpocock
File size: 4154 byte(s)
Include config.h from even more places where it may be needed
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include "resip/stack/MultipartSignedContents.hxx"
6 #include "resip/stack/Pkcs7Contents.hxx"
7 #include "resip/stack/PlainContents.hxx"
8
9 #ifdef USE_SSL
10 #include "resip/stack/ssl/Security.hxx"
11 #endif
12
13 #include "rutil/Log.hxx"
14
15 #include <fstream>
16
17 using resip::BaseSecurity;
18 using resip::Contents;
19 using resip::Data;
20 using resip::Log;
21 using resip::MultipartMixedContents;
22 using resip::MultipartSignedContents;
23 using resip::Pkcs7Contents;
24 using resip::PlainContents;
25 using resip::Security;
26 using resip::SignatureStatus;
27 using resip::h_ContentTransferEncoding;
28
29 int main(int argc, char* argv[])
30 {
31 #if defined(USE_SSL)
32 Log::initialize(Log::Cout, Log::Info, argv[0]);
33
34 if(argc != 4)
35 {
36 std::cout << "Usage: testSMIME certDirectory sender@example.com receiver@example.com";
37 return -1;
38 }
39
40 Security security(argv[1], BaseSecurity::StrongestSuite);
41 security.preload();
42
43 const Data sender(argv[2]);
44 const Data receiver(argv[3]);
45
46 // .bwc. Signed contents
47 {
48 PlainContents contents("hello");
49 contents.header(h_ContentTransferEncoding).value()="binary";
50
51 MultipartSignedContents* mps = security.sign(sender, &contents);
52
53 std::ofstream file("signedContents.out");
54 std::ofstream binFile("binaryBlob1.out");
55 file << *mps;
56
57 Pkcs7Contents* pkcs = dynamic_cast<Pkcs7Contents*>(mps->parts().back());
58 binFile << pkcs->getBodyData();
59
60 SignatureStatus status=resip::SignatureNone;
61 Data sender2(sender);
62 // .bwc. The return here is just a reference into mps. Don't delete it.
63 Contents* verified = security.checkSignature(mps, &sender2, &status);
64 assert(verified);
65 switch(status)
66 {
67 case resip::SignatureTrusted:
68 case resip::SignatureCATrusted:
69 break;
70 case resip::SignatureNotTrusted:
71 case resip::SignatureSelfSigned:
72 case resip::SignatureNone:
73 case resip::SignatureIsBad:
74 default:
75 assert(0);
76 }
77
78 assert(sender2=="sip:"+sender);
79 assert(verified->getBodyData()=="hello");
80
81 delete mps;
82 }
83
84 // .bwc. Encrypted contents
85 {
86 PlainContents contents("hello");
87 contents.header(h_ContentTransferEncoding).value()="binary";
88
89 Pkcs7Contents* pkcs = security.encrypt(&contents,receiver);
90
91 std::ofstream file("binaryBlob2.out");
92 file << *pkcs;
93
94 Contents* decrypted = security.decrypt(receiver, pkcs);
95 assert(decrypted);
96
97 assert(decrypted->getBodyData()=="hello");
98
99 delete pkcs;
100 delete decrypted;
101 }
102
103 // .bwc. Signed and Encrypted contents
104 {
105 PlainContents contents("hello");
106 contents.header(h_ContentTransferEncoding).value()="binary";
107
108 MultipartSignedContents* mps = security.signAndEncrypt(sender, &contents, receiver);
109
110 std::ofstream file("signedAndEncryptedContents.out");
111 file << *mps;
112
113 std::ofstream blob3("binaryBlob3.out");
114 blob3 << *mps->parts().front();
115
116 std::ofstream blob4("binaryBlob4.out");
117 blob4 << *mps->parts().front();
118
119 SignatureStatus status=resip::SignatureNone;
120 Data sender2(sender);
121 // .bwc. The return here is just a reference into mps. Don't delete it.
122 Contents* verified = security.checkSignature(mps, &sender2, &status);
123 assert(verified);
124 switch(status)
125 {
126 case resip::SignatureTrusted:
127 case resip::SignatureCATrusted:
128 break;
129 case resip::SignatureNotTrusted:
130 case resip::SignatureSelfSigned:
131 case resip::SignatureNone:
132 case resip::SignatureIsBad:
133 default:
134 assert(0);
135 }
136
137 assert(sender2=="sip:"+sender);
138
139 Pkcs7Contents* pkcs=dynamic_cast<Pkcs7Contents*>(verified);
140
141 assert(pkcs);
142 Contents* decrypted = security.decrypt(receiver, pkcs);
143 assert(decrypted);
144 assert(decrypted->getBodyData()=="hello");
145
146 delete mps;
147 delete decrypted;
148 }
149
150 return 0;
151 #else
152 // No SSL
153 std::cout << "Compiled without SSL support -- S/MIME Cannot be tested" << std::endl;
154 return -1;
155 #endif
156 }

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27