Determine whether Master or User password entered

Hi,

I’m using the PDFNet C++ SDK to write a Windows application and would be grateful for an answer to the following question.

If a user opens an encrypted PDF document and this is loaded into PDF::PDFDoc then:

PDFDoc::IsEncrypted() returns true and the user is prompted to enter a password.

When the user enters a correct password PDFDoc::InitStdSecurityHandler() returns true.

At this point I want the application to determine whether the user entered a Master password or a User password, however I can see no means of distinguishing between the case of a user entering a User password for a document protected with a User password only and the case of a user entering a Master password for a document protected with both a Master password and a User password.

How can I programatically distinguish between these two cases?

Many thanks for your help.

Terry.

Hi, this post has the full answer.

bool is_owner = pdfdoc.GetSecurityHandler().GetPermission(SecurityHandler.Permission.e_owner)

Hi Ryan,

Thanks for the suggestion, but I had already tried this (and every other PDFNet API call that appears relevant) and it does not distinguish between the two cases that I described.

In both cases ‘GetPermission(SDF::SecurityHandler::Permission::e_owner)’ returns ‘true’, which is to be expected because in both cases the user has ‘owner’ rights.

I would have expected to get the information I want by calling ‘IsUserPasswordRequired()’ and ‘IsMasterPasswordRequired()’ but these also return ‘true’ in both cases, which seems rather odd. When a document is protected with only a User password why does ‘IsMasterPasswordRequired()’ return true?

It is clearly technically possible to do what I want because Adobe Reader does it! If a document protected with a User password only is loaded into Reader and when prompted the User password is entered then ‘Document Properties->Security->Show Details’ states:

Document Open Password: Yes

Permissions Password: No

<<

Whereas if a document protected with both a Master password and a User password is loaded into Reader and when prompted the Master password is entered then ‘Document Properties->Security->Show Details’ states:

Document Open Password: Yes

Permissions Password: Yes

<<

Any further suggestions would be gratefully received, however if the current PDFNet API cannot distinguish between these two cases then could I please request an enhancement. Thanks.

Hi Terry,

Just to reiterate the details, a PDF can have a owner password and/or a user password.

If both are set, then the call

returns the correct value, depending on which password was entered.

If the PDF has just an owner password, and the correct password is entered, then of course the code above will return true.

I suspect the confusion, is that if the PDF has only a user password, and no owner password, and they enter the user password, then calling the code above again returns true.

This is as designed.

Entering the user password, when there is no owner password, implies ownership since there is no restriction.

Furthermore, from the PDF spec.

Documents in which only file attachments are encrypted shall use the same user and owner passwords.

So it is perfectly normal for both passwords to be one and the same, thus making the distinction not always possible.

I would have expected to get the information I want by calling ‘IsUserPasswordRequired()’ and ‘IsMasterPasswordRequired()’ but these also return ‘true’ in both cases, which seems rather odd. When a document is protected with only a User password why does ‘IsMasterPasswordRequired()’ return true?

I do not see this behavior. IsMasterPasswordRequired returns false for documents with only a User Password. Can you send me the document in question, and/or try the latest version of our library?

If this info does not help, then perhaps explained exactly what it is you are trying to do, how you are doing it now (if applicable), and how you would like to do it.