Skip to content
This repository was archived by the owner on Sep 30, 2022. It is now read-only.

Commit ab1ab02

Browse files
committed
add OpenSSL 1.0.2 backport patch
1 parent 17d039b commit ab1ab02

File tree

1 file changed

+297
-0
lines changed

1 file changed

+297
-0
lines changed

0001-backport-to-OpenSSL-1.0.2.patch

+297
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
1+
From e1d5cca98f3ed30613635b6fd718c92044294adb Mon Sep 17 00:00:00 2001
2+
From: Jan Luebbe <[email protected]>
3+
Date: Tue, 1 Dec 2020 12:47:37 +0100
4+
Subject: [PATCH] backport to OpenSSL 1.0.2
5+
6+
This is partially a revert of commit
7+
3c6470ba7c2adbf51e5eaf4601e4affbab0c15c5.
8+
9+
Signed-off-by: Jan Luebbe <[email protected]>
10+
---
11+
configure.ac | 2 +-
12+
m4/ax_check_openssl.m4 | 124 +++++++++++++++++++++++++++++++++++++++++
13+
src/signature.c | 36 ++++++++++--
14+
src/verity_hash.c | 10 ++++
15+
4 files changed, 167 insertions(+), 5 deletions(-)
16+
create mode 100644 m4/ax_check_openssl.m4
17+
18+
diff --git a/configure.ac b/configure.ac
19+
index 7ba36bff8ee1..df04c49fb741 100644
20+
--- a/configure.ac
21+
+++ b/configure.ac
22+
@@ -84,7 +84,7 @@ AS_IF([test "x$enable_json" != "xno"], [
23+
AC_DEFINE([ENABLE_JSON], [0])
24+
])
25+
26+
-PKG_CHECK_MODULES([OPENSSL], [openssl >= 1.1.1])
27+
+AX_CHECK_OPENSSL([],[AC_MSG_ERROR([OpenSSL not found])])
28+
29+
AC_ARG_ENABLE([gpt],
30+
AS_HELP_STRING([--enable-gpt], [Enable GPT support])
31+
diff --git a/m4/ax_check_openssl.m4 b/m4/ax_check_openssl.m4
32+
new file mode 100644
33+
index 000000000000..a87c5a6b6f93
34+
--- /dev/null
35+
+++ b/m4/ax_check_openssl.m4
36+
@@ -0,0 +1,124 @@
37+
+# ===========================================================================
38+
+# http://www.gnu.org/software/autoconf-archive/ax_check_openssl.html
39+
+# ===========================================================================
40+
+#
41+
+# SYNOPSIS
42+
+#
43+
+# AX_CHECK_OPENSSL([action-if-found[, action-if-not-found]])
44+
+#
45+
+# DESCRIPTION
46+
+#
47+
+# Look for OpenSSL in a number of default spots, or in a user-selected
48+
+# spot (via --with-openssl). Sets
49+
+#
50+
+# OPENSSL_INCLUDES to the include directives required
51+
+# OPENSSL_LIBS to the -l directives required
52+
+# OPENSSL_LDFLAGS to the -L or -R flags required
53+
+#
54+
+# and calls ACTION-IF-FOUND or ACTION-IF-NOT-FOUND appropriately
55+
+#
56+
+# This macro sets OPENSSL_INCLUDES such that source files should use the
57+
+# openssl/ directory in include directives:
58+
+#
59+
+# #include <openssl/hmac.h>
60+
+#
61+
+# LICENSE
62+
+#
63+
+# Copyright (c) 2009,2010 Zmanda Inc. <http://www.zmanda.com/>
64+
+# Copyright (c) 2009,2010 Dustin J. Mitchell <[email protected]>
65+
+#
66+
+# Copying and distribution of this file, with or without modification, are
67+
+# permitted in any medium without royalty provided the copyright notice
68+
+# and this notice are preserved. This file is offered as-is, without any
69+
+# warranty.
70+
+
71+
+#serial 8
72+
+
73+
+AU_ALIAS([CHECK_SSL], [AX_CHECK_OPENSSL])
74+
+AC_DEFUN([AX_CHECK_OPENSSL], [
75+
+ found=false
76+
+ AC_ARG_WITH([openssl],
77+
+ [AS_HELP_STRING([--with-openssl=DIR],
78+
+ [root of the OpenSSL directory])],
79+
+ [
80+
+ case "$withval" in
81+
+ "" | y | ye | yes | n | no)
82+
+ AC_MSG_ERROR([Invalid --with-openssl value])
83+
+ ;;
84+
+ *) ssldirs="$withval"
85+
+ ;;
86+
+ esac
87+
+ ], [
88+
+ # if pkg-config is installed and openssl has installed a .pc file,
89+
+ # then use that information and don't search ssldirs
90+
+ AC_PATH_PROG([PKG_CONFIG], [pkg-config])
91+
+ if test x"$PKG_CONFIG" != x""; then
92+
+ OPENSSL_LDFLAGS=`$PKG_CONFIG openssl --libs-only-L 2>/dev/null`
93+
+ if test $? = 0; then
94+
+ OPENSSL_LIBS=`$PKG_CONFIG openssl --libs-only-l 2>/dev/null`
95+
+ OPENSSL_INCLUDES=`$PKG_CONFIG openssl --cflags-only-I 2>/dev/null`
96+
+ found=true
97+
+ fi
98+
+ fi
99+
+
100+
+ # no such luck; use some default ssldirs
101+
+ if ! $found; then
102+
+ ssldirs="/usr/local/ssl /usr/lib/ssl /usr/ssl /usr/pkg /usr/local /usr"
103+
+ fi
104+
+ ]
105+
+ )
106+
+
107+
+
108+
+ # note that we #include <openssl/foo.h>, so the OpenSSL headers have to be in
109+
+ # an 'openssl' subdirectory
110+
+
111+
+ if ! $found; then
112+
+ OPENSSL_INCLUDES=
113+
+ for ssldir in $ssldirs; do
114+
+ AC_MSG_CHECKING([for openssl/ssl.h in $ssldir])
115+
+ if test -f "$ssldir/include/openssl/ssl.h"; then
116+
+ OPENSSL_INCLUDES="-I$ssldir/include"
117+
+ OPENSSL_LDFLAGS="-L$ssldir/lib"
118+
+ OPENSSL_LIBS="-lssl -lcrypto"
119+
+ found=true
120+
+ AC_MSG_RESULT([yes])
121+
+ break
122+
+ else
123+
+ AC_MSG_RESULT([no])
124+
+ fi
125+
+ done
126+
+
127+
+ # if the file wasn't found, well, go ahead and try the link anyway -- maybe
128+
+ # it will just work!
129+
+ fi
130+
+
131+
+ # try the preprocessor and linker with our new flags,
132+
+ # being careful not to pollute the global LIBS, LDFLAGS, and CPPFLAGS
133+
+
134+
+ AC_MSG_CHECKING([whether compiling and linking against OpenSSL works])
135+
+ echo "Trying link with OPENSSL_LDFLAGS=$OPENSSL_LDFLAGS;" \
136+
+ "OPENSSL_LIBS=$OPENSSL_LIBS; OPENSSL_INCLUDES=$OPENSSL_INCLUDES" >&AS_MESSAGE_LOG_FD
137+
+
138+
+ save_LIBS="$LIBS"
139+
+ save_LDFLAGS="$LDFLAGS"
140+
+ save_CPPFLAGS="$CPPFLAGS"
141+
+ LDFLAGS="$LDFLAGS $OPENSSL_LDFLAGS"
142+
+ LIBS="$OPENSSL_LIBS $LIBS"
143+
+ CPPFLAGS="$OPENSSL_INCLUDES $CPPFLAGS"
144+
+ AC_LINK_IFELSE(
145+
+ [AC_LANG_PROGRAM([#include <openssl/ssl.h>], [SSL_new(NULL)])],
146+
+ [
147+
+ AC_MSG_RESULT([yes])
148+
+ $1
149+
+ ], [
150+
+ AC_MSG_RESULT([no])
151+
+ $2
152+
+ ])
153+
+ CPPFLAGS="$save_CPPFLAGS"
154+
+ LDFLAGS="$save_LDFLAGS"
155+
+ LIBS="$save_LIBS"
156+
+
157+
+ AC_SUBST([OPENSSL_INCLUDES])
158+
+ AC_SUBST([OPENSSL_LIBS])
159+
+ AC_SUBST([OPENSSL_LDFLAGS])
160+
+])
161+
diff --git a/src/signature.c b/src/signature.c
162+
index a4a2b14e20dc..a643a9650160 100644
163+
--- a/src/signature.c
164+
+++ b/src/signature.c
165+
@@ -1,3 +1,5 @@
166+
+#include <stdint.h>
167+
+
168+
#include <openssl/asn1.h>
169+
#include <openssl/cms.h>
170+
#include <openssl/conf.h>
171+
@@ -7,10 +9,19 @@
172+
#include <openssl/crypto.h>
173+
#include <openssl/engine.h>
174+
#include <openssl/x509.h>
175+
+#include <openssl/x509v3.h>
176+
177+
#include "context.h"
178+
#include "signature.h"
179+
180+
+/* Define for OpenSSL 1.0.x backwards compatiblity.
181+
+ * We use newer get0 names to be clear about memory ownership and to not use
182+
+ * API deprecated in OpenSSL 1.1.x */
183+
+#if (OPENSSL_VERSION_NUMBER < 0x10100000L)
184+
+#define X509_get0_notAfter X509_get_notAfter
185+
+#define X509_get0_notBefore X509_get_notBefore
186+
+#endif
187+
+
188+
GQuark r_signature_error_quark(void)
189+
{
190+
return g_quark_from_static_string("r_signature_error_quark");
191+
@@ -23,9 +34,15 @@ static int check_purpose_code_sign(const X509_PURPOSE *xp, const X509 *const_x,
192+
* the ex_ variables have already been calculated by other code when
193+
* we are in this callback. */
194+
X509 *x = (X509 *)const_x;
195+
+#if (OPENSSL_VERSION_NUMBER < 0x10100000L)
196+
+ uint32_t ex_flags = x->ex_flags;
197+
+ uint32_t ex_kusage = (x->ex_flags & EXFLAG_KUSAGE) ? x->ex_kusage : UINT32_MAX;
198+
+ uint32_t ex_xkusage = (x->ex_flags & EXFLAG_XKUSAGE) ? x->ex_xkusage : UINT32_MAX;
199+
+#else
200+
uint32_t ex_flags = X509_get_extension_flags(x);
201+
uint32_t ex_kusage = X509_get_key_usage(x);
202+
uint32_t ex_xkusage = X509_get_extended_key_usage(x);
203+
+#endif
204+
205+
if (ca) {
206+
/* If extended key usage is present, it must contain codeSigning for all
207+
@@ -56,7 +73,11 @@ static int check_purpose_code_sign(const X509_PURPOSE *xp, const X509 *const_x,
208+
gboolean signature_init(GError **error)
209+
{
210+
int ret, id;
211+
-
212+
+#if (OPENSSL_VERSION_NUMBER < 0x10100000L)
213+
+ OPENSSL_config(NULL);
214+
+ OpenSSL_add_all_algorithms();
215+
+ ERR_load_crypto_strings();
216+
+#else
217+
g_return_val_if_fail(error == FALSE || *error == NULL, FALSE);
218+
219+
ret = OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
220+
@@ -74,6 +95,7 @@ gboolean signature_init(GError **error)
221+
(flags & ERR_TXT_STRING) ? data : ERR_error_string(err, NULL));
222+
return FALSE;
223+
}
224+
+#endif
225+
226+
id = X509_PURPOSE_get_count() + 1;
227+
if (X509_PURPOSE_get_by_id(id) >= 0) {
228+
@@ -86,7 +108,9 @@ gboolean signature_init(GError **error)
229+
}
230+
231+
/* X509_TRUST_OBJECT_SIGN maps to the Code Signing ID (via OpenSSL's NID_code_sign) */
232+
- ret = X509_PURPOSE_add(id, X509_TRUST_OBJECT_SIGN, 0, check_purpose_code_sign, "Code signing", "codesign", NULL);
233+
+ /* X509_PURPOSE_add calls BUF_strdup on the string arguments and they
234+
+ * are const in newer OpenSSL versions. */
235+
+ ret = X509_PURPOSE_add(id, X509_TRUST_OBJECT_SIGN, 0, check_purpose_code_sign, (char *)"Code signing", (char *)"codesign", NULL);
236+
if (!ret) {
237+
unsigned long err;
238+
const gchar *data;
239+
@@ -439,8 +463,12 @@ X509_STORE* setup_x509_store(const gchar *capath, const gchar *cadir, GError **e
240+
241+
/* Enable purpose checking if configured */
242+
if (check_purpose) {
243+
- const X509_PURPOSE *xp = X509_PURPOSE_get0(X509_PURPOSE_get_by_sname(check_purpose));
244+
- if (!xp || !X509_STORE_set_purpose(store, X509_PURPOSE_get_id(xp))) {
245+
+ /* X509_PURPOSE_get0 calls only strcmp on the string argument and
246+
+ * it is const in newer OpenSSL versions. */
247+
+ const X509_PURPOSE *xp = X509_PURPOSE_get0(X509_PURPOSE_get_by_sname((char *)check_purpose));
248+
+ /* X509_PURPOSE_get_id calls only returns an int field of the
249+
+ * X509_PURPOSE it is const in newer OpenSSL versions. */
250+
+ if (!xp || !X509_STORE_set_purpose(store, X509_PURPOSE_get_id((X509_PURPOSE *)xp))) {
251+
g_set_error(
252+
error,
253+
R_SIGNATURE_ERROR,
254+
diff --git a/src/verity_hash.c b/src/verity_hash.c
255+
index bc53e21952db..58493bc832d5 100644
256+
--- a/src/verity_hash.c
257+
+++ b/src/verity_hash.c
258+
@@ -23,6 +23,7 @@
259+
#include <stdlib.h>
260+
#include <string.h>
261+
#include <stdint.h>
262+
+#include <inttypes.h>
263+
#include <glib.h>
264+
265+
#include <openssl/bio.h>
266+
@@ -80,11 +81,18 @@ static int verify_hash_block(
267+
{
268+
/* SHA256, version 1 only */
269+
EVP_MD_CTX *mdctx;
270+
+#if (OPENSSL_VERSION_NUMBER < 0x10100000L)
271+
+ EVP_MD_CTX mdctx_stack;
272+
+#endif
273+
uint8_t tmp[EVP_MAX_MD_SIZE];
274+
unsigned int tmp_size = 0;
275+
int r = 0;
276+
277+
+#if (OPENSSL_VERSION_NUMBER < 0x10100000L)
278+
+ mdctx = &mdctx_stack;
279+
+#else
280+
mdctx = EVP_MD_CTX_new();
281+
+#endif
282+
if (EVP_DigestInit(mdctx, EVP_sha256()) != 1) {
283+
g_message("init failed");
284+
r = -EINVAL;
285+
@@ -116,7 +124,9 @@ static int verify_hash_block(
286+
out:
287+
if (r)
288+
ERR_print_errors_fp(stderr);
289+
+#if !(OPENSSL_VERSION_NUMBER < 0x10100000L)
290+
EVP_MD_CTX_free(mdctx);
291+
+#endif
292+
return r;
293+
}
294+
295+
--
296+
2.20.1
297+

0 commit comments

Comments
 (0)