Skip to content
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

Add openssl support in univers #42

Merged
merged 3 commits into from
Mar 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/univers/version_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,37 @@ def from_native(cls, string):
return cls(constraints=constraints)


class OpensslVersionRange(VersionRange):
"""
Openssl version range.
openssl doesn't use <,>,<= or >=
For more see 'https://www.openssl.org/news/vulnerabilities.xml'

For example::
>>> from univers.versions import OpensslVersion
>>> constraints = (
... VersionConstraint(version=OpensslVersion("1.0.1af")),
... VersionConstraint(comparator="=", version=OpensslVersion("3.0.1")),
... VersionConstraint(comparator="=", version=OpensslVersion("1.1.1nf")),
... )
>>> range = OpensslVersionRange(constraints=constraints)
>>> assert str(range) == 'vers:openssl/1.0.1af|1.1.1nf|3.0.1'
"""

scheme = "openssl"
version_class = versions.OpensslVersion

@classmethod
def from_native(cls, string):
cleaned = remove_spaces(string).lower()
constraints = []
for version in cleaned.split(","):
version_obj = cls.version_class(version)
constraint = VersionConstraint(comparator="=", version=version_obj)
constraints.append(constraint)
return cls(constraints=constraints)


def is_even(s):
"""
Return True if the string "s" is an even number and False if this is an odd
Expand Down Expand Up @@ -902,4 +933,5 @@ def is_even(s):
"ebuild": EbuildVersionRange,
"archlinux": ArchLinuxVersionRange,
"nginx": NginxVersionRange,
"openssl": OpensslVersionRange,
}
176 changes: 176 additions & 0 deletions src/univers/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,179 @@ def __gt__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return gentoo.vercmp(self.value, other.value) > 0


@attr.s(frozen=True, order=False, eq=False, hash=True)
class LegacyOpensslVersion(Version):
keshav-space marked this conversation as resolved.
Show resolved Hide resolved
"""
Represent an Legacy Openssl Version.

For example::
>>> LegacyOpensslVersion("1.0.1f")
LegacyOpensslVersion(string='1.0.1f')
>>> LegacyOpensslVersion("1.0.2ac")
LegacyOpensslVersion(string='1.0.2ac')
>>> LegacyOpensslVersion("1.0.2a")
LegacyOpensslVersion(string='1.0.2a')
>>> LegacyOpensslVersion("3.0.2")
Traceback (most recent call last):
...
univers.versions.InvalidVersion: '3.0.2' is not a valid <class 'univers.versions.LegacyOpensslVersion'>
"""

@classmethod
def is_valid(cls, string):
return bool(cls.parse(string))

@classmethod
def parse(cls, string):
"""
Return a four-tuple of (major, minor, build, patch) version segments where
major, minor, build are integers and patch is a string possibly empty.
Return False if this is not a valid LegacyOpensslVersion.

For example::
>>> LegacyOpensslVersion.parse("1.0.1f")
(1, 0, 1, 'f')
>>> LegacyOpensslVersion.parse("1.0.2ac")
(1, 0, 2, 'ac')
>>> LegacyOpensslVersion.parse("2.0.2az")
False
"""

# All known legacy base OpenSSL versions
all_legacy_base = (
"0.9.1",
"0.9.2",
"0.9.3",
"0.9.4",
"0.9.5",
"0.9.6",
"0.9.7",
"0.9.8",
"1.0.0",
"1.0.1",
"1.0.2",
"1.1.0",
"1.1.1",
)
if not string.startswith(all_legacy_base):
return False

segments = string.split(".")
if not len(segments) == 3:
return False
major, minor, build = segments
major = int(major)
minor = int(minor)
if build.isdigit():
build = int(build)
patch = ""
else:
patch = build[1:]
build = int(build[0])
if patch[0].isdigit():
return False
return major, minor, build, patch

@classmethod
def build_value(cls, string):
return cls.parse(string)

def __str__(self):
return f"{self.value[0]}.{self.value[1]}.{self.value[2]}{self.value[3]}"


@attr.s(frozen=True, order=False, eq=False, hash=True)
class OpensslVersion(Version):
"""
Internally tracks two types of openssl versions
- LegacyOpensslVersion for versions before version 3.0.0 such as 1.0.1g
- Semver for versions from 3.0.0 and up
For example::
>>> old = OpensslVersion("1.1.0f")
>>> new = OpensslVersion("3.0.1")
>>> assert old == OpensslVersion(string="1.1.0f")
>>> assert new == OpensslVersion(string="3.0.1")
>>> assert old.value == LegacyOpensslVersion(string="1.1.0f")
>>> assert new.value == SemverVersion(string="3.0.1")
>>> OpensslVersion("1.2.4fg")
Traceback (most recent call last):
...
univers.versions.InvalidVersion: '1.2.4fg' is not a valid <class 'univers.versions.OpensslVersion'>
"""

@classmethod
def is_valid(cls, string):
return cls.is_valid_new(string) or cls.is_valid_legacy(string)

@classmethod
def build_value(cls, string):
"""
Return a wrapped version "value" object depending on
whether version is legacy or semver.
"""
if cls.is_valid_legacy(string):
return LegacyOpensslVersion(string)
if cls.is_valid_new(string):
return SemverVersion(string)

@classmethod
def is_valid_new(cls, string):
"""
Check the validity of new Openssl Version.

For example::
>>> OpensslVersion.is_valid_new("1.0.1f")
False
>>> OpensslVersion.is_valid_new("3.0.0")
True
>>> OpensslVersion.is_valid_new("3.0.2")
True
"""
if SemverVersion.is_valid(string):
sem = semantic_version.Version.coerce(string)
return sem.major >= 3

@classmethod
def is_valid_legacy(cls, string):
return LegacyOpensslVersion.is_valid(string)

def __eq__(self, other):
keshav-space marked this conversation as resolved.
Show resolved Hide resolved
if not isinstance(other, self.__class__):
return NotImplemented
if not isinstance(other.value, self.value.__class__):
return NotImplemented
return self.value.__eq__(other.value)

def __lt__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
if isinstance(other.value, self.value.__class__):
return self.value.__lt__(other.value)
# By construction legacy version is always behind Semver
return isinstance(self.value, LegacyOpensslVersion)

def __gt__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
if isinstance(other.value, self.value.__class__):
return self.value.__gt__(other.value)
# By construction semver version is always ahead of legacy
return isinstance(self.value, SemverVersion)

def __le__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
if isinstance(other.value, self.value.__class__):
return self.value.__le__(other.value)
# version value are of diff type, then legacy one is always behind semver
return isinstance(self.value, LegacyOpensslVersion)

def __ge__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
if isinstance(other.value, self.value.__class__):
return self.value.__ge__(other.value)
# version value are of diff type, then semver one is always ahead of legacy
return isinstance(self.value, SemverVersion)
Loading