From 4f8f20fd2d54eada135a4f16832386e8abbc38a6 Mon Sep 17 00:00:00 2001
From: Kai KRETSCHMANN <kai@kretschmann.consulting>
Date: Tue, 15 Mar 2022 10:40:26 +0100
Subject: [PATCH] Add check

---
 .gitlab-ci.yml | 10 +++++++++-
 alice.cpp      |  5 +++++
 alice.hpp      |  1 +
 bob.cpp        |  5 +++++
 bob.hpp        |  1 +
 common.cpp     | 15 +++++++++++++++
 common.hpp     |  2 ++
 manual.sh      |  8 ++++++++
 8 files changed, 46 insertions(+), 1 deletion(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 27e3271..db8899e 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -53,5 +53,13 @@ crypt-test-job:   # This job also runs in the test stage.
     - ls -l
     - ./bob
     - ls -l
-    - sleep 60
+    - sleep 30
+    - |
+      if [ -n "$(cmp alice_secret.pem bob_secret.pem)" ]
+      then
+        echo "mismatch"
+        exit 1
+      else
+        echo "OK"
+      fi
     - ps
diff --git a/alice.cpp b/alice.cpp
index c86e9ff..b74845c 100644
--- a/alice.cpp
+++ b/alice.cpp
@@ -22,6 +22,10 @@ string Alice::getMyPubkeyname() {
 	return string("alice_pub.pem");
 }
 
+string Alice::getMyPrefix() {
+	return string("alice");
+}
+
 void Alice::action() {
 
 	/* Generate the parameters to be used */
@@ -42,4 +46,5 @@ void Alice::action() {
 	/* Do something with the shared secret */
 	/* Note secret_size may be less than DH_size(privkey) */
 	dumpSharedSecret();
+	storeSharedSecret(Alice::getMyPrefix());
 }
diff --git a/alice.hpp b/alice.hpp
index deee9a4..d8cbc81 100644
--- a/alice.hpp
+++ b/alice.hpp
@@ -12,5 +12,6 @@ private:
 public:
 	void action();
 	static string getMyPubkeyname();
+	static string getMyPrefix();
 	Alice(std::string name);
 };
diff --git a/bob.cpp b/bob.cpp
index 13bd7ba..4abc7d5 100644
--- a/bob.cpp
+++ b/bob.cpp
@@ -23,6 +23,10 @@ string Bob::getMyPubkeyname() {
         return string("bob_pub.pem");
 }
 
+string Bob::getMyPrefix() {
+	return string("bob");
+}
+
 void Bob::action() {
 
 	/* Generate the parameters to be used */
@@ -43,5 +47,6 @@ void Bob::action() {
 	/* Do something with the shared secret */
 	/* Note secret_size may be less than DH_size(privkey) */
 	dumpSharedSecret();
+	storeSharedSecret(Bob::getMyPrefix());
 }
 
diff --git a/bob.hpp b/bob.hpp
index 4f00c2e..51b8bf1 100644
--- a/bob.hpp
+++ b/bob.hpp
@@ -13,5 +13,6 @@ private:
 public:
 	void action();
 	static string getMyPubkeyname();
+	static string getMyPrefix();
 	Bob(std::string name);
 };
diff --git a/common.cpp b/common.cpp
index 1ea551a..47bc098 100644
--- a/common.cpp
+++ b/common.cpp
@@ -41,6 +41,12 @@ string Exampledh::getMyPubkeyname() {
 	return "N/A";
 }
 
+string Exampledh::getMyPrefix() {
+	BOOST_LOG_TRIVIAL(fatal) << "base class function called for getPrefix";
+	abort();
+	return "N/A";
+}
+
 void Exampledh::handleErrors() {
 	BOOST_LOG_TRIVIAL(fatal) << "error handler";
 	int iError = ERR_get_error();
@@ -132,3 +138,12 @@ void Exampledh::dumpSharedSecret() {
 	BIO_dump_fp(stdout, (const char *)secret, secret_size);
 }
 
+
+void Exampledh::storeSharedSecret(std::string secname) {
+	ofstream fw(secname + "_secret.pem", std::ofstream::out);
+	if(fw.is_open()) {
+		fw << secret;
+		fw.close();
+	}
+}
+
diff --git a/common.hpp b/common.hpp
index de1a8d7..86217f7 100644
--- a/common.hpp
+++ b/common.hpp
@@ -37,6 +37,7 @@ public:
     Exampledh(std::string name);
     ~Exampledh();
     static string getMyPubkeyname();
+    static string getMyPrefix();
 
 protected:
     string myApp;
@@ -57,5 +58,6 @@ protected:
     void storeMyPubkey(std::string pubname);
     void computeSharedSecret();
     void dumpSharedSecret();
+    void storeSharedSecret(std::string secname);
 };
 
diff --git a/manual.sh b/manual.sh
index 3455984..4bcada3 100755
--- a/manual.sh
+++ b/manual.sh
@@ -11,4 +11,12 @@ ls -l
 ./bob
 ls -l
 sleep 10
+
+if [ -n "$(cmp alice_secret.pem bob_secret.pem)" ]
+then
+	echo "mismatch"
+else
+	echo "OK"
+fi
+
 ps
-- 
GitLab