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

Bringing back @DarthHater work on JSF signatures #146

Closed
wants to merge 8 commits into from
Closed
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
179 changes: 179 additions & 0 deletions cyclonedx/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,185 @@ def sha1sum(filename: str) -> str:
return h.hexdigest()


class DataFlow(Enum):
"""
This is our internal representation of the dataFlowType simple type within the CycloneDX standard.

.. note::
See the CycloneDX Schema: https://cyclonedx.org/docs/1.4/xml/#type_dataFlowType
"""
INBOUND = "inbound"
OUTBOUND = "outbound"
BI_DIRECTIONAL = "bi-directional"
UNKNOWN = "unknown"


class DataClassification:
"""
This is our internal representation of the `dataClassificationType` complex type within the CycloneDX standard.

.. note::
See the CycloneDX Schema for dataClassificationType:
https://cyclonedx.org/docs/1.4/xml/#type_dataClassificationType
"""

def __init__(self, flow: DataFlow, classification: str) -> None:
if not flow and not classification:
raise NoPropertiesProvidedException(
'One of `flow` or `classification` must be supplied - neither supplied'
)

self.flow = flow
self.classification = classification

@property
def flow(self) -> DataFlow:
"""
Specifies the flow direction of the data.

Valid values are: inbound, outbound, bi-directional, and unknown.

Direction is relative to the service.

- Inbound flow states that data enters the service
- Outbound flow states that data leaves the service
- Bi-directional states that data flows both ways
- Unknown states that the direction is not known

Returns:
`DataFlow`
"""
return self._flow

@flow.setter
def flow(self, flow: DataFlow) -> None:
self._flow = flow

@property
def classification(self) -> str:
"""
Data classification tags data according to its type, sensitivity, and value if altered, stolen, or destroyed.

Returns:
`str`
"""
return self._classification

@classification.setter
def classification(self, classification: str) -> None:
self._classification = classification


class SignatureAlgorithm(Enum):
"""
This is out internal representation of the algorithm simple type within the CycloneDX standard.

.. note::
See the CycloneDX Schema: https://cyclonedx.org/docs/1.4/json/#tab-pane_signature_oneOf_i2_algorithm_oneOf_i0
"""
RS256 = "RS256"
RS384 = "RS384"
RS512 = "RS512"
PS256 = "PS256"
PS384 = "PS384"
PS512 = "PS512"
ES256 = "ES256"
ES384 = "ES384"
ES512 = "ES512"
ED25519 = "Ed25519"
ED448 = "Ed448"
HS256 = "HS256"
HS384 = "HS384"
HS512 = "HS512"


class SignaturePublicKeyKty(Enum):
"""
This is our internal representation of the kty simple type within the CycloneDX standard.

.. note::
See the CycloneDX Schema: https://cyclonedx.org/docs/1.4/json/#signature_oneOf_i2_publicKey_kty
"""
EC = "EC"
OKP = "OKP"
RSA = "RSA"


class SignaturePublicKeyCrv(Enum):
"""
This is our internal representation of the crv simple type within the CycloneDX standard.

.. note::
See the CycloneDX Schema: https://cyclonedx.org/docs/1.4/json/#signature_oneOf_i2_publicKey_allOf_i1_then_crv
"""
ED25519 = "Ed25519"
Ed448 = "Ed448"


class SignaturePublicKey:
"""
This is our internal representation of the public key complex type within the CycloneDX standard.

.. note::
See the CycloneDX Schema: https://cyclonedx.org/docs/1.4/json/#signature_oneOf_i2_publicKey
JSON only
"""

