Updated DB_Helper by adding firebase methods.
This commit is contained in:
parent
485cc3bbba
commit
c82121d036
1810 changed files with 537281 additions and 1 deletions
174
venv/Lib/site-packages/Crypto/IO/PEM.py
Normal file
174
venv/Lib/site-packages/Crypto/IO/PEM.py
Normal file
|
@ -0,0 +1,174 @@
|
|||
#
|
||||
# Util/PEM.py : Privacy Enhanced Mail utilities
|
||||
#
|
||||
# ===================================================================
|
||||
#
|
||||
# Copyright (c) 2014, Legrandin <helderijs@gmail.com>
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions
|
||||
# are met:
|
||||
#
|
||||
# 1. Redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer.
|
||||
# 2. Redistributions in binary form must reproduce the above copyright
|
||||
# notice, this list of conditions and the following disclaimer in
|
||||
# the documentation and/or other materials provided with the
|
||||
# distribution.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
# POSSIBILITY OF SUCH DAMAGE.
|
||||
# ===================================================================
|
||||
|
||||
"""Set of functions for encapsulating data according to the PEM format.
|
||||
|
||||
PEM (Privacy Enhanced Mail) was an IETF standard for securing emails via a
|
||||
Public Key Infrastructure. It is specified in RFC 1421-1424.
|
||||
|
||||
Even though it has been abandoned, the simple message encapsulation it defined
|
||||
is still widely used today for encoding *binary* cryptographic objects like
|
||||
keys and certificates into text.
|
||||
"""
|
||||
|
||||
__all__ = ['encode', 'decode']
|
||||
|
||||
from Crypto.Util.py3compat import b, hexlify, unhexlify, tobytes, tostr
|
||||
|
||||
import re
|
||||
from binascii import a2b_base64, b2a_base64
|
||||
|
||||
from Crypto.Hash import MD5
|
||||
from Crypto.Util.Padding import pad, unpad
|
||||
from Crypto.Cipher import DES, DES3, AES
|
||||
from Crypto.Protocol.KDF import PBKDF1
|
||||
from Crypto.Random import get_random_bytes
|
||||
|
||||
|
||||
def encode(data, marker, passphrase=None, randfunc=None):
|
||||
"""Encode a piece of binary data into PEM format.
|
||||
|
||||
:Parameters:
|
||||
data : byte string
|
||||
The piece of binary data to encode.
|
||||
marker : string
|
||||
The marker for the PEM block (e.g. "PUBLIC KEY").
|
||||
Note that there is no official master list for all allowed markers.
|
||||
Still, you can refer to the OpenSSL_ source code.
|
||||
passphrase : byte string
|
||||
If given, the PEM block will be encrypted. The key is derived from
|
||||
the passphrase.
|
||||
randfunc : callable
|
||||
Random number generation function; it accepts an integer N and returns
|
||||
a byte string of random data, N bytes long. If not given, a new one is
|
||||
instantiated.
|
||||
:Returns:
|
||||
The PEM block, as a string.
|
||||
|
||||
.. _OpenSSL: http://cvs.openssl.org/fileview?f=openssl/crypto/pem/pem.h&v=1.66.2.1.4.2
|
||||
"""
|
||||
|
||||
if randfunc is None:
|
||||
randfunc = get_random_bytes
|
||||
|
||||
out = "-----BEGIN %s-----\n" % marker
|
||||
if passphrase:
|
||||
# We only support 3DES for encryption
|
||||
salt = randfunc(8)
|
||||
key = PBKDF1(passphrase, salt, 16, 1, MD5)
|
||||
key += PBKDF1(key + passphrase, salt, 8, 1, MD5)
|
||||
objenc = DES3.new(key, DES3.MODE_CBC, salt)
|
||||
out += "Proc-Type: 4,ENCRYPTED\nDEK-Info: DES-EDE3-CBC,%s\n\n" %\
|
||||
tostr(hexlify(salt).upper())
|
||||
# Encrypt with PKCS#7 padding
|
||||
data = objenc.encrypt(pad(data, objenc.block_size))
|
||||
elif passphrase is not None:
|
||||
raise ValueError("Empty password")
|
||||
|
||||
# Each BASE64 line can take up to 64 characters (=48 bytes of data)
|
||||
# b2a_base64 adds a new line character!
|
||||
chunks = [tostr(b2a_base64(data[i:i + 48]))
|
||||
for i in range(0, len(data), 48)]
|
||||
out += "".join(chunks)
|
||||
out += "-----END %s-----" % marker
|
||||
return out
|
||||
|
||||
|
||||
def decode(pem_data, passphrase=None):
|
||||
"""Decode a PEM block into binary.
|
||||
|
||||
:Parameters:
|
||||
pem_data : string
|
||||
The PEM block.
|
||||
passphrase : byte string
|
||||
If given and the PEM block is encrypted,
|
||||
the key will be derived from the passphrase.
|
||||
:Returns:
|
||||
A tuple with the binary data, the marker string, and a boolean to
|
||||
indicate if decryption was performed.
|
||||
:Raises ValueError:
|
||||
If decoding fails, if the PEM file is encrypted and no passphrase has
|
||||
been provided or if the passphrase is incorrect.
|
||||
"""
|
||||
|
||||
# Verify Pre-Encapsulation Boundary
|
||||
r = re.compile("\s*-----BEGIN (.*)-----\s+")
|
||||
m = r.match(pem_data)
|
||||
if not m:
|
||||
raise ValueError("Not a valid PEM pre boundary")
|
||||
marker = m.group(1)
|
||||
|
||||
# Verify Post-Encapsulation Boundary
|
||||
r = re.compile("-----END (.*)-----\s*$")
|
||||
m = r.search(pem_data)
|
||||
if not m or m.group(1) != marker:
|
||||
raise ValueError("Not a valid PEM post boundary")
|
||||
|
||||
# Removes spaces and slit on lines
|
||||
lines = pem_data.replace(" ", '').split()
|
||||
|
||||
# Decrypts, if necessary
|
||||
if lines[1].startswith('Proc-Type:4,ENCRYPTED'):
|
||||
if not passphrase:
|
||||
raise ValueError("PEM is encrypted, but no passphrase available")
|
||||
DEK = lines[2].split(':')
|
||||
if len(DEK) != 2 or DEK[0] != 'DEK-Info':
|
||||
raise ValueError("PEM encryption format not supported.")
|
||||
algo, salt = DEK[1].split(',')
|
||||
salt = unhexlify(tobytes(salt))
|
||||
if algo == "DES-CBC":
|
||||
# This is EVP_BytesToKey in OpenSSL
|
||||
key = PBKDF1(passphrase, salt, 8, 1, MD5)
|
||||
objdec = DES.new(key, DES.MODE_CBC, salt)
|
||||
elif algo == "DES-EDE3-CBC":
|
||||
# Note that EVP_BytesToKey is note exactly the same as PBKDF1
|
||||
key = PBKDF1(passphrase, salt, 16, 1, MD5)
|
||||
key += PBKDF1(key + passphrase, salt, 8, 1, MD5)
|
||||
objdec = DES3.new(key, DES3.MODE_CBC, salt)
|
||||
elif algo == "AES-128-CBC":
|
||||
key = PBKDF1(passphrase, salt[:8], 16, 1, MD5)
|
||||
objdec = AES.new(key, AES.MODE_CBC, salt)
|
||||
else:
|
||||
raise ValueError("Unsupport PEM encryption algorithm (%s)." % algo)
|
||||
lines = lines[2:]
|
||||
else:
|
||||
objdec = None
|
||||
|
||||
# Decode body
|
||||
data = a2b_base64(b(''.join(lines[1:-1])))
|
||||
enc_flag = False
|
||||
if objdec:
|
||||
data = unpad(objdec.decrypt(data), objdec.block_size)
|
||||
enc_flag = True
|
||||
|
||||
return (data, marker, enc_flag)
|
261
venv/Lib/site-packages/Crypto/IO/PKCS8.py
Normal file
261
venv/Lib/site-packages/Crypto/IO/PKCS8.py
Normal file
|
@ -0,0 +1,261 @@
|
|||
#
|
||||
# PublicKey/PKCS8.py : PKCS#8 functions
|
||||
#
|
||||
# ===================================================================
|
||||
#
|
||||
# Copyright (c) 2014, Legrandin <helderijs@gmail.com>
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions
|
||||
# are met:
|
||||
#
|
||||
# 1. Redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer.
|
||||
# 2. Redistributions in binary form must reproduce the above copyright
|
||||
# notice, this list of conditions and the following disclaimer in
|
||||
# the documentation and/or other materials provided with the
|
||||
# distribution.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
# POSSIBILITY OF SUCH DAMAGE.
|
||||
# ===================================================================
|
||||
|
||||
"""
|
||||
Module for handling private keys wrapped according to `PKCS#8`_.
|
||||
|
||||
PKCS8 is a standard for storing and transferring private key information.
|
||||
The wrapped key can either be clear or encrypted.
|
||||
|
||||
All encryption algorithms are based on passphrase-based key derivation.
|
||||
The following mechanisms are fully supported:
|
||||
|
||||
* *PBKDF2WithHMAC-SHA1AndAES128-CBC*
|
||||
* *PBKDF2WithHMAC-SHA1AndAES192-CBC*
|
||||
* *PBKDF2WithHMAC-SHA1AndAES256-CBC*
|
||||
* *PBKDF2WithHMAC-SHA1AndDES-EDE3-CBC*
|
||||
* *scryptAndAES128-CBC*
|
||||
* *scryptAndAES192-CBC*
|
||||
* *scryptAndAES256-CBC*
|
||||
|
||||
The following mechanisms are only supported for importing keys.
|
||||
They are much weaker than the ones listed above, and they are provided
|
||||
for backward compatibility only:
|
||||
|
||||
* *pbeWithMD5AndRC2-CBC*
|
||||
* *pbeWithMD5AndDES-CBC*
|
||||
* *pbeWithSHA1AndRC2-CBC*
|
||||
* *pbeWithSHA1AndDES-CBC*
|
||||
|
||||
.. _`PKCS#8`: http://www.ietf.org/rfc/rfc5208.txt
|
||||
|
||||
"""
|
||||
|
||||
from Crypto.Util.py3compat import *
|
||||
|
||||
from Crypto.Util.asn1 import (
|
||||
DerNull,
|
||||
DerSequence,
|
||||
DerObjectId,
|
||||
DerOctetString,
|
||||
)
|
||||
|
||||
from Crypto.IO._PBES import PBES1, PBES2, PbesError
|
||||
|
||||
|
||||
__all__ = ['wrap', 'unwrap']
|
||||
|
||||
|
||||
def wrap(private_key, key_oid, passphrase=None, protection=None,
|
||||
prot_params=None, key_params=None, randfunc=None):
|
||||
"""Wrap a private key into a PKCS#8 blob (clear or encrypted).
|
||||
|
||||
:Parameters:
|
||||
|
||||
private_key : byte string
|
||||
The private key encoded in binary form. The actual encoding is
|
||||
algorithm specific. In most cases, it is DER.
|
||||
|
||||
key_oid : string
|
||||
The object identifier (OID) of the private key to wrap.
|
||||
It is a dotted string, like "``1.2.840.113549.1.1.1``" (for RSA keys).
|
||||
|
||||
passphrase : (binary) string
|
||||
The secret passphrase from which the wrapping key is derived.
|
||||
Set it only if encryption is required.
|
||||
|
||||
protection : string
|
||||
The identifier of the algorithm to use for securely wrapping the key.
|
||||
The default value is '``PBKDF2WithHMAC-SHA1AndDES-EDE3-CBC``'.
|
||||
|
||||
prot_params : dictionary
|
||||
Parameters for the protection algorithm.
|
||||
|
||||
+------------------+-----------------------------------------------+
|
||||
| Key | Description |
|
||||
+==================+===============================================+
|
||||
| iteration_count | The KDF algorithm is repeated several times to|
|
||||
| | slow down brute force attacks on passwords |
|
||||
| | (called *N* or CPU/memory cost in scrypt). |
|
||||
| | |
|
||||
| | The default value for PBKDF2 is 1 000. |
|
||||
| | The default value for scrypt is 16 384. |
|
||||
+------------------+-----------------------------------------------+
|
||||
| salt_size | Salt is used to thwart dictionary and rainbow |
|
||||
| | attacks on passwords. The default value is 8 |
|
||||
| | bytes. |
|
||||
+------------------+-----------------------------------------------+
|
||||
| block_size | *(scrypt only)* Memory-cost (r). The default |
|
||||
| | value is 8. |
|
||||
+------------------+-----------------------------------------------+
|
||||
| parallelization | *(scrypt only)* CPU-cost (p). The default |
|
||||
| | value is 1. |
|
||||
+------------------+-----------------------------------------------+
|
||||
|
||||
key_params : DER object
|
||||
The algorithm parameters associated to the private key.
|
||||
It is required for algorithms like DSA, but not for others like RSA.
|
||||
|
||||
randfunc : callable
|
||||
Random number generation function; it should accept a single integer
|
||||
N and return a string of random data, N bytes long.
|
||||
If not specified, a new RNG will be instantiated
|
||||
from ``Crypto.Random``.
|
||||
|
||||
:Return:
|
||||
The PKCS#8-wrapped private key (possibly encrypted),
|
||||
as a binary string.
|
||||
"""
|
||||
|
||||
if key_params is None:
|
||||
key_params = DerNull()
|
||||
|
||||
#
|
||||
# PrivateKeyInfo ::= SEQUENCE {
|
||||
# version Version,
|
||||
# privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
|
||||
# privateKey PrivateKey,
|
||||
# attributes [0] IMPLICIT Attributes OPTIONAL
|
||||
# }
|
||||
#
|
||||
pk_info = DerSequence([
|
||||
0,
|
||||
DerSequence([
|
||||
DerObjectId(key_oid),
|
||||
key_params
|
||||
]),
|
||||
DerOctetString(private_key)
|
||||
])
|
||||
pk_info_der = pk_info.encode()
|
||||
|
||||
if passphrase is None:
|
||||
return pk_info_der
|
||||
|
||||
if not passphrase:
|
||||
raise ValueError("Empty passphrase")
|
||||
|
||||
# Encryption with PBES2
|
||||
passphrase = tobytes(passphrase)
|
||||
if protection is None:
|
||||
protection = 'PBKDF2WithHMAC-SHA1AndDES-EDE3-CBC'
|
||||
return PBES2.encrypt(pk_info_der, passphrase,
|
||||
protection, prot_params, randfunc)
|
||||
|
||||
|
||||
def unwrap(p8_private_key, passphrase=None):
|
||||
"""Unwrap a private key from a PKCS#8 blob (clear or encrypted).
|
||||
|
||||
:Parameters:
|
||||
p8_private_key : byte string
|
||||
The private key wrapped into a PKCS#8 blob, DER encoded.
|
||||
passphrase : (byte) string
|
||||
The passphrase to use to decrypt the blob (if it is encrypted).
|
||||
:Return:
|
||||
A tuple containing:
|
||||
|
||||
#. the algorithm identifier of the wrapped key (OID, dotted string)
|
||||
#. the private key (byte string, DER encoded)
|
||||
#. the associated parameters (byte string, DER encoded) or ``None``
|
||||
|
||||
:Raises ValueError:
|
||||
If decoding fails
|
||||
"""
|
||||
|
||||
if passphrase:
|
||||
passphrase = tobytes(passphrase)
|
||||
|
||||
found = False
|
||||
try:
|
||||
p8_private_key = PBES1.decrypt(p8_private_key, passphrase)
|
||||
found = True
|
||||
except PbesError as e:
|
||||
error_str = "PBES1[%s]" % str(e)
|
||||
except ValueError:
|
||||
error_str = "PBES1[Invalid]"
|
||||
|
||||
if not found:
|
||||
try:
|
||||
p8_private_key = PBES2.decrypt(p8_private_key, passphrase)
|
||||
found = True
|
||||
except PbesError as e:
|
||||
error_str += ",PBES2[%s]" % str(e)
|
||||
except ValueError:
|
||||
error_str += ",PBES2[Invalid]"
|
||||
|
||||
if not found:
|
||||
raise ValueError("Error decoding PKCS#8 (%s)" % error_str)
|
||||
|
||||
pk_info = DerSequence().decode(p8_private_key, nr_elements=(2, 3, 4))
|
||||
if len(pk_info) == 2 and not passphrase:
|
||||
raise ValueError("Not a valid clear PKCS#8 structure "
|
||||
"(maybe it is encrypted?)")
|
||||
|
||||
#
|
||||
# PrivateKeyInfo ::= SEQUENCE {
|
||||
# version Version,
|
||||
# privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
|
||||
# privateKey PrivateKey,
|
||||
# attributes [0] IMPLICIT Attributes OPTIONAL
|
||||
# }
|
||||
# Version ::= INTEGER
|
||||
if pk_info[0] != 0:
|
||||
raise ValueError("Not a valid PrivateKeyInfo SEQUENCE")
|
||||
|
||||
# PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
|
||||
#
|
||||
# EncryptedPrivateKeyInfo ::= SEQUENCE {
|
||||
# encryptionAlgorithm EncryptionAlgorithmIdentifier,
|
||||
# encryptedData EncryptedData
|
||||
# }
|
||||
# EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
|
||||
|
||||
# AlgorithmIdentifier ::= SEQUENCE {
|
||||
# algorithm OBJECT IDENTIFIER,
|
||||
# parameters ANY DEFINED BY algorithm OPTIONAL
|
||||
# }
|
||||
|
||||
algo = DerSequence().decode(pk_info[1], nr_elements=(1, 2))
|
||||
algo_oid = DerObjectId().decode(algo[0]).value
|
||||
if len(algo) == 1:
|
||||
algo_params = None
|
||||
else:
|
||||
try:
|
||||
DerNull().decode(algo[1])
|
||||
algo_params = None
|
||||
except:
|
||||
algo_params = algo[1]
|
||||
|
||||
# EncryptedData ::= OCTET STRING
|
||||
private_key = DerOctetString().decode(pk_info[2]).payload
|
||||
|
||||
return (algo_oid, private_key, algo_params)
|
388
venv/Lib/site-packages/Crypto/IO/_PBES.py
Normal file
388
venv/Lib/site-packages/Crypto/IO/_PBES.py
Normal file
|
@ -0,0 +1,388 @@
|
|||
#
|
||||
# PublicKey/_PBES.py : Password-Based Encryption functions
|
||||
#
|
||||
# ===================================================================
|
||||
#
|
||||
# Copyright (c) 2014, Legrandin <helderijs@gmail.com>
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions
|
||||
# are met:
|
||||
#
|
||||
# 1. Redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer.
|
||||
# 2. Redistributions in binary form must reproduce the above copyright
|
||||
# notice, this list of conditions and the following disclaimer in
|
||||
# the documentation and/or other materials provided with the
|
||||
# distribution.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
# POSSIBILITY OF SUCH DAMAGE.
|
||||
# ===================================================================
|
||||
|
||||
from Crypto.Util.py3compat import *
|
||||
|
||||
from Crypto import Random
|
||||
from Crypto.Util.asn1 import (
|
||||
DerSequence, DerOctetString,
|
||||
DerObjectId, DerInteger,
|
||||
)
|
||||
|
||||
from Crypto.Util.Padding import pad, unpad
|
||||
from Crypto.Hash import MD5, SHA1
|
||||
from Crypto.Cipher import DES, ARC2, DES3, AES
|
||||
from Crypto.Protocol.KDF import PBKDF1, PBKDF2, scrypt
|
||||
|
||||
class PbesError(ValueError):
|
||||
pass
|
||||
|
||||
# These are the ASN.1 definitions used by the PBES1/2 logic:
|
||||
#
|
||||
# EncryptedPrivateKeyInfo ::= SEQUENCE {
|
||||
# encryptionAlgorithm EncryptionAlgorithmIdentifier,
|
||||
# encryptedData EncryptedData
|
||||
# }
|
||||
#
|
||||
# EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
|
||||
#
|
||||
# EncryptedData ::= OCTET STRING
|
||||
#
|
||||
# AlgorithmIdentifier ::= SEQUENCE {
|
||||
# algorithm OBJECT IDENTIFIER,
|
||||
# parameters ANY DEFINED BY algorithm OPTIONAL
|
||||
# }
|
||||
#
|
||||
# PBEParameter ::= SEQUENCE {
|
||||
# salt OCTET STRING (SIZE(8)),
|
||||
# iterationCount INTEGER
|
||||
# }
|
||||
#
|
||||
# PBES2-params ::= SEQUENCE {
|
||||
# keyDerivationFunc AlgorithmIdentifier {{PBES2-KDFs}},
|
||||
# encryptionScheme AlgorithmIdentifier {{PBES2-Encs}}
|
||||
# }
|
||||
#
|
||||
# PBKDF2-params ::= SEQUENCE {
|
||||
# salt CHOICE {
|
||||
# specified OCTET STRING,
|
||||
# otherSource AlgorithmIdentifier {{PBKDF2-SaltSources}}
|
||||
# },
|
||||
# iterationCount INTEGER (1..MAX),
|
||||
# keyLength INTEGER (1..MAX) OPTIONAL,
|
||||
# prf AlgorithmIdentifier {{PBKDF2-PRFs}} DEFAULT algid-hmacWithSHA1
|
||||
# }
|
||||
#
|
||||
# scrypt-params ::= SEQUENCE {
|
||||
# salt OCTET STRING,
|
||||
# costParameter INTEGER (1..MAX),
|
||||
# blockSize INTEGER (1..MAX),
|
||||
# parallelizationParameter INTEGER (1..MAX),
|
||||
# keyLength INTEGER (1..MAX) OPTIONAL
|
||||
# }
|
||||
|
||||
class PBES1(object):
|
||||
"""Deprecated encryption scheme with password-based key derivation
|
||||
(originally defined in PKCS#5 v1.5, but still present in `v2.0`__).
|
||||
|
||||
.. __: http://www.ietf.org/rfc/rfc2898.txt
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def decrypt(data, passphrase):
|
||||
"""Decrypt a piece of data using a passphrase and *PBES1*.
|
||||
|
||||
The algorithm to use is automatically detected.
|
||||
|
||||
:Parameters:
|
||||
data : byte string
|
||||
The piece of data to decrypt.
|
||||
passphrase : byte string
|
||||
The passphrase to use for decrypting the data.
|
||||
:Returns:
|
||||
The decrypted data, as a binary string.
|
||||
"""
|
||||
|
||||
enc_private_key_info = DerSequence().decode(data)
|
||||
encrypted_algorithm = DerSequence().decode(enc_private_key_info[0])
|
||||
encrypted_data = DerOctetString().decode(enc_private_key_info[1]).payload
|
||||
|
||||
pbe_oid = DerObjectId().decode(encrypted_algorithm[0]).value
|
||||
cipher_params = {}
|
||||
if pbe_oid == "1.2.840.113549.1.5.3":
|
||||
# PBE_MD5_DES_CBC
|
||||
hashmod = MD5
|
||||
ciphermod = DES
|
||||
elif pbe_oid == "1.2.840.113549.1.5.6":
|
||||
# PBE_MD5_RC2_CBC
|
||||
hashmod = MD5
|
||||
ciphermod = ARC2
|
||||
cipher_params['effective_keylen'] = 64
|
||||
elif pbe_oid == "1.2.840.113549.1.5.10":
|
||||
# PBE_SHA1_DES_CBC
|
||||
hashmod = SHA1
|
||||
ciphermod = DES
|
||||
elif pbe_oid == "1.2.840.113549.1.5.11":
|
||||
# PBE_SHA1_RC2_CBC
|
||||
hashmod = SHA1
|
||||
ciphermod = ARC2
|
||||
cipher_params['effective_keylen'] = 64
|
||||
else:
|
||||
raise PbesError("Unknown OID for PBES1")
|
||||
|
||||
pbe_params = DerSequence().decode(encrypted_algorithm[1], nr_elements=2)
|
||||
salt = DerOctetString().decode(pbe_params[0]).payload
|
||||
iterations = pbe_params[1]
|
||||
|
||||
key_iv = PBKDF1(passphrase, salt, 16, iterations, hashmod)
|
||||
key, iv = key_iv[:8], key_iv[8:]
|
||||
|
||||
cipher = ciphermod.new(key, ciphermod.MODE_CBC, iv, **cipher_params)
|
||||
pt = cipher.decrypt(encrypted_data)
|
||||
return unpad(pt, cipher.block_size)
|
||||
|
||||
|
||||
class PBES2(object):
|
||||
"""Encryption scheme with password-based key derivation
|
||||
(defined in `PKCS#5 v2.0`__).
|
||||
|
||||
.. __: http://www.ietf.org/rfc/rfc2898.txt."""
|
||||
|
||||
@staticmethod
|
||||
def encrypt(data, passphrase, protection, prot_params=None, randfunc=None):
|
||||
"""Encrypt a piece of data using a passphrase and *PBES2*.
|
||||
|
||||
:Parameters:
|
||||
data : byte string
|
||||
The piece of data to encrypt.
|
||||
passphrase : byte string
|
||||
The passphrase to use for encrypting the data.
|
||||
protection : string
|
||||
The identifier of the encryption algorithm to use.
|
||||
The default value is '``PBKDF2WithHMAC-SHA1AndDES-EDE3-CBC``'.
|
||||
prot_params : dictionary
|
||||
Parameters of the protection algorithm.
|
||||
|
||||
+------------------+-----------------------------------------------+
|
||||
| Key | Description |
|
||||
+==================+===============================================+
|
||||
| iteration_count | The KDF algorithm is repeated several times to|
|
||||
| | slow down brute force attacks on passwords |
|
||||
| | (called *N* or CPU/memory cost in scrypt). |
|
||||
| | |
|
||||
| | The default value for PBKDF2 is 1 000. |
|
||||
| | The default value for scrypt is 16 384. |
|
||||
+------------------+-----------------------------------------------+
|
||||
| salt_size | Salt is used to thwart dictionary and rainbow |
|
||||
| | attacks on passwords. The default value is 8 |
|
||||
| | bytes. |
|
||||
+------------------+-----------------------------------------------+
|
||||
| block_size | *(scrypt only)* Memory-cost (r). The default |
|
||||
| | value is 8. |
|
||||
+------------------+-----------------------------------------------+
|
||||
| parallelization | *(scrypt only)* CPU-cost (p). The default |
|
||||
| | value is 1. |
|
||||
+------------------+-----------------------------------------------+
|
||||
|
||||
|
||||
randfunc : callable
|
||||
Random number generation function; it should accept
|
||||
a single integer N and return a string of random data,
|
||||
N bytes long. If not specified, a new RNG will be
|
||||
instantiated from ``Crypto.Random``.
|
||||
|
||||
:Returns:
|
||||
The encrypted data, as a binary string.
|
||||
"""
|
||||
|
||||
if prot_params is None:
|
||||
prot_params = {}
|
||||
|
||||
if randfunc is None:
|
||||
randfunc = Random.new().read
|
||||
|
||||
if protection == 'PBKDF2WithHMAC-SHA1AndDES-EDE3-CBC':
|
||||
key_size = 24
|
||||
module = DES3
|
||||
cipher_mode = DES3.MODE_CBC
|
||||
enc_oid = "1.2.840.113549.3.7"
|
||||
elif protection in ('PBKDF2WithHMAC-SHA1AndAES128-CBC',
|
||||
'scryptAndAES128-CBC'):
|
||||
key_size = 16
|
||||
module = AES
|
||||
cipher_mode = AES.MODE_CBC
|
||||
enc_oid = "2.16.840.1.101.3.4.1.2"
|
||||
elif protection in ('PBKDF2WithHMAC-SHA1AndAES192-CBC',
|
||||
'scryptAndAES192-CBC'):
|
||||
key_size = 24
|
||||
module = AES
|
||||
cipher_mode = AES.MODE_CBC
|
||||
enc_oid = "2.16.840.1.101.3.4.1.22"
|
||||
elif protection in ('PBKDF2WithHMAC-SHA1AndAES256-CBC',
|
||||
'scryptAndAES256-CBC'):
|
||||
key_size = 32
|
||||
module = AES
|
||||
cipher_mode = AES.MODE_CBC
|
||||
enc_oid = "2.16.840.1.101.3.4.1.42"
|
||||
else:
|
||||
raise ValueError("Unknown PBES2 mode")
|
||||
|
||||
# Get random data
|
||||
iv = randfunc(module.block_size)
|
||||
salt = randfunc(prot_params.get("salt_size", 8))
|
||||
|
||||
# Derive key from password
|
||||
if protection.startswith('PBKDF2'):
|
||||
count = prot_params.get("iteration_count", 1000)
|
||||
key = PBKDF2(passphrase, salt, key_size, count)
|
||||
kdf_info = DerSequence([
|
||||
DerObjectId("1.2.840.113549.1.5.12"), # PBKDF2
|
||||
DerSequence([
|
||||
DerOctetString(salt),
|
||||
DerInteger(count)
|
||||
])
|
||||
])
|
||||
else:
|
||||
# It must be scrypt
|
||||
count = prot_params.get("iteration_count", 16384)
|
||||
scrypt_r = prot_params.get('block_size', 8)
|
||||
scrypt_p = prot_params.get('parallelization', 1)
|
||||
key = scrypt(passphrase, salt, key_size,
|
||||
count, scrypt_r, scrypt_p)
|
||||
kdf_info = DerSequence([
|
||||
DerObjectId("1.3.6.1.4.1.11591.4.11"), # scrypt
|
||||
DerSequence([
|
||||
DerOctetString(salt),
|
||||
DerInteger(count),
|
||||
DerInteger(scrypt_r),
|
||||
DerInteger(scrypt_p)
|
||||
])
|
||||
])
|
||||
|
||||
# Create cipher and use it
|
||||
cipher = module.new(key, cipher_mode, iv)
|
||||
encrypted_data = cipher.encrypt(pad(data, cipher.block_size))
|
||||
enc_info = DerSequence([
|
||||
DerObjectId(enc_oid),
|
||||
DerOctetString(iv)
|
||||
])
|
||||
|
||||
# Result
|
||||
enc_private_key_info = DerSequence([
|
||||
# encryptionAlgorithm
|
||||
DerSequence([
|
||||
DerObjectId("1.2.840.113549.1.5.13"), # PBES2
|
||||
DerSequence([
|
||||
kdf_info,
|
||||
enc_info
|
||||
]),
|
||||
]),
|
||||
DerOctetString(encrypted_data)
|
||||
])
|
||||
return enc_private_key_info.encode()
|
||||
|
||||
@staticmethod
|
||||
def decrypt(data, passphrase):
|
||||
"""Decrypt a piece of data using a passphrase and *PBES2*.
|
||||
|
||||
The algorithm to use is automatically detected.
|
||||
|
||||
:Parameters:
|
||||
data : byte string
|
||||
The piece of data to decrypt.
|
||||
passphrase : byte string
|
||||
The passphrase to use for decrypting the data.
|
||||
:Returns:
|
||||
The decrypted data, as a binary string.
|
||||
"""
|
||||
|
||||
enc_private_key_info = DerSequence().decode(data, nr_elements=2)
|
||||
enc_algo = DerSequence().decode(enc_private_key_info[0])
|
||||
encrypted_data = DerOctetString().decode(enc_private_key_info[1]).payload
|
||||
|
||||
pbe_oid = DerObjectId().decode(enc_algo[0]).value
|
||||
if pbe_oid != "1.2.840.113549.1.5.13":
|
||||
raise PbesError("Not a PBES2 object")
|
||||
|
||||
pbes2_params = DerSequence().decode(enc_algo[1], nr_elements=2)
|
||||
|
||||
### Key Derivation Function selection
|
||||
kdf_info = DerSequence().decode(pbes2_params[0], nr_elements=2)
|
||||
kdf_oid = DerObjectId().decode(kdf_info[0]).value
|
||||
|
||||
# We only support PBKDF2 or scrypt
|
||||
if kdf_oid == "1.2.840.113549.1.5.12":
|
||||
|
||||
pbkdf2_params = DerSequence().decode(kdf_info[1], nr_elements=(2, 3, 4))
|
||||
salt = DerOctetString().decode(pbkdf2_params[0]).payload
|
||||
iteration_count = pbkdf2_params[1]
|
||||
if len(pbkdf2_params) > 2:
|
||||
kdf_key_length = pbkdf2_params[2]
|
||||
else:
|
||||
kdf_key_length = None
|
||||
if len(pbkdf2_params) > 3:
|
||||
raise PbesError("Unsupported PRF for PBKDF2")
|
||||
|
||||
elif kdf_oid == "1.3.6.1.4.1.11591.4.11":
|
||||
|
||||
scrypt_params = DerSequence().decode(kdf_info[1], nr_elements=(4, 5))
|
||||
salt = DerOctetString().decode(scrypt_params[0]).payload
|
||||
iteration_count, scrypt_r, scrypt_p = [scrypt_params[x]
|
||||
for x in (1, 2, 3)]
|
||||
if len(scrypt_params) > 4:
|
||||
kdf_key_length = scrypt_params[4]
|
||||
else:
|
||||
kdf_key_length = None
|
||||
else:
|
||||
raise PbesError("Unsupported PBES2 KDF")
|
||||
|
||||
### Cipher selection
|
||||
enc_info = DerSequence().decode(pbes2_params[1])
|
||||
enc_oid = DerObjectId().decode(enc_info[0]).value
|
||||
|
||||
if enc_oid == "1.2.840.113549.3.7":
|
||||
# DES_EDE3_CBC
|
||||
ciphermod = DES3
|
||||
key_size = 24
|
||||
elif enc_oid == "2.16.840.1.101.3.4.1.2":
|
||||
# AES128_CBC
|
||||
ciphermod = AES
|
||||
key_size = 16
|
||||
elif enc_oid == "2.16.840.1.101.3.4.1.22":
|
||||
# AES192_CBC
|
||||
ciphermod = AES
|
||||
key_size = 24
|
||||
elif enc_oid == "2.16.840.1.101.3.4.1.42":
|
||||
# AES256_CBC
|
||||
ciphermod = AES
|
||||
key_size = 32
|
||||
else:
|
||||
raise PbesError("Unsupported PBES2 cipher")
|
||||
|
||||
if kdf_key_length and kdf_key_length != key_size:
|
||||
raise PbesError("Mismatch between PBES2 KDF parameters"
|
||||
" and selected cipher")
|
||||
|
||||
IV = DerOctetString().decode(enc_info[1]).payload
|
||||
|
||||
# Create cipher
|
||||
if kdf_oid == "1.2.840.113549.1.5.12": # PBKDF2
|
||||
key = PBKDF2(passphrase, salt, key_size, iteration_count)
|
||||
else:
|
||||
key = scrypt(passphrase, salt, key_size, iteration_count,
|
||||
scrypt_r, scrypt_p)
|
||||
cipher = ciphermod.new(key, ciphermod.MODE_CBC, IV)
|
||||
|
||||
# Decrypt data
|
||||
pt = cipher.decrypt(encrypted_data)
|
||||
return unpad(pt, cipher.block_size)
|
44
venv/Lib/site-packages/Crypto/IO/__init__.py
Normal file
44
venv/Lib/site-packages/Crypto/IO/__init__.py
Normal file
|
@ -0,0 +1,44 @@
|
|||
# ===================================================================
|
||||
#
|
||||
# Copyright (c) 2014, Legrandin <helderijs@gmail.com>
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions
|
||||
# are met:
|
||||
#
|
||||
# 1. Redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer.
|
||||
# 2. Redistributions in binary form must reproduce the above copyright
|
||||
# notice, this list of conditions and the following disclaimer in
|
||||
# the documentation and/or other materials provided with the
|
||||
# distribution.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
# POSSIBILITY OF SUCH DAMAGE.
|
||||
# ===================================================================
|
||||
|
||||
"""
|
||||
Modules for reading and writing cryptographic data.
|
||||
|
||||
======================== =============================================
|
||||
Module Description
|
||||
======================== =============================================
|
||||
Crypto.Util.PEM Set of functions for encapsulating data according to the PEM format.
|
||||
Crypto.Util.PKCS8 Set of functions for wrapping/unwrapping private keys.
|
||||
======================== =============================================
|
||||
|
||||
:undocumented: _PBES
|
||||
"""
|
||||
|
||||
__all__ = ['PEM', 'PKCS8']
|
BIN
venv/Lib/site-packages/Crypto/IO/__pycache__/PEM.cpython-36.pyc
Normal file
BIN
venv/Lib/site-packages/Crypto/IO/__pycache__/PEM.cpython-36.pyc
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue