Ejemplo n.º 1
0
    def send_sa_auth(self):
        tr_attr = self.sa.esp_crypto_attr()
        trans = (
            ikev2.IKEv2_payload_Transform(transform_type='Encryption',
                                          transform_id=self.sa.esp_crypto,
                                          length=tr_attr[1],
                                          key_length=tr_attr[0]) /
            ikev2.IKEv2_payload_Transform(transform_type='Integrity',
                                          transform_id=self.sa.esp_integ) /
            ikev2.IKEv2_payload_Transform(
                transform_type='Extended Sequence Number',
                transform_id='No ESN') /
            ikev2.IKEv2_payload_Transform(
                transform_type='Extended Sequence Number', transform_id='ESN'))

        props = (ikev2.IKEv2_payload_Proposal(proposal=1,
                                              proto='ESP',
                                              SPIsize=4,
                                              SPI=os.urandom(4),
                                              trans_nb=4,
                                              trans=trans))

        tsi, tsr = self.sa.generate_ts(self.p.ts_is_ip4)
        plain = (ikev2.IKEv2_payload_IDi(
            next_payload='IDr', IDtype=self.sa.id_type, load=self.sa.i_id) /
                 ikev2.IKEv2_payload_IDr(next_payload='AUTH',
                                         IDtype=self.sa.id_type,
                                         load=self.sa.r_id) /
                 ikev2.IKEv2_payload_AUTH(next_payload='SA',
                                          auth_type=AuthMethod.value(
                                              self.sa.auth_method),
                                          load=self.sa.auth_data) /
                 ikev2.IKEv2_payload_SA(next_payload='TSi', prop=props) /
                 ikev2.IKEv2_payload_TSi(next_payload='TSr',
                                         number_of_TSs=len(tsi),
                                         traffic_selector=tsi) /
                 ikev2.IKEv2_payload_TSr(next_payload='Notify',
                                         number_of_TSs=len(tsr),
                                         traffic_selector=tsr) /
                 ikev2.IKEv2_payload_Notify(type='INITIAL_CONTACT'))

        header = ikev2.IKEv2(init_SPI=self.sa.ispi,
                             resp_SPI=self.sa.rspi,
                             id=self.sa.new_msg_id(),
                             flags='Initiator',
                             exch_type='IKE_AUTH')

        ike_msg = self.encrypt_ike_msg(header, plain, 'IDi')
        packet = self.create_packet(self.pg0, ike_msg, self.sa.sport,
                                    self.sa.dport, self.sa.natt, self.ip6)
        self.pg0.add_stream(packet)
        self.pg0.enable_capture()
        self.pg_start()
        capture = self.pg0.get_capture(1)
        self.verify_sa_auth(capture[0])
Ejemplo n.º 2
0
 def verify_auth(self, api_auth, cfg_auth):
     self.assertEqual(api_auth.method, AuthMethod.value(cfg_auth['method']))
     self.assertEqual(api_auth.data, cfg_auth['data'])
     self.assertEqual(api_auth.data_len, len(cfg_auth['data']))
Ejemplo n.º 3
0
    def send_sa_auth(self):
        tr_attr = self.sa.esp_crypto_attr()
        trans = (
            ikev2.IKEv2_payload_Transform(transform_type='Encryption',
                                          transform_id=self.sa.esp_crypto,
                                          length=tr_attr[1],
                                          key_length=tr_attr[0]) /
            ikev2.IKEv2_payload_Transform(transform_type='Integrity',
                                          transform_id=self.sa.esp_integ) /
            ikev2.IKEv2_payload_Transform(
                transform_type='Extended Sequence Number',
                transform_id='No ESN') /
            ikev2.IKEv2_payload_Transform(
                transform_type='Extended Sequence Number', transform_id='ESN'))

        props = (ikev2.IKEv2_payload_Proposal(proposal=1,
                                              proto='ESP',
                                              SPIsize=4,
                                              SPI=os.urandom(4),
                                              trans_nb=4,
                                              trans=trans))

        tsi, tsr = self.sa.generate_ts()
        plain = (ikev2.IKEv2_payload_IDi(
            next_payload='IDr', IDtype=self.sa.id_type, load=self.sa.i_id) /
                 ikev2.IKEv2_payload_IDr(next_payload='AUTH',
                                         IDtype=self.sa.id_type,
                                         load=self.sa.r_id) /
                 ikev2.IKEv2_payload_AUTH(next_payload='SA',
                                          auth_type=AuthMethod.value(
                                              self.sa.auth_method),
                                          load=self.sa.auth_data) /
                 ikev2.IKEv2_payload_SA(next_payload='TSi', prop=props) /
                 ikev2.IKEv2_payload_TSi(next_payload='TSr',
                                         number_of_TSs=len(tsi),
                                         traffic_selector=tsi) /
                 ikev2.IKEv2_payload_TSr(next_payload='Notify',
                                         number_of_TSs=len(tsr),
                                         traffic_selector=tsr) /
                 ikev2.IKEv2_payload_Notify(type='INITIAL_CONTACT'))

        if self.sa.ike_crypto == 'AES-GCM-16ICV':
            data = self.sa.ike_crypto_alg.pad(raw(plain))
            plen = len(data) + GCM_IV_SIZE + GCM_ICV_SIZE +\
                len(ikev2.IKEv2_payload_Encrypted())
            tlen = plen + len(ikev2.IKEv2())

            # prepare aad data
            sk_p = ikev2.IKEv2_payload_Encrypted(next_payload='IDi',
                                                 length=plen)
            sa_auth = (ikev2.IKEv2(init_SPI=self.sa.ispi,
                                   resp_SPI=self.sa.rspi,
                                   id=1,
                                   length=tlen,
                                   flags='Initiator',
                                   exch_type='IKE_AUTH'))
            sa_auth /= sk_p

            encr = self.sa.encrypt(raw(plain), raw(sa_auth))
            sk_p = ikev2.IKEv2_payload_Encrypted(next_payload='IDi',
                                                 length=plen,
                                                 load=encr)
            sa_auth = (ikev2.IKEv2(init_SPI=self.sa.ispi,
                                   resp_SPI=self.sa.rspi,
                                   id=1,
                                   length=tlen,
                                   flags='Initiator',
                                   exch_type='IKE_AUTH'))
            sa_auth /= sk_p
        else:
            encr = self.sa.encrypt(raw(plain))
            trunc_len = self.sa.ike_integ_alg.trunc_len
            plen = len(encr) + len(ikev2.IKEv2_payload_Encrypted()) + trunc_len
            tlen = plen + len(ikev2.IKEv2())

            sk_p = ikev2.IKEv2_payload_Encrypted(next_payload='IDi',
                                                 length=plen,
                                                 load=encr)
            sa_auth = (ikev2.IKEv2(init_SPI=self.sa.ispi,
                                   resp_SPI=self.sa.rspi,
                                   id=1,
                                   length=tlen,
                                   flags='Initiator',
                                   exch_type='IKE_AUTH'))
            sa_auth /= sk_p

            integ_data = raw(sa_auth)
            hmac_data = self.sa.compute_hmac(self.sa.ike_integ_alg.mod(),
                                             self.sa.my_authkey, integ_data)
            sa_auth = sa_auth / Raw(hmac_data[:trunc_len])

        assert (len(sa_auth) == tlen)
        packet = self.create_ike_msg(self.pg0, sa_auth, self.sa.sport,
                                     self.sa.dport, self.sa.natt)
        self.pg0.add_stream(packet)
        self.pg0.enable_capture()
        self.pg_start()
        capture = self.pg0.get_capture(1)
        self.verify_sa_auth(capture[0])