wtls_state-decl.h
上传用户:gzpyjq
上传日期:2013-01-31
资源大小:1852k
文件大小:29k
源码类别:

手机WAP编程

开发平台:

WINDOWS

  1. /*
  2.  * Macro calls to generate rows of the state table. See the documentation for
  3.  * guidance how to use and update these. 
  4.  *
  5.  * by Nick Clarey <nclarey@3glab.com>
  6.  */
  7. STATE_NAME(NULL_STATE)
  8. STATE_NAME(CREATING)
  9. STATE_NAME(CREATED)
  10. STATE_NAME(EXCHANGE)
  11. STATE_NAME(COMMIT)
  12. STATE_NAME(OPENING)
  13. STATE_NAME(OPEN)
  14. /* If the packet is a ClientHello */
  15. /* We only include this case in state NULL; the others are handled by the
  16.    wtls_find_or_create function */
  17. ROW(NULL_STATE,
  18.     T_Unitdata_Ind,
  19.     1,
  20.     {
  21.         /* The Wap event we have to dispatch */
  22.         WAPEvent *res;
  23.         wtls_Payload* tempPayload;
  24.         wtls_PDU* clientHelloPDU;
  25.         CipherSuite* ciphersuite;
  26.         int randomCounter;
  27.             
  28.         tempPayload = (wtls_Payload*) list_search (event->u.T_Unitdata_Ind.pdu_list,
  29.                                                    (void*) client_hello,
  30.                                                    match_handshake_type);
  31.         clientHelloPDU = wtls_pdu_unpack(tempPayload,wtls_machine);
  32.             
  33.         /* Store the client's random value - use pack for simplicity */
  34.         wtls_machine->client_random = octstr_create("");
  35.         randomCounter = pack_int32(wtls_machine->client_random,0,
  36.                                    clientHelloPDU->u.handshake.client_hello->random->gmt_unix_time);
  37.         octstr_insert(wtls_machine->client_random,
  38.                       clientHelloPDU->u.handshake.client_hello->random->random_bytes,
  39.                       randomCounter);
  40.             
  41.         /* Generate a SEC_Create_Res event, and pass it back into the queue */
  42.         res = wap_event_create(SEC_Create_Res);
  43.         res->u.SEC_Create_Res.addr_tuple =
  44.                 wap_addr_tuple_duplicate(event->u.T_Unitdata_Ind.addr_tuple);
  45.         /* Select the ciphersuite from the supplied list */
  46.         ciphersuite = wtls_choose_ciphersuite(clientHelloPDU->u.handshake.client_hello->ciphersuites);
  47.         /* Set the relevant values in the wtls_machine and PDU structure */
  48.         wtls_machine->bulk_cipher_algorithm = ciphersuite->bulk_cipher_algo;
  49.         wtls_machine->mac_algorithm = ciphersuite->mac_algo;
  50.         res->u.SEC_Create_Res.bulk_cipher_algo = ciphersuite->bulk_cipher_algo;
  51.         res->u.SEC_Create_Res.mac_algo = ciphersuite->mac_algo;
  52.         res->u.SEC_Create_Res.client_key_id =
  53.                 wtls_choose_clientkeyid(clientHelloPDU->u.handshake.client_hello->client_key_ids);
  54.         /* Set the sequence number mode in both the machine and the outgoing packet */
  55.         res->u.SEC_Create_Res.snmode = wtls_choose_snmode(clientHelloPDU->u.handshake.client_hello->snmode);
  56.         wtls_machine->sequence_number_mode = res->u.SEC_Create_Res.snmode;
  57.         /* Set the key refresh mode in both the machine and the outgoing packet */
  58.         res->u.SEC_Create_Res.krefresh = wtls_choose_krefresh(clientHelloPDU->u.handshake.client_hello->krefresh);
  59.         wtls_machine->key_refresh = res->u.SEC_Create_Res.krefresh;
  60.             
  61.         /* Keep the data so we can send it back in EXCHANGE */
  62.         // temporary - needs to delete old one if exists !
  63.         //wtls_machine->handshake_data = octstr_create("");
  64.         octstr_append(wtls_machine->handshake_data, tempPayload->data);
  65.             
  66.         debug("wtls:handle_event", 0,"Dispatching SEC_Create_Res event");
  67.         wtls_dispatch_event(res);
  68. },
  69.     CREATING)
  70. /* Creating State */
  71. /* Termination */
  72. ROW(CREATING,
  73.     SEC_Terminate_Req,
  74.     1,
  75.     {
  76. /* Send off a T_Unitdata_Req containing an alert as specified */
  77.             send_alert(event->u.SEC_Terminate_Req.alert_level,
  78.                        event->u.SEC_Terminate_Req.alert_desc,
  79.                        wtls_machine);
  80.     },
  81.     NULL_STATE)
  82. /* Exception */
  83. ROW(CREATING,
  84.     SEC_Exception_Req,
  85.     1,
  86.     {
  87.             /* Send off a T_Unitdata_Req containing an exception as specified */
  88.             send_alert(event->u.SEC_Exception_Req.alert_level,
  89.                        event->u.SEC_Exception_Req.alert_desc, wtls_machine);
  90.     },
  91.     CREATING)
  92. /* Create Response - create a buffer with a "ServerHello" and possibly a Certificate or something else */
  93. ROW(CREATING,
  94.     SEC_Create_Res,
  95.     1,
  96.     {
  97.             WAPEvent *req;
  98.             wtls_PDU* serverHelloPDU;
  99.             Random* tempRandom;
  100.             int randomCounter = 0;
  101.             
  102.             /* Our serverHello */
  103.             serverHelloPDU = wtls_pdu_create(Handshake_PDU);
  104.             serverHelloPDU->u.handshake.msg_type = server_hello;
  105.             serverHelloPDU->u.handshake.server_hello = (ServerHello*) gw_malloc(sizeof(ServerHello));
  106.             
  107.             /* Set our server version */
  108.             serverHelloPDU->u.handshake.server_hello->serverversion = 1;
  109.             
  110.             /* Get a suitably random number - store it in both the machine structure and outgoing PDU */
  111.             tempRandom = wtls_get_random();
  112.             wtls_machine->server_random = octstr_create("");
  113.             randomCounter = pack_int32(wtls_machine->server_random,0,tempRandom->gmt_unix_time);
  114.             octstr_insert(wtls_machine->server_random,tempRandom->random_bytes,octstr_len(wtls_machine->server_random));
  115.             
  116.             serverHelloPDU->u.handshake.server_hello->random = tempRandom;
  117.             
  118.             /* At the moment, we don't support session caching, so tell them to forget about caching us */
  119.             serverHelloPDU->u.handshake.server_hello->session_id = octstr_create("");
  120.             
  121.             /* We need to select an appropriate mechanism here from the ones listed */
  122.             serverHelloPDU->u.handshake.server_hello->client_key_id = event->u.SEC_Create_Res.client_key_id;
  123.             
  124.             /* Get our ciphersuite details */
  125.             serverHelloPDU->u.handshake.server_hello->ciphersuite = (CipherSuite*) gw_malloc(sizeof(CipherSuite));
  126.             serverHelloPDU->u.handshake.server_hello->ciphersuite->bulk_cipher_algo = event->u.SEC_Create_Res.bulk_cipher_algo;
  127.             serverHelloPDU->u.handshake.server_hello->ciphersuite->mac_algo = event->u.SEC_Create_Res.mac_algo;            
  128.             serverHelloPDU->u.handshake.server_hello->comp_method = null_comp;
  129.             
  130.             /* We need to confirm the client's choice, or if they haven't specified one, select
  131.                one ourselves */
  132.             serverHelloPDU->u.handshake.server_hello->snmode = event->u.SEC_Create_Res.snmode;
  133.             
  134.             /* We need to either confirm the client's choice of key refresh rate, or choose a lower rate */
  135.             serverHelloPDU->u.handshake.server_hello->krefresh = event->u.SEC_Create_Res.krefresh;
  136.             
  137.             /* Add the PDUsto the server's outgoing list  */
  138.             add_pdu(wtls_machine, serverHelloPDU);            
  139.             
  140.             /* Generate and dispatch a SEC_Exchange_Req or maybe a SEC_Commit_Req */
  141.             req = wap_event_create(SEC_Exchange_Req);
  142.             req->u.SEC_Exchange_Req.addr_tuple =
  143.                     wap_addr_tuple_duplicate(event->u.T_Unitdata_Ind.addr_tuple);
  144.             wtls_dispatch_event(req);
  145.             debug("wtls: handle_event", 0,"Dispatching SEC_Exchange_Req event");
  146.             
  147.     },
  148.     CREATED)
  149. /* Created State */
  150. /* Exchange Request - Full Handshake will be performed */
  151. ROW(CREATED,
  152.     SEC_Exchange_Req,
  153.     1,
  154.     {
  155.             wtls_PDU* serverKeyXchgPDU;
  156.             wtls_PDU* serverHelloDonePDU;
  157.             
  158.             /* Assert that the PDU list is valid */
  159.             gw_assert(wtls_machine->packet_to_send != NULL);
  160.             
  161.             /* We'll also need a Server Key Exchange message */
  162.             serverKeyXchgPDU = wtls_pdu_create(Handshake_PDU);
  163.             serverKeyXchgPDU->u.handshake.msg_type = server_key_exchange;
  164.             serverKeyXchgPDU->u.handshake.server_key_exchange = (ServerKeyExchange*) gw_malloc(sizeof(ServerKeyExchange));
  165.             serverKeyXchgPDU->u.handshake.server_key_exchange->param_spec = NULL;
  166.             
  167.             /* Allocate memory for the RSA component */
  168.             debug("wtls: ", 0,"Going to get the RSA public key...");
  169.             serverKeyXchgPDU->u.handshake.server_key_exchange->rsa_params = wtls_get_rsapublickey();
  170.             debug("wtls: ", 0,"...got it.");
  171.             add_pdu(wtls_machine, serverKeyXchgPDU);            
  172.             debug("wtls: ", 0,"in CREATED - just added pdu...");
  173.             /* Add some more PDUs to the List - potentially a ServerKeyExchange,
  174.                a CertificateRequest and a ServerHelloDone */
  175.             /* Just a ServerHelloDone for now */
  176.             serverHelloDonePDU = wtls_pdu_create(Handshake_PDU);
  177.             serverHelloDonePDU->u.handshake.msg_type = server_hello_done;
  178.             add_pdu(wtls_machine, serverHelloDonePDU);
  179.             
  180.             /* Translate the buffer and address details into a T_Unitdata_Req
  181.              * and send it winging it's way across the network */
  182.             send_queuedpdus(wtls_machine);
  183.     },
  184.     EXCHANGE)
  185. /* Commit Request - Abbreviated Handshake will be performed */
  186. ROW(CREATED,
  187.     SEC_Commit_Req,
  188.     1,
  189. {
  190.         /* Assert that the PDU list is valid */
  191.         /* Add some more PDUs to the List - a ChangeCipherSpec and a Finished */
  192.         /* Translate the buffer and address details into a T_Unitdata_Req */
  193.         /* And send it winging it's way across the network */
  194. },
  195.         COMMIT)
  196. /* Terminate Request */
  197. ROW(CREATED,
  198.     SEC_Terminate_Req,
  199.     1,
  200.     {
  201.             /* Send off a T_Unitdata_Req containing an alert as specified */
  202.             send_alert(event->u.SEC_Terminate_Req.alert_level,
  203.                        event->u.SEC_Terminate_Req.alert_desc, wtls_machine);
  204.     },
  205.     NULL_STATE)
  206. /* Exception Request */
  207. ROW(CREATED,
  208.     SEC_Exception_Req,
  209.     1,
  210.     {
  211.             /* Send off a T_Unitdata_Req containing an exception as specified */
  212.             send_alert(event->u.SEC_Exception_Req.alert_level,
  213.                        event->u.SEC_Exception_Req.alert_desc, wtls_machine);
  214.     },
  215.     CREATED)
  216. /* Exchange State */
  217. /* Unitdata arrival - identical ClientHello record */
  218. ROW(EXCHANGE,
  219.     T_Unitdata_Ind,
  220.     clienthellos_are_identical(event->u.T_Unitdata_Ind.pdu_list, wtls_machine->last_received_packet) == 1,
  221.         {
  222.                 /* It appears as though someone has sent us an identical ClientHello to the last one */
  223.                 /* Make ourselves a T_Unitdata_Req with the last_transmitted_packet */
  224.                 /* And send it on it's merry  */
  225.         },
  226.     EXCHANGE)
  227. /* Unitdata arrival - non-identical ClientHello record */
  228. //ROW(EXCHANGE,
  229. //    T_Unitdata_Ind,
  230. //    clienthellos_are_identical(event->u.T_Unitdata_Ind.pdu_list, wtls_machine->last_received_packet) != 1,
  231. //    {
  232. /* So, this one's different. They must have changed their mind about something, so try a CREATING again */
  233. /* Do the necessary SEC_Create_Ind stuff */
  234. //    },
  235. //    CREATING)
  236. /* Unitdata arrival - good packet */
  237. ROW(EXCHANGE,
  238.     T_Unitdata_Ind,
  239.     1,
  240.     {
  241.             RSAPublicKey *public_key = NULL;
  242.             Octstr* key_block;
  243.             Octstr* final_client_write_enc_key = NULL;
  244.             Octstr* final_server_write_enc_key = NULL;
  245.             Octstr* final_client_write_IV = NULL;
  246.             Octstr* final_server_write_IV = NULL;
  247.             Octstr* emptySecret = NULL;
  248. Octstr* checking_data = NULL;
  249.                         
  250.             // packet_contains_changecipherspec (event->u.T_Unitdata_Ind.pdu_list) == 1 &&
  251.             // packet_contains_finished (event->u.T_Unitdata_Ind.pdu_list) == 1 &&
  252.             // packet_contains_optional_stuff (event->u.T_Unitdata_Ind.pdu_list) == 1,
  253.             /* The Wap PDUs we have to dispatch */
  254.             wtls_PDU* changeCipherSpecPDU;
  255.             wtls_PDU* finishedPDU;
  256.             
  257.             /* The PDUs we have to process */
  258.             wtls_Payload* tempPayload;
  259.             wtls_PDU* clientKeyXchgPDU;
  260.             wtls_PDU* changeCipherSpec_incoming_PDU;
  261.             wtls_PDU* finished_incoming_PDU;
  262.             /* For decrypting/encrypting data */
  263.             Octstr* concatenatedRandoms=0;
  264.             Octstr* encryptedData=0;
  265.             Octstr* decryptedData=0;
  266.             Octstr* labelVerify=0;
  267.             Octstr* labelMaster=0;
  268.             
  269.             /* Process the incoming event : ClientKeyExchange*/            
  270.             tempPayload = (wtls_Payload*) list_search (event->u.T_Unitdata_Ind.pdu_list,
  271.                                                       (void*) client_key_exchange,
  272.                                                       match_handshake_type);
  273.             /* Keep the data so we can send it back */
  274.             octstr_insert(wtls_machine->handshake_data, tempPayload->data,
  275.                           octstr_len(wtls_machine->handshake_data));
  276.                                      
  277.             clientKeyXchgPDU = wtls_pdu_unpack(tempPayload,wtls_machine);
  278.             wtls_pdu_dump(clientKeyXchgPDU,0);
  279.                         
  280.             /* Decrypt the client key exchange PDU */
  281.             encryptedData = clientKeyXchgPDU->u.handshake.client_key_exchange->rsa_params->encrypted_secret;
  282.             decryptedData = wtls_decrypt_rsa(encryptedData);
  283.             public_key = wtls_get_rsapublickey();
  284.             pack_int16(decryptedData, octstr_len(decryptedData), octstr_len(public_key->rsa_exponent));
  285.             octstr_insert(decryptedData, public_key->rsa_exponent, octstr_len(decryptedData));
  286.             pack_int16(decryptedData, octstr_len(decryptedData), octstr_len(public_key->rsa_modulus));
  287.             octstr_insert(decryptedData, public_key->rsa_modulus, octstr_len(decryptedData));
  288.             /* Concatenate our random data */
  289.             concatenatedRandoms = octstr_cat(wtls_machine->client_random,
  290.                                              wtls_machine->server_random);
  291.          
  292.             /* Generate our master secret */
  293.             labelMaster = octstr_create("master secret");
  294.             wtls_machine->master_secret = wtls_calculate_prf(decryptedData, labelMaster,
  295.                                               concatenatedRandoms,20, wtls_machine );
  296.             octstr_destroy(labelMaster);
  297.             labelMaster = NULL;
  298. /* calculate the key blocks */
  299. calculate_server_key_block(wtls_machine);
  300. calculate_client_key_block(wtls_machine);
  301.                         
  302.             /* Process the incoming event : ChangeCipherSpec*/            
  303.             tempPayload = (wtls_Payload*) list_search (event->u.T_Unitdata_Ind.pdu_list,
  304.                                                       (void*) ChangeCipher_PDU,
  305.                                                       match_pdu_type);
  306.             changeCipherSpec_incoming_PDU = wtls_pdu_unpack(tempPayload, wtls_machine);
  307.             if(changeCipherSpec_incoming_PDU->u.cc.change == 1) {
  308.                 debug("wtls", 0,"Need to decrypt the PDUs from now on...");
  309.                 wtls_machine->encrypted = 1;
  310.                 wtls_decrypt_pdu_list(wtls_machine, event->u.T_Unitdata_Ind.pdu_list);
  311.             }
  312. octstr_dump(wtls_machine->client_write_MAC_secret,0);
  313.             
  314.             wtls_pdu_dump(changeCipherSpec_incoming_PDU,0);
  315.             /* Process the incoming event : Finished*/            
  316.             tempPayload = (wtls_Payload*) list_search (event->u.T_Unitdata_Ind.pdu_list,
  317.                                                       (void*) finished,
  318.                                                       match_handshake_type);
  319.             if(tempPayload == NULL)
  320.                 debug("wtls", 0, "null finished !!!");
  321.             
  322.             finished_incoming_PDU = wtls_pdu_unpack(tempPayload,wtls_machine);
  323.             debug("wtls", 0, "Client Finished PDU:");
  324.             wtls_pdu_dump(finished_incoming_PDU,0);
  325.             /* Check the verify_data */
  326.             labelVerify = octstr_create("client finished");
  327. checking_data = wtls_calculate_prf(wtls_machine->master_secret, labelVerify,
  328.                   (Octstr *)wtls_hash(wtls_machine->handshake_data, wtls_machine),
  329.   12, wtls_machine);
  330.             octstr_destroy(labelVerify);
  331.             labelVerify = NULL;
  332. if(octstr_compare(finished_incoming_PDU->u.handshake.finished->verify_data, checking_data)==0) {
  333. debug("wtls", 0, "DATA VERIFICATION OK");
  334. }
  335.             /* Keep the data so we can send it back in the next message */
  336.             /*octstr_insert(wtls_machine->handshake_data, tempPayload->data,
  337.                           octstr_len(wtls_machine->handshake_data));
  338. */
  339. // temporary fix
  340. octstr_truncate(tempPayload->data, 15);
  341.             octstr_insert(wtls_machine->handshake_data, tempPayload->data,
  342.                           octstr_len(wtls_machine->handshake_data));
  343.                                      
  344.             /* Create a new PDU List containing a ChangeCipherSpec and a Finished */
  345.             changeCipherSpecPDU = wtls_pdu_create(ChangeCipher_PDU);
  346.             changeCipherSpecPDU->u.cc.change = 1;
  347.             
  348.             /* Generate our verify data */
  349.             finishedPDU = wtls_pdu_create(Handshake_PDU);
  350.             finishedPDU->u.handshake.msg_type = finished;
  351.             finishedPDU->cipher = 1;
  352.             finishedPDU->u.handshake.finished = gw_malloc(sizeof(Finished));
  353.             
  354.             labelVerify = octstr_create("server finished");
  355.             finishedPDU->u.handshake.finished->verify_data = wtls_calculate_prf(wtls_machine->master_secret,
  356.                                             labelVerify,(Octstr *)wtls_hash(wtls_machine->handshake_data, wtls_machine),
  357. 12,wtls_machine);
  358.                        
  359.             /* Reset the accumulated Handshake data */
  360.             octstr_destroy(wtls_machine->handshake_data);
  361.             wtls_machine->handshake_data = octstr_create("");
  362.                         
  363.             octstr_destroy(labelVerify);
  364.             labelVerify = NULL;
  365.             
  366.             /* Add the pdus to our list */
  367.             add_pdu(wtls_machine, changeCipherSpecPDU);            
  368.             add_pdu(wtls_machine, finishedPDU);
  369.             
  370.             /* Send it off */
  371.             send_queuedpdus(wtls_machine);
  372.                         
  373.             /* reset the seq_num */
  374.             wtls_machine->server_seq_num = 0;
  375.     },
  376.     OPENING)
  377. /* Unitdata arrival - critical/fatal alert */
  378. ROW(EXCHANGE,
  379.         T_Unitdata_Ind,
  380.         is_critical_alert(event->u.T_Unitdata_Ind.pdu_list) == 1,
  381.         {
  382. /* Do the necessary SEC_Terminate_Ind stuff */
  383. /* And we're dead :-< */
  384. },
  385.         NULL_STATE)
  386. /* Unitdata arrival - warning alert */
  387. ROW(EXCHANGE,
  388.         T_Unitdata_Ind,
  389.         is_warning_alert(event->u.T_Unitdata_Ind.pdu_list) == 1,
  390.         {
  391. /* Do the necessary SEC_Exception_Ind stuff */
  392. },
  393.         EXCHANGE)
  394. /* Terminate */
  395. ROW(EXCHANGE,
  396.         SEC_Terminate_Req,
  397.         1,
  398.         {
  399. /* Send off a T_Unitdata_Req containing an alert as specified */
  400. send_alert(event->u.SEC_Terminate_Req.alert_level,
  401.         event->u.SEC_Terminate_Req.alert_desc, wtls_machine);
  402. },
  403.         NULL_STATE)
  404. /* Exception */
  405. ROW(EXCHANGE,
  406.         SEC_Exception_Req,
  407.         1,
  408.         {
  409. /* Send off a T_Unitdata_Req containing an exception as specified */
  410. send_alert(event->u.SEC_Exception_Req.alert_level,
  411.         event->u.SEC_Exception_Req.alert_desc, wtls_machine);
  412. },
  413.         EXCHANGE)
  414. /* Commit State */
  415. /* Unitdata arrival - identical ClientHello record */
  416. ROW(COMMIT,
  417.         T_Unitdata_Ind,
  418.         clienthellos_are_identical(event->u.T_Unitdata_Ind.pdu_list, wtls_machine->last_received_packet) == 1,
  419.         {
  420. /* It appears as though someone has sent us an identical ClientHello to the last one */
  421. /* Make ourselves a T_Unitdata_Req with the last_transmitted_packet */
  422. /* And send it on it's merry way */
  423. },
  424.         COMMIT)
  425. /* Unitdata arrival - non-identical ClientHello record */
  426. ROW(COMMIT,
  427.         T_Unitdata_Ind,
  428.         clienthellos_are_identical(event->u.T_Unitdata_Ind.pdu_list, wtls_machine->last_received_packet) != 1,
  429.         {
  430. /* So, this one's different. They must have changed their mind about something, so try a CREATING again */
  431. /* Do the necessary SEC_Create_Ind stuff */
  432. },
  433.         CREATING)
  434. /* Unitdata arrival - good packet with ChangeCipherSpec and Finished */
  435. ROW(COMMIT,
  436.         T_Unitdata_Ind,
  437.         packet_contains_changecipherspec (event->u.T_Unitdata_Ind.pdu_list) == 1 &&
  438. packet_contains_finished (event->u.T_Unitdata_Ind.pdu_list) == 1 &&
  439. packet_contains_userdata (event->u.T_Unitdata_Ind.pdu_list) != 1,
  440.         {
  441. /* Create ourselves a SEC_Commit_Cnf packet to send off */
  442. /* Send it off */
  443. },
  444.         OPEN)
  445. /* Unitdata arrival - good packet with ChangeCipherSpec, Finished and UD */
  446. ROW(COMMIT,
  447.         T_Unitdata_Ind,
  448.         packet_contains_changecipherspec (event->u.T_Unitdata_Ind.pdu_list) == 1 &&
  449. packet_contains_finished (event->u.T_Unitdata_Ind.pdu_list) == 1 &&
  450. packet_contains_userdata (event->u.T_Unitdata_Ind.pdu_list) == 1,
  451.         {
  452. /* Create a SEC_Commit_Cnf packet to send off */
  453. /* Send it off */
  454. /* Relay the contents of the packets up to the WTP or WSP layers,
  455.    depending on the destination port */
  456. },
  457.         OPEN)
  458. /* Unitdata arrival - critical/fatal alert */
  459. ROW(COMMIT,
  460.         T_Unitdata_Ind,
  461.         is_critical_alert(event->u.T_Unitdata_Ind.pdu_list) == 1,
  462.         {
  463. /* Do the necessary SEC_Terminate_Ind stuff */
  464. /* And we're dead :-< */
  465. },
  466.         NULL_STATE)
  467. /* Unitdata arrival - warning alert */
  468. ROW(COMMIT,
  469.         T_Unitdata_Ind,
  470.         is_warning_alert(event->u.T_Unitdata_Ind.pdu_list) == 1,
  471.         {
  472. /* Do the necessary SEC_Exception_Ind stuff */
  473. },
  474.         COMMIT)
  475. /* Terminate */
  476. ROW(COMMIT,
  477.         SEC_Terminate_Req,
  478.         1,
  479.         {
  480. /* Send off a T_Unitdata_Req containing an alert as specified */
  481. send_alert(event->u.SEC_Terminate_Req.alert_level,
  482.         event->u.SEC_Terminate_Req.alert_desc, wtls_machine);
  483. },
  484.         NULL_STATE)
  485. /* Exception */
  486. ROW(COMMIT,
  487.         SEC_Exception_Req,
  488.         1,
  489.         {
  490. /* Send off a T_Unitdata_Req containing an exception as specified */
  491. send_alert(event->u.SEC_Exception_Req.alert_level,
  492.         event->u.SEC_Exception_Req.alert_desc, wtls_machine);
  493. },
  494.         COMMIT)
  495. /* Opening State */
  496. /* Create Request */
  497. ROW(OPENING,
  498.         SEC_Create_Request_Req,
  499.         1,
  500.         {
  501. /* Send off a T_Unitdata_Req containing a HelloRequest */
  502. },
  503.         OPENING)
  504. /* Send out UnitData */
  505. ROW(OPENING,
  506.         SEC_Unitdata_Req,
  507.         1,
  508.         {
  509. /* Apply the negotiated security "stuff" to the received packet */
  510. /* Send out the packet to the destination port/address requested */
  511. },
  512.         OPENING)
  513. /* Unitdata received - ClientHello */
  514. //ROW(OPENING,
  515. //        T_Unitdata_Ind,
  516. //        packet_contains_clienthello (event->u.T_Unitdata_Ind.pdu_list) == 0,
  517. //        {
  518. ///* Hmm, they're obviously not happy with something we discussed, so let's head back to creating */
  519. ///* Do the necessary SEC_Create_Ind stuff */
  520. //},
  521. //        CREATING)
  522. /* Unitdata received */
  523. ROW(OPENING,
  524.         T_Unitdata_Ind,
  525.         packet_contains_userdata(event->u.T_Unitdata_Ind.pdu_list) == 1,
  526.         {
  527.             wtls_Payload* tempPayload;
  528.             wtls_PDU* ApplicationPDU;
  529.             tempPayload = (wtls_Payload*) list_search (event->u.T_Unitdata_Ind.pdu_list,
  530.                                                       (void*) Application_PDU,
  531.                                                       match_pdu_type);
  532.             if(tempPayload == NULL)
  533.                 debug("wtls", 0, "no App PDU found in list !!!");
  534.             
  535. debug("wtls",0, "PDU type: %d", tempPayload->type);
  536. octstr_dump(tempPayload->data,0);
  537. ApplicationPDU = wtls_pdu_unpack(tempPayload, wtls_machine);
  538. wtls_pdu_dump(ApplicationPDU,0);
  539. /* Apply the negotiated decryption/decoding/MAC check to the received data */
  540. /* Take the userdata and pass it on up to the WTP/WSP, depending on the destination port */
  541. /* calculate the padding length */
  542.         /*
  543. contentLength = octstr_len(bufferCopy);
  544.          macSize = hash_table[wtls_machine->mac_algorithm].mac_size;
  545.          blockLength = bulk_table[wtls_machine->bulk_cipher_algorithm].block_size;
  546. paddingLength = (contentLength + macSize + 1) % (blockLength);
  547. */
  548. /* get the MAC */
  549. },
  550.         OPEN)
  551. /* Unitdata arrival - Certificate, ClientKeyExchange ... Finished */
  552. ROW(OPENING,
  553.         T_Unitdata_Ind,
  554.         certificates_are_identical(event->u.T_Unitdata_Ind.pdu_list, wtls_machine->last_received_packet) == 1 &&
  555. clientkeyexchanges_are_identical(event->u.T_Unitdata_Ind.pdu_list, wtls_machine->last_received_packet) == 1 && 
  556. certifcateverifys_are_identical(event->u.T_Unitdata_Ind.pdu_list, wtls_machine->last_received_packet) == 1 &&
  557. changecipherspecs_are_identical(event->u.T_Unitdata_Ind.pdu_list, wtls_machine->last_received_packet) == 1 &&
  558. finisheds_are_indentical(event->u.T_Unitdata_Ind.pdu_list, wtls_machine->last_received_packet) == 1,
  559.         {
  560. /* It appears as though someone has sent us an identical ClientHello to the last one */
  561. /* Make ourselves a T_Unitdata_Req with the last_transmitted_packet */
  562. /* And send it on it's merry way */
  563. },
  564.         OPENING)
  565. /* Unitdata arrival - critical/fatal alert */
  566. ROW(OPENING,
  567.         T_Unitdata_Ind,
  568.         is_critical_alert(event->u.T_Unitdata_Ind.pdu_list) == 1,
  569.         {
  570. /* Do the necessary SEC_Terminate_Ind stuff */
  571. /* And we're dead :-< */
  572. },
  573.         NULL_STATE)
  574. /* Unitdata arrival - warning alert */
  575. ROW(OPENING,
  576.         T_Unitdata_Ind,
  577.         is_warning_alert(event->u.T_Unitdata_Ind.pdu_list) == 1,
  578.         {
  579. /* Do the necessary SEC_Exception_Ind stuff */
  580. },
  581.         OPENING)
  582. /* Terminate */
  583. ROW(OPENING,
  584.         SEC_Terminate_Req,
  585.         1,
  586.         {
  587. /* Send off a T_Unitdata_Req containing an alert as specified */
  588. send_alert(event->u.SEC_Terminate_Req.alert_level,
  589.         event->u.SEC_Terminate_Req.alert_desc, wtls_machine);
  590. },
  591.         NULL_STATE)
  592. /* Exception */
  593. ROW(OPENING,
  594.         SEC_Exception_Req,
  595.         1,
  596.         {
  597. /* Send off a T_Unitdata_Req containing an exception as specified */
  598. send_alert(event->u.SEC_Exception_Req.alert_level,
  599.         event->u.SEC_Exception_Req.alert_desc, wtls_machine);
  600. },
  601.         OPENING)
  602. /* Open State */
  603. /* Create Request */
  604. ROW(OPEN,
  605.         SEC_Create_Request_Req,
  606.         1,
  607.         {
  608. /* Send off a T_Unitdata_Req with a HelloRequest */
  609. },
  610.         OPEN)
  611. /* Send out UnitData */
  612. ROW(OPEN,
  613.         SEC_Unitdata_Req,
  614.         1,
  615.         { 
  616. /* Apply the negotiated security "stuff" to the received packet */
  617. /* Send out the packet to the destination port/address requested */
  618. },
  619.         OPEN)
  620. /* Unitdata received - ClientHello */
  621. ROW(OPEN,
  622.         T_Unitdata_Ind,
  623.         packet_contains_clienthello (event->u.T_Unitdata_Ind.pdu_list) == 1,
  624.         {
  625. /* Hmm, they're obviously not happy with something we discussed, so let's head back to creating */
  626. /* Do the necessary SEC_Create_Ind stuff */
  627. },
  628.         CREATING)
  629. /* Unitdata received */
  630. ROW(OPEN,
  631.         T_Unitdata_Ind,
  632.         packet_contains_userdata(event->u.T_Unitdata_Ind.pdu_list) == 1,
  633.         {
  634. /* Apply the negotiated decryption/decoding/MAC check to the received data */
  635. /* Take the userdata and pass it on up to the WTP/WSP, depending on the destination port */
  636. },
  637.         OPEN)
  638. /* Unitdata arrival - ChangeCipherSpec, Finished */
  639. ROW(OPEN,
  640.         T_Unitdata_Ind,
  641.         packet_contains_changecipherspec (event->u.T_Unitdata_Ind.pdu_list) == 1 &&
  642. packet_contains_finished (event->u.T_Unitdata_Ind.pdu_list) == 1 &&
  643. packet_contains_userdata(event->u.T_Unitdata_Ind.pdu_list) != 1 &&
  644. finisheds_are_indentical(event->u.T_Unitdata_Ind.pdu_list, wtls_machine->last_received_packet) == 1,
  645.         {
  646. /* Just send out a T_Unitdata_Req with an Alert(duplicate_finished_received) */
  647. },
  648.         OPEN)
  649. /* Unitdata arrival - ChangeCipherSpec, Finished and UD */
  650. ROW(OPEN,
  651.         T_Unitdata_Ind,
  652.         packet_contains_changecipherspec (event->u.T_Unitdata_Ind.pdu_list) == 1 &&
  653. packet_contains_finished (event->u.T_Unitdata_Ind.pdu_list) == 1 &&
  654. packet_contains_userdata(event->u.T_Unitdata_Ind.pdu_list) == 1 &&
  655. finisheds_are_indentical(event->u.T_Unitdata_Ind.pdu_list, wtls_machine->last_received_packet) == 1,
  656.         {
  657. /* Apply the negotiated decryption/decoding/MAC check to the received data */
  658. /* Take the userdata and pass it on up to the WTP/WSP, depending on the destination port */
  659. /* Send out a T_Unitdata_Req with an Alert(duplicate_finished_received) */
  660. },
  661.         OPEN)
  662. /* Unitdata arrival - critical/fatal alert */
  663. ROW(OPEN,
  664.         T_Unitdata_Ind,
  665.         is_critical_alert(event->u.T_Unitdata_Ind.pdu_list) == 1,
  666.         {
  667. /* Do the necessary SEC_Terminate_Ind stuff */
  668. /* And we're dead :-< */
  669. },
  670.         NULL_STATE)
  671. /* Unitdata arrival - warning alert */
  672. ROW(OPEN,
  673.         T_Unitdata_Ind,
  674.         is_warning_alert(event->u.T_Unitdata_Ind.pdu_list) == 1,
  675.         {
  676. /* Do the necessary SEC_Terminate_Ind stuff */
  677. },
  678.         OPEN)
  679. /* Terminate */
  680. ROW(OPEN,
  681.         SEC_Terminate_Req,
  682.         1,
  683.         {
  684. /* Send off a T_Unitdata_Req containing an alert as specified */
  685. send_alert(event->u.SEC_Terminate_Req.alert_level,
  686.         event->u.SEC_Terminate_Req.alert_desc, wtls_machine);
  687. },
  688.         NULL_STATE)
  689. /* Exception */
  690. ROW(OPEN,
  691.         SEC_Exception_Req,
  692.         1,
  693.     {
  694. /* Send off a T_Unitdata_Req containing an exception as specified */
  695. send_alert(event->u.SEC_Exception_Req.alert_level,
  696.         event->u.SEC_Exception_Req.alert_desc, wtls_machine);
  697. },
  698.         OPEN)
  699. #undef ROW
  700. #undef STATE_NAME