def setUp(self): from josepy.jwk import JWKRSA self.jwk256 = JWKRSA(key=RSA256_KEY.public_key()) self.jwk256json = { 'kty': 'RSA', 'e': 'AQAB', 'n': 'm2Fylv-Uz7trgTW8EBHP3FQSMeZs2GNQ6VRo1sIVJEk', } # pylint: disable=protected-access self.jwk256_not_comparable = JWKRSA( key=RSA256_KEY.public_key()._wrapped) self.jwk512 = JWKRSA(key=RSA512_KEY.public_key()) self.jwk512json = { 'kty': 'RSA', 'e': 'AQAB', 'n': 'rHVztFHtH92ucFJD_N_HW9AsdRsUuHUBBBDlHwNlRd3fp5' '80rv2-6QWE30cWgdmJS86ObRz6lUTor4R0T-3C5Q', } self.private = JWKRSA(key=RSA256_KEY) self.private_json_small = self.jwk256json.copy() self.private_json_small['d'] = ( 'lPQED_EPTV0UIBfNI3KP2d9Jlrc2mrMllmf946bu-CE') self.private_json = self.jwk256json.copy() self.private_json.update({ 'd': 'lPQED_EPTV0UIBfNI3KP2d9Jlrc2mrMllmf946bu-CE', 'p': 'zUVNZn4lLLBD1R6NE8TKNQ', 'q': 'wcfKfc7kl5jfqXArCRSURQ', 'dp': 'CWJFq43QvT5Bm5iN8n1okQ', 'dq': 'bHh2u7etM8LKKCF2pY2UdQ', 'qi': 'oi45cEkbVoJjAbnQpFY87Q', }) self.jwk = self.private
def maybe_key(pem_path): acme_key_file = pem_path.child(u'client.key') if acme_key_file.exists(): key = serialization.load_pem_private_key(acme_key_file.getContent(), password=None, backend=default_backend()) else: key = generate_private_key(u'rsa') acme_key_file.setContent( key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.TraditionalOpenSSL, encryption_algorithm=serialization.NoEncryption())) acme_key = JWKRSA(key=key) return acme_key
def test_create_key(self): """ `~txacme.endpoint.load_or_create_client_key` creates a new key if one does not exist. """ tempdir = self.useFixture(TempDir()).path temp_path = FilePath(tempdir) key_path = temp_path.child('client.key') self.assertThat(key_path.isfile(), Equals(False)) self.assertThat( load_or_create_client_key(temp_path), Equals(JWKRSA(key=load_pem_private_key( key_path.getContent(), password=None, backend=default_backend()))))
def test_thumbprint_go_jose(self): # https://github.com/square/go-jose/blob/4ddd71883fa547d37fbf598071f04512d8bafee3/jwk.go#L155 # https://github.com/square/go-jose/blob/4ddd71883fa547d37fbf598071f04512d8bafee3/jwk_test.go#L331-L344 # https://github.com/square/go-jose/blob/4ddd71883fa547d37fbf598071f04512d8bafee3/jwk_test.go#L384 from josepy.jwk import JWKRSA key = JWKRSA.json_loads("""{ "kty": "RSA", "kid": "*****@*****.**", "use": "sig", "n": "n4EPtAOCc9AlkeQHPzHStgAbgs7bTZLwUBZdR8_KuKPEHLd4rHVTeT-O-XV2jRojdNhxJWTDvNd7nqQ0VEiZQHz_AJmSCpMaJMRBSFKrKb2wqVwGU_NsYOYL-QtiWN2lbzcEe6XC0dApr5ydQLrHqkHHig3RBordaZ6Aj-oBHqFEHYpPe7Tpe-OfVfHd1E6cS6M1FZcD1NNLYD5lFHpPI9bTwJlsde3uhGqC0ZCuEHg8lhzwOHrtIQbS0FVbb9k3-tVTU4fg_3L_vniUFAKwuCLqKnS2BYwdq_mzSnbLY7h_qixoR7jig3__kRhuaxwUkRz5iaiQkqgc5gHdrNP5zw", "e": "AQAB" }""") # noqa self.assertEqual( binascii.hexlify(key.thumbprint()), b"f63838e96077ad1fc01c3f8405774dedc0641f558ebb4b40dccf5f9b6d66a932")
def get_things_done(): """ Here is where the service part is setup and action is done. """ responders = yield start_responders() store = MemoryStore() # We first validate the directory. account_key = _get_account_key() try: client = yield Client.from_url( reactor, URL.fromText(acme_url.decode('utf-8')), key=JWKRSA(key=account_key), alg=RS256, ) except Exception as error: print('\n\nFailed to connect to ACME directory. %s' % (error, )) yield reactor.stop() defer.returnValue(None) service = AcmeIssuingService( email='[email protected],[email protected]', cert_store=store, client=client, clock=reactor, responders=responders, panic=on_panic, ) # Start the service and wait for it to start. yield service.start() # Wait for the existing certificate from the storage to be available. yield service.when_certs_valid() # Request a SAN ... if passed via command line. yield service.issue_cert(','.join(requested_domains)) yield service.stopService() print('That was all the example.')
def load_or_create_client_key(pem_path): """ Load the client key from a directory, creating it if it does not exist. .. note:: The client key that will be created will be a 2048-bit RSA key. :type pem_path: ``twisted.python.filepath.FilePath`` :param pem_path: The certificate directory to use, as with the endpoint. """ acme_key_file = pem_path.asTextMode().child(u'client.key') if acme_key_file.exists(): key = serialization.load_pem_private_key(acme_key_file.getContent(), password=None, backend=default_backend()) else: key = generate_private_key(u'rsa') acme_key_file.setContent( key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.TraditionalOpenSSL, encryption_algorithm=serialization.NoEncryption())) return JWKRSA(key=key)
"addff2ec7564c6b64bc670d250b6f24b0b8db6b2810099813b7e7658cecf5c39", 16), dmq1=int("463ae9c6b77aedcac1397781e50e4afc060d4b216dc2778494ebe42a6850c81", 16), iqmp=int( "54deef8548f65cad1d411527a32dcb8e712d3e128e4e0ff118663fae82a758f4", 16), public_numbers=rsa.RSAPublicNumbers( e=65537, n=int( "ae5411f963c50e3267fafcf76381c8b1e5f7b741fdb2a544bcf48bd607b10c991" "90caeb8011dc22cf83d921da55ec32bd05cac3ee02ca5e1dbef93952850b525", 16), )).private_key(default_backend()) RSA_KEY_512 = JWKRSA(key=RSA_KEY_512_RAW) class RequestSequence(treq_RequestSequence): @contextmanager def consume(self, sync_failure_reporter): yield if not self.consumed(): sync_failure_reporter("\n".join( ["Not all expected requests were made. Still expecting:"] + ["- {0!r})".format(e) for e, _ in self._sequence])) def __call__(self, method, url, params, headers, data): """ :return: the next response in the sequence, provided that the parameters match the next in the sequence.
def _test_create_client(self): with start_action(action_type=u'integration:create_client').context(): self.key = JWKRSA(key=generate_private_key('rsa')) return ( DeferredContext(self._create_client(self.key)) .addActionFinish())
class JWKRSATest(unittest.TestCase, JWKTestBaseMixin): """Tests for josepy.jwk.JWKRSA.""" # pylint: disable=too-many-instance-attributes thumbprint = (b'\x83K\xdc#3\x98\xca\x98\xed\xcb\x80\x80<\x0c' b'\xf0\x95\xb9H\xb2*l\xbd$\xe5&|O\x91\xd4 \xb0Y') def setUp(self): from josepy.jwk import JWKRSA self.jwk256 = JWKRSA(key=RSA256_KEY.public_key()) self.jwk256json = { 'kty': 'RSA', 'e': 'AQAB', 'n': 'm2Fylv-Uz7trgTW8EBHP3FQSMeZs2GNQ6VRo1sIVJEk', } # pylint: disable=protected-access self.jwk256_not_comparable = JWKRSA( key=RSA256_KEY.public_key()._wrapped) self.jwk512 = JWKRSA(key=RSA512_KEY.public_key()) self.jwk512json = { 'kty': 'RSA', 'e': 'AQAB', 'n': 'rHVztFHtH92ucFJD_N_HW9AsdRsUuHUBBBDlHwNlRd3fp5' '80rv2-6QWE30cWgdmJS86ObRz6lUTor4R0T-3C5Q', } self.private = JWKRSA(key=RSA256_KEY) self.private_json_small = self.jwk256json.copy() self.private_json_small['d'] = ( 'lPQED_EPTV0UIBfNI3KP2d9Jlrc2mrMllmf946bu-CE') self.private_json = self.jwk256json.copy() self.private_json.update({ 'd': 'lPQED_EPTV0UIBfNI3KP2d9Jlrc2mrMllmf946bu-CE', 'p': 'zUVNZn4lLLBD1R6NE8TKNQ', 'q': 'wcfKfc7kl5jfqXArCRSURQ', 'dp': 'CWJFq43QvT5Bm5iN8n1okQ', 'dq': 'bHh2u7etM8LKKCF2pY2UdQ', 'qi': 'oi45cEkbVoJjAbnQpFY87Q', }) self.jwk = self.private def test_init_auto_comparable(self): self.assertIsInstance(self.jwk256_not_comparable.key, util.ComparableRSAKey) self.assertEqual(self.jwk256, self.jwk256_not_comparable) def test_encode_param_zero(self): from josepy.jwk import JWKRSA # pylint: disable=protected-access # TODO: move encode/decode _param to separate class self.assertEqual('AA', JWKRSA._encode_param(0)) def test_equals(self): self.assertEqual(self.jwk256, self.jwk256) self.assertEqual(self.jwk512, self.jwk512) def test_not_equals(self): self.assertNotEqual(self.jwk256, self.jwk512) self.assertNotEqual(self.jwk512, self.jwk256) def test_load(self): from josepy.jwk import JWKRSA self.assertEqual(self.private, JWKRSA.load(test_util.load_vector('rsa256_key.pem'))) def test_public_key(self): self.assertEqual(self.jwk256, self.private.public_key()) def test_to_partial_json(self): self.assertEqual(self.jwk256.to_partial_json(), self.jwk256json) self.assertEqual(self.jwk512.to_partial_json(), self.jwk512json) self.assertEqual(self.private.to_partial_json(), self.private_json) def test_from_json(self): from josepy.jwk import JWK self.assertEqual(self.jwk256, JWK.from_json(self.jwk256json)) self.assertEqual(self.jwk512, JWK.from_json(self.jwk512json)) self.assertEqual(self.private, JWK.from_json(self.private_json)) def test_from_json_private_small(self): from josepy.jwk import JWK self.assertEqual(self.private, JWK.from_json(self.private_json_small)) def test_from_json_missing_one_additional(self): from josepy.jwk import JWK del self.private_json['q'] self.assertRaises(errors.Error, JWK.from_json, self.private_json) def test_from_json_hashable(self): from josepy.jwk import JWK hash(JWK.from_json(self.jwk256json)) def test_from_json_non_schema_errors(self): # valid against schema, but still failing from josepy.jwk import JWK self.assertRaises(errors.DeserializationError, JWK.from_json, { 'kty': 'RSA', 'e': 'AQAB', 'n': '' }) self.assertRaises(errors.DeserializationError, JWK.from_json, { 'kty': 'RSA', 'e': 'AQAB', 'n': '1' }) def test_thumbprint_go_jose(self): # https://github.com/square/go-jose/blob/4ddd71883fa547d37fbf598071f04512d8bafee3/jwk.go#L155 # https://github.com/square/go-jose/blob/4ddd71883fa547d37fbf598071f04512d8bafee3/jwk_test.go#L331-L344 # https://github.com/square/go-jose/blob/4ddd71883fa547d37fbf598071f04512d8bafee3/jwk_test.go#L384 from josepy.jwk import JWKRSA key = JWKRSA.json_loads("""{ "kty": "RSA", "kid": "*****@*****.**", "use": "sig", "n": "n4EPtAOCc9AlkeQHPzHStgAbgs7bTZLwUBZdR8_KuKPEHLd4rHVTeT-O-XV2jRojdNhxJWTDvNd7nqQ0VEiZQHz_AJmSCpMaJMRBSFKrKb2wqVwGU_NsYOYL-QtiWN2lbzcEe6XC0dApr5ydQLrHqkHHig3RBordaZ6Aj-oBHqFEHYpPe7Tpe-OfVfHd1E6cS6M1FZcD1NNLYD5lFHpPI9bTwJlsde3uhGqC0ZCuEHg8lhzwOHrtIQbS0FVbb9k3-tVTU4fg_3L_vniUFAKwuCLqKnS2BYwdq_mzSnbLY7h_qixoR7jig3__kRhuaxwUkRz5iaiQkqgc5gHdrNP5zw", "e": "AQAB" }""") # noqa self.assertEqual( binascii.hexlify(key.thumbprint()), b"f63838e96077ad1fc01c3f8405774dedc0641f558ebb4b40dccf5f9b6d66a932" )
def test_load(self): from josepy.jwk import JWKRSA self.assertEqual(self.private, JWKRSA.load(test_util.load_vector('rsa256_key.pem')))
def test_encode_param_zero(self): from josepy.jwk import JWKRSA # pylint: disable=protected-access # TODO: move encode/decode _param to separate class self.assertEqual('AA', JWKRSA._encode_param(0))
def get_things_done(): """ Here is where the client part is setup and action is done. """ responders = yield start_responders() # We first validate the directory. account_key = _get_account_key() try: client = yield Client.from_url( reactor, URL.fromText(acme_url.decode('utf-8')), key=JWKRSA(key=account_key), alg=RS256, ) except Exception as error: print('\n\nFailed to connect to ACME directory. %s' % (error, )) yield reactor.stop() defer.returnValue(None) # Then we register a new account or update an existing account. # First register a new account with a contact set, then using the same # key call register with a different contact and see that it was updated. response = yield client.start( email='[email protected],[email protected]') print('Account URI: %s' % (response.uri, )) print('Account contact: %s' % (response.body.contact, )) # We request a single certificate for a list of domains and get an "order" cert_key = generate_private_key('rsa') orderr = yield client.submit_order(cert_key, requested_domains) # Each order had a list of "authorizations" for which the challenge needs # to be validated. for authorization in orderr.authorizations: try: # Make sure all ACME server requests are sequential. # For now, answering to the challenges in parallel will not work. yield answer_challenge(authorization, client, responders, clock=reactor) except Exception as error: print('\n\nFailed to validate a challenge. %s' % (error, )) yield reactor.stop() defer.returnValue(None) certificate = yield get_certificate(orderr, client, clock=reactor) print('Got a new cert:\n') print(certificate.body) cert_key_pem = cert_key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption(), ) # Cleanup the client and disconnect any persistent connection to the # ACME server. yield client.stop() # The new certificate is available and we can start a demo HTTPS server # using it. yield start_https_demo_server(cert_key_pem, certificate.body) print('txacme demo done.')
def test_encode_param_zero(self): from josepy.jwk import JWKRSA # TODO: move encode/decode _param to separate class self.assertEqual('AA', JWKRSA._encode_param(0))