Persits Software, Inc. Web Site
 Navigator:  Home |  Manual |  Chapter 8: Security
Chapter 9: Working with Existing PDFs Chapter 7: Tables
  Chapter 8: Security
8.1 Overview of PDF Security
8.2 PdfDocument.Encrypt Method
8.3 Digital Signing
8.4 PDF-based Secure Mail

8.1 Overview of PDF Security

A secure PDF is a document that imposes a set of restrictions on this document’s user, such as requiring a password on opening, or preventing copying/pasting of the document’s content.

There are two aspects to the standard (or built-in) PDF security: password protection and permission flags.

8.1.1 Password Protection

When a secure PDF is created, the document author supplies two secret strings: the Owner and User passwords. Applying these two passwords to the document using an algorithm described in Adobe PDF specifications produces a secure document.

A secure PDF’s content is encrypted with the RC4 algorithm, a stream cipher invented by Ron Rivest of RSA Security. Either a 40-bit or 128-bit key can be used. As of AspPDF.NET 2.0, the 128-bit Advanced Encryption Standard (AES) cipher is also supported. An encryption key is derived from the user password alone. The owner password is used to encrypt the user password and also to protect the immutability of the document’s permission flags. An encrypted copy of the user password is embedded in a secure PDF for validation purposes.

To open an encrypted document, the viewer must specify either the user or owner password. Specifying the valid user password enables a user to view the document, but also makes her subject to the permission flags associated with the document. For example, a user may not be able to modify or print the document. Specifying the valid owner password gives the user full control over the document: not only can she view it, but also change or remove its security settings.

An empty string is a perfectly valid value for either password. There are 4 possible password use scenarios:

Scenario 1: Both user and owner passwords are non-empty and not equal to each other.

A user must specify a password to open the document. Use this scenario to create private password-protected documents with certain usage restrictions imposed, such as "no printing". If the two passwords are the same, the owner password is ignored and this turns into Scenario 3.
Scenario 2: The owner password is non-empty, the user password is empty.
The document can be viewed without specifying a password, but a user is subject to permission flags. Use this scenario to create publicly accessible documents with certain usage restrictions imposed.
Scenario 3: The owner password is empty, the user password is non-empty.
A user must specify a password to open the document. Use this scenario to create password-protected documents with no usage restrictions.
Scenario 4: Both the owner and user passwords are empty.
Although technically the document is encrypted, there is no true security in it as anyone can read the document and modify/remove permission flags. Using this scenario is not recommended.

8.1.2 Permission Flags

Version 4.0 and earlier of the Adobe Acrobat family of products supported 40-bit encryption, and recognized the following four permission flags:

This version of security specifications is referred to as Revision 2.

Adobe Acrobat 5.0 and higher allowed both 40-bit and 128-bit encryption, and introduced a more granular permission system. The new version of security specifications is referred to as Revision 3. The following permission flags are used in Revision 3:

  • Bit 3: Print the document, possibly not at high-resolution depending on Bit 12;
  • Bit 4: Modify content of the document by operations other than those controlled by Bits 6,9, and 11;
  • Bit 5: Copy/ extract content by operations other than those controlled by Bit 10;
  • Bit 6: Add and change annotations, fill in form fields, and if Bit 4 is also set, create or modify interactive form fields;
  • Bit 9: Fill in existing interactive form fields, even if Bit 6 is clear;
  • Bit 10: Extract text and graphics (in support of accessibility to disabled users)
  • Bit 11: Assemble the document (insert, rotate, or delete pages, and create bookmarks or thumbnail images), even if Bit 4 is clear.
  • Bit 12: Print the document to a representation from which a faithful digital copy of the PDF content could be generated. When this bit is clear (and Bit 3 is set) printing is limited to a low-level representation of the appearance.
8.2 PdfDocument.Encrypt Method
To make a PDF document secure with AspPDF.NET, all you have to do is call the Encrypt method of the PdfDocument object. This method accepts 4 arguments, all optional: the owner password, user password, key length, and permission flags.

The owner and user password arguments are empty strings by default. To avoid Scenario 4 described above, you should set at least one of the passwords, or both, to non-empty strings. Specifying identical strings for both passwords will result in the owner password being ignored.

For RC4 encryption, the key length argument must be set to either 40 or 128. The default value is 40 but 128 is strongly recommended. For AES encryption (requires AspPDF.NET 2.0+) the value must be -128 (negative 128). The negative sign specifies AES as opposed to RC4.

