Skip to content

Commit 242b98f

Browse files
author
Brett Hazen
committed
Simplify OpenSSL version checking
1 parent bf00c88 commit 242b98f

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

riak/security.py

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@
2222
import select
2323
import string
2424
import datetime
25+
import calendar
2526
from riak import RiakError
27+
from distutils.version import LooseVersion
2628
try:
2729
from cStringIO import StringIO
2830
except ImportError:
2931
from StringIO import StringIO
3032

3133
OPENSSL_VERSION_101G = 268439679
32-
OPENSSL_VERSION_101 = 1000*1000*1 + 1000*0 + 1
34+
OPENSSL_VERSION_101 = "1.0.1"
3335
OPENSSL_VERSION_NUM_POS = 1
3436
OPENSSL_VERSION_DAY_POS = 4
3537
OPENSSL_VERSION_MON_POS = 3
@@ -41,29 +43,16 @@
4143
too_old = False
4244
# Check the build date on older versions
4345
verstring = OpenSSL.SSL.SSLeay_version(OpenSSL.SSL.SSLEAY_VERSION)
44-
versions = string.split(verstring)
45-
# Convert version string to integer
46-
verdots = string.split(versions[OPENSSL_VERSION_NUM_POS], '.')
47-
if len(verdots) == 3:
48-
verint = 1000 * 1000 * verdots[0] + 1000 * verdots[1] + \
49-
verdots[2].translate(None, "abcdefghijklmnopqrstuvwxyz")
50-
# Is this at least 1.0.1 built after April 2014 (hopefully patched)
51-
if verint < OPENSSL_VERSION_101:
52-
too_old = True
53-
else:
54-
builtstr = OpenSSL.SSL.SSLeay_version(OpenSSL.SSL.SSLEAY_BUILT_ON)
55-
timestamp = string.split(builtstr)
56-
import calendar
57-
calmap = {v: k for k,v in enumerate(calendar.month_abbr)}
58-
day = int(timestamp[OPENSSL_VERSION_DAY_POS])
59-
mon = calmap[timestamp[OPENSSL_VERSION_MON_POS]]
60-
year = int(timestamp[OPENSSL_VERSION_YEAR_POS])
61-
build = datetime.date(year, mon, day)
62-
if build < ssldate:
63-
too_old = True
64-
else:
65-
too_old = True
66-
if too_old:
46+
verdots = string.split(verstring)[OPENSSL_VERSION_NUM_POS]
47+
builtstr = OpenSSL.SSL.SSLeay_version(OpenSSL.SSL.SSLEAY_BUILT_ON)
48+
timestamp = string.split(builtstr)
49+
calmap = {v: k for k,v in enumerate(calendar.month_abbr)}
50+
day = int(timestamp[OPENSSL_VERSION_DAY_POS])
51+
mon = calmap[timestamp[OPENSSL_VERSION_MON_POS]]
52+
year = int(timestamp[OPENSSL_VERSION_YEAR_POS])
53+
build = datetime.date(year, mon, day)
54+
if LooseVersion(verdots) < LooseVersion(OPENSSL_VERSION_101) or \
55+
build < ssldate:
6756
raise RuntimeError("Found {0} version, but expected at least "
6857
"OpenSSL 1.0.1g".format(verstring))
6958

0 commit comments

Comments
 (0)