@@ -35,7 +35,7 @@ typedef struct etsiSvcCtx {
3535 pthread_mutex_t lock ; /* queue lock */
3636 pthread_t thread ; /* key gen worker */
3737} etsiSvcCtx ;
38- static etsiSvcCtx svcCtx ;
38+ static etsiSvcCtx gSvcCtx ;
3939
4040/* the top level service */
4141static svcInfo etsiService = {
@@ -57,14 +57,14 @@ static svcInfo etsiService = {
5757 .caBuffer = NULL ,
5858 .caBufferSz = 0 ,
5959
60- .svcCtx = & svcCtx ,
60+ .svcCtx = & gSvcCtx ,
6161};
6262
6363/* worker thread objects */
6464typedef struct etsiSvcThread {
65- word32 index ;
66- byte * httpRspBuf ;
67- word32 httpRspSz ;
65+ word32 index ;
66+ byte * httpRspBuf ;
67+ word32 httpRspSz ;
6868} etsiSvcThread ;
6969
7070typedef struct etsiSvcConn {
@@ -109,16 +109,26 @@ static int SetupKeyPackage(etsiSvcCtx* svcCtx, etsiSvcThread* etsiThread)
109109 int ret = 0 ;
110110 byte rsp [ETSI_MAX_RESPONSE_SZ ], keyBuf [ECC_BUFSIZE ];
111111 word32 rspSz = (word32 )sizeof (rsp ), keyBufSz = (word32 )sizeof (keyBuf );
112- HttpHeader headers [2 ];
112+ char expiresStr [100 ];
113+ HttpHeader headers [3 ];
113114 headers [0 ].type = HTTP_HDR_CONTENT_TYPE ;
114115 headers [0 ].string = "application/pkcs8" ;
115116 headers [1 ].type = HTTP_HDR_CONNECTION ;
116117 headers [1 ].string = "Keep-Alive" ;
117- /* TODO: Add key expiration using HTTP_HDR_EXPIRES */
118- /* Example "Expires: Wed, 21 Oct 2015 07:28:00 GMT" */
118+ headers [2 ].type = HTTP_HDR_EXPIRES ;
119+ headers [2 ].string = expiresStr ;
120+ memset (expiresStr , 0 , sizeof (expiresStr ));
119121
120122 pthread_mutex_lock (& svcCtx -> lock );
123+ XLOG (WOLFKM_LOG_DEBUG , "Synchronizing key to worker thread\n" );
121124 if (etsiThread -> index != svcCtx -> index ) {
125+ /* Format Expires Time */
126+ time_t t = wolfGetCurrentTimeT ();
127+ struct tm tm ;
128+ t += svcCtx -> renewSec ; /* offset by key renewal period */
129+ localtime_r (& t , & tm );
130+ strftime (expiresStr , sizeof (expiresStr ), HTTP_DATE_FMT , & tm );
131+
122132 /* Export as DER IETF RFC 5915 */
123133 ret = wc_EccKeyToDer (& svcCtx -> key , keyBuf , keyBufSz );
124134 if (ret < 0 ) {
@@ -259,14 +269,13 @@ void wolfEtsiSvc_ConnClose(svcConn* conn)
259269
260270int wolfEtsiSvc_DoNotify (svcConn * conn )
261271{
262- int ret = 0 ;
272+ int ret ;
263273 svcInfo * svc ;
264274 etsiSvcCtx * svcCtx ;
265275 etsiSvcThread * etsiThread ;
266276 etsiSvcConn * etsiConn ;
267277
268- if (conn == NULL || conn -> stream == NULL || conn -> svc == NULL ||
269- conn -> svcThreadCtx == NULL || conn -> svcConnCtx == NULL ) {
278+ if (conn == NULL || conn -> svc == NULL || conn -> svcThreadCtx == NULL ) {
270279 XLOG (WOLFKM_LOG_ERROR , "Bad ETSI notify pointers\n" );
271280 return WOLFKM_BAD_ARGS ;
272281 }
@@ -276,13 +285,14 @@ int wolfEtsiSvc_DoNotify(svcConn* conn)
276285 etsiThread = (etsiSvcThread * )conn -> svcThreadCtx ;
277286 etsiConn = (etsiSvcConn * )conn -> svcConnCtx ;
278287
279- if (etsiConn -> req .type == HTTP_METHOD_PUT ) {
280- /* updated key */
281- ret = SetupKeyPackage (svcCtx , etsiThread );
282- if (ret == 0 ) {
283- /* send updated key */
284- ret = wolfEtsiSvc_DoResponse (conn );
285- }
288+ /* update key */
289+ ret = SetupKeyPackage (svcCtx , etsiThread );
290+
291+ /* push key to active push threads */
292+ if (ret == 0 && etsiConn != NULL &&
293+ etsiConn -> req .type == HTTP_METHOD_PUT ) {
294+ /* send updated key */
295+ ret = wolfEtsiSvc_DoResponse (conn );
286296 }
287297
288298 return ret ;
0 commit comments