Esempio n. 1
0
def License_verify_valid():
    """Tests that License.verify does not raise exception for valid
    certificate"""
    License.issue(certificate(), key(),
        license_data = LicenseData(
            '2014-01-01T00:00:00',
            '2014-01-01T00:00:01')).verify(certificate())
Esempio n. 2
0
def License_verify_invalid():
    """Tests that License.verify raises exception for invalid signatures"""
    with assert_exception(License.InvalidSignatureException):
        License.issue(certificate(), key(),
            license_data = LicenseData(
                '2014-01-01T00:00:00',
                '2014-01-01T00:00:01')).verify(other_certificate())
Esempio n. 3
0
 def test_verify_invalid(self):
     """Tests that License.verify raises exception for invalid signatures"""
     with self.assertRaises(License.InvalidSignatureException):
         License.issue(CERTIFICATE,
                       key(),
                       license_data=LicenseData(
                           '2014-01-01T00:00:00',
                           '2014-01-01T00:00:01')).verify(OTHER_CERTIFICATE)
Esempio n. 4
0
def License_verify_valid():
    """Tests that License.verify does not raise exception for valid
    certificate"""
    License.issue(certificate(),
                  key(),
                  license_data=LicenseData('2014-01-01T00:00:00',
                                           '2014-01-01T00:00:01')).verify(
                                               certificate())
Esempio n. 5
0
def License_verify_invalid():
    """Tests that License.verify raises exception for invalid signatures"""
    with assert_exception(License.InvalidSignatureException):
        License.issue(certificate(),
                      key(),
                      license_data=LicenseData('2014-01-01T00:00:00',
                                               '2014-01-01T00:00:01')).verify(
                                                   other_certificate())
Esempio n. 6
0
def License_store():
    """Tests that a license can be loaded from the stored data"""
    f = io.BytesIO()
    License.issue(certificate(), key(),
        license_data = LicenseData(
            '2014-01-01T00:00:00',
            '2014-01-01T00:00:01')).store(f, b'valid password')
    License.load(io.BytesIO(f.getvalue()), b'valid password')
Esempio n. 7
0
 def test_verify_valid(self):
     """Tests that License.verify does not raise exception for valid
     certificate"""
     License.issue(CERTIFICATE,
                   key(),
                   license_data=LicenseData(
                       '2014-01-01T00:00:00',
                       '2014-01-01T00:00:01')).verify(CERTIFICATE)
Esempio n. 8
0
 def test_verify_valid(self):
     """Tests that License.verify does not raise exception for valid
     certificate"""
     License.issue(
         CERTIFICATE,
         key(),
         license_data=LicenseData(
             '2014-01-01T00:00:00',
             '2014-01-01T00:00:01')).verify(CERTIFICATE)
Esempio n. 9
0
 def test_store(self):
     """Tests that a license can be loaded from the stored data"""
     f = io.BytesIO()
     License.issue(CERTIFICATE,
                   key(),
                   license_data=LicenseData('2014-01-01T00:00:00',
                                            '2014-01-01T00:00:01')).store(
                                                f, b'valid password')
     License.load(io.BytesIO(f.getvalue()), b'valid password')
Esempio n. 10
0
def License_store():
    """Tests that a license can be loaded from the stored data"""
    f = io.BytesIO()
    License.issue(certificate(),
                  key(),
                  license_data=LicenseData('2014-01-01T00:00:00',
                                           '2014-01-01T00:00:01')).store(
                                               f, b'valid password')
    License.load(io.BytesIO(f.getvalue()), b'valid password')
Esempio n. 11
0
 def test_verify_invalid(self):
     """Tests that License.verify raises exception for invalid signatures"""
     with self.assertRaises(License.InvalidSignatureException):
         License.issue(
             CERTIFICATE,
             key(),
             license_data=LicenseData(
                 '2014-01-01T00:00:00',
                 '2014-01-01T00:00:01')).verify(OTHER_CERTIFICATE)
Esempio n. 12
0
 def test_store(self):
     """Tests that a license can be loaded from the stored data"""
     f = io.BytesIO()
     License.issue(
         CERTIFICATE,
         key(),
         license_data=LicenseData(
             '2014-01-01T00:00:00',
             '2014-01-01T00:00:01')).store(f, b'valid password')
     License.load(io.BytesIO(f.getvalue()), b'valid password')