def __init__(self, kty: SignaturePublicKeyKty = None, crv: Optional[SignaturePublicKeyCrv] = None,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incompatible variable type: kty is declared to have type SignaturePublicKeyKty but is used as type None.
(at-me in a reply with help or ignore)

x: Optional[str] = None, y: Optional[str] = None,
n: Optional[str] = None, e: Optional[str] = None,
value: str = None) -> None:
if not kty and not value:
raise NoPropertiesProvidedException(
'`kty` must be supplied'
)
if kty == SignaturePublicKeyKty.EC and not crv and not x and not y:
raise NoPropertiesProvidedException(
'if `kty` equals EC, `crv`, `x` and `y` must be supplied'
)
if kty == SignaturePublicKeyKty.OKP and not crv and not x:
raise NoPropertiesProvidedException(
'if `kty` equals OKP, `crv`, and `x` must be supplied'
)
if kty == SignaturePublicKeyKty.RSA and not n and not e:
raise NoPropertiesProvidedException(
'if `kty` equals RSA, `n`, and `e` must be supplied'
)
self.kty = kty
self.crv = crv
self.x = x
self.y = y
self.n = n
self.e = e
self.value = value


class Signature:
"""
This is out internal representation of the signature complex type within the CycloneDX standard.

.. note::
See the CycloneDX Schema: https://cyclonedx.org/docs/1.4/json/#signature
JSON only
"""

def __init__(self, algorithm: SignatureAlgorithm, key_id: Optional[str],
public_key: Optional[SignaturePublicKey] = None,
certificate_path: Optional[List[str]] = None,
excludes: Optional[List[str]] = None,
value: str = None) -> None:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incompatible variable type: value is declared to have type str but is used as type None.
(at-me in a reply with help or ignore)

if not algorithm and not value:
raise NoPropertiesProvidedException(
'One of `algorithm` or `value` must be supplied - neither supplied'
)
self.algorithm = algorithm
self.key_id = key_id
self.public_key = public_key
self.certificate_path = certificate_path
self.excludes = excludes
self.value = value


class Encoding(Enum):
"""
This is out internal representation of the encoding simple type within the CycloneDX standard.
Expand Down
30 changes: 26 additions & 4 deletions cyclonedx/model/bom.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from . import ThisTool, Tool
from .component import Component
from .service import Service
from ..parser import BaseParser


Expand Down Expand Up @@ -133,7 +134,7 @@ def from_parser(parser: BaseParser) -> 'Bom':
bom.add_components(parser.get_components())
return bom

def __init__(self) -> None:
def __init__(self, components: Optional[List[Component]] = None, services: Optional[List[Service]] = None) -> None:
"""
Create a new Bom that you can manually/programmatically add data to later.

Expand All @@ -142,7 +143,8 @@ def __init__(self) -> None:
"""
self.uuid = uuid4()
self.metadata = BomMetaData()
self._components: List[Component] = []
self.components = components
self.services = services

@property
def uuid(self) -> UUID:
Expand Down Expand Up @@ -200,7 +202,9 @@ def add_component(self, component: Component) -> None:
Returns:
None
"""
if not self.has_component(component=component):
if not self.components:
self.components = [component]
elif not self.has_component(component=component):
self._components.append(component)

def add_components(self, components: List[Component]) -> None:
Expand Down Expand Up @@ -263,7 +267,9 @@ def has_component(self, component: Component) -> bool:
Returns:
`bool` - `True` if the supplied Component is part of this Bom, `False` otherwise.
"""
return component in self._components
if not self.components:
return False
return component in self.components

def has_vulnerabilities(self) -> bool:
"""
Expand All @@ -278,3 +284,19 @@ def has_vulnerabilities(self) -> bool:
return True

return False

@property
def services(self) -> Optional[List[Service]]:
"""
A list of services.

This may include microservices, function-as-a-service, and other types of network or intra-process services.

Returns:
List of `Service` or `None`
"""
return self._services

@services.setter
def services(self, services: Optional[List[Service]]) -> None:
self._services = services
2 changes: 1 addition & 1 deletion cyclonedx/model/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class IssueClassification(Enum):
This is out internal representation of the enum `issueClassification`.

.. note::
See the CycloneDX Schema definition: hhttps://cyclonedx.org/docs/1.4/xml/#type_issueClassification
See the CycloneDX Schema definition: https://cyclonedx.org/docs/1.4/xml/#type_issueClassification
"""
DEFECT = 'defect'
ENHANCEMENT = 'enhancement'
Expand Down
Loading