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