def test_only_tink_output_prefix_type_encodes_a_kid_header(self):
    handle = tink.new_keyset_handle(jwt.raw_jwt_es256_template())
    sign = handle.primitive(jwt.JwtPublicKeySign)
    verify = handle.public_keyset_handle().primitive(jwt.JwtPublicKeyVerify)

    tink_handle = _change_output_prefix_to_tink(handle)
    tink_sign = tink_handle.primitive(jwt.JwtPublicKeySign)
    tink_verify = tink_handle.public_keyset_handle().primitive(
        jwt.JwtPublicKeyVerify)

    raw_jwt = jwt.new_raw_jwt(issuer='issuer', without_expiration=True)

    token = sign.sign_and_encode(raw_jwt)
    token_with_kid = tink_sign.sign_and_encode(raw_jwt)

    _, header, _, _ = _jwt_format.split_signed_compact(token)
    self.assertNotIn('kid', _json_util.json_loads(header))

    _, header_with_kid, _, _ = _jwt_format.split_signed_compact(token_with_kid)
    self.assertIn('kid', _json_util.json_loads(header_with_kid))

    validator = jwt.new_validator(
        expected_issuer='issuer', allow_missing_expiration=True)

    verify.verify_and_decode(token, validator)
    tink_verify.verify_and_decode(token_with_kid, validator)

    other_handle = _change_key_id(tink_handle)
    other_verify = other_handle.public_keyset_handle().primitive(
        jwt.JwtPublicKeyVerify)

    verify.verify_and_decode(token_with_kid, validator)
    # For output prefix type TINK, the kid header is required.
    with self.assertRaises(tink.TinkError):
      tink_verify.verify_and_decode(token, validator)
    # This should fail because value of the kid header is wrong.
    with self.assertRaises(tink.TinkError):
      other_verify.verify_and_decode(token_with_kid, validator)
예제 #2
0
    def test_es256_key_with_custom_kid_header(self):
        keyset_handle = tink.new_keyset_handle(jwt.raw_jwt_es256_template())

        # Add a custom kid to the key in keyset_handle
        value = keyset_handle._keyset.key[0].key_data.value
        ecdsa_key = jwt_ecdsa_pb2.JwtEcdsaPrivateKey.FromString(value)
        ecdsa_key.public_key.custom_kid.value = 'my kid'
        keyset_handle._keyset.key[
            0].key_data.value = ecdsa_key.SerializeToString()
        sign = keyset_handle.primitive(jwt.JwtPublicKeySign)

        raw_jwt = jwt.new_raw_jwt(issuer='issuer', without_expiration=True)
        signed_compact = sign.sign_and_encode(raw_jwt)

        _, json_header, _, _ = _jwt_format.split_signed_compact(signed_compact)
        header = _jwt_format.json_loads(json_header)
        self.assertEqual(header['kid'], 'my kid')

        # Now, change the output prefix type to TINK. This should fail.
        keyset_handle._keyset.key[0].output_prefix_type = tink_pb2.TINK
        with self.assertRaises(tink.TinkError):
            tink_sign = keyset_handle.primitive(jwt.JwtPublicKeySign)
            tink_sign.sign_and_encode(raw_jwt)