The permission flags argument should be set to a bit-wise combination of the permission flag constants defined by the AspPDF.NET component. These constants are:

enum pdfPermissions
{

  pdfFull = &HFFFFFFFC (all significant bits)
  pdfPrint = &H04 (Bit 3)
  pdfModify = &H08 (Bit 4)
  pdfCopy = &H10 (Bit 5)
  pdfAnnotations = &H20 (Bit 6)
  pdfForm = &H0100 (Bit 9)
  pdfExtract = &H0200 (Bit 10)
  pdfAssemble = &H0400 (Bit 11)
  pdfPrintHigh = &H0800 (Bit 12)
}

The default value for the permission flags argument is pdfPermissions.pdfFull. For example:

C#
...
objDoc.Encrypt( "abc", "xyz", 128, pdfPermissions.pdfFull & (~pdfPermissions.pdfModify) );
...
VB.NET
objDoc.Encrypt( "abc", "xyz", 128, pdfPermissions.pdfFull And (Not pdfPermissions.pdfModify) )

The code sample 08_encrypt.cs.aspx/.vb.aspx (not shown here) is almost identical to the Hello World sample used in Chapter 3, except that it also calls the Encrypt method. The string "abc" is used for the owner password, "xyz" for the user password, 128 for key length and no-printing/no-changing for the permission flags.

Click the links below to run this code sample:

http://localhost/asppdf.net/manual_08/08_encrypt.cs.aspx
http://localhost/asppdf.net/manual_08/08_encrypt.vb.aspx  Make sure AspPDF.NET is installed on your local machine and IIS is running for these links to work.

You will be prompted to enter a password when opening this document in Acrobat Reader. You can either enter abc or xyz. The Reader will display a yellow key icon in the status bar indicating that the document is secure:

If you click on the key icon and select Document Security..., the Document Security dialog box comes up confirming the correct security properties of the document:

8.3 Digital Signing
A digital signature protects a document's integrity and provides proof of the signer's identity. AspPDF.NET is capable of adding a X.509 certificate-based digital signature to a PDF document in PKCS#7 format via the Sign method of the PdfDocument object.

8.3.1 Support for Certificate-based Cryptography in .NET

To create a digitally signed document, or sign an existing PDF, AspPDF.NET needs to obtain an instance of the X509Certificate2 object (System.Security.Cryptography.X509Certificates namespace.) Certificates usually reside in "certificate stores". The following code snippet obtains a certificate by its subject name from the "ROOT" certificate store residing in the "local machine" section of the server's storage:

using System.Security.Cryptography;
using System.Security.Cryptography.Pkcs;
using System.Security.Cryptography.X509Certificates;

...

X509Store objStoreMy = new X509Store( StoreName.Root, StoreLocation.LocalMachine );
objStoreMy.Open(OpenFlags.ReadOnly);

X509Certificate2Collection objCertColl =
  objStoreMy.Certificates.Find( X509FindType.FindBySubjectName,
  "Persits Software", false );

X509Certificate2 objCert = objCertColl[0];

The objCert object thus obtained can now be passed directly to the PdfDocument.Sign method for AspPDF.NET to perform the digital signing of the document.

Digital signing is often a tricky process as it involves private keys that are inherently secure entities. The code shown above, when run in an ASP.NET environment, will not produce an error but an attempt to use the certificate in the Sign method probably will, as we are now trying to access the private key and causing a security violation. To avoid the error, "user impersonation" may be required. The PdfManager object offers the LogonUser method which impersonates an arbitrary user account. The method expects the domain name (or an empty string if this is a local domain), username and password as arguments. Call this method before calling the certificate-store methods, as follows:

objPDF.LogonUser( "", "Administrator", "MyPassword" );
X509Store objStoreMy = new X509Store( StoreName.Root, StoreLocation.LocalMachine );
...

