Skip to content

Commit

Permalink
Support multisignature and signaturechain
Browse files Browse the repository at this point in the history
Signed-off-by: andreas hilti <69210561+andreas-hilti@users.noreply.github.com>
  • Loading branch information
andreas-hilti committed Sep 14, 2024
1 parent c06a8c7 commit 787f82c
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 11 deletions.
99 changes: 99 additions & 0 deletions src/CycloneDX.Core/Json/Converters/SignatureChoiceConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// This file is part of CycloneDX Library for .NET
//
// Licensed under the Apache License, Version 2.0 (the “License”);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an “AS IS” BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) OWASP Foundation. All Rights Reserved.

using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Text.Json;
using System.Text.Json.Serialization;
using CycloneDX.Models;

namespace CycloneDX.Json.Converters
{


public class SignatureChoiceConverter : JsonConverter<SignatureChoice>
{
public override SignatureChoice Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
return null;
}
else if (reader.TokenType == JsonTokenType.StartObject)
{
var signatureChoice = new SignatureChoice();
var doc = JsonDocument.ParseValue(ref reader);
if (doc.RootElement.TryGetProperty("signers", out var signersValue))
{
var signers = signersValue.Deserialize<List<Signature>>(options);
signatureChoice.Signers = signers;
}
else if (doc.RootElement.TryGetProperty("chain", out var chainValue))
{
var chain = chainValue.Deserialize<List<Signature>>(options);
signatureChoice.Chain = chain;
}
else
{
var signature = doc.Deserialize<Signature>(options);
signatureChoice.Signature = signature;
}
return signatureChoice;
}
else
{
throw new JsonException();
}
}

public override void Write(
Utf8JsonWriter writer,
SignatureChoice value,
JsonSerializerOptions options)
{
Contract.Requires(writer != null);
Contract.Requires(value != null);

if (value != null)
{

if (value.Signers != null)
{
writer.WriteStartObject();
writer.WritePropertyName("signers");
JsonSerializer.Serialize(writer, value.Signers, options);
writer.WriteEndObject();
}
if (value.Chain != null)
{
writer.WriteStartObject();
writer.WritePropertyName("chain");
JsonSerializer.Serialize(writer, value.Chain, options);
writer.WriteEndObject();
}
if (value.Signature != null)
{
JsonSerializer.Serialize(writer, value.Signature, options);
}
}
}
}
}
1 change: 1 addition & 0 deletions src/CycloneDX.Core/Json/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public static JsonSerializerOptions GetJsonSerializerOptions()
options.Converters.Add(new HyphenEnumConverter<Data.DataType>());
options.Converters.Add(new HyphenEnumConverter<ActivityType>());
options.Converters.Add(new HyphenEnumConverter<EnergySource>());
options.Converters.Add(new SignatureChoiceConverter());

options.Converters.Add(new JsonStringEnumConverter());

Expand Down
2 changes: 1 addition & 1 deletion src/CycloneDX.Core/Models/Bom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,6 @@ public int NonNullableVersion
[JsonIgnore]
public XmlElement XmlSignature { get; set; }
[XmlIgnore]
public Signature Signature { get; set; }
public SignatureChoice Signature { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/CycloneDX.Core/Models/Component.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public bool NonNullableModified
[JsonIgnore]
public XmlElement XmlSignature { get; set; }
[XmlIgnore]
public Signature Signature { get; set; }
public SignatureChoice Signature { get; set; }

public override bool Equals(object obj)
{
Expand Down
2 changes: 1 addition & 1 deletion src/CycloneDX.Core/Models/Composition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public enum AggregateType
[JsonIgnore]
public XmlElement XmlSignature { get; set; }
[XmlIgnore]
public Signature Signature { get; set; }
public SignatureChoice Signature { get; set; }

public System.Xml.Schema.XmlSchema GetSchema() {
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/CycloneDX.Core/Models/Declarations/Affirmation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class Affirmation
[JsonIgnore]
public XmlElement XmlSignature { get; set; }
[XmlIgnore]
public Signature Signature { get; set; }
public SignatureChoice Signature { get; set; }

}
}
2 changes: 1 addition & 1 deletion src/CycloneDX.Core/Models/Declarations/Attestation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class Attestation : IEquatable<Attestation>
public List<System.Xml.XmlElement> Any { get; set; }

[XmlIgnore]
public Signature Signature { get; set; }
public SignatureChoice Signature { get; set; }

public override bool Equals(object obj)
{
Expand Down
2 changes: 1 addition & 1 deletion src/CycloneDX.Core/Models/Declarations/Claim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class Claim : IEquatable<Claim>, IHasBomRef
[JsonIgnore]
public XmlElement XmlSignature { get; set; }
[XmlIgnore]
public Signature Signature { get; set; }
public SignatureChoice Signature { get; set; }

public override bool Equals(object obj)
{
Expand Down
2 changes: 1 addition & 1 deletion src/CycloneDX.Core/Models/Declarations/Declarations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class Declarations
[JsonIgnore]
public XmlElement XmlSignature { get; set; }
[XmlIgnore]
public Signature Signature { get; set; }
public SignatureChoice Signature { get; set; }

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public DateTime? Expires
[JsonIgnore]
public XmlElement XmlSignature { get; set; }
[XmlIgnore]
public Signature Signature { get; set; }
public SignatureChoice Signature { get; set; }

}
}
2 changes: 1 addition & 1 deletion src/CycloneDX.Core/Models/Declarations/Signatory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ public class Signatory
[JsonIgnore]
public XmlElement XmlSignature { get; set; }
[XmlIgnore]
public Signature Signature { get; set; }
public SignatureChoice Signature { get; set; }
}
}
7 changes: 7 additions & 0 deletions src/CycloneDX.Core/Models/Declarations/Signature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ public class Signature
public string Value { get; set; }
}

public class SignatureChoice
{
public List<Signature> Signers { get; set; }
public List<Signature> Chain { get; set; }
public Signature Signature { get; set; }
}

public enum KeyTypeIndicator
{
EC,
Expand Down
2 changes: 1 addition & 1 deletion src/CycloneDX.Core/Models/Definitions/Standard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class Standard : IEquatable<Standard>, IHasBomRef
public System.Xml.XmlAttribute[] AnyAttr { get; set; }

[XmlIgnore]
public Signature Signature { get; set; }
public SignatureChoice Signature { get; set; }



Expand Down
2 changes: 1 addition & 1 deletion src/CycloneDX.Core/Models/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public LicenseChoiceList LicensesSerialized
[XmlAnyElement("Signature", Namespace = "http://www.w3.org/2000/09/xmldsig#")]
public XmlElement XmlSignature { get; set; }
[XmlIgnore]
public Signature Signature { get; set; }
public SignatureChoice Signature { get; set; }

[XmlArray("tags")]
[XmlArrayItem("tag")]
Expand Down

0 comments on commit 787f82c

Please sign in to comment.