예제 #3
0
 'AES_CMAC_PRF':
     prf.prf_key_templates.AES_CMAC,
 'HMAC_PRF_SHA256':
     prf.prf_key_templates.HMAC_SHA256,
 'HMAC_PRF_SHA512':
     prf.prf_key_templates.HMAC_SHA512,
 'HKDF_PRF_SHA256':
     prf.prf_key_templates.HKDF_SHA256,
 'JWT_HS256': jwt.jwt_hs256_template(),
 'JWT_HS256_RAW': jwt.raw_jwt_hs256_template(),
 'JWT_HS384': jwt.jwt_hs384_template(),
 'JWT_HS384_RAW': jwt.raw_jwt_hs384_template(),
 'JWT_HS512': jwt.jwt_hs512_template(),
 'JWT_HS512_RAW': jwt.raw_jwt_hs512_template(),
 'JWT_ES256': jwt.jwt_es256_template(),
 'JWT_ES256_RAW': jwt.raw_jwt_es256_template(),
 'JWT_ES384': jwt.jwt_es384_template(),
 'JWT_ES384_RAW': jwt.raw_jwt_es384_template(),
 'JWT_ES512': jwt.jwt_es512_template(),
 'JWT_ES512_RAW': jwt.raw_jwt_es512_template(),
 'JWT_RS256_2048_F4': jwt.jwt_rs256_2048_f4_template(),
 'JWT_RS256_2048_F4_RAW': jwt.raw_jwt_rs256_2048_f4_template(),
 'JWT_RS256_3072_F4': jwt.jwt_rs256_3072_f4_template(),
 'JWT_RS256_3072_F4_RAW': jwt.raw_jwt_rs256_3072_f4_template(),
 'JWT_RS384_3072_F4': jwt.jwt_rs384_3072_f4_template(),
 'JWT_RS384_3072_F4_RAW': jwt.raw_jwt_rs384_3072_f4_template(),
 'JWT_RS512_4096_F4': jwt.jwt_rs512_4096_f4_template(),
 'JWT_RS512_4096_F4_RAW': jwt.raw_jwt_rs512_4096_f4_template(),
 'JWT_PS256_2048_F4': jwt.jwt_ps256_2048_f4_template(),
 'JWT_PS256_2048_F4_RAW': jwt.raw_jwt_ps256_2048_f4_template(),
 'JWT_PS256_3072_F4': jwt.jwt_ps256_3072_f4_template(),
예제 #4
0
class JwtKeyTemplatesTest(parameterized.TestCase):
    @parameterized.named_parameters([
        ('JWT_HS256', jwt.jwt_hs256_template()),
        ('JWT_HS256_RAW', jwt.raw_jwt_hs256_template()),
        ('JWT_HS384', jwt.jwt_hs384_template()),
        ('JWT_HS384_RAW', jwt.raw_jwt_hs384_template()),
        ('JWT_HS512', jwt.jwt_hs512_template()),
        ('JWT_HS512_RAW', jwt.raw_jwt_hs512_template()),
    ])
    def test_mac_success(self, key_template):
        keyset_handle = tink.new_keyset_handle(key_template)
        jwt_hmac = keyset_handle.primitive(jwt.JwtMac)
        token = jwt.new_raw_jwt(issuer='issuer',
                                subject='subject',
                                without_expiration=True)
        compact = jwt_hmac.compute_mac_and_encode(token)
        output_token = jwt_hmac.verify_mac_and_decode(
            compact,
            jwt.new_validator(expected_issuer='issuer',
                              allow_missing_expiration=True))
        self.assertEqual(output_token.issuer(), token.issuer())
        self.assertEqual(output_token.subject(), token.subject())

    @parameterized.named_parameters([
        ('JWT_ES256', jwt.jwt_es256_template()),
        ('JWT_ES256_RAW', jwt.raw_jwt_es256_template()),
        ('JWT_ES384', jwt.jwt_es384_template()),
        ('JWT_ES384_RAW', jwt.raw_jwt_es384_template()),
        ('JWT_ES512', jwt.jwt_es512_template()),
        ('JWT_ES512_RAW', jwt.raw_jwt_es512_template()),
        ('JWT_RS256_2048_F4', jwt.jwt_rs256_2048_f4_template()),
        ('JWT_RS256_2048_F4_RAW', jwt.raw_jwt_rs256_2048_f4_template()),
        ('JWT_RS256_3072_F4', jwt.jwt_rs256_3072_f4_template()),
        ('JWT_RS256_3072_F4_RAW', jwt.raw_jwt_rs256_3072_f4_template()),
        ('JWT_RS384_3072_F4', jwt.jwt_rs384_3072_f4_template()),
        ('JWT_RS384_3072_F4_RAW', jwt.raw_jwt_rs384_3072_f4_template()),
        ('JWT_RS512_4096_F4', jwt.jwt_rs512_4096_f4_template()),
        ('JWT_RS512_4096_F4_RAW', jwt.raw_jwt_rs512_4096_f4_template()),
        ('JWT_PS256_2048_F4', jwt.jwt_ps256_2048_f4_template()),
        ('JWT_PS256_2048_F4_RAW', jwt.raw_jwt_ps256_2048_f4_template()),
        ('JWT_PS256_3072_F4', jwt.jwt_ps256_3072_f4_template()),
        ('JWT_PS256_3072_F4_RAW', jwt.raw_jwt_ps256_3072_f4_template()),
        ('JWT_PS384_3072_F4', jwt.jwt_ps384_3072_f4_template()),
        ('JWT_PS384_3072_F4_RAW', jwt.raw_jwt_ps384_3072_f4_template()),
        ('JWT_PS512_4096_F4', jwt.jwt_ps512_4096_f4_template()),
        ('JWT_PS512_4096_F4_RAW', jwt.raw_jwt_ps512_4096_f4_template()),
    ])
    def test_new_keydata_primitive_success(self, template):
        private_handle = tink.new_keyset_handle(template)
        sign = private_handle.primitive(jwt.JwtPublicKeySign)
        verify = private_handle.public_keyset_handle().primitive(
            jwt.JwtPublicKeyVerify)
        raw_jwt = jwt.new_raw_jwt(issuer='issuer',
                                  subject='subject',
                                  without_expiration=True)
        compact = sign.sign_and_encode(raw_jwt)
        verified_jwt = verify.verify_and_decode(
            compact,
            jwt.new_validator(expected_issuer='issuer',
                              allow_missing_expiration=True))
        self.assertEqual(verified_jwt.issuer(), 'issuer')
        self.assertEqual(verified_jwt.subject(), 'subject')