The signer certificate does not have to reside in a certificate store, it can also be obtained directly from a password-protected .pfx file (PKCS#12 format). An instance of the X509Certificate2 object is created by calling its constructor and passing the .pfx file path and password as arguments. Note that in a ASP.NET environment you must call LogonUser before creating the certificate object this way, or the signing will fail. For example:

objPDF.LogonUser( "", "Administrator", "MyPassword" );
X509Certificate2 objCert = new X509Certificate2( @"c:\path\mycert.pfx", "my_password" );
...

For more general information about X509 certificates, visit the AspEncrypt.com site.

8.3.2 Sign Method

The Sign method expects a instance of the X509Certificate2 object with its HasPrivateKey property set to true. It also accepts the signer name, reason for signing, and location arguments (the first one is required, the other two are optional), and also an optional parameter string or parameter object controlling the visibility and location of the signature field within the document.

The following code sample adds a signature to a PDF document:

objDoc.Sign( Cert, "John Smith", "I created this document.", "New York, NY" );

It is recommended that Acrobat Reader 6.0+ be used to view digitally signed documents. Acrobat 5.0 (the full version, not the Reader) is also acceptable, but you must also download and install the VeriSign Document Signer plug-in, and only signatures based on VeriSign certificates seem to be recognized as valid by this plug-in. A free 60-day VeriSign certificate can be obtained here.

NOTE: A digitally signed document can only be saved to disk (via the Save method). Saving to memory or an HTTP stream cannot be used once Sign is called, and an attempt to call SaveToMemory or SaveHttp will result in an error exception.

8.3.3 Visible Signatures

By default, the Sign method creates an invisible signature. Using the last optional argument of the Sign method, it is possible to specify a page and location within that page where the signature icon is to appear.

The following code fragment draws a signature icon in the upper-left corner of the first page of the document (middle arguments omitted for brevity):

objDoc.Sign( Cert, ..., "visible=true;x=10,y=750;width=20;height=20;pageindex=1" );

It is also possible to change the default appearance of a signature by drawing on the canvas of a PdfAnnot object returned by the Sign method. Annotations are described in detail in Chapter 10.

8.3.4 Signature Validation

AspPDF.NET's digital signature functionality would not be complete without a way to verify an existing signature in a document.

Signature verification is implemented via the VerifySignature method of the PdfDocument object. VerifySignature can only be called on an instance of PdfDocument created via the file version of OpenDocument, not the byte array version. VerifySignature does not take any arguments.

VerifySignature returns null if no PKCS#7 signatures are found in the document. Otherwise, it returns an instance of the PdfSignature object encapsulating various property of the signature, including its validation status (valid/invalid), signer name, reason for signing, location, and other information. If a document contains multiple signatures (such as, when an already signed document was signed again), the VerifySignature method validates and returns the one that covers the largest portion of the document, which is usually the latest signature.

The following code fragment opens a PDF document from a file, and attempts to validate a signature, if one is present:

PdfDocument objDoc = objPDF.OpenDocument(@"c:\somefile.pdf");
PdfSignature objSig = objDoc.VerifySignature();

if( objSig == null )
{
   Console.WriteLine( "No signature found." );
}
else
{
   Console.WriteLine( "Status = " + objSig.Status );
   Console.WriteLine( "Name = " + objSig.Name );
   Console.WriteLine( "Reason = " + objSig.Reason );
   Console.WriteLine( "Contents = " + objSig.Contents );
}

8.4 PDF-based Secure Mail
Some web applications require that the users be periodically sent sensitive information via email securely. Persits Software, Inc., the maker of AspPDF and AspPDF.NET, offers certificate-based secure email functionality via the components AspEmail and AspEncrypt.

In order to receive and decrypt secure email, the recipient needs to have acquired a personal certificate from a certification authority such as Verisign or Thawte. The public-key portion of the certificate needs to be exported to a file and placed on the web sever generating the secure email. Also, the recipient must use stand-alone S/MIME-enabled email software to decrypt and read the messages such as Outlook, Outlook Express, etc. Web-based email services such as gmail, hotmail, etc. cannot be used.

PDF format offers an alternative to certificate-based email in which the secure content is embedded in a password-protected PDF document and sent to the user as an attachment via regular, unencrypted, email. Upon the receipt of the message, the user opens the attachment with Acrobat Reader and enters his password to view the content.

PDF is a great vehicle for delivering secure email for the following reasons:

PDF-based secure message functionality is demonstrated by the following AspPDF.NET live demo:

http://support.persits.com/pdf_net/demo_email.cs.aspx

Chapter 9: Working with Existing PDFs Chapter 7: Tables

Search AspPDF.net

Newsletter Signup

Other Products
AspPDF
AspUpload
AspJpeg
AspEmail
AspEncrypt
AspGrid
AspUser
  This site is owned and maintained by Persits Software, Inc. Copyright © 2003 - 2010. All Rights Reserved.