Esempio n. 13
0
 def test_issue_invalid_license_data(self):
     """Tests that License.issue with invalid license_data fails"""
     with self.assertRaises(ValueError):
         License.issue(
             CERTIFICATE,
             key(),
             license_data=None)
     with self.assertRaises(ValueError):
         License.issue(
             CERTIFICATE,
             key(),
             license_data=LicenseData(
                 '2014-01-01T00:00:00',
                 '2014-01-01T00:00:01'),
             not_before='2014-01-01T00:00:00')
     with self.assertRaises(ValueError):
         License.issue(
             CERTIFICATE,
             key(),
             license_data=LicenseData(
                 '2014-01-01T00:00:00',
                 '2014-01-01T00:00:01'),
             issuer='CN=must not be passed')
     with self.assertRaises(ValueError):
         License.issue(
             CERTIFICATE,
             key(),
             not_before='2014-01-01T00:00:00')
Esempio n. 14
0
    def test_issue_valid(self):
        """Tests that the signature is correctly constructed"""
        # Generated with command below:
        '''
        python -c 'import tests.license_test; \
            open("key.pem", "w").write(tests.license_test.KEY)'

        python -c 'import sys, truepy; sys.stdout.write( \
            truepy._bean.to_document( \
                truepy._bean.serialize( \
                    truepy.LicenseData( \
                        not_before = "2014-01-01T00:00:00", \
                        not_after = "2014-01-01T00:00:01"))))' \
        | openssl sha -sign key.pem -sha1 \
        | base64
        '''
        expected = (
            'BwaJUYJTcY22EiC5x/qZQVMKGeIxAwTiIejRjrch2Q/uVoUrB1ptKRn1ffGgYs5zc'
            'agsj+7YTi8bB8nim+W+ANy93WttNrgz5hl2k75D4hGmR3EGV+f45l91RYMdTHukuK'
            'ZkA+agc/At5ByHC8Qaw4+4Jdz2e0XJMJaR3aEAYIJ/5NDVKSHD2OjpGDLEc70Qwdo'
            'rUU10B4X2URasRKHuRZTv9jVz2t4Mgk4wrJHiT/gw6sHbR1U7u7PbbnbQ8Xx/c37U'
            'L54jy9ZqM+j7yEhEXqPGW5rXvj0IQmYrODLdyVzNMUa/ReC66oERy2JZA0aaFyY8l'
            'Fr5V1yC1xT4r6yMnw==')

        self.assertEqual(
            expected,
            License.issue(
                CERTIFICATE,
                key(),
                license_data=LicenseData(
                    '2014-01-01T00:00:00',
                    '2014-01-01T00:00:01')).signature)
Esempio n. 15
0
    def test_issue_valid(self):
        """Tests that the signature is correctly constructed"""
        # Generated with command below:
        '''
        python -c 'import tests.license_test; \
            open("key.pem", "w").write(tests.license_test.KEY)'

        python -c 'import sys, truepy; sys.stdout.write( \
            truepy._bean.to_document( \
                truepy._bean.serialize( \
                    truepy.LicenseData( \
                        not_before = "2014-01-01T00:00:00", \
                        not_after = "2014-01-01T00:00:01"))))' \
        | openssl sha -sign key.pem -sha1 \
        | base64
        '''
        expected = (
            'BwaJUYJTcY22EiC5x/qZQVMKGeIxAwTiIejRjrch2Q/uVoUrB1ptKRn1ffGgYs5zc'
            'agsj+7YTi8bB8nim+W+ANy93WttNrgz5hl2k75D4hGmR3EGV+f45l91RYMdTHukuK'
            'ZkA+agc/At5ByHC8Qaw4+4Jdz2e0XJMJaR3aEAYIJ/5NDVKSHD2OjpGDLEc70Qwdo'
            'rUU10B4X2URasRKHuRZTv9jVz2t4Mgk4wrJHiT/gw6sHbR1U7u7PbbnbQ8Xx/c37U'
            'L54jy9ZqM+j7yEhEXqPGW5rXvj0IQmYrODLdyVzNMUa/ReC66oERy2JZA0aaFyY8l'
            'Fr5V1yC1xT4r6yMnw==')

        self.assertEqual(
            expected,
            License.issue(
                CERTIFICATE,
                key(),
                license_data=LicenseData('2014-01-01T00:00:00',
                                         '2014-01-01T00:00:01')).signature)
Esempio n. 16
0
    def IssueLicense(self,
                     key_folder,
                     licence_input,
                     password='******',
                     licence_file='license.key',
                     validate_days=365):
        with open(os.path.join(key_folder, 'certificate.pem'), 'rb') as f:
            certificate = f.read()

        # Load the private key
        with open(os.path.join(key_folder, 'key.pem'), 'rb') as f:
            key = serialization.load_pem_private_key(
                f.read(),
                password=str.encode(password, encoding='UTF-8'),
                backend=backends.default_backend())

        # Issue the license
        license = License.issue(
            certificate,
            key,
            license_data=LicenseData(
                datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S'),
                (datetime.datetime.now() + datetime.timedelta(
                    days=validate_days)).strftime('%Y-%m-%dT%H:%M:%S')))

        # Store the license
        with open(licence_file, 'wb') as f:
            license.store(f, str.encode(licence_input, encoding='UTF-8'))