예제 #5
0
 'JWT_HS256':
 jwt.jwt_hs256_template(),
 'JWT_HS256_RAW':
 jwt.raw_jwt_hs256_template(),
 'JWT_HS384':
 jwt.jwt_hs384_template(),
 'JWT_HS384_RAW':
 jwt.raw_jwt_hs384_template(),
 'JWT_HS512':
 jwt.jwt_hs512_template(),
 'JWT_HS512_RAW':
 jwt.raw_jwt_hs512_template(),
 'JWT_ES256':
 jwt.jwt_es256_template(),
 'JWT_ES256_RAW':
 jwt.raw_jwt_es256_template(),
 'JWT_ES384':
 jwt.jwt_es384_template(),
 'JWT_ES384_RAW':
 jwt.raw_jwt_es384_template(),
 'JWT_ES512':
 jwt.jwt_es512_template(),
 'JWT_ES512_RAW':
 jwt.raw_jwt_es512_template(),
 'JWT_RS256_2048_F4':
 jwt.jwt_rs256_2048_f4_template(),
 'JWT_RS256_2048_F4_RAW':
 jwt.raw_jwt_rs256_2048_f4_template(),
 'JWT_RS256_3072_F4':
 jwt.jwt_rs256_3072_f4_template(),
 'JWT_RS256_3072_F4_RAW':
    def test_weird_tokens_with_valid_signatures(self):
        handle = tink.new_keyset_handle(jwt.raw_jwt_es256_template())
        sign = handle.primitive(jwt.JwtPublicKeySign)
        # Get the internal PublicKeySign primitive to create valid signatures.
        wrapped = cast(_jwt_signature_wrappers._WrappedJwtPublicKeySign, sign)
        raw_sign = cast(
            _jwt_signature_key_manager._JwtPublicKeySign,
            wrapped._primitive_set.primary().primitive)._public_key_sign

        verify = handle.public_keyset_handle().primitive(
            jwt.JwtPublicKeyVerify)
        validator = jwt.new_validator(expected_issuer='issuer',
                                      allow_missing_expiration=True)

        # Normal token.
        valid = gen_compact('{"alg":"ES256"}', '{"iss":"issuer"}', raw_sign)
        verified_jwt = verify.verify_and_decode(valid, validator)
        self.assertEqual(verified_jwt.issuer(), 'issuer')

        # Token with unknown header is valid.
        unknown_header = gen_compact(
            '{"alg":"ES256","unknown_header":"abc"} \n ', '{"iss":"issuer" }',
            raw_sign)
        verified_jwt = verify.verify_and_decode(unknown_header, validator)
        self.assertEqual(verified_jwt.issuer(), 'issuer')

        # Token with unknown kid is valid, since primitives with output prefix type
        # RAW ignore kid headers.
        unknown_header = gen_compact('{"alg":"ES256","kid":"unknown"} \n ',
                                     '{"iss":"issuer" }', raw_sign)
        verified_jwt = verify.verify_and_decode(unknown_header, validator)
        self.assertEqual(verified_jwt.issuer(), 'issuer')

        # Token with invalid alg header
        alg_invalid = gen_compact('{"alg":"ES384"}', '{"iss":"issuer"}',
                                  raw_sign)
        with self.assertRaises(tink.TinkError):
            verify.verify_and_decode(alg_invalid, validator)

        # Token with empty header
        empty_header = gen_compact('{}', '{"iss":"issuer"}', raw_sign)
        with self.assertRaises(tink.TinkError):
            verify.verify_and_decode(empty_header, validator)

        # Token header is not valid JSON
        header_invalid = gen_compact('{"alg":"ES256"', '{"iss":"issuer"}',
                                     raw_sign)
        with self.assertRaises(tink.TinkError):
            verify.verify_and_decode(header_invalid, validator)

        # Token payload is not valid JSON
        payload_invalid = gen_compact('{"alg":"ES256"}', '{"iss":"issuer"',
                                      raw_sign)
        with self.assertRaises(tink.TinkError):
            verify.verify_and_decode(payload_invalid, validator)

        # Token with whitespace in header JSON string is valid.
        whitespace_in_header = gen_compact(' {"alg":   \n  "ES256"} \n ',
                                           '{"iss":"issuer" }', raw_sign)
        verified_jwt = verify.verify_and_decode(whitespace_in_header,
                                                validator)
        self.assertEqual(verified_jwt.issuer(), 'issuer')

        # Token with whitespace in payload JSON string is valid.
        whitespace_in_payload = gen_compact('{"alg":"ES256"}',
                                            ' {"iss": \n"issuer" } \n',
                                            raw_sign)
        verified_jwt = verify.verify_and_decode(whitespace_in_payload,
                                                validator)
        self.assertEqual(verified_jwt.issuer(), 'issuer')

        # Token with whitespace in base64-encoded header is invalid.
        with_whitespace = (_jwt_format.encode_header('{"alg":"ES256"}') +
                           b' .' +
                           _jwt_format.encode_payload('{"iss":"issuer"}'))
        token_with_whitespace = _jwt_format.create_signed_compact(
            with_whitespace, raw_sign.sign(with_whitespace))
        with self.assertRaises(tink.TinkError):
            verify.verify_and_decode(token_with_whitespace, validator)

        # Token with invalid character is invalid.
        with_invalid_char = (_jwt_format.encode_header('{"alg":"ES256"}') +
                             b'.?' +
                             _jwt_format.encode_payload('{"iss":"issuer"}'))
        token_with_invalid_char = _jwt_format.create_signed_compact(
            with_invalid_char, raw_sign.sign(with_invalid_char))
        with self.assertRaises(tink.TinkError):
            verify.verify_and_decode(token_with_invalid_char, validator)

        # Token with additional '.' is invalid.
        with_dot = (_jwt_format.encode_header('{"alg":"ES256"}') + b'.' +
                    _jwt_format.encode_payload('{"iss":"issuer"}') + b'.')
        token_with_dot = _jwt_format.create_signed_compact(
            with_dot, raw_sign.sign(with_dot))
        with self.assertRaises(tink.TinkError):
            verify.verify_and_decode(token_with_dot, validator)

        # num_recursions has been chosen such that parsing of this token fails
        # in all languages. We want to make sure that the algorithm does not
        # hang or crash in this case, but only returns a parsing error.
        num_recursions = 10000
        rec_payload = ('{"a":' * num_recursions) + '""' + ('}' *
                                                           num_recursions)
        rec_token = gen_compact('{"alg":"ES256"}', rec_payload, raw_sign)
        with self.assertRaises(tink.TinkError):
            verify.verify_and_decode(
                rec_token,
                validator=jwt.new_validator(allow_missing_expiration=True))

        # test wrong types
        with self.assertRaises(tink.TinkError):
            verify.verify_and_decode(cast(str, None), validator)
        with self.assertRaises(tink.TinkError):
            verify.verify_and_decode(cast(str, 123), validator)
        with self.assertRaises(tink.TinkError):
            valid_bytes = valid.encode('utf8')
            verify.verify_and_decode(cast(str, valid_bytes), validator)
