| 1 |
/** |
| 2 |
* SPDX-FileCopyrightText: (C) 2016 zyx <[email protected]> |
| 3 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <podofo/private/PdfDeclarationsPrivate.h> |
| 7 |
#include <podofo/podofo.h> |
| 8 |
#include <podofo/optional/PdfConvert.h> |
| 9 |
|
| 10 |
#include <cstdlib> |
| 11 |
#include <cstdio> |
| 12 |
#include <string> |
| 13 |
#include <iostream> |
| 14 |
|
| 15 |
#include <podofo/private/OpenSSLInternal.h> |
| 16 |
|
| 17 |
#if defined(_WIN64) |
| 18 |
#define fseeko _fseeki64 |
| 19 |
#define ftello _ftelli64 |
| 20 |
#else |
| 21 |
#define fseeko fseek |
| 22 |
#define ftello ftell |
| 23 |
#endif |
| 24 |
|
| 25 |
using namespace std; |
| 26 |
using namespace PoDoFo; |
| 27 |
|
| 28 |
static void print_help(bool bOnlyUsage) |
| 29 |
{ |
| 30 |
if (!bOnlyUsage) |
| 31 |
{ |
| 32 |
cout << "Digitally signs existing PDF file with the given certificate and private key." << endl; |
| 33 |
} |
| 34 |
|
| 35 |
cout << endl; |
| 36 |
cout << "Usage: podofosign [arguments]" << endl; |
| 37 |
cout << "The required arguments:" << endl; |
| 38 |
cout << " -in [inputfile] ... an input file to sign; if no -out is set, updates the input file" << endl; |
| 39 |
cout << " -cert [certfile] ... a file with a PEM-encoded certificate to include in the document" << endl; |
| 40 |
cout << " -pkey [pkeyfile] ... a file with a PEM-encoded private key to sign the document with" << endl; |
| 41 |
cout << "The optional arguments:" << endl; |
| 42 |
cout << " -out [outputfile] ... an output file to save the signed document to; cannot be the same as the input file" << endl; |
| 43 |
cout << " -password [password] ... a password to unlock the private key file" << endl; |
| 44 |
cout << " -digest [name] ... a digest name to use for the signature; default is SHA512" << endl; |
| 45 |
cout << " -reason [utf8-string] ... a UTF-8 encoded string with the reason of the signature; default reason is \"I agree\"" << endl; |
| 46 |
cout << " -sigsize [size] ... how many bytes to allocate for the signature; the default is derived from the certificate and private key file size" << endl; |
| 47 |
cout << " -field-name [name] ... field name to use; defaults to 'PoDoFoSignatureFieldXXX', where XXX is the object number" << endl; |
| 48 |
cout << " -field-use-existing ... whether to use existing signature field, if such named exists; the field type should be a signature" << endl; |
| 49 |
cout << " -annot-units [mm|inch] ... set units for the annotation positions; default is mm" << endl; |
| 50 |
cout << " -annot-position [page,left,top,width,height] ... where to place the annotation" << endl; |
| 51 |
cout << " page ... a 1-based page index (integer), where '1' means the first page, '2' the second, and so on" << endl; |
| 52 |
cout << " left,top,width,height ... a rectangle (in annot-units) where to place the annotation on the page (double)" << endl; |
| 53 |
cout << " -annot-print ... use that to have the annotation printable, otherwise it's not printed (the default is not to print it)" << endl; |
| 54 |
cout << " -annot-font [size,rrggbb,name] ... sets a font for the following annot-text; default is \"5,000000,Helvetica\" in mm" << endl; |
| 55 |
cout << " size ... the font size, in annot-units" << endl; |
| 56 |
cout << " rrggbb ... the font color, where rr is for red, gg for green and bb for blue, all two-digit hexa values between 00 and ff" << endl; |
| 57 |
cout << " name ... the font name to use; if a Base14 font is recognized, then it is used, instead of embedding a new font" << endl; |
| 58 |
cout << " -annot-text [left,top,utf8-string] ... a UTF-8 encoded string to add to the annotation" << endl; |
| 59 |
cout << " left,top ... the position (in annot-units, relative to annot-position) where to place the text (double)" << endl; |
| 60 |
cout << " text ... the actual UTF-8 encoded string to add to the annotation" << endl; |
| 61 |
cout << " -annot-image [left,top,width,height,filename] ... an image to add to the annotation" << endl; |
| 62 |
cout << " left,top,width,height ... a rectangle (in annot-units) where to place the image (double), relative to annot-position" << endl; |
| 63 |
cout << " filename ... a filename of the image to add" << endl; |
| 64 |
cout << "The annotation arguments can be repeated, except of the -annot-position and -annot-print, which can appear up to once." << endl; |
| 65 |
cout << "The -annot-print, -annot-font, -annot-text and -annot-image can appear only after -annot-position." << endl; |
| 66 |
cout << "All the left,top positions are treated with 0,0 being at the left-top of the page." << endl; |
| 67 |
cout << "No drawing is done when using existing field." << endl; |
| 68 |
} |
| 69 |
|
| 70 |
static float convert_to_pdf_units(const string_view& annot_units, float value) |
| 71 |
{ |
| 72 |
if (annot_units == "mm") |
| 73 |
{ |
| 74 |
return 72.f * value / 25.4f; |
| 75 |
} |
| 76 |
else if (annot_units == "inch") |
| 77 |
{ |
| 78 |
return 72.f * value; |
| 79 |
} |
| 80 |
else |
| 81 |
{ |
| 82 |
string err = "Unknown annotation unit '"; |
| 83 |
err += annot_units; |
| 84 |
err += "'"; |
| 85 |
|
| 86 |
PODOFO_RAISE_ERROR_INFO(PdfErrorCode::InvalidEnumValue, err); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
static bool parse_annot_position(const string_view& annot_position, |
| 91 |
const string_view& annot_units, |
| 92 |
int& annot_page, |
| 93 |
double& annot_left, |
| 94 |
double& annot_top, |
| 95 |
double& annot_width, |
| 96 |
double& annot_height) |
| 97 |
{ |
| 98 |
float fLeft, fTop, fWidth, fHeight; |
| 99 |
|
| 100 |
if (sscanf(annot_position.data(), "%d,%f,%f,%f,%f", &annot_page, &fLeft, &fTop, &fWidth, &fHeight) != 5) |
| 101 |
{ |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
annot_left = convert_to_pdf_units(annot_units, fLeft); |
| 106 |
annot_top = convert_to_pdf_units(annot_units, fTop); |
| 107 |
annot_width = convert_to_pdf_units(annot_units, fWidth); |
| 108 |
annot_height = convert_to_pdf_units(annot_units, fHeight); |
| 109 |
|
| 110 |
if (annot_page < 1) |
| 111 |
return false; |
| 112 |
|
| 113 |
annot_page--; |
| 114 |
|
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
static const char* skip_commas(const string_view& text, int ncommas) |
| 119 |
{ |
| 120 |
if (text.empty()) |
| 121 |
{ |
| 122 |
PODOFO_RAISE_ERROR(PdfErrorCode::InvalidHandle); |
| 123 |
} |
| 124 |
|
| 125 |
const char* res = text.data(); |
| 126 |
|
| 127 |
while (*res != '\0' && ncommas > 0) |
| 128 |
{ |
| 129 |
if (*res == ',') |
| 130 |
ncommas--; |
| 131 |
|
| 132 |
res++; |
| 133 |
} |
| 134 |
|
| 135 |
if (ncommas > 0) |
| 136 |
{ |
| 137 |
string err = "The text '"; |
| 138 |
err += text; |
| 139 |
err += "' does not conform to the specified format (no enougt commas)"; |
| 140 |
PODOFO_RAISE_ERROR_INFO(PdfErrorCode::InvalidDataType, err.c_str()); |
| 141 |
} |
| 142 |
|
| 143 |
return res; |
| 144 |
} |
| 145 |
|
| 146 |
static void draw_annotation(PdfDocument& document, |
| 147 |
PdfPainter& painter, |
| 148 |
const cspan<string_view>& args, |
| 149 |
const Rect& annot_rect) |
| 150 |
{ |
| 151 |
string_view annot_units = "mm"; |
| 152 |
double font_size = convert_to_pdf_units("mm", 5.0); |
| 153 |
PdfColor font_color(0.0, 0.0, 0.0); |
| 154 |
const char* font_name = "Helvetica"; |
| 155 |
bool updateFont = true; |
| 156 |
for (unsigned i = 1; i < args.size(); i++) |
| 157 |
{ |
| 158 |
if (args[i] == "-annot-units") |
| 159 |
{ |
| 160 |
annot_units = args[i + 1]; |
| 161 |
} |
| 162 |
else if (args[i] == "-annot-font") |
| 163 |
{ |
| 164 |
float fSize; |
| 165 |
int rr, gg, bb; |
| 166 |
|
| 167 |
if (sscanf(args[i + 1].data(), "%f,%02x%02x%02x,", &fSize, &rr, &gg, &bb) != 4) |
| 168 |
{ |
| 169 |
string err = "The value for -annot-font '"; |
| 170 |
err += args[i + 1]; |
| 171 |
err += "' doesn't conform to format 'size,rrggbb,name'"; |
| 172 |
|
| 173 |
PODOFO_RAISE_ERROR_INFO(PdfErrorCode::InvalidDataType, err.c_str()); |
| 174 |
} |
| 175 |
|
| 176 |
font_size = convert_to_pdf_units(annot_units, fSize); |
| 177 |
font_color = PdfColor(static_cast<double>(rr) / 255.0, static_cast<double>(gg) / 255.0, static_cast<double>(bb) / 255.0); |
| 178 |
font_name = skip_commas(args[i + 1], 2); |
| 179 |
updateFont = true; |
| 180 |
} |
| 181 |
else if (args[i] == "-annot-text") |
| 182 |
{ |
| 183 |
float left, top; |
| 184 |
|
| 185 |
if (sscanf(args[i + 1].data(), "%f,%f,", &left, &top) != 2) |
| 186 |
{ |
| 187 |
string err = "The value for -annot-text '"; |
| 188 |
err += args[i + 1]; |
| 189 |
err += "' doesn't conform to format 'left,top,text'"; |
| 190 |
|
| 191 |
PODOFO_RAISE_ERROR_INFO(PdfErrorCode::InvalidDataType, err.c_str()); |
| 192 |
} |
| 193 |
|
| 194 |
const char* text = skip_commas(args[i + 1], 2); |
| 195 |
|
| 196 |
if (updateFont) |
| 197 |
{ |
| 198 |
PdfFont* font; |
| 199 |
|
| 200 |
font = document.GetFonts().SearchFont(font_name); |
| 201 |
if (!font) |
| 202 |
{ |
| 203 |
string err = "Failed to create font '"; |
| 204 |
err += font_name; |
| 205 |
err += "'"; |
| 206 |
|
| 207 |
PODOFO_RAISE_ERROR_INFO(PdfErrorCode::OutOfMemory, err.c_str()); |
| 208 |
} |
| 209 |
|
| 210 |
painter.TextState.SetFont(*font, font_size); |
| 211 |
painter.GraphicsState.SetStrokingColor(font_color); |
| 212 |
} |
| 213 |
|
| 214 |
left = convert_to_pdf_units(annot_units, left); |
| 215 |
top = convert_to_pdf_units(annot_units, top); |
| 216 |
|
| 217 |
painter.DrawTextMultiLine(text, left, |
| 218 |
0.0, |
| 219 |
annot_rect.Width - left, |
| 220 |
annot_rect.Height - top); |
| 221 |
} |
| 222 |
else if (args[i] == "-annot-image") |
| 223 |
{ |
| 224 |
float left, top, width, height; |
| 225 |
|
| 226 |
if (sscanf(args[i + 1].data(), "%f,%f,%f,%f,", &left, &top, &width, &height) != 4) |
| 227 |
{ |
| 228 |
string err = "The value for -annot-image '"; |
| 229 |
err += args[i + 1]; |
| 230 |
err += "' doesn't conform to format 'left,top,width,height,filename'"; |
| 231 |
|
| 232 |
PODOFO_RAISE_ERROR_INFO(PdfErrorCode::InvalidDataType, err.c_str()); |
| 233 |
} |
| 234 |
|
| 235 |
const char* filename = skip_commas(args[i + 1], 4); |
| 236 |
|
| 237 |
left = convert_to_pdf_units(annot_units, left); |
| 238 |
top = convert_to_pdf_units(annot_units, top); |
| 239 |
width = convert_to_pdf_units(annot_units, width); |
| 240 |
height = convert_to_pdf_units(annot_units, height); |
| 241 |
|
| 242 |
auto image = document.CreateImage(); |
| 243 |
image->Load(filename); |
| 244 |
|
| 245 |
double dScaleX = left / image->GetWidth(); |
| 246 |
double dScaleY = height / image->GetHeight(); |
| 247 |
|
| 248 |
painter.DrawImage(*image, left, annot_rect.Height - top - height, dScaleX, dScaleY); |
| 249 |
} |
| 250 |
|
| 251 |
// these are the only parameters without additional value |
| 252 |
if (args[i] != "-annot-print" && |
| 253 |
args[i] != "-field-use-existing") |
| 254 |
{ |
| 255 |
i++; |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
static PdfObject* find_existing_signature_field(PdfAcroForm& acroForm, const PdfString& name) |
| 261 |
{ |
| 262 |
PdfObject* fields = acroForm.GetDictionary().GetKey("Fields"); |
| 263 |
if (fields) |
| 264 |
{ |
| 265 |
if (fields->GetDataType() == PdfDataType::Reference) |
| 266 |
fields = acroForm.GetDocument().GetObjects().GetObject(fields->GetReference()); |
| 267 |
|
| 268 |
if (fields && fields->GetDataType() == PdfDataType::Array) |
| 269 |
{ |
| 270 |
PdfArray& rArray = fields->GetArray(); |
| 271 |
PdfArray::iterator it, end = rArray.end(); |
| 272 |
for (it = rArray.begin(); it != end; it++) |
| 273 |
{ |
| 274 |
// require references in the Fields array |
| 275 |
if (it->GetDataType() == PdfDataType::Reference) |
| 276 |
{ |
| 277 |
PdfObject* item = acroForm.GetDocument().GetObjects().GetObject(it->GetReference()); |
| 278 |
|
| 279 |
if (item && item->GetDictionary().HasKey("T") && |
| 280 |
item->GetDictionary().GetKey("T")->GetString() == name) |
| 281 |
{ |
| 282 |
// found a field with the same name |
| 283 |
const PdfObject* ft = item->GetDictionary().GetKey("FT"); |
| 284 |
if (!ft && item->GetDictionary().HasKey("Parent")) |
| 285 |
{ |
| 286 |
const PdfObject* temp = item->GetDictionary().FindKey("Parent"); |
| 287 |
if (!temp) |
| 288 |
{ |
| 289 |
PODOFO_RAISE_ERROR(PdfErrorCode::InvalidDataType); |
| 290 |
} |
| 291 |
|
| 292 |
ft = temp->GetDictionary().GetKey("FT"); |
| 293 |
} |
| 294 |
|
| 295 |
if (!ft) |
| 296 |
{ |
| 297 |
PODOFO_RAISE_ERROR(PdfErrorCode::InvalidObject); |
| 298 |
} |
| 299 |
|
| 300 |
const PdfName fieldType = ft->GetName(); |
| 301 |
if (fieldType != "Sig") |
| 302 |
{ |
| 303 |
string err = "Existing field '"; |
| 304 |
err += name.GetString(); |
| 305 |
err += "' isn't of a signature type, but '"; |
| 306 |
err += fieldType.GetString(); |
| 307 |
err += "' instead"; |
| 308 |
|
| 309 |
PODOFO_RAISE_ERROR_INFO(PdfErrorCode::InvalidName, err.c_str()); |
| 310 |
} |
| 311 |
|
| 312 |
return item; |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
return NULL; |
| 320 |
} |
| 321 |
|
| 322 |
#if 0 /* TODO */ |
| 323 |
static void update_default_appearance_streams(PdfAcroForm* pAcroForm) |
| 324 |
{ |
| 325 |
if (!pAcroForm || |
| 326 |
!pAcroForm->GetObject()->GetDictionary().HasKey("Fields") || |
| 327 |
pAcroForm->GetObject()->GetDictionary().GetKey("Fields")->GetDataType() != PdfDataType::Array) |
| 328 |
return; |
| 329 |
|
| 330 |
PdfArray& rFields = pAcroForm->GetObject()->GetDictionary().GetKey("Fields")->GetArray(); |
| 331 |
|
| 332 |
PdfArray::iterator it, end = rFields.end(); |
| 333 |
for (it = rFields.begin(); it != end; it++) |
| 334 |
{ |
| 335 |
if (it->GetDataType() == PdfDataType::Reference) |
| 336 |
{ |
| 337 |
PdfObject* pObject = pAcroForm->GetDocument()->GetObjects()->GetObject(it->GetReference()); |
| 338 |
if (!pObject || pObject->GetDataType() != PdfDataType::Dictionary) |
| 339 |
continue; |
| 340 |
|
| 341 |
PdfDictionary& rFielDict = pObject->GetDictionary(); |
| 342 |
if (rFielDict.HasKey("FT") && |
| 343 |
rFielDict.GetKey("FT")->GetDataType() == PdfDataType::Name && |
| 344 |
(rFielDict.GetKey("FT")->GetName() == "Tx" || rFielDict.GetKey("FT")->GetName() == "Ch")) |
| 345 |
{ |
| 346 |
PdfString rDA, rV, rDV; |
| 347 |
|
| 348 |
if (rFielDict.HasKey("V") && |
| 349 |
(rFielDict.GetKey("V")->GetDataType() == PdfDataType::String || rFielDict.GetKey("V")->GetDataType() == PdfDataType::HexString)) |
| 350 |
{ |
| 351 |
rV = rFielDict.GetKey("V")->GetString(); |
| 352 |
} |
| 353 |
|
| 354 |
if (rFielDict.HasKey("DV") && |
| 355 |
(rFielDict.GetKey("DV")->GetDataType() == PdfDataType::String || rFielDict.GetKey("DV")->GetDataType() == PdfDataType::HexString)) |
| 356 |
{ |
| 357 |
rDV = rFielDict.GetKey("DV")->GetString(); |
| 358 |
} |
| 359 |
|
| 360 |
if (rV.IsValid() && rV.GetCharacterLength() > 0) |
| 361 |
{ |
| 362 |
rDV = rV; |
| 363 |
} |
| 364 |
|
| 365 |
if (!rDV.IsValid() || rDV.GetCharacterLength() <= 0) |
| 366 |
continue; |
| 367 |
|
| 368 |
if (rDV.GetLength() >= 2 && rDV.GetString()[0] == static_cast<char>(0xFE) && rDV.GetString()[1] == static_cast<char>(0xFF)) |
| 369 |
{ |
| 370 |
if (rDV.GetLength() == 2) |
| 371 |
continue; |
| 372 |
} |
| 373 |
|
| 374 |
if (rFielDict.HasKey("DA") && |
| 375 |
rFielDict.GetKey("DA")->GetDataType() == PdfDataType::String) |
| 376 |
{ |
| 377 |
rDA = rFielDict.GetKey("DA")->GetString(); |
| 378 |
} |
| 379 |
|
| 380 |
if (rFielDict.HasKey("AP") && |
| 381 |
rFielDict.GetKey("AP")->GetDataType() == PdfDataType::Dictionary && |
| 382 |
rFielDict.GetKey("AP")->GetDictionary().HasKey("N") && |
| 383 |
rFielDict.GetKey("AP")->GetDictionary().GetKey("N")->GetDataType() == PdfDataType::Reference) |
| 384 |
{ |
| 385 |
pObject = pAcroForm->GetDocument()->GetObjects()->GetObject(rFielDict.GetKey("AP")->GetDictionary().GetKey("N")->GetReference()); |
| 386 |
if (pObject->GetDataType() == PdfDataType::Dictionary && |
| 387 |
pObject->GetDictionary().HasKey("Type") && |
| 388 |
pObject->GetDictionary().GetKey("Type")->GetDataType() == PdfDataType::Name && |
| 389 |
pObject->GetDictionary().GetKey("Type")->GetName() == "XObject") |
| 390 |
{ |
| 391 |
PdfXObject xObject(pObject); |
| 392 |
PdfStream* pCanvas = xObject.GetContentsForAppending()->GetStream(); |
| 393 |
|
| 394 |
if (rFielDict.GetKey("FT")->GetName() == "Tx") |
| 395 |
{ |
| 396 |
pCanvas->BeginAppend(true); |
| 397 |
|
| 398 |
PdfRefCountedBuffer rBuffer; |
| 399 |
PdfOutputDevice rOutputDevice(&rBuffer); |
| 400 |
|
| 401 |
rDV.Write(&rOutputDevice, ePdfWriteMode_Compact); |
| 402 |
|
| 403 |
ostringstream oss; |
| 404 |
|
| 405 |
oss << "/Tx BMC" << endl; |
| 406 |
oss << "BT" << endl; |
| 407 |
if (rDA.IsValid()) |
| 408 |
oss << rDA.GetString() << endl; |
| 409 |
oss << "2.0 2.0 Td" << endl; |
| 410 |
oss << rBuffer.GetBuffer() << " Tj" << endl; |
| 411 |
oss << "ET" << endl; |
| 412 |
oss << "EMC" << endl; |
| 413 |
|
| 414 |
pCanvas->Append(oss.str()); |
| 415 |
|
| 416 |
pCanvas->EndAppend(); |
| 417 |
} |
| 418 |
else if (rFielDict.GetKey("FT")->GetName() == "Ch") |
| 419 |
{ |
| 420 |
} |
| 421 |
} |
| 422 |
} |
| 423 |
} |
| 424 |
} |
| 425 |
} |
| 426 |
} |
| 427 |
#endif |
| 428 |
|
| 429 |
void Main(const cspan<string_view>& args) |
| 430 |
{ |
| 431 |
string_view inputfile; |
| 432 |
string_view outputfile; |
| 433 |
string_view certfile; |
| 434 |
string_view pkeyfile; |
| 435 |
string_view password; |
| 436 |
string_view digest; |
| 437 |
string_view reason = "I agree"; |
| 438 |
string_view sigsizestr; |
| 439 |
string_view annot_units = "mm"; |
| 440 |
string_view annot_position; |
| 441 |
string_view field_name; |
| 442 |
int annot_page = 0; |
| 443 |
double annot_left = 0.0, annot_top = 0.0, annot_width = 0.0, annot_height = 0.0; |
| 444 |
bool annot_print = false; |
| 445 |
bool field_use_existing = false; |
| 446 |
|
| 447 |
PdfCommon::SetMaxLoggingSeverity(PdfLogSeverity::None); |
| 448 |
|
| 449 |
for (unsigned i = 1; i < args.size(); i++) |
| 450 |
{ |
| 451 |
string_view* value = nullptr; |
| 452 |
|
| 453 |
if (args[i] == "-in") |
| 454 |
{ |
| 455 |
value = &inputfile; |
| 456 |
} |
| 457 |
else if (args[i] == "-out") |
| 458 |
{ |
| 459 |
value = &outputfile; |
| 460 |
} |
| 461 |
else if (args[i] == "-cert") |
| 462 |
{ |
| 463 |
value = &certfile; |
| 464 |
} |
| 465 |
else if (args[i] == "-pkey") |
| 466 |
{ |
| 467 |
value = &pkeyfile; |
| 468 |
} |
| 469 |
else if (args[i] == "-digest") |
| 470 |
{ |
| 471 |
value = &digest; |
| 472 |
} |
| 473 |
else if (args[i] == "-password") |
| 474 |
{ |
| 475 |
value = &password; |
| 476 |
} |
| 477 |
else if (args[i] == "-reason") |
| 478 |
{ |
| 479 |
value = &reason; |
| 480 |
} |
| 481 |
else if (args[i] == "-sigsize") |
| 482 |
{ |
| 483 |
value = &sigsizestr; |
| 484 |
} |
| 485 |
else if (args[i] == "-annot-units") |
| 486 |
{ |
| 487 |
value = &annot_units; |
| 488 |
} |
| 489 |
else if (args[i] == "-annot-position") |
| 490 |
{ |
| 491 |
if (!annot_position.empty()) |
| 492 |
{ |
| 493 |
cerr << "Only one -annot-position can be specified" << endl; |
| 494 |
exit(-1); |
| 495 |
} |
| 496 |
|
| 497 |
value = &annot_position; |
| 498 |
} |
| 499 |
else if (args[i] == "-annot-print") |
| 500 |
{ |
| 501 |
if (annot_position.empty()) |
| 502 |
{ |
| 503 |
cerr << "Missing -annot-position argument, which should be defined before '" << args[i] << "'" << endl; |
| 504 |
exit(-2); |
| 505 |
} |
| 506 |
|
| 507 |
if (annot_print) |
| 508 |
{ |
| 509 |
cerr << "Only one -annot-print can be specified" << endl; |
| 510 |
exit(-1); |
| 511 |
} |
| 512 |
|
| 513 |
annot_print = !annot_print; |
| 514 |
continue; |
| 515 |
} |
| 516 |
else if (args[i] == "-annot-font" || |
| 517 |
args[i] == "-annot-text" || |
| 518 |
args[i] == "-annot-image") |
| 519 |
{ |
| 520 |
if (annot_position.empty()) |
| 521 |
{ |
| 522 |
cerr << "Missing -annot-position argument, which should be defined before '" << args[i] << "'" << endl; |
| 523 |
exit(-2); |
| 524 |
} |
| 525 |
// value is left NULL, these are parsed later |
| 526 |
} |
| 527 |
else if (args[i] == "-field-name") |
| 528 |
{ |
| 529 |
value = &field_name; |
| 530 |
} |
| 531 |
else if (args[i] == "-field-use-existing") |
| 532 |
{ |
| 533 |
if (field_use_existing) |
| 534 |
{ |
| 535 |
cerr << "Only one -field-use-existing can be specified" << endl; |
| 536 |
exit(-1); |
| 537 |
} |
| 538 |
|
| 539 |
field_use_existing = !field_use_existing; |
| 540 |
continue; |
| 541 |
} |
| 542 |
else |
| 543 |
{ |
| 544 |
cerr << "Unknown argument '" << args[i] << "'" << endl; |
| 545 |
print_help(true); |
| 546 |
exit(-3); |
| 547 |
} |
| 548 |
|
| 549 |
if (i + 1 >= args.size()) |
| 550 |
{ |
| 551 |
cerr << "Missing value for argument '" << args[i] << "'" << endl; |
| 552 |
print_help(true); |
| 553 |
exit(-4); |
| 554 |
} |
| 555 |
|
| 556 |
if (value) |
| 557 |
{ |
| 558 |
*value = args[i + 1]; |
| 559 |
|
| 560 |
if (*value == annot_units && annot_units != "mm" && annot_units != "inch") |
| 561 |
{ |
| 562 |
cerr << "Invalid -annot-units value '" << *value << "', only 'mm' and 'inch' are supported" << endl; |
| 563 |
exit(-5); |
| 564 |
} |
| 565 |
|
| 566 |
try |
| 567 |
{ |
| 568 |
if (*value == annot_position && !parse_annot_position(annot_position, annot_units, annot_page, annot_left, annot_top, annot_width, annot_height)) |
| 569 |
{ |
| 570 |
cerr << "Invalid -annot-position value '" << *value << "', expected format \"page,left,top,width,height\"" << endl; |
| 571 |
exit(-6); |
| 572 |
} |
| 573 |
} |
| 574 |
catch (...) |
| 575 |
{ |
| 576 |
cerr << "Invalid -annot-position value '" << *value << "', expected format \"page,left,top,width,height\"" << endl; |
| 577 |
exit(-6); |
| 578 |
} |
| 579 |
} |
| 580 |
i++; |
| 581 |
} |
| 582 |
|
| 583 |
if (inputfile.empty() || certfile.empty() || pkeyfile.empty()) |
| 584 |
{ |
| 585 |
if (args.size() != 1) |
| 586 |
cerr << "Not all required arguments specified." << endl; |
| 587 |
|
| 588 |
print_help(true); |
| 589 |
|
| 590 |
exit(-7); |
| 591 |
} |
| 592 |
|
| 593 |
int sigsize = -1; |
| 594 |
|
| 595 |
if (!sigsizestr.empty()) |
| 596 |
{ |
| 597 |
sigsize = atoi(sigsizestr.data()); |
| 598 |
|
| 599 |
if (sigsize <= 0) |
| 600 |
{ |
| 601 |
cerr << "Invalid value for signature size specified (" << sigsizestr << "), use a positive integer, please" << endl; |
| 602 |
exit(-8); |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
if (!outputfile.empty() && outputfile == inputfile) |
| 607 |
{ |
| 608 |
// even I told you not to do it, you still specify the same output file |
| 609 |
// as the input file. Just ignore that. |
| 610 |
outputfile = { }; |
| 611 |
} |
| 612 |
|
| 613 |
OPENSSL_init_crypto(0, NULL); |
| 614 |
|
| 615 |
PdfSignature* signature = NULL; |
| 616 |
|
| 617 |
const EVP_MD* md_digest; |
| 618 |
|
| 619 |
if (!digest.empty()) |
| 620 |
{ |
| 621 |
md_digest = EVP_get_digestbyname(digest.data()); |
| 622 |
if (!md_digest) |
| 623 |
{ |
| 624 |
string err = "Unknown digest '"; |
| 625 |
err += digest; |
| 626 |
err += "'"; |
| 627 |
PODOFO_RAISE_ERROR_INFO(PdfErrorCode::InvalidName, err.c_str()); |
| 628 |
} |
| 629 |
} |
| 630 |
else |
| 631 |
{ |
| 632 |
md_digest = EVP_sha512(); |
| 633 |
if (!md_digest) |
| 634 |
cerr << "Cannot get SHA512 digest, using default OpenSSL digest instead." << endl; |
| 635 |
} |
| 636 |
|
| 637 |
PdfMemDocument document; |
| 638 |
|
| 639 |
document.Load(inputfile); |
| 640 |
|
| 641 |
if (!document.GetPages().GetCount()) |
| 642 |
PODOFO_RAISE_ERROR_INFO(PdfErrorCode::ValueOutOfRange, "The document has no page. Only documents with at least one page can be signed"); |
| 643 |
|
| 644 |
auto& acroForm = document.GetOrCreateAcroForm(); |
| 645 |
if (!acroForm.GetDictionary().HasKey("SigFlags") || |
| 646 |
!acroForm.GetDictionary().MustGetKey("SigFlags").IsNumber() || |
| 647 |
acroForm.GetDictionary().FindKeyAsSafe<int64_t>("SigFlags") != 3) |
| 648 |
{ |
| 649 |
if (acroForm.GetDictionary().HasKey("SigFlags")) |
| 650 |
acroForm.GetDictionary().RemoveKey("SigFlags"); |
| 651 |
|
| 652 |
int64_t val = 3; |
| 653 |
acroForm.GetDictionary().AddKey("SigFlags", val); |
| 654 |
} |
| 655 |
|
| 656 |
if (acroForm.GetNeedAppearances()) |
| 657 |
{ |
| 658 |
#if 0 /* TODO */ |
| 659 |
update_default_appearance_streams(pAcroForm); |
| 660 |
#endif |
| 661 |
|
| 662 |
acroForm.SetNeedAppearances(false); |
| 663 |
} |
| 664 |
|
| 665 |
PdfString name; |
| 666 |
PdfObject* existingSigField = NULL; |
| 667 |
|
| 668 |
if (!field_name.empty()) |
| 669 |
{ |
| 670 |
name = PdfString(field_name); |
| 671 |
|
| 672 |
existingSigField = find_existing_signature_field(acroForm, name); |
| 673 |
if (existingSigField && !field_use_existing) |
| 674 |
{ |
| 675 |
string err = "Signature field named '"; |
| 676 |
err += name.GetString(); |
| 677 |
err += "' already exists"; |
| 678 |
|
| 679 |
PODOFO_RAISE_ERROR_INFO(PdfErrorCode::WrongDestinationType, err.c_str()); |
| 680 |
} |
| 681 |
} |
| 682 |
else |
| 683 |
{ |
| 684 |
char fldName[96]; // use bigger buffer to make sure sprintf does not overflow |
| 685 |
sprintf(fldName, "PodofoSignatureField%u", document.GetObjects().GetObjectCount()); |
| 686 |
|
| 687 |
name = PdfString(fldName); |
| 688 |
} |
| 689 |
|
| 690 |
if (existingSigField) |
| 691 |
{ |
| 692 |
if (!existingSigField->GetDictionary().HasKey("P")) |
| 693 |
{ |
| 694 |
string err = "Signature field named '"; |
| 695 |
err += name.GetString(); |
| 696 |
err += "' doesn't have a page reference"; |
| 697 |
|
| 698 |
PODOFO_RAISE_ERROR_INFO(PdfErrorCode::ValueOutOfRange, err.c_str()); |
| 699 |
} |
| 700 |
|
| 701 |
auto& page = document.GetPages().GetPage(existingSigField->GetDictionary().GetKey("P")->GetReference()); |
| 702 |
signature = &static_cast<PdfSignature&>( |
| 703 |
static_cast<PdfAnnotationWidget&>(page.GetAnnotations().GetAnnot(existingSigField->GetIndirectReference())).GetField()); |
| 704 |
signature->EnsureValueObject(); |
| 705 |
} |
| 706 |
else |
| 707 |
{ |
| 708 |
auto& page = document.GetPages().GetPageAt(annot_page); |
| 709 |
Rect annot_rect; |
| 710 |
if (!annot_position.empty()) |
| 711 |
{ |
| 712 |
annot_rect = Rect(annot_left, page.GetMediaBox().Height - annot_top - annot_height, annot_width, annot_height); |
| 713 |
} |
| 714 |
|
| 715 |
signature = &page.CreateField<PdfSignature>(name, annot_rect); |
| 716 |
if (!annot_position.empty() && annot_print) |
| 717 |
signature->MustGetWidget().SetFlags(PdfAnnotationFlags::Print); |
| 718 |
else if (annot_position.empty() && (field_name.empty() || !field_use_existing)) |
| 719 |
signature->MustGetWidget().SetFlags(PdfAnnotationFlags::Invisible | PdfAnnotationFlags::Hidden); |
| 720 |
|
| 721 |
if (!annot_position.empty()) |
| 722 |
{ |
| 723 |
Rect annotSize(0.0, 0.0, annot_rect.Width, annot_rect.Height); |
| 724 |
auto sigXObject = document.CreateXObjectForm(annotSize); |
| 725 |
PdfPainter painter; |
| 726 |
|
| 727 |
try |
| 728 |
{ |
| 729 |
painter.SetCanvas(*sigXObject); |
| 730 |
|
| 731 |
/* Workaround Adobe's reader error 'Expected a dict object.' when the stream |
| 732 |
contains only one object which does Save()/Restore() on its own, like |
| 733 |
the image XObject. */ |
| 734 |
painter.Save(); |
| 735 |
painter.Restore(); |
| 736 |
|
| 737 |
draw_annotation(document, painter, args, annot_rect); |
| 738 |
|
| 739 |
signature->MustGetWidget().SetAppearanceStream(*sigXObject); |
| 740 |
} |
| 741 |
catch (...) |
| 742 |
{ |
| 743 |
} |
| 744 |
|
| 745 |
painter.FinishDrawing(); |
| 746 |
} |
| 747 |
} |
| 748 |
|
| 749 |
signature->SetSignatureReason(PdfString(reason)); |
| 750 |
signature->SetSignatureDate(PdfDate()); |
| 751 |
|
| 752 |
PdfSignerCmsParams params; |
| 753 |
if (digest.length() != 0) |
| 754 |
params.Hashing = PoDoFo::ConvertTo<PdfHashingAlgorithm>(digest); |
| 755 |
|
| 756 |
charbuff cert; |
| 757 |
utls::ReadTo(cert, certfile); |
| 758 |
|
| 759 |
charbuff pkey; |
| 760 |
utls::ReadTo(pkey, pkeyfile); |
| 761 |
PdfSignerCms signer(cert, pkey, params); |
| 762 |
|
| 763 |
FileStreamDevice device(outputfile.empty() ? inputfile : outputfile, FileMode::Open, DeviceAccess::Write); |
| 764 |
|
| 765 |
PoDoFo::SignDocument(document, device, signer, *signature); |
| 766 |
} |
| 767 |
|