#!/usr/bin/python3

"""
CVE checker for libnss

References:
http://www.cvedetails.com/vulnerability-list/vendor_id-452/product_id-4052/Mozilla-Network-Security-Services.html

RSS feed: http://www.cvedetails.com/vulnerability-feed.php?vendor_id=452&product_id=4052&version_id=&orderby=3&cvssscoremin=0
"""
import re

def guess_nss_version_from_content(lines):
    """Guesses the libnss version from the file contents
    """
    new_guess = ""
    pattern1a = re.compile(r"\$Header: NSS ([01234]+\.[0-9]+) ")
    pattern1b = re.compile(r"\$Header: NSS ([01234]+\.[0-9]+\.[0-9]+) ")
    pattern1c = re.compile(r"\$Header: NSS ([01234]+\.[0-9]+\.[0-9]+)\.[0-9]+ ")
    pattern2a = re.compile(r"Version: NSS ([01234]+\.[0-9]+) Basic")
    pattern2b = re.compile(r"Version: NSS ([01234]+\.[0-9]+\.[0-9]+) Basic")
    pattern2c = re.compile(r"Version: NSS ([01234]+\.[0-9]+\.[0-9]+)\.[0-9]+ Basic")
    pattern3a = re.compile(r"\@\(#\)NSS ([01234]+\.[0-9]+) Basic")
    pattern3b = re.compile(r"\@\(#\)NSS ([01234]+\.[0-9]+\.[0-9]+) Basic")
    pattern3c = re.compile(r"\@\(#\)NSS ([01234]+\.[0-9]+\.[0-9]+)\.[0-9]+ Basic")

    for line in lines:
        match = pattern1a.search(line)
        if match:
            new_guess2 = match.group(1).strip()
            if len(new_guess2) > len(new_guess):
                new_guess = new_guess2
        match = pattern1b.search(line)
        if match:
            new_guess2 = match.group(1).strip()
            if len(new_guess2) > len(new_guess):
                new_guess = new_guess2
        match = pattern1c.search(line)
        if match:
            new_guess2 = match.group(1).strip()
            if len(new_guess2) > len(new_guess):
                new_guess = new_guess2
        match = pattern2a.search(line)
        if match:
            new_guess2 = match.group(1).strip()
            if len(new_guess2) > len(new_guess):
                new_guess = new_guess2

        match = pattern2b.search(line)
        if match:
            new_guess2 = match.group(1).strip()
            if len(new_guess2) > len(new_guess):
                new_guess = new_guess2
        match = pattern2c.search(line)
        if match:
            new_guess2 = match.group(1).strip()
            if len(new_guess2) > len(new_guess):
                new_guess = new_guess2
        match = pattern3a.search(line)
        if match:
            new_guess2 = match.group(1).strip()
            if len(new_guess2) > len(new_guess):
                new_guess = new_guess2

        match = pattern3b.search(line)
        if match:
            new_guess2 = match.group(1).strip()
            if len(new_guess2) > len(new_guess):
                new_guess = new_guess2
        match = pattern3c.search(line)
        if match:
            new_guess2 = match.group(1).strip()
            if len(new_guess2) > len(new_guess):
                new_guess = new_guess2

    return new_guess

def guess_contains_nss(lines):
    """Tries to determine if a file includes libnss
    """
    for line in lines:
        if "Client authentication failed: private key in key database does not match public key in certificate database." in line:
            return 1
        if "The next protocol negotiation extension was enabled, but the callback was cleared prior to being needed." in line:
            return 1
        if "# SSL/TLS secrets log file, generated by NSS" in line:
            return 1
    return 0

def get_version(lines, filename):
    """returns version information for libnss as found in a given file.
    The version info is returned as a tuple:
        [modulename, is_or_contains, version]

    modulename will be libnss if libnss is found (and blank otherwise)
    is_or_contains idicates if the file is a copy of libnss or contains one
    version gives the actual version number

    VPkg: mozilla, network_security_services
    """
    version_info = dict()
    if "libnss.so." in filename or "libssl3.so" in filename:
        version_info["is_or_contains"] = "is"
    elif guess_contains_nss(lines):
        version_info["is_or_contains"] = "contains"

    if "is_or_contains" in version_info:
        version_info["modulename"] = "nss"
        version_info["version"] = guess_nss_version_from_content(lines)

    return version_info