Esempio n. 17
0
def License_issue_invalid_license_data():
    """Tests that License.issue with invalid license_data fails"""
    with assert_exception(ValueError):
        License.issue(certificate(), key(), license_data=None)
    with assert_exception(ValueError):
        License.issue(certificate(),
                      key(),
                      license_data=LicenseData('2014-01-01T00:00:00',
                                               '2014-01-01T00:00:01'),
                      not_before='2014-01-01T00:00:00')
    with assert_exception(ValueError):
        License.issue(certificate(),
                      key(),
                      license_data=LicenseData('2014-01-01T00:00:00',
                                               '2014-01-01T00:00:01'),
                      issuer='CN=must not be passed')
    with assert_exception(ValueError):
        License.issue(certificate(), key(), not_before='2014-01-01T00:00:00')
Esempio n. 18
0
 def test_issue_invalid_license_data(self):
     """Tests that License.issue with invalid license_data fails"""
     with self.assertRaises(ValueError):
         License.issue(CERTIFICATE, key(), license_data=None)
     with self.assertRaises(ValueError):
         License.issue(CERTIFICATE,
                       key(),
                       license_data=LicenseData('2014-01-01T00:00:00',
                                                '2014-01-01T00:00:01'),
                       not_before='2014-01-01T00:00:00')
     with self.assertRaises(ValueError):
         License.issue(CERTIFICATE,
                       key(),
                       license_data=LicenseData('2014-01-01T00:00:00',
                                                '2014-01-01T00:00:01'),
                       issuer='CN=must not be passed')
     with self.assertRaises(ValueError):
         License.issue(CERTIFICATE, key(), not_before='2014-01-01T00:00:00')
Esempio n. 19
0
def License_issue_invalid_license_data():
    """Tests that License.issue with invalid license_data fails"""
    with assert_exception(ValueError):
        License.issue(certificate(), key(),
            license_data = None)
    with assert_exception(ValueError):
        License.issue(certificate(), key(),
            license_data = LicenseData(
                '2014-01-01T00:00:00',
                '2014-01-01T00:00:01'),
            not_before = '2014-01-01T00:00:00')
    with assert_exception(ValueError):
        License.issue(certificate(), key(),
            license_data = LicenseData(
                '2014-01-01T00:00:00',
                '2014-01-01T00:00:01'),
            issuer = 'CN=must not be passed')
    with assert_exception(ValueError):
        License.issue(certificate(), key(),
            not_before = '2014-01-01T00:00:00')
Esempio n. 20
0
def issue(license_file, license_description, issuer_certificate, issuer_key,
          license_file_password, **args):
    """issue [license file] [digest] [license description]
    Issues a new license and shows information about it. You must specify the
    issuer certificate and key as --issuer-certificate/key on the command line,
    and the license file password as --license-file-password.

    [digest] The environment digest file. 
    
    [license description] must be one command line argument on the form
    not_before=2014-01-01T00:00:00,not_after=2016-01-01T00:00:00,... containing
    license data fields.
    
        - The license of the subject of the request, possible
         arguments are:
           node          - Authorization Number of nodes, default 10000
           not_before    - The timestamp when this license starts to be valid.
           not_after     - The timestamp when this license ceases to be valid.
                           This must be strictly after `not_before`.
           issued        - The timestamp when this license was issued. This
                           defaults to not_before.
           issuer        - The issuer of this certificate. If not specified,
                           UNKNOWN_NAME will be used.
           holder        - The holder of this certificate. If not specified,
                           UNKNOWN_NAME will be used.
           subject       - Free-form string data to associate with the
                           license. This value will be stringified.
           consumer_type - Free-form string data to associate with the
                           license. This value will be stringified.
           info          - Free-form string data to associate with the
                           license. This value will be stringified.
           extra         - Any type of data to store in the license. If this
                           is not a string, it will be JSON serialised.               


    """
    try:
        license_data_parameters = dict(
            (p.strip() for p in i.split('=', 1))
            for i in license_description.split(','))
    except Exception as e:
        raise RuntimeError(
            'Invalid license data description (%s): %s',
            license_description,
            e)

    extra = {'node': license_data_parameters.pop('node', 10000),
             'digests': args.get('digest'),
             'other_extra': license_data_parameters.get('extra')}
    license_data_parameters['extra'] = extra

    try:
        license_data = LicenseData(**license_data_parameters)
    except TypeError as e:
        raise RuntimeError(
            'Incomplete license data description (%s): %s',
            license_description,
            e)

    license = License.issue(issuer_certificate, issuer_key,
                            license_data=license_data)
    with open(license_file, 'wb') as f:
        license.store(f, license_file_password)

    show(license_file, issuer_certificate, license_file_password)