Commit graph

1623 commits

Author SHA1 Message Date
Paul Kehrer
7a13085afc enforce password must be bytes when loading PEM/DER asymmetric keys (#3383)
* enforce password must be bytes when loading PEM/DER asymmetric keys

Previously we were using an ffi.buffer on the Python string, which was
allowing text implicitly, but our documentation explicitly requires
bytes.

* add changelog entry
2017-02-08 16:55:34 -05:00
Ofek Lev
0e6a129724 replace pyasn1 with asn1crypto (#3361)
* replace pyasn1 with asn1crypto

* allow trailing bytes

* fix x509 test

* update CHANGELOG.rst

* fix assert

* make asn1crypto code more idiomatic

* find tag

* final clean-up

* leave trailing byte logic unchanged

* document dependency change

* spelling

* fix spelling
2017-02-08 13:09:41 +08:00
Aviv Palivoda
6723a12712 DH serialization (#3297)
* DH keys support serialization

* Add DH serialization documentation

* Add tests for DH keys serialization in DER encoding

* update version to 1.8

* Allow only SubjectPublicKeyInfo serialization

* Remove support in TraditionalOpenSSL format

* Fix pep8

* Refactor dh serialization tests
2017-02-07 21:24:56 +08:00
Paul Kehrer
d74ba3298d add memory limit check for scrypt (#3328)
* add memory limit check for scrypt

fixes #3323

* test a pass

* move _MEM_LIMIT to the scrypt module
2017-01-05 14:11:17 -05:00
Paul Kehrer
5685baa212 add openssl_version_number & doc openssl_version_text (#3329)
* add openssl_version_number & doc openssl_version_text

fixes #3315

* more docs + actually assert on the test...

* text
2016-12-21 22:10:03 -05:00
Alex Gaynor
0e8cdf1023 Drop 1.0.0 (#3312)
* delete the 1.0.0 support

* drop the version check

* drop the AES-CTR stuff

* Update the example

* openssl truncates for us now

* delete unused test

* unused imports

* Remove a bunch of conditional bindings for NPN

* no more 1.0.0 builders

* libressl fix

* update the docs

* remove dead branches

* oops

* this is a word, damnit

* spelling

* try removing this

* this test is not needed

* unused import
2016-12-13 20:05:35 -06:00
Paul Kehrer
874445aea9 fix a regression in int_from_bytes (#3316)
* fix a regression in int_from_bytes

* add a new test file
2016-12-13 12:09:35 -05:00
Christian Heimes
2e717761c3 New osrandom_engine in C (#3229)
* New osrandom_engine in C

Inspired by Python/random.c and the old implementation.

Signed-off-by: Christian Heimes <christian@python.org>

* osrandom_engine

* Fix naming bug caused by search 'n replace mistake
* Make it easier to override osrandom auto-detection
* Add engine ctrl and backend API to get implementation from ENGINE

Signed-off-by: Christian Heimes <christian@python.org>

* Better test coverage, documentation, LICENSE

Signed-off-by: Christian Heimes <christian@python.org>

* Coverage is hard.

Signed-off-by: Christian Heimes <christian@python.org>

* * enable win32 check
* read() returns size_t

Signed-off-by: Christian Heimes <christian@python.org>

* Add macOS to spelling list. Remove dead code from header file.

Signed-off-by: Christian Heimes <christian@python.org>

* remove CCRandomGenerateBytes path and update getentropy to work on macOS

This change allows us to test all the engines in our CI:
* getentropy (tested by macOS sierra)
* getrandom (tested on several linux builders)
* /dev/urandom (tested on FreeBSD, OS X 10.11 and below, & older linux)
* CryptGenRandom (tested on windows builders)

I also fixed bugs preventing compilation in the getentropy code

* getentropy() returns int and is restricted to 256 bytes on macOS, too.

Signed-off-by: Christian Heimes <christian@python.org>

* add versionadded

* Re-add import of os module

* Fixes related to Alex's recent review.

Signed-off-by: Christian Heimes <christian@python.org>

* Add error reporting and fail for EAGAIN

Add error reporting strings for various error cases. This gives us much
nicer and understandable error messages.

SYS_getrandom() EAGAIN is now an error. Cryptography refuses to
initialize its osrandom engine when the Kernel's CPRNG hasn't been
seeded yet.

Signed-off-by: Christian Heimes <christian@python.org>
2016-12-09 10:02:26 -06:00
Paul Kehrer
0cf3690df9 friendly error if you put a date too far in the future on windows (#3279) 2016-12-05 08:12:43 -05:00
Aviv Palivoda
495f21a9c6 OpenSSL DH backend implementation [Second attempt] (#2914)
* Start of OpenSSL DH backend implementation

* Supporting DH in MultiBackend

* DHBackend has dh_parameters_supported method

* Removed DHParametersWithNumbers and DHPrivateKeyWithNumbers from documentation

* Removed ExchangeContext. exchange is a method of DHPrivateKeyWithSerialization

* PEP8 fixes

* Fixed TestDH.test_bad_tls_exchange

* Fixed generate_private_key reference in dh documentation

* test DH multibackend support

* testing DH coversion to serialized

* Validating that we receive serialized class in test_generate_dh

* Testing DH exchange symmetric key padding

* struct DH is now opaqued

* PEP8 fixes

* Testing load_dh_private_numbers throws ValueError when DH_check fails

* Using openssl_assert

* Passing keywords arguments in DH key exchange example

* test_dh::test_bad_tls_exchange now uses pre calculated parameters

* TestDH - Add test that the computed secret is equivalent to the definition by comparing with secret computed in pure python

* Add missing generator parameter to DHBackend interface docs.

* Include parameter type in DHBackend abc docs.

* Add docs for dh.generate_parameters function

* Remove the dh Numbers section, and move the DHNumbers class docs to where they are first used.

* Add note of big endian byte packing to DH exchange method.

* DH documentation updates.

Add single sentence overview with wikipedia link.
Add paragraph on assembling using Numbers objects.
Add link to backend interface docs.
First section was all indented, I think by mistake.

* Add exchange method to DHPrivateKey abstract base class.

* Small tweaks to DH documentation - remove Provider.

* Add endian to dictionary

* Use utils.int_from_bytes in test_tls_exchange_algorithm

* Removed duplicate line

* Change dh.rst exchange algorithm from doctest to code-block

The example in the Diffie-Hellman exhange algorithm is using
2048 bits key. Generating the parameters of 2048 takes long
time. This caused the automated tests to fail. In order to
pass the tests we change the example to code-block so it
will not run in the doc tests.

* Fix dh docs

* Document the generator in DHBackend relevant methods

* Fix dh tests

* use DHparams_dup

* Fix key type to unsigned char as expected by DH_compute_key

* Validate that DH generator is 2 or 5

* test dh exchange using botan vectors

* group all numbers classes

* Simplify _DHPrivateKey

* Rename test with serialized to numbers

* Move bad exchange params to external vector file

* update exchange versionadded to 1.7

* Make key_size bit accurate

* Change botan link

* Added CHANGELOG entry
2016-11-26 00:51:28 +08:00
Alex Gaynor
29b2ebc480 Error out on OpenSSL 1.0.0 by default (#3276)
* Error out on OpenSSL 1.0.0 by default

* what the heck
2016-11-22 22:25:17 +08:00
Paul Kehrer
191e6e90bf error if private_value is <= 0 in ec.derive_private_key (#3273) 2016-11-21 21:49:42 -05:00
Paul Kehrer
80ec631601 add support for prehashing in ECDSA sign/verify (#3267)
* add support for prehashing in ECDSA sign/verify

* move signature_algorithm check to its own function
2016-11-20 11:20:52 -05:00
Paul Kehrer
6012ccff0d support prehashed sign/verify in DSA (#3266) 2016-11-20 10:45:06 -05:00
Paul Kehrer
f555c74d54 support RSA verify with prehashing (#3265)
* support RSA verify with prehashing

* review feedback

* more dedupe

* refactor and move to a separate module
2016-11-20 09:48:10 -05:00
Paul Kehrer
033bd7167d support prehashing in RSA sign (#3238)
* support prehashing in RSA sign

* check to make sure digest size matches prehashed data provided

* move doctest for prehashed
2016-11-20 08:13:23 -05:00
Paul Kehrer
533a3c909d change derive_elliptic_curve_public_point to return EllipticCurvePubl… (#3243)
* change derive_elliptic_curve_public_point to return EllipticCurvePublicKey

* also rename the backend interface method

* review feedback

* Rename to derive_elliptic_curve_private_key
* Returns EllipticCurvePrivateKey
* Reuses the EC_POINT in the openssl impl
* Rename "secret" arg to "private_value" which is consistent with our
  naming for the value in ECPrivateNumbers.
2016-11-18 20:49:10 -05:00
Terry Chia
e9b87d5de4 Raise padding block_size limit to what is allowed by the specs. (#3108)
* Raize padding block_size limit to what is allowed by the specs.

* Add tests for raising padding limits.

* Amend C code for padding check to use uint16_t instead of uint8_t.

* Fix test to work in Python 3.

* Fix typo.

* Fix another typo.

* Fix return type of the padding checks.

* Change hypothesis test on padding.

* Update comment.
2016-11-15 09:56:02 +08:00
Paul Kehrer
9977786b56 workaround for application bundling tools (#3235)
* cx_freeze support for default_backend

* updated tabing to spaces

* corrected spacing

* moved finding backend to backends __init__

* update to check to see if sys is frozen

* corrected pep8 issues

* update based on comments

* changes to simplify, support testing, and improve comments

* add changelog entry

* right, coverage. I remember now. Time for some contortions.

* updated with review feedback
2016-11-14 18:34:55 -05:00
Paul Kehrer
3a15b03e92 Add a bytes method to get the DER ASN.1 encoding of an X509 name. (#3236)
* Add a bytes method to get the DER ASN.1 encoding of an X509 name.

This is useful for creating an OpenSSL style subject_name_hash (#3011)

* add to backend interface and update multibackend

* bytes -> public_bytes
2016-11-13 17:30:11 -05:00
Alex Gaynor
d862933de5 C locking callback (#3226)
* Remove Python OpenSSL locking callback and replace it with one in C

The Python OpenSSL locking callback is unsafe; if GC is triggered
during the callback's invocation, it can result in the callback being
invoked reentrantly, which can lead to deadlocks. This patch replaces
it with one in C that gets built at compile time via cffi along with
the rest of the OpenSSL binding.

* fixes for some issues

* unused

* revert these changes

* these two for good measure

* missing param

* sigh, syntax

* delete tests that assumed an ability to mess with locks

* style fixes

* licensing stuff

* utf8

* Unicode. Huh. What it isn't good for, absolutely nothing.
2016-11-13 12:55:22 -08:00
Paul Kehrer
f6f238e4c0 add alternate signature OID for RSA with SHA1 + test and vector (#3227)
* add alternate signature OID for RSA with SHA1 + test and vector

* mozilla is a proper noun leave me alone spellchecker
2016-11-11 13:41:31 -05:00
Ofek Lev
c816735f0e add ec.private_key_from_secret_and_curve (#3225)
* finish https://github.com/pyca/cryptography/pull/1973

* change API & add test

Function will now return an instance of EllipticCurvePrivateKey, as that
is the users' ultimate goal anyway.

* fix test

* improve coverage

* complete coverage

* final fix

* centos fix

* retry

* cleanup asserts

* use openssl_assert

* skip unsupported platforms

* change API name to derive_private_key

* change version added

* improve description of `secret` param

* separate successful and failure test cases

* simplify successful case

* add docs for derive_elliptic_curve_public_point

* add period
2016-11-11 07:54:00 -08:00
Fraser Tweedale
01ee6f5e39 Name: add support for multi-value RDNs (#3202)
Update the Name class to accept and internally store a list of
RelativeDistinguishedName objects.  Add the 'rdns' attribute to give
access to the RDNs.  Update ASN.1 routines to correctly decode and
encode multi-value RDNs.

Fixes: https://github.com/pyca/cryptography/issues/3199
2016-11-11 07:28:56 -08:00
Fraser Tweedale
02467dda61 Make DistributionPoint relative_name a set of NameAttribute (#3210)
* Add RelativeDistinguishedName class

* Make relative_name a RelativeDistinguishedName

DistributionPoint relative_name is currently a Name but RFC 5280
defines it as RelativeDistinguishedName, i.e. a non-empty SET OF
name attributes.  Change the DistributionPoint relative_name
attribute to be a RelativeDistinguishedName.
2016-11-07 13:54:04 +08:00
Alex Gaynor
b924696b2e Fixes #3211 -- fixed hkdf's output with short length (#3215) 2016-11-06 10:08:21 +08:00
Paul Kehrer
3feeec87b3 support encoding IPv4Network and IPv6Network, useful for NameConstraints (#3182)
* support encoding IPv4Network and IPv6Network, useful for NameConstraints

* add changelog entry

* add more networks with full and no masking (/32, /128, /0)

* parametrize the nc tests to fix coverage
2016-10-01 08:12:27 -04:00
Paul Kehrer
9e66d10732 reduce a bit of duplication in x509 tests (#3183) 2016-09-29 22:59:33 -04:00
Paul Kehrer
8b8d51b752 1.0.2i changed the way COMP_METHOD is exported if NO_COMP is set (#3162)
* 1.0.2i changed the way COMP_METHOD is exported if NO_COMP is set

* add a comment explaining why we changed this

* 1.0.2i handles NUMERICSTRING properly now so need only test < 1.0.2i

* needs to be visible
2016-09-22 11:22:54 -04:00
Paul Kehrer
eba7171234 fix warnings in cffi 1.8.3 due to wrong buffer types (#3155) 2016-09-21 20:59:43 -04:00
Alex Gaynor
adaf3b0021 make this test assert the right thing. (#3133)
right now it just always skips
2016-09-03 12:04:26 -05:00
Paul Kehrer
8b89bcc5b9 support random_serial_number in the CertificateBuilder (#3132)
* support random_serial_number in the CertificateBuilder

* turns out pytest's monkeypatch has an undo

* random_serial_number now a function

* just certs
2016-09-03 12:31:43 -04:00
Terry Chia
a2d0da9bcd Add bounds checking for Scrypt parameters. (#3130)
* Add bounds checking for Scrypt parameters.

* Pep8.

* More PEP8.

* Change wording.
2016-09-02 19:57:45 -04:00
Terry Chia
d8a27df32b Scrypt Implementation (#3117)
* Scrypt implementation.

* Docs stuff.

* Make example just an example and not a doctest.

* Add changelog entry.

* Docs cleanup.

* Add more tests.

* Add multibackend tests.

* PEP8.

* Add docs about Scrypt parameters.

* Docs cleanup.

* Add AlreadyFinalized.
2016-09-01 10:39:57 -05:00
Paul Kehrer
c7b29b86cd add support for signature_algorithm_oid to cert, CSR, and CRL (#3124)
* add support for signature_algorithm_oid to cert, CSR, and CRL

* refactor _SIG_OIDS_TO_HASH to use ObjectIdentifiers and use that
2016-08-31 21:17:21 -04:00
Paul Kehrer
21353a4673 fix an overindented line. not sure why our linters didn't catch this (#3123) 2016-08-30 09:09:15 -04:00
Terry Chia
3c35ef1ea7 Add a register_interface_if decorator. (#3120)
* Add a register_interface_if decorator.

* Add tests.

* PEP 8.
2016-08-29 08:21:28 -04:00
Paul Kehrer
306ce512a2 blake2b/blake2s support (#3116)
* blake2b/blake2s support

Doesn't support keying, personalization, salting, or tree hashes so
the API is pretty simple right now.

* implement digest_size via utils.read_only_property

* un-keyed for spelling's sake

* test copying + digest_size checks

* unkeyed is too a word

* line wrap

* reword the docs

* use the evp algorithm name in the error

This will make BLAKE2 alternate digest size errors a bit less confusing

* add changelog entry and docs about supported digest_size
2016-08-28 21:36:09 -04:00
Paul Kehrer
07ea3cd3ea OpenSSL 1.1.0 support (#2826)
* make pre5 work

* add a blank line to make the diff happier

* 1.1.0-pre6 working

* support the changes since 1.1.0-pre6

* fixes

* add 1.1.0 to travis

* expose the symbol

* better testing for numericstring

* handle libre...

* actually use the 1.1.0 we compile

* cache the ossl-110 dir on travis

* add some newlines

* changelog entry for 1.1.0 support

* note that we test on 1.1.0

* proper skip on this test

* reorder
2016-08-26 09:48:24 -04:00
Marti
40f1999de7 Allow passing iterators where collections are expected (#3078)
Iterators can only be enumerated once, breaking code like this in
Python 3 for example:

    san = SubjectAlternativeName(map(DNSName, lst))

This is also a slight behavior change if the caller modifies the list
after passing it to the constructor, because input lists are now copied.
Which seems like a good thing.

Also:
* Name now checks that attributes elements are of type NameAttribute
* NoticeReference now allows notice_numbers to be any iterable
2016-08-26 09:26:31 +08:00
InvalidInterrupt
8e66ca6813 CertificateBuilder accepts aware datetimes for not_valid_after and not_valid_before (#2920)
* CertificateBuilder accepts aware datetimes for not_valid_after and not_valid_before
These functions now accept aware datetimes and convert them to UTC

* Added pytz to test requirements

* Correct pep8 error and improve Changelog wording

* Improve tests and clarify changelog message

* Trim Changelog line length

* Allow RevokedCertificateBuilder and CertificateRevocationListBuilder to accept aware datetimes

* Fix accidental changelog entry
2016-08-16 22:39:31 -04:00
Коренберг Марк
9e75830ad1 Disallow X509 certificate serial numbers bigger than 159 bits (#3064) (#3067) 2016-08-02 09:08:21 +08:00
Marti
4739cfca29 Add code style settings, new excludes, run 'test_x509_ext (Py3)' (#3041)
Fix DNSName wildcard encoding for NameConstraints

Previously '.example.com' would get normalised to 'example.com', making
it impossible to add wildcard NameConstraints.
2016-08-02 09:03:25 +08:00
Dirkjan Ochtman
0edf7993c6 Enforce that p > q to improve OpenSSL compatibility (fixes #2990) (#3010) 2016-07-19 14:12:59 -05:00
Terry Chia
69617caca7 Add flag to toggle key length check for HOTP and TOTP. (#3012)
* Add an enforce_key_length parameter to HOTP and TOTP.

* Document changes in docs.

* Add some words to the wordlist.

* Add versionadded to docs.
2016-07-16 08:22:32 -06:00
Alex Gaynor
3e3444fa96 Use a series of constants for OpenSSL version checks (#3037)
* Use a series of constants for OpenSSL version checks.

N.B. I removed several qualifiers that were being used to express beta vs. release in OpenSSL version numbers. Reviewers please look closely!

* Convert some python as well, also add the file

* flake8

* Simplify code, remove functionality that can be expressed more simply

* clean up the tests as well

* more constants

* wrap long lines

* reflect feedback

* unused

* add this back?
2016-07-11 21:03:13 +00:00
Paul Kehrer
dbb64bd2a4 disable blowfish in commoncrypto backend for key lengths under 64-bit (#3040)
This is due to a bug in CommonCrypto present in 10.11.x. Filed as
radar://26636600
2016-07-10 22:13:40 -04:00
Aviv Palivoda
2120a8e090 One shot sign/verification ECDSA (#3029)
* Add sign and verify methods to ECDSA

* Documented ECDSA sign/verify methods

* Added CHANGELOG entry

* Skipping test verify and sign if curve is not supported

* Fixed typo in documentation return type

* Removed provider language from EllipticCurvePrivateKey and EllipticCurvePublicKey
2016-07-02 12:43:06 -04:00
Aviv Palivoda
f67429b9d1 One shot sign/verify DSA (#3003)
* Add sign and verify methods to DSA

* Documented DSA sign/verify methods

* Added CHANGELOG entry
2016-06-30 13:42:46 -05:00
Alex Gaynor
17097bf957 Fixed #3008 -- expose calculate max pss salt length (#3014)
* Fixed #3008 -- expose calculate max pss salt length

* Fixed a few mistakes in the docs

* move all the code around

* oops

* write a unit test

* versionadded + changelog
2016-06-27 21:29:38 -05:00