| 1 |
/** |
| 2 |
* SPDX-FileCopyrightText: (C) 2005 Dominik Seichter <[email protected]> |
| 3 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <podofo/podofo.h> |
| 7 |
|
| 8 |
using namespace std; |
| 9 |
using namespace PoDoFo; |
| 10 |
|
| 11 |
#include <cstdlib> |
| 12 |
#include <cstdio> |
| 13 |
|
| 14 |
void encrypt(const string_view& inputPath, const string_view& outputPath, |
| 15 |
const string_view& userPass, const string_view& ownerPass, |
| 16 |
const PdfEncryptionAlgorithm algorithm, PdfPermissions permissions) |
| 17 |
{ |
| 18 |
PdfMemDocument doc; |
| 19 |
doc.Load(inputPath); |
| 20 |
|
| 21 |
PdfKeyLength keyLength; |
| 22 |
PdfVersion version; |
| 23 |
switch (algorithm) |
| 24 |
{ |
| 25 |
#ifndef PODOFO_HAVE_OPENSSL_NO_RC4 |
| 26 |
case PdfEncryptionAlgorithm::RC4V1: |
| 27 |
keyLength = PdfKeyLength::L40; |
| 28 |
version = PdfVersion::V1_3; |
| 29 |
break; |
| 30 |
#endif // PODOFO_HAVE_OPENSSL_NO_RC4 |
| 31 |
case PdfEncryptionAlgorithm::AESV3R5:; |
| 32 |
keyLength = PdfKeyLength::L256; |
| 33 |
version = PdfVersion::V1_3; |
| 34 |
break; |
| 35 |
#ifndef PODOFO_HAVE_OPENSSL_NO_RC4 |
| 36 |
case PdfEncryptionAlgorithm::RC4V2: |
| 37 |
#endif // PODOFO_HAVE_OPENSSL_NO_RC4 |
| 38 |
case PdfEncryptionAlgorithm::AESV2: |
| 39 |
default: |
| 40 |
keyLength = PdfKeyLength::L128; |
| 41 |
version = PdfVersion::V1_5; |
| 42 |
break; |
| 43 |
} |
| 44 |
|
| 45 |
doc.GetMetadata().SetPdfVersion(version); |
| 46 |
doc.SetEncrypted(userPass, ownerPass, permissions, algorithm, keyLength); |
| 47 |
doc.Save(outputPath); |
| 48 |
} |
| 49 |
|
| 50 |
void print_help() |
| 51 |
{ |
| 52 |
printf("Usage: podofoencrypt [--rc4v1] [--rc4v2] [--aesv2] [--aesv3] [-u <userpassword>]\n"); |
| 53 |
printf(" -o <ownerpassword> <inputfile> <outputfile>\n\n"); |
| 54 |
printf(" This tool encrypts an existing PDF file.\n\n"); |
| 55 |
printf(" --help Display this help text\n"); |
| 56 |
printf(" Algorithm:\n"); |
| 57 |
printf(" --rc4v1 Use rc4v1 encryption\n"); |
| 58 |
printf(" --rc4v2 Use rc4v2 encryption (Default value)\n"); |
| 59 |
printf(" --aesv2 Use aes-128 encryption\n"); |
| 60 |
printf(" --aesv3 Use aes-256 encryption\n"); |
| 61 |
printf(" Passwords:\n"); |
| 62 |
printf(" -u <password> An optional userpassword\n"); |
| 63 |
printf(" -o <password> The required owner password\n"); |
| 64 |
printf(" Permissions:\n"); |
| 65 |
printf(" --print Allow printing the document\n"); |
| 66 |
printf(" --edit Allow modifying the document besides annotations, form fields or changing pages\n"); |
| 67 |
printf(" --copy Allow text and graphic extraction\n"); |
| 68 |
printf(" --editnotes Add or modify text annotations or form fields (if PdfPermissions::Edit is set also allow the creation interactive form fields including signature)\n"); |
| 69 |
printf(" --fillandsign Fill in existing form or signature fields\n"); |
| 70 |
printf(" --accessible Extract text and graphics to support user with disabillities\n"); |
| 71 |
printf(" --assemble Assemble the document: insert, create, rotate delete pages or add bookmarks\n"); |
| 72 |
printf(" --highprint Print a high resolution version of the document\n"); |
| 73 |
printf("\n\n"); |
| 74 |
} |
| 75 |
|
| 76 |
void Main(const cspan<string_view>& args) |
| 77 |
{ |
| 78 |
string_view inputPath; |
| 79 |
string_view outputPath; |
| 80 |
PdfEncryptionAlgorithm algorithm = PdfEncryptionAlgorithm::AESV2; |
| 81 |
PdfPermissions permissions = PdfPermissions::None; |
| 82 |
string userPass; |
| 83 |
string ownerPass; |
| 84 |
|
| 85 |
if (args.size() < 3) |
| 86 |
{ |
| 87 |
print_help(); |
| 88 |
exit(-1); |
| 89 |
} |
| 90 |
|
| 91 |
// Parse the commandline options |
| 92 |
for (unsigned i = 1; i < args.size(); i++) |
| 93 |
{ |
| 94 |
if (args[i][0] == '-') |
| 95 |
{ |
| 96 |
#ifndef PODOFO_HAVE_OPENSSL_NO_RC4 |
| 97 |
if (args[i] == "--rc4v1") |
| 98 |
algorithm = PdfEncryptionAlgorithm::RC4V1; |
| 99 |
else if (args[i] == "--rc4v2") |
| 100 |
algorithm = PdfEncryptionAlgorithm::RC4V2; |
| 101 |
else |
| 102 |
#endif // PODOFO_HAVE_OPENSSL_NO_RC4 |
| 103 |
if (args[i] == "--aesv2") |
| 104 |
algorithm = PdfEncryptionAlgorithm::AESV2; |
| 105 |
else if (args[i] == "--aesv3") |
| 106 |
algorithm = PdfEncryptionAlgorithm::AESV3R5; |
| 107 |
else if (args[i] == "-u") |
| 108 |
{ |
| 109 |
i++; |
| 110 |
if (i < args.size()) |
| 111 |
userPass = args[i]; |
| 112 |
else |
| 113 |
{ |
| 114 |
fprintf(stderr, "ERROR: -u given on the commandline but no userpassword!\n"); |
| 115 |
exit(-1); |
| 116 |
} |
| 117 |
} |
| 118 |
else if (args[i] == "-o") |
| 119 |
{ |
| 120 |
i++; |
| 121 |
if (i < args.size()) |
| 122 |
ownerPass = args[i]; |
| 123 |
else |
| 124 |
{ |
| 125 |
fprintf(stderr, "ERROR: -o given on the commandline but no ownerpassword!\n"); |
| 126 |
exit(-1); |
| 127 |
} |
| 128 |
} |
| 129 |
else if (args[i] == "--help") |
| 130 |
{ |
| 131 |
print_help(); |
| 132 |
exit(-1); |
| 133 |
} |
| 134 |
else if (args[i] == "--print") |
| 135 |
permissions |= PdfPermissions::Print; |
| 136 |
else if (args[i] == "--edit") |
| 137 |
permissions |= PdfPermissions::Edit; |
| 138 |
else if (args[i] == "--copy") |
| 139 |
permissions |= PdfPermissions::Copy; |
| 140 |
else if (args[i] == "--editnotes") |
| 141 |
permissions |= PdfPermissions::EditNotes; |
| 142 |
else if (args[i] == "--fillandsign") |
| 143 |
permissions |= PdfPermissions::FillAndSign; |
| 144 |
else if (args[i] == "--accessible") |
| 145 |
permissions |= PdfPermissions::Accessible; |
| 146 |
else if (args[i] == "--assemble") |
| 147 |
permissions |= PdfPermissions::DocAssembly; |
| 148 |
else if (args[i] == "--highprint") |
| 149 |
permissions |= PdfPermissions::HighPrint; |
| 150 |
else |
| 151 |
{ |
| 152 |
fprintf(stderr, "WARNING: Do not know what to do with argument: %s\n", args[i].data()); |
| 153 |
} |
| 154 |
} |
| 155 |
else |
| 156 |
{ |
| 157 |
if (inputPath.empty()) |
| 158 |
{ |
| 159 |
inputPath = args[i]; |
| 160 |
} |
| 161 |
else if (outputPath.empty()) |
| 162 |
{ |
| 163 |
outputPath = args[i]; |
| 164 |
} |
| 165 |
else |
| 166 |
{ |
| 167 |
fprintf(stderr, "WARNING: Do not know what to do with argument: %s\n", args[i].data()); |
| 168 |
} |
| 169 |
|
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
// Check for errors in the commandline options |
| 174 |
if (inputPath.empty()) |
| 175 |
{ |
| 176 |
fprintf(stderr, "ERROR: No input file specified\n"); |
| 177 |
exit(-1); |
| 178 |
} |
| 179 |
|
| 180 |
if (outputPath.empty()) |
| 181 |
{ |
| 182 |
fprintf(stderr, "ERROR: No output file specified\n"); |
| 183 |
exit(-1); |
| 184 |
} |
| 185 |
|
| 186 |
if (ownerPass.empty()) |
| 187 |
{ |
| 188 |
fprintf(stderr, "ERROR: No owner password specified\n"); |
| 189 |
exit(-1); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
// Do the actual encryption |
| 194 |
encrypt(inputPath, outputPath, userPass, ownerPass, algorithm, permissions); |
| 195 |
|
| 196 |
printf("%s was successfully encrypted to: %s\n", inputPath.data(), outputPath.data()); |
| 197 |
} |