Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid empty password #44

Closed
ggoulart opened this issue May 23, 2023 · 14 comments
Closed

Invalid empty password #44

ggoulart opened this issue May 23, 2023 · 14 comments

Comments

@ggoulart
Copy link

In version 1.16 the argument pkcs12_password can not be an empty string, but in version 1.15 it works.
This is the error received in version 1.16 raise ValueError("Password must be 1 or more bytes.")

This is an expected behavior?

@vog
Copy link
Member

vog commented May 23, 2023

No, this change in behavior is unintentional.

The only relevant change from 1.15 to 1.16 was to drop pyOpenSSL support and to use cryptography instead. The relevant commit is:

Pull requests with improvements to create_sslcontext() are welcome.

@vog
Copy link
Member

vog commented May 23, 2023

As a side note, I also don't like how we need to go through temporary files to use cryptography, with pyOpenSSL this wasn't necessary, and maybe cryptography provides a way without that as well, perhaps we just overlooked something here.

@yonting
Copy link
Contributor

yonting commented May 23, 2023

I take responsibility for the problematic commits. The exception is raised by

cryptography.hazmat.primitives.serialization.BestAvailableEncryption(password=pkcs12_password_bytes)
. I dunno how to fix this. cryptography.hazmat.primitives.serialization.NoEncryption maybe works but maybe not wise. Using empty string passwords is considered a bad practice so the "cryptography" decided to put in place some enforcement.

Regarding the side note I conducted some research into avoiding temporary files. This was a longstanding feature request for the standard library ssl: python/cpython#60691. and urllib3/urllib3#2680 (comment). Now closed. This might all be old news to you folk.

Maybe the only options to avoiding temporary files are to find or wait for another library that implements the now deprecated functionality of urllib3.contrib.pyopenssl, or attempt to sustain the patching in this repo.

@vog
Copy link
Member

vog commented May 23, 2023

. I dunno how to fix this. cryptography.hazmat.primitives.serialization.NoEncryption maybe works but maybe not wise. Using empty string passwords is considered a bad practice so the "cryptography" decided to put in place some enforcement.

I'm not against this, but would like to point out that this would only make sense if it was equivalent to an empty password in the pyOpenSSL implementation. Otherwise it would not be backwards compatible and hence still make old code fail.

@ggoulart What kind of PKCS12 files did you open by providing an empty string as password? Are they just unencrypted, or do they use some kind of encryption, just with a dead simple password?

Regarding the side note I conducted some research into avoiding temporary files. This was a longstanding feature request for the standard library ssl: python/cpython#60691. and urllib3/urllib3#2680 (comment). Now closed.

Okay, so it seems we didn't overlook anything, it just doesn't exist. This is too bad.

@ggoulart
Copy link
Author

Hi @vog I'm using a pfx certificate with an empty string as password. Yes, it is encryption with a dead simple password.

@olehb007
Copy link
Contributor

olehb007 commented May 24, 2023

I have a case with no password when loading certificate from memory, creating pull request. If needed it can be bound specifically pkcs12_data parameter. Pull request #45

@gprestes
Copy link

gprestes commented May 24, 2023

Hi @vog, I am working together with @ggoulart. We are getting the file from a Key Vault and file is retrieved passwordless. We could re-add the password, but since you said this is not the intended behaviour we will stick with the previous version while there is not a fix.

@vog
Copy link
Member

vog commented May 24, 2023

I'm a bit confused. What exactly do you mean by "could re-add the password"?

@vog
Copy link
Member

vog commented Jul 13, 2023

@ggoulart @gprestes Could you please have a look at the latest version 1.18? Does this work for you as expected, or are there still issues?

@magnethy
Copy link

magnethy commented Sep 13, 2023

I have the same issue for 1.18. Works when downgrading to 1.15. Running Python 3.9 and did a clean dependency installation.

Traceback (most recent call last):
    [...]
    rval = requests_pkcs12.post(url, json=body, headers=self._get_api_headers(), pkcs12_filename=self.cert_path, pkcs12_password='')
  File "/home/site/wwwroot/.python_packages/lib/site-packages/requests_pkcs12.py", line 168, in post
    return request('post', *args, **kwargs)
  File "/home/site/wwwroot/.python_packages/lib/site-packages/requests_pkcs12.py", line 140, in request
    pkcs12_adapter = Pkcs12Adapter(
  File "/home/site/wwwroot/.python_packages/lib/site-packages/requests_pkcs12.py", line 99, in __init__
    self.ssl_context = create_sslcontext(pkcs12_data, pkcs12_password_bytes, ssl_protocol)
  File "/home/site/wwwroot/.python_packages/lib/site-packages/requests_pkcs12.py", line 53, in create_sslcontext
    cryptography.hazmat.primitives.serialization.BestAvailableEncryption(password=pkcs12_password_bytes)
  File "/home/site/wwwroot/.python_packages/lib/site-packages/cryptography/hazmat/primitives/_serialization.py", line 67, in __init__
    raise ValueError("Password must be 1 or more bytes.")
ValueError: Password must be 1 or more bytes.```

@vog
Copy link
Member

vog commented Sep 18, 2023

I think I found an acceptable solution for this messy situation.

If verified that the old pyOpenSSL based implementation in 1.15 behaves identically for an empty password and a None password. So I believe it is safe to treat those cases equally, i.e. to treat an empty password as if it was None.

@vog
Copy link
Member

vog commented Sep 18, 2023

@ggoulart @gprestes @yonting @olehb007 @magnethy

Please have a look at the latest version 1.19.

Does this fix the problem for all of you, or are there still issues?

vog added a commit that referenced this issue Sep 18, 2023
@vog
Copy link
Member

vog commented Sep 18, 2023

@ggoulart @gprestes @yonting @olehb007 @magnethy

Meanwhile I added some more improvements with respect to password handling, improved the README, and released 1.20 as well as 1.21, but the question remains:

Does this fix the problem for all of you, or are there still issues?

@vog
Copy link
Member

vog commented May 6, 2024

@ggoulart @gprestes @yonting @olehb007 @magnethy

As there was no further feedback, I'm closing this issue for now. Please feel free to open another ticket if you still have trouble with the latest requests_pkcs12 version.

@vog vog closed this as completed May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants