cms demos: print signingTime attributes
authorJames Muir <james@openssl.org>
Fri, 3 Nov 2023 17:15:04 +0000 (13:15 -0400)
committerTomas Mraz <tomas@openssl.org>
Fri, 10 Nov 2023 12:06:46 +0000 (13:06 +0100)
Add a makefile for the cms demos, and add a routine to cms_ver.c to
print any signingTime attributes from the CMS_ContentInfo object.
This provides an example that could be extended if an application
wants to examine the purported signing times.

Part of #8026

Testing:

  $ cd demos/cms
  $ make test

Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/22618)

demos/cms/Makefile [new file with mode: 0644]
demos/cms/cms_dec.c
demos/cms/cms_enc.c
demos/cms/cms_sign2.c
demos/cms/cms_ver.c

diff --git a/demos/cms/Makefile b/demos/cms/Makefile
new file mode 100644 (file)
index 0000000..7c8f30d
--- /dev/null
@@ -0,0 +1,35 @@
+#
+# To run the demos when linked with a shared library (default) ensure that
+# libcrypto is on the library path. For example, to run the
+# cms_enc demo:
+#
+#    LD_LIBRARY_PATH=../.. ./cms_enc
+
+TESTS = cms_comp \
+        cms_ddec \
+        cms_dec \
+        cms_denc \
+        cms_enc \
+        cms_sign \
+        cms_sign2 \
+        cms_uncomp \
+        cms_ver
+
+CFLAGS  = -I../../include -g
+LDFLAGS = -L../..
+LDLIBS  = -lcrypto
+
+all: $(TESTS)
+
+clean:
+       $(RM) $(TESTS) *.o
+
+cms_%: cms_%.c
+       $(CC) $(CFLAGS) $(LDFLAGS) -o "$@" "$<" $(LDLIBS)
+
+test: all
+       @echo "\nCMS tests:"
+       LD_LIBRARY_PATH=../.. ./cms_enc
+       LD_LIBRARY_PATH=../.. ./cms_dec
+       LD_LIBRARY_PATH=../.. ./cms_sign2
+       LD_LIBRARY_PATH=../.. ./cms_ver
index ebc34a5f94cbba169ed6b67d9e503122961b9475..f64a68ab42bc8100a1ef3ef9bdf1165dc7f19ba6 100644 (file)
@@ -59,6 +59,8 @@ int main(int argc, char **argv)
     if (!CMS_decrypt(cms, rkey, rcert, NULL, out, 0))
         goto err;
 
+    printf("Decryption Successful\n");
+
     ret = EXIT_SUCCESS;
 
  err:
index a0af2c47742a5e2efd6390238067f5358495afbc..1f69571a17fc5e00a01f371807fc1d5020a59537 100644 (file)
@@ -73,6 +73,8 @@ int main(int argc, char **argv)
     if (!SMIME_write_CMS(out, cms, in, flags))
         goto err;
 
+    printf("Encryption Successful\n");
+
     ret = EXIT_SUCCESS;
  err:
     if (ret != EXIT_SUCCESS) {
index b10043f92137ffe15c06163ccf341e34d6fe99fb..61d9f8bbe83ed83bad60aac582766b3a8c5ef045 100644 (file)
@@ -77,6 +77,8 @@ int main(int argc, char **argv)
     if (!SMIME_write_CMS(out, cms, in, CMS_STREAM))
         goto err;
 
+    printf("Signing Successful\n");
+
     ret = EXIT_SUCCESS;
  err:
     if (ret != EXIT_SUCCESS) {
index f7d3a9bc854318c95bb634c6ac18e0113286bf0a..43e9d0985408dfde81eb9f7852f7465185ae7237 100644 (file)
 #include <openssl/cms.h>
 #include <openssl/err.h>
 
+/*
+ * print any signingTime attributes.
+ * signingTime is when each party purportedly signed the message.
+ */
+static void print_signingTime(CMS_ContentInfo *cms)
+{
+    STACK_OF(CMS_SignerInfo) *sis;
+    CMS_SignerInfo *si;
+    X509_ATTRIBUTE *attr;
+    ASN1_TYPE *t;
+    ASN1_UTCTIME *utctime;
+    ASN1_GENERALIZEDTIME *gtime;
+    BIO *b;
+    int i, loc;
+
+    b = BIO_new_fp(stdout, BIO_NOCLOSE | BIO_FP_TEXT);
+    sis = CMS_get0_SignerInfos(cms);
+    for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++) {
+        si = sk_CMS_SignerInfo_value(sis, i);
+        loc = CMS_signed_get_attr_by_NID(si, NID_pkcs9_signingTime, -1);
+        attr = CMS_signed_get_attr(si, loc);
+        t = X509_ATTRIBUTE_get0_type(attr, 0);
+        if (t == NULL)
+            continue;
+        switch (t->type) {
+        case V_ASN1_UTCTIME:
+            utctime = t->value.utctime;
+            ASN1_UTCTIME_print(b, utctime);
+            break;
+        case V_ASN1_GENERALIZEDTIME:
+            gtime = t->value.generalizedtime;
+            ASN1_GENERALIZEDTIME_print(b, gtime);
+            break;
+        default:
+            fprintf(stderr, "unrecognized signingTime type\n");
+            break;
+        }
+        BIO_printf(b, ": signingTime from SignerInfo %i\n", i);
+    }
+    BIO_free(b);
+    return;
+}
+
 int main(int argc, char **argv)
 {
     BIO *in = NULL, *out = NULL, *tbio = NULL, *cont = NULL;
@@ -56,6 +99,8 @@ int main(int argc, char **argv)
     if (cms == NULL)
         goto err;
 
+    print_signingTime(cms);
+
     /* File to output verified content to */
     out = BIO_new_file("smver.txt", "w");
     if (out == NULL)
@@ -66,9 +111,10 @@ int main(int argc, char **argv)
         goto err;
     }
 
-    fprintf(stderr, "Verification Successful\n");
+    printf("Verification Successful\n");
 
     ret = EXIT_SUCCESS;
+
  err:
     if (ret != EXIT_SUCCESS) {
         fprintf(stderr, "Error Verifying Data\n");