class JwtSignatureWrapperTest(parameterized.TestCase):

  def test_interesting_error(self):
    private_handle = tink.new_keyset_handle(jwt.jwt_es256_template())
    sign = private_handle.primitive(jwt.JwtPublicKeySign)
    verify = private_handle.public_keyset_handle().primitive(
        jwt.JwtPublicKeyVerify)
    raw_jwt = jwt.new_raw_jwt(issuer='issuer', without_expiration=True)
    compact = sign.sign_and_encode(raw_jwt)
    with self.assertRaisesRegex(jwt.JwtInvalidError,
                                'invalid JWT; expected issuer'):
      verify.verify_and_decode(compact, jwt.new_validator(
          expected_issuer='unknown', allow_missing_expiration=True))

  @parameterized.parameters([
      (jwt.raw_jwt_es256_template(), jwt.raw_jwt_es256_template()),
      (jwt.raw_jwt_es256_template(), jwt.jwt_es256_template()),
      (jwt.jwt_es256_template(), jwt.raw_jwt_es256_template()),
      (jwt.jwt_es256_template(), jwt.jwt_es256_template()),
  ])
  def test_key_rotation(self, old_key_tmpl, new_key_tmpl):
    builder = keyset_builder.new_keyset_builder()
    older_key_id = builder.add_new_key(old_key_tmpl)

    builder.set_primary_key(older_key_id)
    handle1 = builder.keyset_handle()
    sign1 = handle1.primitive(jwt.JwtPublicKeySign)
    verify1 = handle1.public_keyset_handle().primitive(jwt.JwtPublicKeyVerify)

    newer_key_id = builder.add_new_key(new_key_tmpl)
    handle2 = builder.keyset_handle()
    sign2 = handle2.primitive(jwt.JwtPublicKeySign)
    verify2 = handle2.public_keyset_handle().primitive(jwt.JwtPublicKeyVerify)

    builder.set_primary_key(newer_key_id)
    handle3 = builder.keyset_handle()
    sign3 = handle3.primitive(jwt.JwtPublicKeySign)
    verify3 = handle3.public_keyset_handle().primitive(jwt.JwtPublicKeyVerify)

    builder.disable_key(older_key_id)
    handle4 = builder.keyset_handle()
    sign4 = handle4.primitive(jwt.JwtPublicKeySign)
    verify4 = handle4.public_keyset_handle().primitive(jwt.JwtPublicKeyVerify)

    raw_jwt = jwt.new_raw_jwt(issuer='a', without_expiration=True)
    validator = jwt.new_validator(
        expected_issuer='a', allow_missing_expiration=True)

    self.assertNotEqual(older_key_id, newer_key_id)
    # 1 uses the older key. So 1, 2 and 3 can verify the signature, but not 4.
    compact1 = sign1.sign_and_encode(raw_jwt)
    self.assertEqual(
        verify1.verify_and_decode(compact1, validator).issuer(), 'a')
    self.assertEqual(
        verify2.verify_and_decode(compact1, validator).issuer(), 'a')
    self.assertEqual(
        verify3.verify_and_decode(compact1, validator).issuer(), 'a')
    with self.assertRaises(tink.TinkError):
      verify4.verify_and_decode(compact1, validator)

    # 2 uses the older key. So 1, 2 and 3 can verify the signature, but not 4.
    compact2 = sign2.sign_and_encode(raw_jwt)
    self.assertEqual(
        verify1.verify_and_decode(compact2, validator).issuer(), 'a')
    self.assertEqual(
        verify2.verify_and_decode(compact2, validator).issuer(), 'a')
    self.assertEqual(
        verify3.verify_and_decode(compact2, validator).issuer(), 'a')
    with self.assertRaises(tink.TinkError):
      verify4.verify_and_decode(compact2, validator)

    # 3 uses the newer key. So 2, 3 and 4 can verify the signature, but not 1.
    compact3 = sign3.sign_and_encode(raw_jwt)
    with self.assertRaises(tink.TinkError):
      verify1.verify_and_decode(compact3, validator)
    self.assertEqual(
        verify2.verify_and_decode(compact3, validator).issuer(), 'a')
    self.assertEqual(
        verify3.verify_and_decode(compact3, validator).issuer(), 'a')
    self.assertEqual(
        verify4.verify_and_decode(compact3, validator).issuer(), 'a')

    # 4 uses the newer key. So 2, 3 and 4 can verify the signature, but not 1.
    compact4 = sign4.sign_and_encode(raw_jwt)
    with self.assertRaises(tink.TinkError):
      verify1.verify_and_decode(compact4, validator)
    self.assertEqual(
        verify2.verify_and_decode(compact4, validator).issuer(), 'a')
    self.assertEqual(
        verify3.verify_and_decode(compact4, validator).issuer(), 'a')
    self.assertEqual(
        verify4.verify_and_decode(compact4, validator).issuer(), 'a')

  def test_only_tink_output_prefix_type_encodes_a_kid_header(self):
    handle = tink.new_keyset_handle(jwt.raw_jwt_es256_template())
    sign = handle.primitive(jwt.JwtPublicKeySign)
    verify = handle.public_keyset_handle().primitive(jwt.JwtPublicKeyVerify)

    tink_handle = _change_output_prefix_to_tink(handle)
    tink_sign = tink_handle.primitive(jwt.JwtPublicKeySign)
    tink_verify = tink_handle.public_keyset_handle().primitive(
        jwt.JwtPublicKeyVerify)

    raw_jwt = jwt.new_raw_jwt(issuer='issuer', without_expiration=True)

    token = sign.sign_and_encode(raw_jwt)
    token_with_kid = tink_sign.sign_and_encode(raw_jwt)

    _, header, _, _ = _jwt_format.split_signed_compact(token)
    self.assertNotIn('kid', _json_util.json_loads(header))

    _, header_with_kid, _, _ = _jwt_format.split_signed_compact(token_with_kid)
    self.assertIn('kid', _json_util.json_loads(header_with_kid))

    validator = jwt.new_validator(
        expected_issuer='issuer', allow_missing_expiration=True)

    verify.verify_and_decode(token, validator)
    tink_verify.verify_and_decode(token_with_kid, validator)

    other_handle = _change_key_id(tink_handle)
    other_verify = other_handle.public_keyset_handle().primitive(
        jwt.JwtPublicKeyVerify)

    verify.verify_and_decode(token_with_kid, validator)
    # For output prefix type TINK, the kid header is required.
    with self.assertRaises(tink.TinkError):
      tink_verify.verify_and_decode(token, validator)
    # This should fail because value of the kid header is wrong.
    with self.assertRaises(tink.TinkError):
      other_verify.verify_and_decode(token_with_kid, validator)

  @parameterized.named_parameters([
      ('JWT_ES256_RAW', jwt.raw_jwt_es256_template()),
      ('JWT_RS256_RAW', jwt.raw_jwt_rs256_2048_f4_template()),
      ('JWT_PS256_RAW', jwt.raw_jwt_ps256_3072_f4_template()),
  ])
  def test_raw_key_with_custom_kid_header(self, template):
    # normal key with output prefix RAW
    handle = tink.new_keyset_handle(template)
    raw_jwt = jwt.new_raw_jwt(issuer='issuer', without_expiration=True)
    validator = jwt.new_validator(
        expected_issuer='issuer', allow_missing_expiration=True)

    sign = handle.primitive(jwt.JwtPublicKeySign)
    token = sign.sign_and_encode(raw_jwt)
    verify = handle.public_keyset_handle().primitive(jwt.JwtPublicKeyVerify)
    verify.verify_and_decode(token, validator)

    _, json_header, _, _ = _jwt_format.split_signed_compact(token)
    self.assertNotIn('kid', _json_util.json_loads(json_header))

    # key with a custom_kid set
    custom_kid_handle = _set_custom_kid(handle, custom_kid=LONG_CUSTOM_KID)
    custom_kid_sign = custom_kid_handle.primitive(jwt.JwtPublicKeySign)
    token_with_kid = custom_kid_sign.sign_and_encode(raw_jwt)
    custom_kid_verify = custom_kid_handle.public_keyset_handle().primitive(
        jwt.JwtPublicKeyVerify)
    custom_kid_verify.verify_and_decode(token_with_kid, validator)

    _, header_with_kid, _, _ = _jwt_format.split_signed_compact(token_with_kid)
    self.assertEqual(_json_util.json_loads(header_with_kid)['kid'],
                     LONG_CUSTOM_KID)

    # The primitive with a custom_kid set accepts tokens without kid header.
    custom_kid_verify.verify_and_decode(token, validator)

    # The primitive without a custom_kid set ignores the kid header.
    verify.verify_and_decode(token_with_kid, validator)

    # key with a different custom_kid set
    other_handle = _set_custom_kid(handle, custom_kid='other kid')
    other_verify = other_handle.public_keyset_handle().primitive(
        jwt.JwtPublicKeyVerify)
    # Fails because the kid value do not match.
    with self.assertRaises(tink.TinkError):
      other_verify.verify_and_decode(token_with_kid, validator)

    tink_handle = _change_output_prefix_to_tink(custom_kid_handle)
    tink_sign = tink_handle.primitive(jwt.JwtPublicKeySign)
    tink_verify = tink_handle.public_keyset_handle().primitive(
        jwt.JwtPublicKeyVerify)
    # Having custom_kid set with output prefix TINK is not allowed.
    with self.assertRaises(tink.TinkError):
      tink_sign.sign_and_encode(raw_jwt)
    with self.assertRaises(tink.TinkError):
      tink_verify.verify_and_decode(token, validator)
    with self.assertRaises(tink.TinkError):
      tink_verify.verify_and_decode(token_with_kid, validator)

  def test_legacy_template_fails(self):
    template = keyset_builder.legacy_template(jwt.jwt_es256_template())
    builder = keyset_builder.new_keyset_builder()
    key_id = builder.add_new_key(template)
    builder.set_primary_key(key_id)
    handle = builder.keyset_handle()
    with self.assertRaises(tink.TinkError):
      handle.primitive(jwt.JwtPublicKeySign)
    with self.assertRaises(tink.TinkError):
      handle.public_keyset_handle().primitive(jwt.JwtPublicKeyVerify)

  def test_legacy_non_primary_key_fails(self):
    builder = keyset_builder.new_keyset_builder()
    old_template = keyset_builder.legacy_template(jwt.jwt_es256_template())
    _ = builder.add_new_key(old_template)
    current_key_id = builder.add_new_key(jwt.jwt_es256_template())
    builder.set_primary_key(current_key_id)
    handle = builder.keyset_handle()
    with self.assertRaises(tink.TinkError):
      handle.primitive(jwt.JwtPublicKeySign)
    with self.assertRaises(tink.TinkError):
      handle.public_keyset_handle().primitive(jwt.JwtPublicKeyVerify)

  def test_jwt_mac_from_keyset_without_primary_fails(self):
    builder = keyset_builder.new_keyset_builder()
    builder.add_new_key(jwt.jwt_es256_template())
    with self.assertRaises(tink.TinkError):
      builder.keyset_handle()