Skip to content

Commit cea1f2e

Browse files
authored
Remove deprecated PKCS12 and NetscapeSPKI classes (#1288)
1 parent 510b0e5 commit cea1f2e

File tree

4 files changed

+16
-632
lines changed

4 files changed

+16
-632
lines changed

CHANGELOG.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ Changelog
44
Versions are year-based with a strict backward-compatibility policy.
55
The third digit is only for regressions.
66

7+
24.1.0 (UNRELEASED)
8+
-------------------
9+
10+
Backward-incompatible changes:
11+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
* Removed the deprecated ``OpenSSL.crypto.PKCS12`` and
14+
``OpenSSL.crypto.NetscapeSPKI``. ``OpenSSL.crypto.PKCS12`` may be replaced
15+
by the PKCS#12 APIs in the ``cryptography`` package.
16+
17+
Deprecations:
18+
^^^^^^^^^^^^^
19+
20+
Changes:
21+
^^^^^^^^
22+
723
24.0.0 (2024-01-22)
824
-------------------
925

doc/api/crypto.rst

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,6 @@ PKey objects
160160
161161
Key type constants.
162162

163-
.. _openssl-pkcs12:
164-
165-
PKCS12 objects
166-
--------------
167-
168-
.. autoclass:: PKCS12
169-
:members:
170-
171163
.. _openssl-509ext:
172164

173165
X509Extension objects
@@ -178,16 +170,6 @@ X509Extension objects
178170
:special-members:
179171
:exclude-members: __weakref__
180172

181-
.. _openssl-netscape-spki:
182-
183-
NetscapeSPKI objects
184-
--------------------
185-
186-
.. autoclass:: NetscapeSPKI
187-
:members:
188-
:special-members:
189-
:exclude-members: __weakref__
190-
191173
.. _crl:
192174

193175
CRL objects

src/OpenSSL/crypto.py

Lines changed: 0 additions & 300 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@
7777
"dump_privatekey",
7878
"Revoked",
7979
"CRL",
80-
"PKCS12",
81-
"NetscapeSPKI",
8280
"load_publickey",
8381
"load_privatekey",
8482
"dump_certificate_request",
@@ -2617,304 +2615,6 @@ def export(
26172615
)
26182616

26192617

2620-
class PKCS12:
2621-
"""
2622-
A PKCS #12 archive.
2623-
"""
2624-
2625-
def __init__(self) -> None:
2626-
self._pkey: Optional[PKey] = None
2627-
self._cert: Optional[X509] = None
2628-
self._cacerts: Optional[List[X509]] = None
2629-
self._friendlyname: Optional[bytes] = None
2630-
2631-
def get_certificate(self) -> Optional[X509]:
2632-
"""
2633-
Get the certificate in the PKCS #12 structure.
2634-
2635-
:return: The certificate, or :py:const:`None` if there is none.
2636-
:rtype: :py:class:`X509` or :py:const:`None`
2637-
"""
2638-
return self._cert
2639-
2640-
def set_certificate(self, cert: X509) -> None:
2641-
"""
2642-
Set the certificate in the PKCS #12 structure.
2643-
2644-
:param cert: The new certificate, or :py:const:`None` to unset it.
2645-
:type cert: :py:class:`X509` or :py:const:`None`
2646-
2647-
:return: ``None``
2648-
"""
2649-
if not isinstance(cert, X509):
2650-
raise TypeError("cert must be an X509 instance")
2651-
self._cert = cert
2652-
2653-
def get_privatekey(self) -> Optional[PKey]:
2654-
"""
2655-
Get the private key in the PKCS #12 structure.
2656-
2657-
:return: The private key, or :py:const:`None` if there is none.
2658-
:rtype: :py:class:`PKey`
2659-
"""
2660-
return self._pkey
2661-
2662-
def set_privatekey(self, pkey: PKey) -> None:
2663-
"""
2664-
Set the certificate portion of the PKCS #12 structure.
2665-
2666-
:param pkey: The new private key, or :py:const:`None` to unset it.
2667-
:type pkey: :py:class:`PKey` or :py:const:`None`
2668-
2669-
:return: ``None``
2670-
"""
2671-
if not isinstance(pkey, PKey):
2672-
raise TypeError("pkey must be a PKey instance")
2673-
self._pkey = pkey
2674-
2675-
def get_ca_certificates(self) -> Optional[Tuple[X509, ...]]:
2676-
"""
2677-
Get the CA certificates in the PKCS #12 structure.
2678-
2679-
:return: A tuple with the CA certificates in the chain, or
2680-
:py:const:`None` if there are none.
2681-
:rtype: :py:class:`tuple` of :py:class:`X509` or :py:const:`None`
2682-
"""
2683-
if self._cacerts is not None:
2684-
return tuple(self._cacerts)
2685-
return None
2686-
2687-
def set_ca_certificates(self, cacerts: Optional[Iterable[X509]]) -> None:
2688-
"""
2689-
Replace or set the CA certificates within the PKCS12 object.
2690-
2691-
:param cacerts: The new CA certificates, or :py:const:`None` to unset
2692-
them.
2693-
:type cacerts: An iterable of :py:class:`X509` or :py:const:`None`
2694-
2695-
:return: ``None``
2696-
"""
2697-
if cacerts is None:
2698-
self._cacerts = None
2699-
else:
2700-
cacerts = list(cacerts)
2701-
for cert in cacerts:
2702-
if not isinstance(cert, X509):
2703-
raise TypeError(
2704-
"iterable must only contain X509 instances"
2705-
)
2706-
self._cacerts = cacerts
2707-
2708-
def set_friendlyname(self, name: Optional[bytes]) -> None:
2709-
"""
2710-
Set the friendly name in the PKCS #12 structure.
2711-
2712-
:param name: The new friendly name, or :py:const:`None` to unset.
2713-
:type name: :py:class:`bytes` or :py:const:`None`
2714-
2715-
:return: ``None``
2716-
"""
2717-
if name is None:
2718-
self._friendlyname = None
2719-
elif not isinstance(name, bytes):
2720-
raise TypeError(
2721-
f"name must be a byte string or None (not {name!r})"
2722-
)
2723-
self._friendlyname = name
2724-
2725-
def get_friendlyname(self) -> Optional[bytes]:
2726-
"""
2727-
Get the friendly name in the PKCS# 12 structure.
2728-
2729-
:returns: The friendly name, or :py:const:`None` if there is none.
2730-
:rtype: :py:class:`bytes` or :py:const:`None`
2731-
"""
2732-
return self._friendlyname
2733-
2734-
def export(
2735-
self,
2736-
passphrase: Optional[bytes] = None,
2737-
iter: int = 2048,
2738-
maciter: int = 1,
2739-
) -> bytes:
2740-
"""
2741-
Dump a PKCS12 object as a string.
2742-
2743-
For more information, see the :c:func:`PKCS12_create` man page.
2744-
2745-
:param passphrase: The passphrase used to encrypt the structure. Unlike
2746-
some other passphrase arguments, this *must* be a string, not a
2747-
callback.
2748-
:type passphrase: :py:data:`bytes`
2749-
2750-
:param iter: Number of times to repeat the encryption step.
2751-
:type iter: :py:data:`int`
2752-
2753-
:param maciter: Number of times to repeat the MAC step.
2754-
:type maciter: :py:data:`int`
2755-
2756-
:return: The string representation of the PKCS #12 structure.
2757-
:rtype:
2758-
"""
2759-
passphrase = _text_to_bytes_and_warn("passphrase", passphrase)
2760-
2761-
if self._cacerts is None:
2762-
cacerts = _ffi.NULL
2763-
else:
2764-
cacerts = _lib.sk_X509_new_null()
2765-
cacerts = _ffi.gc(cacerts, _lib.sk_X509_free)
2766-
for cert in self._cacerts:
2767-
_lib.sk_X509_push(cacerts, cert._x509)
2768-
2769-
if passphrase is None:
2770-
passphrase = _ffi.NULL
2771-
2772-
friendlyname = self._friendlyname
2773-
if friendlyname is None:
2774-
friendlyname = _ffi.NULL
2775-
2776-
if self._pkey is None:
2777-
pkey = _ffi.NULL
2778-
else:
2779-
pkey = self._pkey._pkey
2780-
2781-
if self._cert is None:
2782-
cert = _ffi.NULL
2783-
else:
2784-
cert = self._cert._x509
2785-
2786-
pkcs12 = _lib.PKCS12_create(
2787-
passphrase,
2788-
friendlyname,
2789-
pkey,
2790-
cert,
2791-
cacerts,
2792-
_lib.NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
2793-
_lib.NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
2794-
iter,
2795-
maciter,
2796-
0,
2797-
)
2798-
if pkcs12 == _ffi.NULL:
2799-
_raise_current_error()
2800-
pkcs12 = _ffi.gc(pkcs12, _lib.PKCS12_free)
2801-
2802-
bio = _new_mem_buf()
2803-
_lib.i2d_PKCS12_bio(bio, pkcs12)
2804-
return _bio_to_string(bio)
2805-
2806-
2807-
utils.deprecated(
2808-
PKCS12,
2809-
__name__,
2810-
(
2811-
"PKCS#12 support in pyOpenSSL is deprecated. You should use the APIs "
2812-
"in cryptography."
2813-
),
2814-
DeprecationWarning,
2815-
name="PKCS12",
2816-
)
2817-
2818-
2819-
class NetscapeSPKI:
2820-
"""
2821-
A Netscape SPKI object.
2822-
"""
2823-
2824-
def __init__(self) -> None:
2825-
spki = _lib.NETSCAPE_SPKI_new()
2826-
self._spki = _ffi.gc(spki, _lib.NETSCAPE_SPKI_free)
2827-
2828-
def sign(self, pkey: PKey, digest: str) -> None:
2829-
"""
2830-
Sign the certificate request with this key and digest type.
2831-
2832-
:param pkey: The private key to sign with.
2833-
:type pkey: :py:class:`PKey`
2834-
2835-
:param digest: The message digest to use.
2836-
:type digest: :py:class:`str`
2837-
2838-
:return: ``None``
2839-
"""
2840-
if pkey._only_public:
2841-
raise ValueError("Key has only public part")
2842-
2843-
if not pkey._initialized:
2844-
raise ValueError("Key is uninitialized")
2845-
2846-
digest_obj = _lib.EVP_get_digestbyname(_byte_string(digest))
2847-
if digest_obj == _ffi.NULL:
2848-
raise ValueError("No such digest method")
2849-
2850-
sign_result = _lib.NETSCAPE_SPKI_sign(
2851-
self._spki, pkey._pkey, digest_obj
2852-
)
2853-
_openssl_assert(sign_result > 0)
2854-
2855-
def verify(self, key: PKey) -> bool:
2856-
"""
2857-
Verifies a signature on a certificate request.
2858-
2859-
:param PKey key: The public key that signature is supposedly from.
2860-
2861-
:return: ``True`` if the signature is correct.
2862-
:rtype: bool
2863-
2864-
:raises OpenSSL.crypto.Error: If the signature is invalid, or there was
2865-
a problem verifying the signature.
2866-
"""
2867-
answer = _lib.NETSCAPE_SPKI_verify(self._spki, key._pkey)
2868-
if answer <= 0:
2869-
_raise_current_error()
2870-
return True
2871-
2872-
def b64_encode(self) -> bytes:
2873-
"""
2874-
Generate a base64 encoded representation of this SPKI object.
2875-
2876-
:return: The base64 encoded string.
2877-
:rtype: :py:class:`bytes`
2878-
"""
2879-
encoded = _lib.NETSCAPE_SPKI_b64_encode(self._spki)
2880-
result = _ffi.string(encoded)
2881-
_lib.OPENSSL_free(encoded)
2882-
return result
2883-
2884-
def get_pubkey(self) -> PKey:
2885-
"""
2886-
Get the public key of this certificate.
2887-
2888-
:return: The public key.
2889-
:rtype: :py:class:`PKey`
2890-
"""
2891-
pkey = PKey.__new__(PKey)
2892-
pkey._pkey = _lib.NETSCAPE_SPKI_get_pubkey(self._spki)
2893-
_openssl_assert(pkey._pkey != _ffi.NULL)
2894-
pkey._pkey = _ffi.gc(pkey._pkey, _lib.EVP_PKEY_free)
2895-
pkey._only_public = True
2896-
return pkey
2897-
2898-
def set_pubkey(self, pkey: PKey) -> None:
2899-
"""
2900-
Set the public key of the certificate
2901-
2902-
:param pkey: The public key
2903-
:return: ``None``
2904-
"""
2905-
set_result = _lib.NETSCAPE_SPKI_set_pubkey(self._spki, pkey._pkey)
2906-
_openssl_assert(set_result == 1)
2907-
2908-
2909-
utils.deprecated(
2910-
NetscapeSPKI,
2911-
__name__,
2912-
"NetscapeSPKI support in pyOpenSSL is deprecated.",
2913-
DeprecationWarning,
2914-
name="NetscapeSPKI",
2915-
)
2916-
2917-
29182618
class _PassphraseHelper:
29192619
def __init__(
29202620
self,

0 commit comments

Comments
 (0)