Fix openssl_pkcs12 crash with cryptography backend when loading passphrase-protected files (#248)

* Convert passphrase to bytes when loading PKCS#12 file with cryptography.

* Add tests with PKCS#12 passphrase.

* Add changelog fragment.
pull/249/head
Felix Fontein 2021-06-11 20:03:16 +02:00 committed by GitHub
parent cda2edf92c
commit 0df33de73e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 6 deletions

View File

@ -0,0 +1,2 @@
bugfixes:
- "openssl_pkcs12 - fix crash when loading passphrase-protected PKCS#12 files with ``cryptography`` backend (https://github.com/ansible-collections/community.crypto/issues/247, https://github.com/ansible-collections/community.crypto/pull/248)."

View File

@ -23,7 +23,7 @@ import base64
import binascii import binascii
import re import re
from ansible.module_utils._text import to_text from ansible.module_utils._text import to_text, to_bytes
from ._asn1 import serialize_asn1_string_as_der from ._asn1 import serialize_asn1_string_as_der
try: try:
@ -444,7 +444,8 @@ def parse_pkcs12(pkcs12_bytes, passphrase=None):
''' '''
if _load_key_and_certificates is None: if _load_key_and_certificates is None:
raise ValueError('load_key_and_certificates() not present in the current cryptography version') raise ValueError('load_key_and_certificates() not present in the current cryptography version')
private_key, certificate, additional_certificates = _load_key_and_certificates(pkcs12_bytes, passphrase) private_key, certificate, additional_certificates = _load_key_and_certificates(
pkcs12_bytes, to_bytes(passphrase) if passphrase is not None else None)
friendly_name = None friendly_name = None
if certificate: if certificate:

View File

@ -107,11 +107,12 @@
check_mode: true check_mode: true
register: p12_dumped_check_mode register: p12_dumped_check_mode
- name: "({{ select_crypto_backend }}) Generate PKCS#12 file with multiple certs" - name: "({{ select_crypto_backend }}) Generate PKCS#12 file with multiple certs and passphrase"
openssl_pkcs12: openssl_pkcs12:
select_crypto_backend: '{{ select_crypto_backend }}' select_crypto_backend: '{{ select_crypto_backend }}'
path: '{{ output_dir }}/ansible_multi_certs.p12' path: '{{ output_dir }}/ansible_multi_certs.p12'
friendly_name: abracadabra friendly_name: abracadabra
passphrase: hunter3
privatekey_path: '{{ output_dir }}/ansible_pkey1.pem' privatekey_path: '{{ output_dir }}/ansible_pkey1.pem'
certificate_path: '{{ output_dir }}/ansible1.crt' certificate_path: '{{ output_dir }}/ansible1.crt'
other_certificates: other_certificates:
@ -120,11 +121,12 @@
state: present state: present
register: p12_multiple_certs register: p12_multiple_certs
- name: "({{ select_crypto_backend }}) Generate PKCS#12 file with multiple certs, again (idempotency)" - name: "({{ select_crypto_backend }}) Generate PKCS#12 file with multiple certs and passphrase, again (idempotency)"
openssl_pkcs12: openssl_pkcs12:
select_crypto_backend: '{{ select_crypto_backend }}' select_crypto_backend: '{{ select_crypto_backend }}'
path: '{{ output_dir }}/ansible_multi_certs.p12' path: '{{ output_dir }}/ansible_multi_certs.p12'
friendly_name: abracadabra friendly_name: abracadabra
passphrase: hunter3
privatekey_path: '{{ output_dir }}/ansible_pkey1.pem' privatekey_path: '{{ output_dir }}/ansible_pkey1.pem'
certificate_path: '{{ output_dir }}/ansible1.crt' certificate_path: '{{ output_dir }}/ansible1.crt'
other_certificates: other_certificates:
@ -133,11 +135,12 @@
state: present state: present
register: p12_multiple_certs_idempotency register: p12_multiple_certs_idempotency
- name: "({{ select_crypto_backend }}) Dump PKCS#12 with multiple certs" - name: "({{ select_crypto_backend }}) Dump PKCS#12 with multiple certs and passphrase"
openssl_pkcs12: openssl_pkcs12:
select_crypto_backend: '{{ select_crypto_backend }}' select_crypto_backend: '{{ select_crypto_backend }}'
src: '{{ output_dir }}/ansible_multi_certs.p12' src: '{{ output_dir }}/ansible_multi_certs.p12'
path: '{{ output_dir }}/ansible_parse_multi_certs.pem' path: '{{ output_dir }}/ansible_parse_multi_certs.pem'
passphrase: hunter3
action: parse action: parse
state: present state: present

View File

@ -8,7 +8,7 @@
register: p12_validate_no_pkey register: p12_validate_no_pkey
- name: '({{ select_crypto_backend }}) Validate PKCS#12 with multiple certs' - name: '({{ select_crypto_backend }}) Validate PKCS#12 with multiple certs'
shell: "{{ openssl_binary }} pkcs12 -info -in {{ output_dir }}/ansible_multi_certs.p12 -nodes -passin pass:'' | grep subject" shell: "{{ openssl_binary }} pkcs12 -info -in {{ output_dir }}/ansible_multi_certs.p12 -nodes -passin pass:'hunter3' | grep subject"
register: p12_validate_multi_certs register: p12_validate_multi_certs
- name: '({{ select_crypto_backend }}) Validate PKCS#12 (assert)' - name: '({{ select_crypto_backend }}) Validate PKCS#12 (assert)'