Python OpenSSL.crypto.TYPE_DSA Examples

The following are 15 code examples of OpenSSL.crypto.TYPE_DSA(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module OpenSSL.crypto , or try the search function .
Example #1
Source File: test_crypto.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_failedGeneration(self):
        """
        :py:meth:`PKeyType.generate_key` takes two arguments, the first giving the key
        type as one of :py:data:`TYPE_RSA` or :py:data:`TYPE_DSA` and the second giving the
        number of bits to generate.  If an invalid type is specified or
        generation fails, :py:exc:`Error` is raised.  If an invalid number of bits is
        specified, :py:exc:`ValueError` or :py:exc:`Error` is raised.
        """
        key = PKey()
        self.assertRaises(TypeError, key.generate_key)
        self.assertRaises(TypeError, key.generate_key, 1, 2, 3)
        self.assertRaises(TypeError, key.generate_key, "foo", "bar")
        self.assertRaises(Error, key.generate_key, -1, 0)

        self.assertRaises(ValueError, key.generate_key, TYPE_RSA, -1)
        self.assertRaises(ValueError, key.generate_key, TYPE_RSA, 0)

        # XXX RSA generation for small values of bits is fairly buggy in a wide
        # range of OpenSSL versions.  I need to figure out what the safe lower
        # bound for a reasonable number of OpenSSL versions is and explicitly
        # check for that in the wrapper.  The failure behavior is typically an
        # infinite loop inside OpenSSL.

        # self.assertRaises(Error, key.generate_key, TYPE_RSA, 2)

        # XXX DSA generation seems happy with any number of bits.  The DSS
        # says bits must be between 512 and 1024 inclusive.  OpenSSL's DSA
        # generator doesn't seem to care about the upper limit at all.  For
        # the lower limit, it uses 512 if anything smaller is specified.
        # So, it doesn't seem possible to make generate_key fail for
        # TYPE_DSA with a bits argument which is at least an int.

        # self.assertRaises(Error, key.generate_key, TYPE_DSA, -7) 
Example #2
Source File: test_crypto.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_dsaGeneration(self):
        """
        :py:meth:`PKeyType.generate_key` generates a DSA key when passed
        :py:data:`TYPE_DSA` as a type and a reasonable number of bits.
        """
        # 512 is a magic number.  The DSS (Digital Signature Standard)
        # allows a minimum of 512 bits for DSA.  DSA_generate_parameters
        # will silently promote any value below 512 to 512.
        bits = 512
        key = PKey()
        key.generate_key(TYPE_DSA, bits)
        # self.assertEqual(key.type(), TYPE_DSA)
        # self.assertEqual(key.bits(), bits)
        # self.assertRaises(TypeError, key.check) 
Example #3
Source File: test_crypto.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_regeneration(self):
        """
        :py:meth:`PKeyType.generate_key` can be called multiple times on the same
        key to generate new keys.
        """
        key = PKey()
        for type, bits in [(TYPE_RSA, 512), (TYPE_DSA, 576)]:
             key.generate_key(type, bits)
             self.assertEqual(key.type(), type)
             self.assertEqual(key.bits(), bits) 
Example #4
Source File: _sslverify.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def inspect(self):
        t = self.original.type()
        if t == crypto.TYPE_RSA:
            ts = 'RSA'
        elif t == crypto.TYPE_DSA:
            ts = 'DSA'
        else:
            ts = '(Unknown Type!)'
        L = (self.original.bits(), ts, self.keyHash())
        return '%s-bit %s Key Pair with Hash: %s' % L 
Example #5
Source File: certs.py    From pycopia with Apache License 2.0 5 votes vote down vote up
def create_dsa_keypair(bits=2048):
    pkey = crypto.PKey()
    pkey.generate_key(crypto.TYPE_DSA, bits)
    return PrivateKey(_key=pkey) 
Example #6
Source File: _sslverify.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def inspect(self):
        t = self.original.type()
        if t == crypto.TYPE_RSA:
            ts = 'RSA'
        elif t == crypto.TYPE_DSA:
            ts = 'DSA'
        else:
            ts = '(Unknown Type!)'
        L = (self.original.bits(), ts, self.keyHash())
        return '%s-bit %s Key Pair with Hash: %s' % L 
Example #7
Source File: _sslverify.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def inspect(self):
        t = self.original.type()
        if t == crypto.TYPE_RSA:
            ts = 'RSA'
        elif t == crypto.TYPE_DSA:
            ts = 'DSA'
        else:
            ts = '(Unknown Type!)'
        L = (self.original.bits(), ts, self.keyHash())
        return '%s-bit %s Key Pair with Hash: %s' % L 
Example #8
Source File: thread-key-gen.py    From pyopenssl with Apache License 2.0 5 votes vote down vote up
def generate_dsa():
    keys = []
    for i in range(100):
        key = PKey()
        key.generate_key(TYPE_DSA, 512)
        keys.append(key) 
Example #9
Source File: test_crypto.py    From pyopenssl with Apache License 2.0 5 votes vote down vote up
def test_failed_generation(self):
        """
        `PKey.generate_key` takes two arguments, the first giving the key type
        as one of `TYPE_RSA` or `TYPE_DSA` and the second giving the number of
        bits to generate.  If an invalid type is specified or generation fails,
        `Error` is raised.  If an invalid number of bits is specified,
        `ValueError` or `Error` is raised.
        """
        key = PKey()
        with pytest.raises(TypeError):
            key.generate_key("foo", "bar")
        with pytest.raises(Error):
            key.generate_key(-1, 0)

        with pytest.raises(ValueError):
            key.generate_key(TYPE_RSA, -1)
        with pytest.raises(ValueError):
            key.generate_key(TYPE_RSA, 0)

        with pytest.raises(TypeError):
            key.generate_key(TYPE_RSA, object())

        # XXX RSA generation for small values of bits is fairly buggy in a wide
        # range of OpenSSL versions.  I need to figure out what the safe lower
        # bound for a reasonable number of OpenSSL versions is and explicitly
        # check for that in the wrapper.  The failure behavior is typically an
        # infinite loop inside OpenSSL.

        # with pytest.raises(Error):
        #     key.generate_key(TYPE_RSA, 2)

        # XXX DSA generation seems happy with any number of bits.  The DSS
        # says bits must be between 512 and 1024 inclusive.  OpenSSL's DSA
        # generator doesn't seem to care about the upper limit at all.  For
        # the lower limit, it uses 512 if anything smaller is specified.
        # So, it doesn't seem possible to make generate_key fail for
        # TYPE_DSA with a bits argument which is at least an int.

        # with pytest.raises(Error):
        #     key.generate_key(TYPE_DSA, -7) 
Example #10
Source File: test_crypto.py    From pyopenssl with Apache License 2.0 5 votes vote down vote up
def test_dsa_generation(self):
        """
        `PKey.generate_key` generates a DSA key when passed `TYPE_DSA` as a
        type and a reasonable number of bits.
        """
        # 512 is a magic number.  The DSS (Digital Signature Standard)
        # allows a minimum of 512 bits for DSA.  DSA_generate_parameters
        # will silently promote any value below 512 to 512.
        bits = 512
        key = PKey()
        key.generate_key(TYPE_DSA, bits)
        assert key.type() == TYPE_DSA
        assert key.bits() == bits
        with pytest.raises(TypeError):
            key.check() 
Example #11
Source File: test_crypto.py    From pyopenssl with Apache License 2.0 5 votes vote down vote up
def test_regeneration(self):
        """
        `PKey.generate_key` can be called multiple times on the same key to
        generate new keys.
        """
        key = PKey()
        for type, bits in [(TYPE_RSA, 512), (TYPE_DSA, 576)]:
            key.generate_key(type, bits)
            assert key.type() == type
            assert key.bits() == bits 
Example #12
Source File: test_crypto.py    From pyopenssl with Apache License 2.0 5 votes vote down vote up
def test_dump_privatekey_not_rsa_key(self):
        """
        `dump_privatekey` raises `TypeError` if called with a key that is
        not RSA.
        """
        key = PKey()
        key.generate_key(TYPE_DSA, 512)
        with pytest.raises(TypeError):
            dump_privatekey(FILETYPE_TEXT, key) 
Example #13
Source File: certgen.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def createKeyPair(type, bits):
    """
    Create a public/private key pair.
    Arguments: type - Key type, must be one of TYPE_RSA and TYPE_DSA
               bits - Number of bits to use in the key
    Returns:   The public/private key pair in a PKey object
    """
    pkey = crypto.PKey()
    pkey.generate_key(type, bits)
    return pkey 
Example #14
Source File: certificate.py    From lecm with Apache License 2.0 5 votes vote down vote up
def _create_account_key(self):
        account_key = crypto.PKey()

        if self.type == 'RSA':
            crypto_type = crypto.TYPE_RSA
        else:
            crypto_type = crypto.TYPE_DSA

        try:
            LOG.info('[global] Generating account key: %s \
                     (type: %s, size: %s)' %
                     (self.account_key_name, self.type, self.size))
            account_key.generate_key(crypto_type, self.size)
        except (TypeError, ValueError):
            raise

        try:
            LOG.debug('[global] Writting account key: %s/private/%s' %
                      (self.path, self.account_key_name))
            accountkey_file = os.open('%s/private/%s' %
                                      (self.path, self.account_key_name),
                                      os.O_WRONLY | os.O_CREAT | os.O_TRUNC,
                                      0o600)
            os.write(accountkey_file,
                     crypto.dump_privatekey(crypto.FILETYPE_PEM, account_key))
            os.close(accountkey_file)
        except IOError:
            try:
                os.remove('%s/private/%s.key' %
                          (self.path, self.account_key_name))
            except OSError:
                pass
            raise 
Example #15
Source File: certificate.py    From lecm with Apache License 2.0 5 votes vote down vote up
def _create_private_key(self):
        private_key = crypto.PKey()

        if self.type == 'RSA':
            crypto_type = crypto.TYPE_RSA
        else:
            crypto_type = crypto.TYPE_DSA

        try:
            LOG.info('[%s] Generating private key (type: %s, size: %s)' %
                     (self.name, self.type, self.size))
            private_key.generate_key(crypto_type, self.size)
        except (TypeError, ValueError):
            raise

        try:
            LOG.debug('[%s] Writting private key: %s/private/%s.key' %
                      (self.name, self.path, self.name))
            privatekey_file = os.open('%s/private/%s.key' %
                                      (self.path, self.name),
                                      os.O_WRONLY | os.O_CREAT | os.O_TRUNC,
                                      0o600)
            os.write(privatekey_file,
                     crypto.dump_privatekey(crypto.FILETYPE_PEM, private_key))
            os.close(privatekey_file)
        except IOError:
            try:
                os.remove('%s/private/%s.key' % (self.path, self.name))
            except OSError:
                pass
            raise