Skip to content

Instantly share code, notes, and snippets.

@datalogics-pgallot
Created July 1, 2016 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save datalogics-pgallot/8c74307fdea8bdfe3ae41be1523b6ecb to your computer and use it in GitHub Desktop.
Save datalogics-pgallot/8c74307fdea8bdfe3ae41be1523b6ecb to your computer and use it in GitHub Desktop.
Sample app which reproduces Acrobat's Document Properties Security tab.
/*
// Project: DocSecurity - Sample app which reproduces Acrobat's Document Properties Security tab.
*/
#ifndef MAC_ENV
#include <sys/types.h>
#include <sys/stat.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include "PDFInit.h"
#include "CosCalls.h"
#include "CorCalls.h"
#include "ASCalls.h"
#include "ASExtraCalls.h"
#include "PDCalls.h"
#include "PagePDECntCalls.h"
#include "PDMetadataCalls.h"
#include "MyPDFLibUtils.h"
#ifdef MAC_ENV
#include "MacUtils.h"
#endif
void MainProc(int argc, char **argv)
{
PDDoc pdDoc;
int i = 0;
if (argc > 1)
pdDoc = MyPDDocOpen(argv[1]);
else
return;
DURING
CosDoc cosPDFDoc = PDDocGetCosDoc(pdDoc);
CosObj DocRoot = CosDocGetRoot(cosPDFDoc);
fprintf(stdout, "%20s", "Security Method: ");
StdSecurityData data = (StdSecurityData)PDDocGetSecurityData(pdDoc);
if (data != NULL)
fprintf(stdout, "Password Security\n");
else
fprintf(stdout, "No Security\n");
fprintf(stdout, "%20s", "Can be opened by: ");
if (data != NULL)
switch (data->revision)
{
case 6: fprintf(stdout, "Acrobat X and later\n"); break;
case 5: fprintf(stdout, "Acrobat 9.0 and later\n"); break;
case 4: fprintf(stdout, "Acrobat %d.0 and later\n",data->encryptMethod == STDSEC_METHOD_AES_V2 ? 7 : 6); break;
case 3: fprintf(stdout, "Acrobat %d.0 and later\n", (data->version > 1 ? 5 : 3)); break;
case 2: fprintf(stdout, "Acrobat 3.0 and later\n"); break;
default: fprintf(stdout, "???\n");
}
else
fprintf(stdout, "All versions of Acrobat\n");
if (data != NULL && data->revision >= 4 && data->encryptMetadata == false)
fprintf(stdout, "All contents of the document are encrypted but search engines can access the document's metadata.\n");
else if (data != NULL && (data->revision < 4 || data->encryptMetadata == true))
fprintf(stdout, "All contents of the document are encrypted and search engines cannot access the document's metadata.\n");
ASBool bHasDocMDPPerms = false;
ASBool bHasUR = false;
ASBool bHasURLacksFillin = false;
ASBool bHasURLacksSpawnTmplt = false;
ASBool bHasURLacksSigMod = false;
ASBool bHasURLacksAnnotCreat = false;
//TODO: do a recursive search in AcroForm for signature fields that have already been signed.
// A signed signature means that changing the document and docAssembly are not allowed because
// those sorts of changes would invalidate the signature. If it has signed signatures, then
// a User Rights signature does not trigger the asterisk for changing the doc and docAssembly.
CosObj cosPerms = CosDictGet(DocRoot, ASAtomFromString("Perms"));
if (CosObjGetType(cosPerms) == CosDict)
{
CosObj cosDocMDP = CosDictGet(cosPerms, ASAtomFromString("DocMDP"));
bHasDocMDPPerms = (CosObjGetType(cosDocMDP) == CosDict);
CosObj cosUR = CosDictGet(cosPerms, ASAtomFromString("UR3"));
if (CosObjGetType(cosUR) == CosDict)
bHasUR = true;
else{
CosObj cosUR = CosDictGet(cosPerms, ASAtomFromString("UR"));
if (CosObjGetType(cosUR) == CosDict)
bHasUR = true;
}
if (bHasUR)
{
bHasURLacksFillin = true;
bHasURLacksSpawnTmplt = true;
bHasURLacksSigMod = true;
bHasURLacksAnnotCreat = true;
CosObj cosURReference = CosDictGet(cosUR, ASAtomFromString("Reference"));
CosObj cosURRef0 = CosArrayGet(cosURReference, 0);
CosObj cosTransfParams = CosDictGet(cosURRef0, ASAtomFromString("TransformParams"));
CosObj cosFormArray = CosDictGet(cosTransfParams, ASAtomFromString("Form"));
if (CosObjGetType(cosFormArray) == CosArray)
{
ASAtom asaFillin = ASAtomFromString("FillIn");
ASAtom asaSpawn = ASAtomFromString("SpawnTemplate");
int ralen = CosArrayLength(cosFormArray);
for (int i = 0; i < ralen; i++)
{
ASAtom asaRAEntry = CosNameValue(CosArrayGet(cosFormArray, i));
if (asaRAEntry == asaFillin)
bHasURLacksFillin = false;
if (asaRAEntry == asaSpawn)
bHasURLacksSpawnTmplt = false;
}
}
CosObj cosSigArray = CosDictGet(cosTransfParams, ASAtomFromString("Signature"));
if (CosObjGetType(cosSigArray) == CosArray)
{
ASAtom asaModify = ASAtomFromString("Modify");
int ralen = CosArrayLength(cosSigArray);
for (int i = 0; i < ralen; i++)
{
if (CosNameValue(CosArrayGet(cosSigArray, i)) == asaModify)
bHasURLacksSigMod = false;
}
}
CosObj cosAnnotArray = CosDictGet(cosTransfParams, ASAtomFromString("Annots"));
if (CosObjGetType(cosAnnotArray) == CosArray)
{
ASAtom asaCreate = ASAtomFromString("Create");
int ralen = CosArrayLength(cosAnnotArray);
for (int i = 0; i < ralen; i++)
{
if (CosNameValue(CosArrayGet(cosAnnotArray, i)) == asaCreate)
bHasURLacksAnnotCreat = false;
}
}
}
}
ASBool bAllowed = false;
ASBool bWasAllowed = bAllowed;
fprintf(stdout, "%35s","Printing: ");
bAllowed = (data == NULL) || data->perms & pdPermPrint;
fprintf(stdout, "%sAllowed\n", !bAllowed ? "Not " : "");
fprintf(stdout, "%35s", "Changing the Document: ");
bAllowed = (data == NULL && !bHasDocMDPPerms) || (data != NULL && data->perms & pdPermEdit);
bWasAllowed = bAllowed;
bAllowed = bAllowed && !bHasUR;
fprintf(stdout, "%sAllowed %s\n", !bAllowed ? "Not " : "", bWasAllowed && !bAllowed && bHasUR ? "*" : "");
fprintf(stdout, "%35s", "Document Assembly: ");
bAllowed = (data == NULL && !bHasDocMDPPerms)
|| (data != NULL && data->revision == 2 && data->perms & pdPermEdit)
|| (data != NULL && data->revision >= 3 && (data->perms & pdPrivPermDocAssembly || data->perms & pdPermEdit));
bWasAllowed = bAllowed;
bAllowed = bAllowed && !bHasUR;
fprintf(stdout, "%sAllowed %s\n", !bAllowed ? "Not " : "",bWasAllowed && !bAllowed && bHasUR?"*":"");
fprintf(stdout, "%35s", "Content Copying: ");
bAllowed = (data == NULL) || data->perms & pdPermCopy;
fprintf(stdout, "%sAllowed\n", !bAllowed ? "Not " : "");
fprintf(stdout, "%35s", "Content Copying for Accessibility: ");
bAllowed = (data == NULL)
|| (data->revision <= 3 && data->perms & pdPermCopy)
|| (data->revision >= 3 && data->perms & pdPrivPermAccessible)
|| (PDPermReqGranted == PDDocPermRequest(pdDoc, PDPermReqObjDoc, PDPermReqOprAccessible, data));
fprintf(stdout, "%sAllowed\n", !bAllowed ? "Not " : "");
fprintf(stdout, "%35s", "Page Extraction: ");
bAllowed = (PDPermReqGranted == PDDocPermRequest(pdDoc, PDPermReqObjPage, PDPermReqOprExport, data));
fprintf(stdout, "%sAllowed\n", !bAllowed ? "Not " : "");
fprintf(stdout, "%35s", "Commenting: ");
bAllowed = (data == NULL && !bHasDocMDPPerms) || (data != NULL && data->perms & pdPermEditNotes);
bAllowed = bAllowed && !bHasURLacksAnnotCreat;
fprintf(stdout, "%sAllowed%s\n", !bAllowed ? "Not " : "", bHasURLacksAnnotCreat?"*":"");
fprintf(stdout, "%35s", "Filling of form fields: ");
bAllowed = (data == NULL && !bHasDocMDPPerms)
|| (data != NULL && data->perms & pdPermEditNotes)
|| (data != NULL && data->revision >= 3 && data->perms & pdPrivPermFillandSign)
|| (data != NULL && PDPermReqGranted == PDDocPermRequest(pdDoc, PDPermReqObjForm, PDPermReqOprFillIn, data));
bAllowed = bAllowed && !bHasURLacksFillin;
fprintf(stdout, "%sAllowed %s\n", !bAllowed ? "Not " : "", bHasURLacksFillin?"*":"");
fprintf(stdout, "%35s", "Signing: ");
bAllowed = (data == NULL && !bHasDocMDPPerms)
|| (data != NULL && data->perms & pdPermEditNotes)
|| (data != NULL && data->revision >= 3 && data->perms & pdPrivPermFillandSign)
|| (data != NULL && PDPermReqGranted == PDDocPermRequest(pdDoc, PDPermReqObjSignature, PDPermReqOprAny, data));
bAllowed = bAllowed && !bHasURLacksSigMod;
fprintf(stdout, "%sAllowed %s\n", !bAllowed ? "Not " : "", bHasURLacksSigMod?"*":"");
fprintf(stdout, "%35s", "Creation of Template Pages: ");
bAllowed = (data == NULL && !bHasDocMDPPerms)
|| (data != NULL && data->perms & pdPermEditNotes && data->perms & pdPermEdit)
|| (data != NULL && data->revision >= 3 && data->perms & pdPrivPermFillandSign)
|| (data != NULL && PDPermReqGranted == PDDocPermRequest(pdDoc, PDPermReqObjForm, PDPermReqOprSpawnTemplate, data));
bAllowed = bAllowed && !bHasURLacksSpawnTmplt;
fprintf(stdout, "%sAllowed %s\n", !bAllowed ? "Not " : "",bHasURLacksSpawnTmplt?"*":"");
if (bHasDocMDPPerms)
{
fprintf(stdout, "\n* The Author has certified this document to prevent changes.\n");
}
if (bHasUR)
{
fprintf(stdout, "\n* This document restricts some Acrobat features to allow for extended features in Adobe Reader.\n");
}
HANDLER
char buf[512];
ASGetErrorString(ERRORCODE, buf, sizeof(buf));
fprintf(stderr, "Error code: 0x%lx, Error Message: %s\n", ERRORCODE, buf);
END_HANDLER
PDDocClose(pdDoc);
}
#define INCLUDE_MYPDFLIBAPP_CPP 1
#include "MyPDFLibApp.cpp"
#undef INCLUDE_MYPDFLIBAPP_CPP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment