Note: PoDoFo allows AES encryption for memory-based documents only.
1 /* 2 * (c) 2013-2016 http://www.litePDF.cz 3 * (c) 2017 zyx [@:] zyx gmx [dot] us 4 * 5 * This software is provided 'as-is', without any express or implied 6 * warranty. In no event will the authors be held liable for any damages 7 * arising from the use of this software. 8 * 9 * Permission is granted to anyone to use this software for any purpose, 10 * including commercial applications, and to alter it and redistribute it 11 * freely, subject to the following restrictions: 12 * 13 * 1. The origin of this software must not be misrepresented; you must not 14 * claim that you wrote the original software. If you use this software 15 * in a product, an acknowledgment in the product documentation would be 16 * appreciated but is not required. 17 * 2. Altered source versions must be plainly marked as such, and must not be 18 * misrepresented as being the original software. 19 * 3. This notice may not be removed or altered from any source distribution. 20 */ 21 22 #include <windows.h> 23 #include <stdio.h> 24 #include <string.h> 25 #include <string> 26 27 #include "share/litePDF.h" 28 29 static void drawPage(litePDF::TLitePDF &litePDF, 30 const char *msg) 31 { 32 // add a new page to it, with large-enough pixel scale 33 HDC hDC = litePDF.AddPage(litePDF.MMToUnit(210), litePDF.MMToUnit(297), 2100, 2970, LitePDFDrawFlag_SubstituteFonts); 34 35 // draw the text 36 LOGFONTA lf = {0, }; 37 lf.lfHeight = -50; // ~5mm 38 strcpy(lf.lfFaceName, "Helvetica"); 39 40 HFONT fnt; 41 HGDIOBJ oldFnt; 42 43 fnt = CreateFontIndirect(&lf); 44 oldFnt = SelectObject(hDC, fnt); 45 46 SetTextColor(hDC, RGB(128, 0, 0)); 47 TextOut(hDC, 100, 100, msg, strlen(msg)); 48 49 SelectObject(hDC, oldFnt); 50 DeleteObject(fnt); 51 52 // finish drawing 53 litePDF.FinishPage(hDC); 54 } 55 56 static void createEncryptedFiles(void) 57 { 58 using namespace litePDF; 59 TLitePDF litePDF; 60 61 //----------------------------------------------------------------- 62 63 // setup encryption to be used when creating the document 64 litePDF.PrepareEncryption(NULL, "owner", 65 LitePDFEncryptPermission_All, 66 LitePDFEncryptAlgorithm_RC4V1); 67 68 // begin write-only PDF file 69 litePDF.CreateFileDocument("encrypt-rc4v1-no-user-pass.pdf"); 70 71 // fill a page 72 drawPage(litePDF, "Encrypted, without user password, RC4 V1"); 73 74 // close the document 75 litePDF.Close(); 76 77 //----------------------------------------------------------------- 78 79 // setup encryption to be used when creating the document 80 litePDF.PrepareEncryption("user", "owner", 81 LitePDFEncryptPermission_All, 82 LitePDFEncryptAlgorithm_RC4V2); 83 84 // begin memory-based PDF document 85 litePDF.CreateMemDocument(); 86 87 // fill a page 88 drawPage(litePDF, "Encrypted, with user and owner password, RC4 V2"); 89 90 // save to file 91 litePDF.SaveToFile("encrypt-rc4v2.pdf"); 92 93 // close the document 94 litePDF.Close(); 95 96 //----------------------------------------------------------------- 97 98 // setup encryption to be used when creating the document 99 litePDF.PrepareEncryption("user", "owner", 100 LitePDFEncryptPermission_All, 101 LitePDFEncryptAlgorithm_AESV2); 102 103 // begin memory-based PDF document 104 litePDF.CreateMemDocument(); 105 106 // fill a page 107 drawPage(litePDF, "Encrypted, with user and owner password, AES V2"); 108 109 // save to file 110 litePDF.SaveToFile("encrypt-aesv2.pdf"); 111 112 // close the document 113 litePDF.Close(); 114 115 //----------------------------------------------------------------- 116 117 // setup encryption to be used when creating the document 118 litePDF.PrepareEncryption("user", "owner", 119 LitePDFEncryptPermission_All, 120 LitePDFEncryptAlgorithm_AESV3); 121 122 // begin memory-based PDF document 123 litePDF.CreateMemDocument(); 124 125 // fill a page 126 drawPage(litePDF, "Encrypted, with user and owner password, AES V3"); 127 128 // save to file 129 litePDF.SaveToFile("encrypt-aesv3.pdf"); 130 131 // close the document 132 litePDF.Close(); 133 134 //----------------------------------------------------------------- 135 136 // setup encryption to be used when creating the document 137 litePDF.PrepareEncryption("user", "owner", 138 LitePDFEncryptPermission_All, 139 LitePDFEncryptAlgorithm_AESV2); 140 141 try { 142 // begin file-based PDF document 143 litePDF.CreateFileDocument("encrypt-aesv2-file.pdf"); 144 145 throw TLitePDFException(ERROR_CANNOT_MAKE, 146 "Should fail with an exception, because AES encryption works only with memory-based documents currently"); 147 } catch (TLitePDFException &ex) { 148 if (ex.getCode() != ERROR_NOT_SUPPORTED) { 149 throw TLitePDFException(ex); 150 } 151 } 152 153 // close the document 154 litePDF.Close(); 155 156 //----------------------------------------------------------------- 157 158 // setup encryption to be used when creating the document 159 litePDF.PrepareEncryption("user", "owner", 160 LitePDFEncryptPermission_All, 161 LitePDFEncryptAlgorithm_AESV3); 162 163 try { 164 // begin file-based PDF document 165 litePDF.CreateFileDocument("encrypt-aesv3-file.pdf"); 166 167 throw TLitePDFException(ERROR_CANNOT_MAKE, 168 "Should fail with an exception, because AES encryption works only with memory-based documents currently"); 169 } catch (TLitePDFException &ex) { 170 if (ex.getCode() != ERROR_NOT_SUPPORTED) { 171 throw TLitePDFException(ex); 172 } 173 } 174 175 // close the document 176 litePDF.Close(); 177 } 178 179 static void checkDocumentDecrypt(litePDF::TLitePDF &litePDF) 180 { 181 std::wstring value; 182 183 value = litePDF.GetDocumentInfo(LitePDFDocumentInfo_Producer); 184 185 if (value.find_first_of(L"litePDF") == value.npos) { 186 fprintf (stderr, "Text 'litePDF' not found in '%S'\n", value.c_str()); 187 } 188 189 } 190 191 int main(void) 192 { 193 int res = 0; 194 195 using namespace litePDF; 196 197 try { 198 TLitePDF litePDF; 199 200 // create the files 201 createEncryptedFiles(); 202 203 //----------------------------------------------------------------- 204 // now try to open them 205 //----------------------------------------------------------------- 206 207 // no user password, then open it as a user 208 litePDF.LoadFromFile("encrypt-rc4v1-no-user-pass.pdf", NULL, false); 209 210 checkDocumentDecrypt(litePDF); 211 212 // close the document 213 litePDF.Close(); 214 215 //----------------------------------------------------------------- 216 217 try { 218 // this should fail, because no password was provided 219 litePDF.LoadFromFile("encrypt-rc4v2.pdf", NULL, false); 220 221 throw TLitePDFException(ERROR_CANNOT_MAKE, 222 "Should fail to open encrypted file without provided password"); 223 } catch (TLitePDFException &ex) { 224 if (ex.getCode() != ERROR_WRONG_PASSWORD) { 225 throw TLitePDFException(ex); 226 } 227 228 // re-try with user's password 229 litePDF.LoadFromFile("encrypt-rc4v2.pdf", "user", false); 230 231 checkDocumentDecrypt(litePDF); 232 } 233 234 // close the document 235 litePDF.Close(); 236 237 //----------------------------------------------------------------- 238 239 // try to open as owner 240 litePDF.LoadFromFile("encrypt-rc4v1-no-user-pass.pdf", "owner", false); 241 242 checkDocumentDecrypt(litePDF); 243 244 // close the document 245 litePDF.Close(); 246 247 //----------------------------------------------------------------- 248 249 // try to open as user 250 litePDF.LoadFromFile("encrypt-rc4v2.pdf", "user", false); 251 252 checkDocumentDecrypt(litePDF); 253 254 // close the document 255 litePDF.Close(); 256 257 //----------------------------------------------------------------- 258 259 // try to open as owner 260 litePDF.LoadFromFile("encrypt-rc4v2.pdf", "owner", false); 261 262 checkDocumentDecrypt(litePDF); 263 264 // close the document 265 litePDF.Close(); 266 267 //----------------------------------------------------------------- 268 269 // try to open as user 270 litePDF.LoadFromFile("encrypt-aesv2.pdf", "user", false); 271 272 checkDocumentDecrypt(litePDF); 273 274 // close the document 275 litePDF.Close(); 276 277 //----------------------------------------------------------------- 278 279 // try to open as owner 280 litePDF.LoadFromFile("encrypt-aesv2.pdf", "owner", false); 281 282 checkDocumentDecrypt(litePDF); 283 284 // close the document 285 litePDF.Close(); 286 287 //----------------------------------------------------------------- 288 289 // try to open as user 290 litePDF.LoadFromFile("encrypt-aesv3.pdf", "user", false); 291 292 checkDocumentDecrypt(litePDF); 293 294 // close the document 295 litePDF.Close(); 296 297 //----------------------------------------------------------------- 298 299 // try to open as owner 300 litePDF.LoadFromFile("encrypt-aesv3.pdf", "owner", false); 301 302 checkDocumentDecrypt(litePDF); 303 304 // close the document 305 litePDF.Close(); 306 307 //----------------------------------------------------------------- 308 309 } catch (TLitePDFException &ex) { 310 fprintf(stderr, "litePDF Exception: %x: %s\n", ex.getCode(), ex.getMessage()); 311 res = 1; 312 } 313 314 return res; 315 }