cmp_mock_srv.c: Fix polling mode such that it can be done multiple times
authorDr. David von Oheimb <David.von.Oheimb@siemens.com>
Mon, 21 Jun 2021 11:54:32 +0000 (13:54 +0200)
committerDr. David von Oheimb <dev@ddvo.net>
Wed, 23 Jun 2021 15:20:49 +0000 (17:20 +0200)
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15848)

apps/lib/cmp_mock_srv.c

index 669e695fdc69bcae6b474ccd0e1ff55b31942b74..cf82000c7f02f20b50284ceca5ed908c6e371b3a 100644 (file)
@@ -26,6 +26,7 @@ typedef struct
     OSSL_CMP_MSG *certReq;     /* ir/cr/p10cr/kur remembered while polling */
     int certReqId;             /* id of last ir/cr/kur, used for polling */
     int pollCount;             /* number of polls before actual cert response */
+    int curr_pollCount;        /* number of polls so far for current request */
     int checkAfterTime;        /* time the client should wait between polling */
 } mock_srv_ctx;
 
@@ -195,13 +196,22 @@ static OSSL_CMP_PKISI *process_cert_request(OSSL_CMP_SRV_CTX *srv_ctx,
     *chainOut = NULL;
     *caPubs = NULL;
     ctx->certReqId = certReqId;
-    if (ctx->pollCount > 0) {
-        ctx->pollCount--;
-        OSSL_CMP_MSG_free(ctx->certReq);
+
+    if (ctx->pollCount > 0 && ctx->curr_pollCount == 0) {
+        /* start polling */
+        if (ctx->certReq != NULL) {
+            /* already in polling mode */
+            ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
+            return NULL;
+        }
         if ((ctx->certReq = OSSL_CMP_MSG_dup(cert_req)) == NULL)
             return NULL;
         return OSSL_CMP_STATUSINFO_new(OSSL_CMP_PKISTATUS_waiting, 0, NULL);
     }
+    if (ctx->curr_pollCount >= ctx->pollCount)
+        /* give final response after polling */
+        ctx->curr_pollCount = 0;
+
     if (ctx->certOut != NULL
             && (*certOut = X509_dup(ctx->certOut)) == NULL)
         goto err;
@@ -369,18 +379,24 @@ static int process_pollReq(OSSL_CMP_SRV_CTX *srv_ctx,
         ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT);
         return 0;
     }
-    if (ctx->sendError || ctx->certReq == NULL) {
+    if (ctx->sendError) {
         *certReq = NULL;
         ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
         return 0;
     }
+    if (ctx->certReq == NULL) {
+        /* not currently in polling mode */
+        *certReq = NULL;
+        ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PKIBODY);
+        return 0;
+    }
 
-    if (ctx->pollCount == 0) {
+    if (++ctx->curr_pollCount >= ctx->pollCount) {
+        /* end polling */
         *certReq = ctx->certReq;
         ctx->certReq = NULL;
         *check_after = 0;
     } else {
-        ctx->pollCount--;
         *certReq = NULL;
         *check_after = ctx->checkAfterTime;
     }