diff --git a/OpenSSL_version.c b/OpenSSL_version.c index 010c103..39bf776 100644 --- a/OpenSSL_version.c +++ b/OpenSSL_version.c @@ -13,29 +13,42 @@ #include #include -#if OPENSSL_VERSION_NUMBER < 0x10002000L -#error Should not use OpenSSL versions older than 1.0.2. They are unsupported and insecure. +#if OPENSSL_VERSION_NUMBER < 0x30000000L + #error Should not use OpenSSL versions older than 3. They are unsupported and insecure. #endif + #if OPENSSL_VERSION_NUMBER < 0x10100000L -#define OpenSSL_version_num SSLeay + #define OpenSSL_version_num SSLeay + #define DEBUG_MESSAGE "[DEBUG] Using SSLeay() for OpenSSL version\n" #elif OPENSSL_VERSION_NUMBER >= 0x30000000L -#define OpenSSL_version_num() ((unsigned long) \ - ((OPENSSL_version_major()<<28) \ - |(OPENSSL_version_minor()<<20) \ - |(OPENSSL_version_patch()<< 4) \ - |_OPENSSL_VERSION_PRE_RELEASE )) + #define OpenSSL_version_num() ((unsigned long) \ + ((OPENSSL_version_major()<<28) \ + |(OPENSSL_version_minor()<<20) \ + |(OPENSSL_version_patch()<< 4) \ + |_OPENSSL_VERSION_PRE_RELEASE )) + #define DEBUG_MESSAGE "[DEBUG] Defining ourselves OpenSSL_version_num() for OpenSSL version\n" +#else + #define DEBUG_MESSAGE "[DEBUG] Using existing OpenSSL_version_num() for OpenSSL version\n" + #endif int main(int argc, char *argv[]) { + fprintf(stderr, "[DEBUG] Starting OpenSSL version check\n"); + fprintf(stderr, DEBUG_MESSAGE); + + unsigned long static_version = (unsigned long)OPENSSL_VERSION_NUMBER; + fprintf(stderr, "[DEBUG] OPENSSL_VERSION_NUMBER: 0x%lx\n", static_version); + unsigned long runtime_version = OpenSSL_version_num(); + fprintf(stderr, "[DEBUG] runtime_version: 0x%lx\n", runtime_version); + #define MAJOR_MINOR_MASK 0xfff00000L - if ((MAJOR_MINOR_MASK & runtime_version ) != - (MAJOR_MINOR_MASK & OPENSSL_VERSION_NUMBER)) { - fprintf(stderr, "OpenSSL runtime version 0x%lx does not match version 0x%lx used by compiler\n", - runtime_version, (unsigned long)OPENSSL_VERSION_NUMBER); + if ((MAJOR_MINOR_MASK & runtime_version ) != (MAJOR_MINOR_MASK & OPENSSL_VERSION_NUMBER)) { + fprintf(stderr, "OpenSSL runtime version 0x%lx does not match version 0x%lx used by compiler\n", runtime_version, static_version); return EXIT_FAILURE; } + fprintf(stdout, "%s (0x%lx)\n", OPENSSL_VERSION_TEXT, runtime_version); return EXIT_SUCCESS; } diff --git a/OpenSSL_version.mk b/OpenSSL_version.mk index bbac73c..d270e78 100644 --- a/OpenSSL_version.mk +++ b/OpenSSL_version.mk @@ -1,80 +1,107 @@ ifeq ($(LIB),) + $(warning [DEBUG] LIB is empty or not set) - -ifeq ($(OS),Windows_NT) - EXE=.exe - LIB=bin -else - EXE= - LIB=lib - ifeq ($(shell uname -s),Darwin) - OS=MacOS - endif -endif - -#CC=gcc -ifneq ($(OPENSSL_DIR),) - ifeq ($(shell echo $(OPENSSL_DIR) | grep "^/"),) - # $(OPENSSL_DIR) is relative path - OPENSSL_LIB=$(OPENSSL_DIR) + ifeq ($(OS),Windows_NT) + $(warning [DEBUG] OS is Windows_NT) + EXE = .exe + LIB = bin else - # $(OPENSSL_DIR) is absolute path - OPENSSL_LIB=$(OPENSSL_DIR)/$(LIB) + $(warning [DEBUG] OS is supposed to be Unix-like) + EXE = + LIB = lib + ifeq ($(shell uname -s),Darwin) + $(warning [DEBUG] OS is Darwin (MacOS)) + OS = MacOS + endif endif + CFLAGS+=-isystem $(OPENSSL_DIR)/include LDFLAGS+=-L$(OPENSSL_DIR) -L$(OPENSSL_LIB) -Wl,-rpath,$(OPENSSL_DIR) -Wl,-rpath,$(OPENSSL_LIB) -endif -LDLIBS=-lcrypto + LDLIBS=-lcrypto + + # CC = gcc + ifneq ($(OPENSSL_DIR),) + $(warning [DEBUG] OPENSSL_DIR is set: $(OPENSSL_DIR)) + ifeq ($(shell echo $(OPENSSL_DIR) | grep "^/"),) + $(warning [DEBUG] OPENSSL_DIR is a relative path) + # $(OPENSSL_DIR) is relative path + OPENSSL_LIB = $(OPENSSL_DIR) + else + $(warning [DEBUG] OPENSSL_DIR is an absolute path) + # $(OPENSSL_DIR) is absolute path + OPENSSL_LIB = $(OPENSSL_DIR)/$(LIB) + endif + $(warning [TRACE] After OPENSSL_DIR check: OPENSSL_LIB="$(OPENSSL_LIB)") + CFLAGS += -isystem $(OPENSSL_DIR)/include + LDFLAGS += -L$(OPENSSL_DIR) -L$(OPENSSL_LIB) -Wl,-rpath,$(OPENSSL_DIR) -Wl,-rpath,$(OPENSSL_LIB) + endif + + LDLIBS = -lcrypto -.phony: default build show clean + $(warning [TRACE] After OPENSSL_DIR set: EXE=$(EXE)) + $(warning [TRACE] After OPENSSL_DIR set: LIB=$(LIB)) + $(warning [TRACE] After OPENSSL_DIR set: OPENSSL_LIB=$(OPENSSL_LIB)) + $(warning [TRACE] After OPENSSL_DIR set: CFLAGS=$(CFLAGS)) + $(warning [TRACE] After OPENSSL_DIR set: LDFLAGS=$(LDFLAGS)) + $(warning [TRACE] After OPENSSL_DIR set: LDLIBS=$(LDLIBS)) -default: build show clean + .PHONY: default build show clean -build: OpenSSL_version + default: build show clean -show: build + build: OpenSSL_version + + show: build @./OpenSSL_version$(EXE) -clean: + clean: @rm -f OpenSSL_version$(EXE) - else ifeq ($(LIB),header) + $(warning [DEBUG] LIB is set to header) + OPENSSL_NUMBER_SEL = head -n 1 | sed -r 's/.*OpenSSL //' | awk '{print ($$0+0)}' + OPENSSLV_H = $(OPENSSL_DIR)/include/openssl/opensslv.h + ifeq ($(shell fgrep OPENSSL_VERSION_MAJOR "$(OPENSSLV_H)"),) + $(warning [DEBUG] OPENSSL_VERSION_MAJOR not found in OPENSSLV_H) + OPENSSL_VERSION = $(shell grep 'OPENSSL_VERSION_TEXT\s* "OpenSSL ' "$(OPENSSLV_H)" | $(OPENSSL_NUMBER_SEL)) + else + $(warning [DEBUG] OPENSSL_VERSION_MAJOR found in OPENSSLV_H) + ifeq ($(OS),MacOS) + $(warning [DEBUG] OS is MacOS) + OPENSSL_VERSION = $(shell fgrep OPENSSL_VERSION_M "$(OPENSSLV_H)" | head -n 2 | awk -v RS="" '{print $4"."$8 }') + else + $(warning [DEBUG] OS is not MacOS) + OPENSSL_VERSION = $(shell fgrep OPENSSL_VERSION_M "$(OPENSSLV_H)" | head -n 2 | awk -v RS="" '{print $$4"."$$8 }') + endif + $(warning [TRACE] After OS check in header: OPENSSL_VERSION=$(OPENSSL_VERSION)) + endif -OPENSSL_NUMBER_SEL=head -n 1 | sed -r 's/.*OpenSSL //' | awk '{print ($$0+0)}' -OPENSSLV_H=$(OPENSSL_DIR)/include/openssl/opensslv.h -ifeq ($(shell fgrep OPENSSL_VERSION_MAJOR "$(OPENSSLV_H)"),) - OPENSSL_VERSION=$(shell grep 'OPENSSL_VERSION_TEXT\s* "OpenSSL ' "$(OPENSSLV_H)" | $(OPENSSL_NUMBER_SEL)) -else - ifeq ($(OS),MacOS) - OPENSSL_VERSION=$(shell fgrep OPENSSL_VERSION_M "$(OPENSSLV_H)" | head -n 2 | awk -v RS="" '{print $4"."$8 }') - else - OPENSSL_VERSION=$(shell fgrep OPENSSL_VERSION_M "$(OPENSSLV_H)" | head -n 2 | awk -v RS="" '{print $$4"."$$8 }') - endif -endif - -ifeq ($(OPENSSL_VERSION),1) - OPENSSL_VERSION=1.0 -endif + ifeq ($(OPENSSL_VERSION),1) + OPENSSL_VERSION = 1.0 + endif + $(warning [TRACE] OPENSSL_NUMBER_SEL=$(OPENSSL_NUMBER_SEL)) + $(warning [TRACE] OPENSSLV_H=$(OPENSSLV_H)) + $(warning [TRACE] OPENSSL_VERSION=$(OPENSSL_VERSION)) else # $(LIB) is name of library file + $(warning [DEBUG] LIB is supposed to be a library file: $(LIB)) + OPENSSL_VERSION = $(shell strings "$(LIB)" | grep -E 'OpenSSL [0-9]+\.[0-9]+\.' | head -n 1 | sed -r 's/.*OpenSSL //' | awk -v FS="." '{print $$1"."$$2}') + ifeq ($(OPENSSL_VERSION),) + $(warning [DEBUG] OpenSSL version info not found in library file contents; now trying to get it from the file name) + OPENSSL_VERSION = $(shell strings "$(LIB)" | grep -E 'libcrypto\.' | head -n 1 | sed -r 's/.*libcrypto(.[a-z]+)?\.//') + endif + ifeq ($(OPENSSL_VERSION),1.0.0) + OPENSSL_VERSION = 1.0 + endif -OPENSSL_VERSION=$(shell strings "$(LIB)" | grep -E 'OpenSSL [0-9]+\.[0-9]+\.' | head -n 1 | sed -r 's/.*OpenSSL //' | awk -v FS="." '{print $$1"."$$2}') -ifeq ($(OPENSSL_VERSION),) - OPENSSL_VERSION=$(shell strings "$(LIB)" | grep -E 'libcrypto\.' | head -n 1 | sed -r 's/.*libcrypto(.[a-z]+)?\.//') -endif -ifeq ($(OPENSSL_VERSION),1.0.0) - OPENSSL_VERSION=1.0 -endif - + $(warning [TRACE] OPENSSL_VERSION=$(OPENSSL_VERSION)) endif # $(LIB) - -.phony: detect +.PHONY: detect detect: $(info $(OPENSSL_VERSION)) diff --git a/README.md b/README.md index a8dd739..cba2ea9 100644 --- a/README.md +++ b/README.md @@ -111,13 +111,11 @@ you can execute in a shell on a Unix-like system: ``` git clone https://github.com/siemens/gencmpclient.git cd genCMPClient -make -f OpenSSL_version.mk - +make -s -f OpenSSL_version.mk 2>/dev/null ``` This should output on the console something like ``` -cc [...] OpenSSL_version.c -lcrypto -o OpenSSL_version OpenSSL 3.0.13 30 Jan 2024 (0x300000d0) ``` @@ -129,6 +127,12 @@ make sure that the system-level configuration for finding header and library fil as well as the optional environment variables `OPENSSL_DIR` and `OPENSSL_LIB` described [below](#configuring) are set up in a consistent way. +When having trouble building, which may be due to unsuitably set environment variables, +``` +make -f OpenSSL_version.mk +``` +can provides useful diagnostics. + ## Getting the software