| 1 |
// SPDX-FileCopyrightText: 2023 Francesco Pretto <[email protected]> |
| 2 |
// SPDX-License-Identifier: MIT-0 |
| 3 |
|
| 4 |
#include <PdfTest.h> |
| 5 |
#include <podofo/private/OpenSSLInternal.h> |
| 6 |
|
| 7 |
using namespace std; |
| 8 |
using namespace PoDoFo; |
| 9 |
|
| 10 |
constexpr string_view TestSignatureRefHash = "1CC60CEA1A7A8D3ECDD18B20FAAAEFE7"sv; |
| 11 |
|
| 12 |
TEST_CASE("TestLoadCertificate") |
| 13 |
{ |
| 14 |
// Load a PEM certificate now works |
| 15 |
string cert; |
| 16 |
TestUtils::ReadTestInputFileTo(cert, "mycert.pem"); |
| 17 |
|
| 18 |
PdfSignerCms signer(cert); |
| 19 |
// Dummy data append to enforce certificate load |
| 20 |
signer.AppendData(""); |
| 21 |
} |
| 22 |
|
| 23 |
// Test signing with supplied private key |
| 24 |
TEST_CASE("TestSignature1") |
| 25 |
{ |
| 26 |
charbuff buff; |
| 27 |
auto inputPath = TestUtils::GetTestInputFilePath("TestSignature.pdf"); |
| 28 |
auto outputPath = TestUtils::GetTestOutputFilePath("TestSignature1.pdf"); |
| 29 |
|
| 30 |
// RSA Private key coefficients in der PKCS1 format (binary) |
| 31 |
string pkey1; |
| 32 |
TestUtils::ReadTestInputFileTo(pkey1, "mykey-pkcs1.der"); |
| 33 |
|
| 34 |
// RSA Private key coefficients in der PKCS8 format (binary) |
| 35 |
string pkey8; |
| 36 |
TestUtils::ReadTestInputFileTo(pkey8, "mykey-pkcs8.der"); |
| 37 |
|
| 38 |
auto testSignature = [&](const shared_ptr<StreamDevice>& stream, const bufferview& pkey) |
| 39 |
{ |
| 40 |
// X509 Certificate |
| 41 |
string cert; |
| 42 |
TestUtils::ReadTestInputFileTo(cert, "mycert.der"); |
| 43 |
|
| 44 |
PdfMemDocument doc(stream); |
| 45 |
auto& page = doc.GetPages().GetPageAt(0); |
| 46 |
auto& annot = page.GetAnnotations().GetAnnotAt(0); |
| 47 |
auto& field = dynamic_cast<PdfAnnotationWidget&>(annot).GetField(); |
| 48 |
auto& signature = dynamic_cast<PdfSignature&>(field); |
| 49 |
|
| 50 |
auto signer = PdfSignerCms(cert, pkey); |
| 51 |
PoDoFo::SignDocument(doc, *stream, signer, signature, PdfSaveOptions::NoMetadataUpdate); |
| 52 |
}; |
| 53 |
|
| 54 |
{ |
| 55 |
FileStreamDevice input(inputPath); |
| 56 |
stringstream ss; |
| 57 |
auto stream = std::make_shared<StandardStreamDevice>(ss); |
| 58 |
input.CopyTo(*stream); |
| 59 |
testSignature(stream, pkey1); |
| 60 |
REQUIRE(ssl::ComputeMD5Str(ss.str()) == TestSignatureRefHash); |
| 61 |
} |
| 62 |
|
| 63 |
{ |
| 64 |
utls::ReadTo(buff, inputPath); |
| 65 |
auto stream = std::make_shared<BufferStreamDevice>(buff); |
| 66 |
testSignature(stream, pkey8); |
| 67 |
REQUIRE(ssl::ComputeMD5Str(buff) == TestSignatureRefHash); |
| 68 |
} |
| 69 |
|
| 70 |
{ |
| 71 |
fs::copy_file(fs::u8path(inputPath), fs::u8path(outputPath), fs::copy_options::overwrite_existing); |
| 72 |
auto stream = std::make_shared<FileStreamDevice>(outputPath, FileMode::Open); |
| 73 |
testSignature(stream, pkey8); |
| 74 |
utls::ReadTo(buff, outputPath); |
| 75 |
REQUIRE(ssl::ComputeMD5Str(buff) == TestSignatureRefHash); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
// Test event driven signing with external service |
| 80 |
TEST_CASE("TestSignature2") |
| 81 |
{ |
| 82 |
charbuff buff; |
| 83 |
auto inputPath = TestUtils::GetTestInputFilePath("TestSignature.pdf"); |
| 84 |
auto outputPath = TestUtils::GetTestOutputFilePath("TestSignature2.pdf"); |
| 85 |
|
| 86 |
fs::copy_file(fs::u8path(inputPath), fs::u8path(outputPath), fs::copy_options::overwrite_existing); |
| 87 |
auto stream = std::make_shared<FileStreamDevice>(outputPath, FileMode::Open); |
| 88 |
|
| 89 |
// X509 Certificate |
| 90 |
string cert; |
| 91 |
TestUtils::ReadTestInputFileTo(cert, "mycert.der"); |
| 92 |
|
| 93 |
// RSA Private key coefficients in der format (binary) |
| 94 |
string pkey; |
| 95 |
TestUtils::ReadTestInputFileTo(pkey, "mykey-pkcs1.der"); |
| 96 |
|
| 97 |
PdfMemDocument doc(stream); |
| 98 |
auto& page = doc.GetPages().GetPageAt(0); |
| 99 |
auto& annot = page.GetAnnotations().GetAnnotAt(0); |
| 100 |
auto& field = dynamic_cast<PdfAnnotationWidget&>(annot).GetField(); |
| 101 |
auto& signature = dynamic_cast<PdfSignature&>(field); |
| 102 |
|
| 103 |
PdfSignerCmsParams params; |
| 104 |
params.SigningService = [&pkey, ¶ms](bufferview hashToSign, bool dryrun, charbuff& signedHash) |
| 105 |
{ |
| 106 |
(void)dryrun; |
| 107 |
ssl::DoSignHash(hashToSign, pkey, params.Hashing, signedHash); |
| 108 |
}; |
| 109 |
auto signer = PdfSignerCms(cert, params); |
| 110 |
PoDoFo::SignDocument(doc, *stream, signer, signature, PdfSaveOptions::NoMetadataUpdate); |
| 111 |
|
| 112 |
utls::ReadTo(buff, outputPath); |
| 113 |
REQUIRE(ssl::ComputeMD5Str(buff) == TestSignatureRefHash); |
| 114 |
|
| 115 |
// Resign should work |
| 116 |
signature.SetCreatingApplication(PdfName("Sample Application")); |
| 117 |
PoDoFo::SignDocument(doc, *stream, signer, signature, PdfSaveOptions::NoMetadataUpdate); |
| 118 |
utls::ReadTo(buff, outputPath); |
| 119 |
REQUIRE(ssl::ComputeMD5Str(buff) == "2AA706A0A84C662CEE3886C908C06557"); |
| 120 |
} |
| 121 |
|
| 122 |
// Test deferred signing with external service |
| 123 |
TEST_CASE("TestSignature3") |
| 124 |
{ |
| 125 |
charbuff buff; |
| 126 |
auto inputPath = TestUtils::GetTestInputFilePath("TestSignature.pdf"); |
| 127 |
auto outputPath = TestUtils::GetTestOutputFilePath("TestSignature3.pdf"); |
| 128 |
|
| 129 |
fs::copy_file(fs::u8path(inputPath), fs::u8path(outputPath), fs::copy_options::overwrite_existing); |
| 130 |
auto stream = std::make_shared<FileStreamDevice>(outputPath, FileMode::Open); |
| 131 |
|
| 132 |
// X509 Certificate |
| 133 |
string cert; |
| 134 |
TestUtils::ReadTestInputFileTo(cert, "mycert.der"); |
| 135 |
|
| 136 |
// RSA Private key coefficients in der format (binary) |
| 137 |
string pkey; |
| 138 |
TestUtils::ReadTestInputFileTo(pkey, "mykey-pkcs8.der"); |
| 139 |
|
| 140 |
PdfMemDocument doc(stream); |
| 141 |
auto& page = doc.GetPages().GetPageAt(0); |
| 142 |
auto& annot = page.GetAnnotations().GetAnnotAt(0); |
| 143 |
auto& field = dynamic_cast<PdfAnnotationWidget&>(annot).GetField(); |
| 144 |
auto& signature = dynamic_cast<PdfSignature&>(field); |
| 145 |
|
| 146 |
PdfSignerCmsParams params; |
| 147 |
auto signer = std::make_shared<PdfSignerCms>(cert, params); |
| 148 |
PdfSigningContext ctx; |
| 149 |
auto signerId = ctx.AddSigner(signature, signer); |
| 150 |
PdfSigningResults results; |
| 151 |
ctx.StartSigning(doc, stream, results, PdfSaveOptions::NoMetadataUpdate); |
| 152 |
charbuff signedHash; |
| 153 |
ssl::DoSignHash(results.Intermediate[signerId], pkey, params.Hashing, signedHash); |
| 154 |
results.Intermediate[signerId] = signedHash; |
| 155 |
ctx.FinishSigning(results); |
| 156 |
|
| 157 |
utls::ReadTo(buff, outputPath); |
| 158 |
REQUIRE(ssl::ComputeMD5Str(buff) == TestSignatureRefHash); |
| 159 |
} |
| 160 |
|
| 161 |
// Test deferred signing with external service and context dumping/restore |
| 162 |
TEST_CASE("TestSignatureDumpRestore") |
| 163 |
{ |
| 164 |
charbuff buff; |
| 165 |
auto inputPath = TestUtils::GetTestInputFilePath("TestSignature.pdf"); |
| 166 |
utls::ReadTo(buff, inputPath); |
| 167 |
|
| 168 |
// X509 Certificate |
| 169 |
string cert; |
| 170 |
TestUtils::ReadTestInputFileTo(cert, "mycert.der"); |
| 171 |
|
| 172 |
// RSA Private key coefficients in der format (binary) |
| 173 |
string pkey; |
| 174 |
TestUtils::ReadTestInputFileTo(pkey, "mykey-pkcs8.der"); |
| 175 |
|
| 176 |
charbuff hashToSign; |
| 177 |
PdfSignerId signerId; |
| 178 |
PdfSignerCmsParams params; |
| 179 |
|
| 180 |
// NOTE: This block simulates loosing all the original objects |
| 181 |
// and restore the context in a subsequent phase |
| 182 |
{ |
| 183 |
auto stream = std::make_shared<BufferStreamDevice>(buff); |
| 184 |
PdfMemDocument doc(stream); |
| 185 |
auto& page = doc.GetPages().GetPageAt(0); |
| 186 |
auto& annot = page.GetAnnotations().GetAnnotAt(0); |
| 187 |
auto& field = dynamic_cast<PdfAnnotationWidget&>(annot).GetField(); |
| 188 |
auto& signature = dynamic_cast<PdfSignature&>(field); |
| 189 |
signature.SetSignatureDate(PdfDate::Parse("D:20250205192456+06'00'")); |
| 190 |
|
| 191 |
auto signer = std::make_shared<PdfSignerCms>(cert, params); |
| 192 |
PdfSigningContext ctx; |
| 193 |
signerId = ctx.AddSigner(signature, signer); |
| 194 |
PdfSigningResults results; |
| 195 |
ctx.StartSigning(doc, stream, results, PdfSaveOptions::NoMetadataUpdate); |
| 196 |
|
| 197 |
hashToSign = results.Intermediate[signerId]; |
| 198 |
|
| 199 |
ctx.DumpInPlace(); |
| 200 |
utls::WriteTo(TestUtils::GetTestOutputFilePath("TestSignatureDumpRestore.bin"), buff); |
| 201 |
} |
| 202 |
|
| 203 |
auto newStream = std::make_shared<BufferStreamDevice>(buff); |
| 204 |
PdfSigningContext newCtx; |
| 205 |
auto doc = newCtx.Restore(newStream); |
| 206 |
|
| 207 |
utls::WriteTo(TestUtils::GetTestOutputFilePath("TestSignatureDumpRestore1.pdf"), buff); |
| 208 |
|
| 209 |
charbuff signedHash; |
| 210 |
ssl::DoSignHash(hashToSign, pkey, params.Hashing, signedHash); |
| 211 |
PdfSigningResults newResults; |
| 212 |
newResults.Intermediate[signerId] = signedHash; |
| 213 |
newCtx.FinishSigning(newResults); |
| 214 |
|
| 215 |
utls::WriteTo(TestUtils::GetTestOutputFilePath("TestSignatureDumpRestore2.pdf"), buff); |
| 216 |
|
| 217 |
REQUIRE(ssl::ComputeMD5Str(buff) == "4162823DB0FD7A43B7A3FDDFE4FDEC38"); |
| 218 |
} |
| 219 |
|
| 220 |
TEST_CASE("TestCertificateRSA") |
| 221 |
{ |
| 222 |
{ |
| 223 |
string cert; |
| 224 |
TestUtils::ReadTestInputFileTo(cert, "RSA1024Cert.pem"); |
| 225 |
|
| 226 |
PdfSignerCmsParams params; |
| 227 |
PdfSignerCms signer(cert, params); |
| 228 |
REQUIRE(signer.GetSignedHashSize() == 128); |
| 229 |
} |
| 230 |
|
| 231 |
{ |
| 232 |
string cert; |
| 233 |
TestUtils::ReadTestInputFileTo(cert, "RSA3072Cert.pem"); |
| 234 |
|
| 235 |
PdfSignerCmsParams params; |
| 236 |
PdfSignerCms signer(cert, params); |
| 237 |
REQUIRE(signer.GetSignedHashSize() == 384); |
| 238 |
} |
| 239 |
|
| 240 |
{ |
| 241 |
string cert; |
| 242 |
TestUtils::ReadTestInputFileTo(cert, "RSA4096Cert.pem"); |
| 243 |
|
| 244 |
PdfSignerCmsParams params; |
| 245 |
PdfSignerCms signer(cert, params); |
| 246 |
REQUIRE(signer.GetSignedHashSize() == 512); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
TEST_CASE("TestSignEncryptedDoc") |
| 251 |
{ |
| 252 |
auto inputPath = TestUtils::GetTestInputFilePath("AESV3R6-256.pdf"); |
| 253 |
auto outputPath = TestUtils::GetTestOutputFilePath("TestSignEncryptedDoc.pdf"); |
| 254 |
|
| 255 |
fs::copy_file(fs::u8path(inputPath), fs::u8path(outputPath), fs::copy_options::overwrite_existing); |
| 256 |
auto stream = std::make_shared<FileStreamDevice>(outputPath, FileMode::Open); |
| 257 |
|
| 258 |
// X509 Certificate |
| 259 |
string cert; |
| 260 |
TestUtils::ReadTestInputFileTo(cert, "mycert.der"); |
| 261 |
|
| 262 |
// RSA Private key coefficients in der format (binary) |
| 263 |
string pkey; |
| 264 |
TestUtils::ReadTestInputFileTo(pkey, "mykey-pkcs8.der"); |
| 265 |
|
| 266 |
auto date = PdfDate::ParseW3C("2024-07-31T17:03:42+02:00"); |
| 267 |
|
| 268 |
{ |
| 269 |
PdfMemDocument doc(stream, "userpass"); |
| 270 |
auto& page = doc.GetPages().GetPageAt(0); |
| 271 |
auto& signature = page.CreateField<PdfSignature>("Signature", Rect()); |
| 272 |
signature.SetSignatureDate(date); |
| 273 |
auto signer = PdfSignerCms(cert, pkey); |
| 274 |
PoDoFo::SignDocument(doc, *stream, signer, signature, PdfSaveOptions::NoMetadataUpdate); |
| 275 |
} |
| 276 |
|
| 277 |
{ |
| 278 |
// Just reload the signed document with owner password as a simple test |
| 279 |
PdfMemDocument doc(stream, "ownerpass"); |
| 280 |
auto& page = doc.GetPages().GetPageAt(0); |
| 281 |
auto& annot = page.GetAnnotations().GetAnnotAt(0); |
| 282 |
auto& field = dynamic_cast<PdfAnnotationWidget&>(annot).GetField(); |
| 283 |
auto& signature = dynamic_cast<PdfSignature&>(field); |
| 284 |
REQUIRE(signature.GetSignatureDate() == date); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
TEST_CASE("TestSaveOnSigning") |
| 289 |
{ |
| 290 |
PdfMemDocument doc; |
| 291 |
auto& page = doc.GetPages().CreatePage(PdfPageSize::A4); |
| 292 |
string x509certbuffer; |
| 293 |
TestUtils::ReadTestInputFileTo(x509certbuffer, "mycert.der"); |
| 294 |
|
| 295 |
string pkeybuffer; |
| 296 |
TestUtils::ReadTestInputFileTo(pkeybuffer, "mykey-pkcs8.der"); |
| 297 |
|
| 298 |
auto& signature = page.CreateField<PdfSignature>("Signature", Rect(100, 600, 100, 100)); |
| 299 |
signature.SetSignatureDate(PdfDate::LocalNow()); |
| 300 |
auto image = doc.CreateImage(); |
| 301 |
image->Load(TestUtils::GetTestInputFilePath("ReferenceImage.png")); |
| 302 |
auto xformObj = doc.CreateXObjectForm(Rect(0, 0, image->GetWidth(), image->GetHeight())); |
| 303 |
|
| 304 |
PdfPainter painter; |
| 305 |
painter.SetCanvas(*xformObj); |
| 306 |
painter.DrawImage(*image, 0, 0, 1, 1); |
| 307 |
painter.FinishDrawing(); |
| 308 |
|
| 309 |
auto signer = PdfSignerCms(x509certbuffer, pkeybuffer); |
| 310 |
|
| 311 |
signature.MustGetWidget().SetAppearanceStream(*xformObj); |
| 312 |
|
| 313 |
FileStreamDevice output(TestUtils::GetTestOutputFilePath("TestSaveOnSigning.pdf"), FileMode::Create); |
| 314 |
PoDoFo::SignDocument(doc, output, signer, signature, PdfSaveOptions::SaveOnSigning); |
| 315 |
} |
| 316 |
|
| 317 |
TEST_CASE("TestPdfSignerCms") |
| 318 |
{ |
| 319 |
// X509 Certificate |
| 320 |
string cert; |
| 321 |
TestUtils::ReadTestInputFileTo(cert, "mycert.der"); |
| 322 |
|
| 323 |
charbuff buff; |
| 324 |
{ |
| 325 |
PdfSignerCms signer(cert); |
| 326 |
signer.ComputeSignatureDeferred({ }, buff, true); |
| 327 |
|
| 328 |
try |
| 329 |
{ |
| 330 |
signer.ComputeSignature(buff, true); |
| 331 |
} |
| 332 |
catch (PdfError& error) |
| 333 |
{ |
| 334 |
// If a deferred signing is started we can't switch to event based |
| 335 |
REQUIRE(error.GetCode() == PdfErrorCode::InternalLogic); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
{ |
| 340 |
PdfSignerCms signer(cert); |
| 341 |
try |
| 342 |
{ |
| 343 |
signer.ComputeSignature(buff, true); |
| 344 |
} |
| 345 |
catch (PdfError& error) |
| 346 |
{ |
| 347 |
// An event based signing requires a private key or a signing service |
| 348 |
REQUIRE(error.GetCode() == PdfErrorCode::InternalLogic); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
{ |
| 353 |
PdfSignerCmsParams params; |
| 354 |
params.SigningService = [](bufferview, bool, charbuff&) |
| 355 |
{ |
| 356 |
// Do nothing |
| 357 |
}; |
| 358 |
|
| 359 |
PdfSignerCms signer(cert, params); |
| 360 |
signer.ComputeSignature(buff, true); |
| 361 |
|
| 362 |
try |
| 363 |
{ |
| 364 |
signer.ComputeSignatureDeferred({ }, buff, true); |
| 365 |
} |
| 366 |
catch (PdfError& error) |
| 367 |
{ |
| 368 |
// If a event based signing is started we can't switch to deferred |
| 369 |
REQUIRE(error.GetCode() == PdfErrorCode::InternalLogic); |
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
TEST_CASE("TestGetPreviousRevision") |
| 375 |
{ |
| 376 |
{ |
| 377 |
charbuff currBuffer; |
| 378 |
|
| 379 |
utls::ReadTo(currBuffer, TestUtils::GetTestInputFilePath("TestBlankSigned.pdf")); |
| 380 |
auto input = std::make_shared<BufferStreamDevice>(currBuffer); |
| 381 |
|
| 382 |
PdfMemDocument doc; |
| 383 |
doc.Load(input); |
| 384 |
auto& signature = dynamic_cast<PdfSignature&>( |
| 385 |
dynamic_cast<PdfAnnotationWidget&>( |
| 386 |
doc.GetPages().GetPageAt(0).GetAnnotations().GetAnnotAt(0)).GetField()); |
| 387 |
|
| 388 |
charbuff prevBuffer; |
| 389 |
BufferStreamDevice output(prevBuffer); |
| 390 |
|
| 391 |
REQUIRE(signature.TryGetPreviousRevision(*input, output)); |
| 392 |
|
| 393 |
charbuff refBuffer; |
| 394 |
utls::ReadTo(refBuffer, TestUtils::GetTestInputFilePath("blank.pdf")); |
| 395 |
REQUIRE(prevBuffer == refBuffer); |
| 396 |
} |
| 397 |
|
| 398 |
{ |
| 399 |
charbuff currBuffer; |
| 400 |
|
| 401 |
utls::ReadTo(currBuffer, TestUtils::GetTestInputFilePath("TestSaveOnSigning.pdf")); |
| 402 |
auto input = std::make_shared<BufferStreamDevice>(currBuffer); |
| 403 |
|
| 404 |
PdfMemDocument doc; |
| 405 |
doc.Load(input); |
| 406 |
auto& signature = dynamic_cast<PdfSignature&>( |
| 407 |
dynamic_cast<PdfAnnotationWidget&>( |
| 408 |
doc.GetPages().GetPageAt(0).GetAnnotations().GetAnnotAt(0)).GetField()); |
| 409 |
|
| 410 |
charbuff prevBuffer; |
| 411 |
BufferStreamDevice output(prevBuffer); |
| 412 |
|
| 413 |
// This file is signed but has not incremental updates, |
| 414 |
// so the previous revision is undefined |
| 415 |
REQUIRE(!signature.TryGetPreviousRevision(*input, output)); |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
TEST_CASE("TestSignatureOffsetStart") |
| 420 |
{ |
| 421 |
string x509certbuffer; |
| 422 |
TestUtils::ReadTestInputFileTo(x509certbuffer, "mycert.der"); |
| 423 |
|
| 424 |
string pkeybuffer; |
| 425 |
TestUtils::ReadTestInputFileTo(pkeybuffer, "mykey-pkcs8.der"); |
| 426 |
|
| 427 |
charbuff currBuffer; |
| 428 |
utls::ReadTo(currBuffer, TestUtils::GetTestInputFilePath("blank-with-offset-start.pdf")); |
| 429 |
auto inputOutput = std::make_shared<BufferStreamDevice>(currBuffer); |
| 430 |
|
| 431 |
PdfMemDocument doc; |
| 432 |
doc.Load(inputOutput); |
| 433 |
auto& page = doc.GetPages().GetPageAt(0); |
| 434 |
auto& signature = page.CreateField<PdfSignature>("Signature", Rect()); |
| 435 |
|
| 436 |
PdfSignerCms signer(x509certbuffer, pkeybuffer); |
| 437 |
PoDoFo::SignDocument(doc, *inputOutput, signer, signature, PdfSaveOptions::NoMetadataUpdate); |
| 438 |
|
| 439 |
utls::WriteTo(TestUtils::GetTestOutputFilePath("TestSignatureOffsetStart.pdf"), currBuffer); |
| 440 |
|
| 441 |
// Try to reload the document |
| 442 |
doc.Load(inputOutput); |
| 443 |
|
| 444 |
REQUIRE(ssl::ComputeMD5Str(currBuffer) == "7063AD6AFCB797D361D2DAF943002298"); |
| 445 |
} |
| 446 |
|
| 447 |
TEST_CASE("TestSignatureCorrupted") |
| 448 |
{ |
| 449 |
auto currentLogSeverity = PdfCommon::GetMaxLoggingSeverity(); |
| 450 |
PdfCommon::SetMaxLoggingSeverity(PdfLogSeverity::None); |
| 451 |
string x509certbuffer; |
| 452 |
TestUtils::ReadTestInputFileTo(x509certbuffer, "mycert.der"); |
| 453 |
|
| 454 |
string pkeybuffer; |
| 455 |
TestUtils::ReadTestInputFileTo(pkeybuffer, "mykey-pkcs8.der"); |
| 456 |
|
| 457 |
charbuff currBuffer; |
| 458 |
|
| 459 |
auto doTest = [&currBuffer, &x509certbuffer, &pkeybuffer](string_view outFilename, string_view expectedMD5) |
| 460 |
{ |
| 461 |
auto inputOutput = std::make_shared<BufferStreamDevice>(currBuffer); |
| 462 |
|
| 463 |
PdfMemDocument doc; |
| 464 |
doc.Load(inputOutput); |
| 465 |
auto& page = doc.GetPages().GetPageAt(0); |
| 466 |
auto& signature = page.CreateField<PdfSignature>("Signature", Rect()); |
| 467 |
|
| 468 |
PdfSignerCms signer(x509certbuffer, pkeybuffer); |
| 469 |
|
| 470 |
try |
| 471 |
{ |
| 472 |
PoDoFo::SignDocument(doc, *inputOutput, signer, signature, PdfSaveOptions::NoMetadataUpdate); |
| 473 |
throw runtime_error("Signing should have failed with corrupted document"); |
| 474 |
} |
| 475 |
catch (const PdfError& e) |
| 476 |
{ |
| 477 |
REQUIRE(e.GetCode() == PdfErrorCode::InvalidXRef); |
| 478 |
} |
| 479 |
|
| 480 |
PoDoFo::SignDocument(doc, *inputOutput, signer, signature, PdfSaveOptions::NoMetadataUpdate | PdfSaveOptions::IgnoreXRefErrors); |
| 481 |
utls::WriteTo(TestUtils::GetTestOutputFilePath(outFilename), currBuffer); |
| 482 |
|
| 483 |
// Try to reload the document |
| 484 |
doc.Load(inputOutput); |
| 485 |
|
| 486 |
REQUIRE(ssl::ComputeMD5Str(currBuffer) == expectedMD5); |
| 487 |
}; |
| 488 |
|
| 489 |
try |
| 490 |
{ |
| 491 |
utls::ReadTo(currBuffer, TestUtils::GetTestInputFilePath("TestXRefRecovery1.pdf")); |
| 492 |
doTest("TestSignatureCorrupted1.pdf", "FF1B6A133940DED8C0890E3A9707C151"); |
| 493 |
|
| 494 |
// Repeat the test with some garbage at the beginning of the test |
| 495 |
utls::ReadTo(currBuffer, TestUtils::GetTestInputFilePath("TestXRefRecovery1.pdf")); |
| 496 |
currBuffer.insert(0, "% Some garbage before the PDF header\n\n"); |
| 497 |
doTest("TestSignatureCorrupted2.pdf", "8AA3CB1D40DC13E652AD935A16DA927C"); |
| 498 |
PdfCommon::SetMaxLoggingSeverity(currentLogSeverity); |
| 499 |
} |
| 500 |
catch (...) |
| 501 |
{ |
| 502 |
PdfCommon::SetMaxLoggingSeverity(currentLogSeverity); |
| 503 |
throw; |
| 504 |
} |
| 505 |
} |