evse_cm_set_key.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted (subject to the limitations
  9. * in the disclaimer below) provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * * Neither the name of Qualcomm Atheros nor the names of
  21. * its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  26. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  27. * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  31. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  37. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. *--------------------------------------------------------------------*/
  41. /*====================================================================*
  42. *
  43. * signed evse_cm_set_key (struct session * session, struct channel * channel, struct message * message);
  44. *
  45. * slac.h
  46. *
  47. * PEV-HLE sets the NMK and NID on PEV-PLC using CM_SET_KEY.REQ;
  48. * the NMK and NID must match those provided by EVSE-HLE using
  49. * CM_SLAC_MATCH.CNF;
  50. *
  51. * Contributor(s):
  52. * Charles Maier
  53. *
  54. *--------------------------------------------------------------------*/
  55. #ifndef EVSE_CM_SET_KEY_SOURCE
  56. #define EVSE_CM_SET_KEY_SOURCE
  57. #include <string.h>
  58. #include "../ether/channel.h"
  59. #include "../tools/memory.h"
  60. #include "../tools/error.h"
  61. #include "../tools/flags.h"
  62. #include "../mme/qualcomm.h"
  63. #include "../mme/homeplug.h"
  64. #include "../slac/slac.h"
  65. signed evse_cm_set_key (struct session * session, struct channel * channel, struct message * message)
  66. {
  67. #ifndef __GNUC__
  68. #pragma pack(push,1)
  69. #endif
  70. struct __packed cm_set_key_request
  71. {
  72. struct ethernet_hdr ethernet;
  73. struct homeplug_fmi homeplug;
  74. uint8_t KEYTYPE;
  75. uint32_t MYNOUNCE;
  76. uint32_t YOURNOUNCE;
  77. uint8_t PID;
  78. uint16_t PRN;
  79. uint8_t PMN;
  80. uint8_t CCOCAP;
  81. uint8_t NID [SLAC_NID_LEN];
  82. uint8_t NEWEKS;
  83. uint8_t NEWKEY [SLAC_NMK_LEN];
  84. uint8_t RSVD [3];
  85. }
  86. * request = (struct cm_set_key_request *) (message);
  87. struct __packed cm_set_key_confirm
  88. {
  89. struct ethernet_hdr ethernet;
  90. struct homeplug_fmi homeplug;
  91. uint8_t RESULT;
  92. uint32_t MYNOUNCE;
  93. uint32_t YOURNOUNCE;
  94. uint8_t PID;
  95. uint16_t PRN;
  96. uint8_t PMN;
  97. uint8_t CCOCAP;
  98. uint8_t RSVD [27];
  99. }
  100. * confirm = (struct cm_set_key_confirm *) (message);
  101. #ifndef __GNUC__
  102. #pragma pack (pop)
  103. #endif
  104. memset (message, 0, sizeof (* message));
  105. slac_debug (session, 0, __func__, "--> CM_SET_KEY.REQ");
  106. EthernetHeader (& request->ethernet, channel->peer, channel->host, channel->type);
  107. HomePlugHeader1 (& request->homeplug, HOMEPLUG_MMV, (CM_SET_KEY | MMTYPE_REQ));
  108. request->KEYTYPE = SLAC_CM_SETKEY_KEYTYPE;
  109. memset (& request->MYNOUNCE, 0xAA, sizeof (request->MYNOUNCE));
  110. memset (& request->YOURNOUNCE, 0x00, sizeof (request->YOURNOUNCE));
  111. request->PID = SLAC_CM_SETKEY_PID;
  112. request->PRN = HTOLE16 (SLAC_CM_SETKEY_PRN);
  113. request->PMN = SLAC_CM_SETKEY_PMN;
  114. request->CCOCAP = SLAC_CM_SETKEY_CCO;
  115. memcpy (request->NID, session->NID, sizeof (request->NID));
  116. request->NEWEKS = SLAC_CM_SETKEY_EKS;
  117. memcpy (request->NEWKEY, session->NMK, sizeof (request->NEWKEY));
  118. #if SLAC_DEBUG
  119. if (_anyset (session->flags, SLAC_VERBOSE))
  120. {
  121. char string [1024];
  122. slac_debug (session, 0, __func__, "CM_SET_KEY.KEYTYPE %d", request->KEYTYPE);
  123. slac_debug (session, 0, __func__, "CM_SET_KEY.MYNOUNCE %s", hexstring (string, sizeof (string), & request->MYNOUNCE, sizeof (request->MYNOUNCE)));
  124. slac_debug (session, 0, __func__, "CM_SET_KEY.YOURNOUNCE %s", hexstring (string, sizeof (string), & request->YOURNOUNCE, sizeof (request->MYNOUNCE)));
  125. slac_debug (session, 0, __func__, "CM_SET_KEY.PID %d", request->PID);
  126. slac_debug (session, 0, __func__, "CM_SET_KEY.PRN %d", LE32TOH (request->PRN));
  127. slac_debug (session, 0, __func__, "CM_SET_KEY.PMN %d", request->PMN);
  128. slac_debug (session, 0, __func__, "CM_SET_KEY.CCoCAP %d", request->CCOCAP);
  129. slac_debug (session, 0, __func__, "CM_SET_KEY.NID %s", HEXSTRING (string, request->NID));
  130. slac_debug (session, 0, __func__, "CM_SET_KEY.NEWEKS %d", request->NEWEKS);
  131. slac_debug (session, 0, __func__, "CM_SET_KEY.NEWKEY %s", HEXSTRING (string, request->NEWKEY));
  132. }
  133. #endif
  134. if (sendpacket (channel, request, sizeof (* request)) <= 0)
  135. {
  136. return (slac_debug (session, 1, __func__, CHANNEL_CANTSEND));
  137. }
  138. while (readpacket (channel, confirm, sizeof (* confirm)) > 0)
  139. {
  140. if (ntohs (confirm->ethernet.MTYPE) != ETH_P_HPAV)
  141. {
  142. slac_debug (session, session->exit, __func__, "Ignore MTYPE 0x%04X", htons (confirm->ethernet.MTYPE));
  143. continue;
  144. }
  145. if (confirm->homeplug.MMV != HOMEPLUG_MMV)
  146. {
  147. slac_debug (session, session->exit, __func__, "Ignore MMV 0x%02X", confirm->homeplug.MMV);
  148. continue;
  149. }
  150. if (LE32TOH (confirm->homeplug.MMTYPE) != (CM_SET_KEY | MMTYPE_CNF))
  151. {
  152. slac_debug (session, session->exit, __func__, "Ignore MMTYPE 0x%04X", LE32TOH (confirm->homeplug.MMTYPE));
  153. continue;
  154. }
  155. slac_debug (session, 0, __func__, "<-- CM_SET_KEY.CNF");
  156. if (! confirm->RESULT)
  157. {
  158. return (slac_debug (session, session->exit, __func__, "Device refused request"));
  159. }
  160. #if SLAC_DEBUG
  161. if (_anyset (session->flags, SLAC_VERBOSE))
  162. {
  163. char string [1024];
  164. slac_debug (session, 0, __func__, "CM_SET_KEY.RESULT %d", confirm->RESULT);
  165. slac_debug (session, 0, __func__, "CM_SET_KEY.MYNOUNCE %s", hexstring (string, sizeof (string), & confirm->MYNOUNCE, sizeof (confirm->MYNOUNCE)));
  166. slac_debug (session, 0, __func__, "CM_SET_KEY.YOURNOUNCE %s", hexstring (string, sizeof (string), & confirm->YOURNOUNCE, sizeof (confirm->MYNOUNCE)));
  167. slac_debug (session, 0, __func__, "CM_SET_KEY.PID %d", confirm->PID);
  168. slac_debug (session, 0, __func__, "CM_SET_KEY.PRN %d", LE32TOH (confirm->PRN));
  169. slac_debug (session, 0, __func__, "CM_SET_KEY.PMN %d", confirm->PMN);
  170. slac_debug (session, 0, __func__, "CM_SET_KEY.CCoCAP %d", confirm->CCOCAP);
  171. }
  172. #endif
  173. return (0);
  174. }
  175. return (slac_debug (session, session->exit, __func__, "<-- CM_SET_KEY.REQ ?"));
  176. }
  177. #endif