-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed OpenSSL bindings to recognize LibreSSL #5676
Fixed OpenSSL bindings to recognize LibreSSL #5676
Conversation
src/openssl/lib_ssl.cr
Outdated
@@ -2,8 +2,8 @@ require "./lib_crypto" | |||
|
|||
{% begin %} | |||
lib LibSSL | |||
OPENSSL_110 = {{ `command -v pkg-config > /dev/null && pkg-config --atleast-version=1.1.0 libssl || printf %s false`.stringify != "false" }} | |||
OPENSSL_102 = {{ `command -v pkg-config > /dev/null && pkg-config --atleast-version=1.0.2 libssl || printf %s false`.stringify != "false" }} | |||
OPENSSL_110 = {{ LibCrypto::LIBRESSL_VERSION.nil? && (LibCrypto::OPENSSL_VERSION >= 0x1010100) }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just {{ LibCrypto::OPENSSL_110 }}
should work, surely?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, my bad.
I just realized the |
I think I fixed it. Ready for another review :) |
Thanks! Maybe we could add some LIBRESSL_VERSION checks to enable some features, for example ALPN support was added in LibreSSL 2.5.0 (https://github.com/libressl-portable/openbsd/blob/libressl-v2.5.0/src/lib/libcrypto/opensslv.h). |
@ysbaddaden another PR, please. Let's make it compile first before we add new libressl-spcific features. Also, having library-specific features is absolutely the opposite of the direction I want to do. I want to remove the |
FYI, CI fails on OS X with |
@RX14 Please reactor SSL in crystal, that would be awesome! Yet, there is no other way around the issue right now (and for the months to come). Fixing LibreSSL compatibility is great, enabling ALPN on LibreSSL would be even better, doing it in this pull request be awesome. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great! Just had a small issue with cc
usage.
src/openssl/lib_crypto.cr
Outdated
OPENSSL_102 = {{ `command -v pkg-config > /dev/null && pkg-config --atleast-version=1.0.2 libcrypto || printf %s false`.stringify != "false" }} | ||
# An extra zero is appended to the output of LIBRESSL_VERSION to make it 0 when LibreSSL does not exist on the system. | ||
# Any comparisons to it should be affixed with an extra zero as well e.g. `(LIBRESSL_VERSION_NUMBER >= 0x2050500F0)`. | ||
LIBRESSL_VERSION = {{ `echo "#include <openssl/opensslv.h>\nLIBRESSL_VERSION_NUMBER" | cc -E -`.chomp.split('\n').last.split('L').first.id + "0" }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be using CC
from environment and only then fall back to cc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ENV["CC"]? || "cc"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is in a shell, just use${CC:-cc}
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That might need to be revised once this work on non-bash/sh platforms like Windows. Better use now ENV than hunt down that later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It needs to be revised anyway because it uses cc
... And because it uses openssl in the first place. There's a million ways that this snippet, this file and this library are platform specific and env vars are the absolute least of them.
@ysbaddaden is alpn available in openssl? Do you mean you just need a different API to enable ALPN in libressl? In that case then yes, that's a good idea and would fit in this PR (but not required). |
@LVMBDV It should be possible to get this to work by installing openssl using brew (if not already):
and then setting the export CPPFLAGS=-I/usr/local/opt/openssl/include
export LDFLAGS=-L/usr/local/opt/openssl/lib |
@Sija so everyone on mac is just going to get the same cryptic error as CI does unless you run a command nobody knows about? A solution that passes CI isn't enough. We should fall back to the old way if this way fails. |
Chill your boots dude. My comment refers to running this snippet on Mac OSX with
I agree, yet it's IMO different case; to which you haven't provided solutions either, yet. |
@ About the Mac issue, I think Crystal as a mere compiler is not responsible for finding include paths. I'm sure developers who use Macs are well aware of the platform's eccentricities. Also P.S. I guess |
Fixed that one space, Travis is happy now. Ready for another review :) |
@RX14 ALPN is in OpenSSL >= 1.0.2 and LibreSSL >= 2.5.0 with the same public API, and we already support it in Crystal for OpenSSL >= 1.0.2. It would be very kind to enable it for LibreSSL too. See: https://github.com/crystal-lang/crystal/blob/master/src/openssl/ssl/context.cr#L308 But beware of https://github.com/crystal-lang/crystal/blob/master/src/openssl/ssl/context.cr#L341 (and below) that may not be supported in LibreSSL. |
@ysbaddaden ah, thanks, that makes a lot more sense. I thought you were proposing ALPN as a new feature, I was unaware it was already supported. My bad. |
I really don't want to deal with the hundreds of bug reports we'll get if we merge this and everyone on OSX suddenly starts getting these errrors. |
Fixed the header problem for OS X but a new one popped up. I think crystal is linking against system's (older) openssl libs while using brew's openssl headers (which are newer). This is mentioned briefly in this blog post. I'm trying to fix it while looking at results over at CircleCI, might take slightly longer than usual since I have to wait for it :) P.S. I might look at supporting ALPN for LibreSSL as well, after this. |
All checks passed! I was right about brew linking the system openssl instead of brew's more recent one. It must have been that way before I even started working on this. Anyways, I'll work on ALPN support with LibreSSL tomorrow. |
src/openssl/lib_crypto.cr
Outdated
@@ -14,6 +14,10 @@ | |||
end | |||
{% end %} | |||
|
|||
# Check for brew's openssl libs on OS X | |||
{% if flag?(:darwin) %} | |||
@[Link(ldflags: "-L/usr/local/opt/openssl/lib ")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Homebrew can be installed in many different locations, hardcoding a location inside crystal sourcecode isn't a valid fix.
Surely homebrew should be setting the linker and compiler env vars correctly to link to the correct place? PATH
,LIBRARY_PATH
, CPATH
etc?
Replaced hardcoded paths in |
Newest Mac OSX (High Sierra) comes with updated ssl/crypto libraries, there's no need (and in fact it might be wrong) to use homebrew for this. I just tried changing @[Link("ssl")]
@[Link("crypto")]
lib LibSSL
# ...
end and it works fine. But this didn't work well with a previous Mac version, so I don't know what advice to give on that. Maybe force users to update to the latest Mac OSX version is good. |
@LVMBDV you can use |
I tried my hand at ALPN support, it passes Moreover, I found out that Apple switched to LibreSSL as system default at High Sierra so I need to figure out a way to suppress the EDIT: OR modify behaviour with a compile time flag like |
A warning can be printed to let the user know about this issue.
Should be using brew be the default behaviour or vice versa? CircleCI uses El Capitan so the build will fail if the default is not using brew, so will the rest of Mac users with older versions. |
I think you missed my comment that homebrew shouldn't be used for this. |
Oh, sorry, nevermind |
@LVMBDV here you go:
|
Rebased but the deleted |
@LVMBDV the circle.yml is now in |
Added the openssl path to |
It currently doesn't work with LibreSSL anyway.
Seems like GTG, merge? |
LGTM 👍 |
I compiled Crystal with this change, then tried to compile my program, got this:
I confirmed that this is the culprit LLVM 6.0 on Arch Linux |
Ugh, I kinda expected this. |
Per @RX14's request
|
@[Link(ldflags: "`command -v pkg-config > /dev/null && pkg-config --libs --silence-errors libcrypto || printf %s '-lcrypto'`")] | ||
{% begin %} | ||
lib LibCrypto | ||
OPENSSL_110 = {{ (LibCrypto::LIBRESSL_VERSION == 0) && (LibCrypto::OPENSSL_VERSION >= 0x10101000) }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't 0x101010
checking if openssl is greater than 1.1.1
, not 1.1.0
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
0x10101000
→ 0x10100000
makes the problem go away
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Openssl 1.1.1
is in prerelease, so i'm not sure how this happened.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe someone just got too excited typing alternating ones and zeros 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remember fixing the same thing in somewhere else, version integers are still new to me. My bad, thanks for fixing that.
Maybe someone just got too excited typing alternating ones and zeros
Probably 😆
…)" This reverts commit 72ce28f.
…)" (crystal-lang#6062) This reverts commit 72ce28f.
Used the C preprocessor from
cc
to getLIBRESSL_VERSION_NUMBER
andLIBRESSL_VERSION_NUMBER
which are used to determineOPENSSL_110
andOPENSSL_102
, as @RX14 suggested. This fixes #4676. Tested on my Void Linux machine with LibreSSL.