update exportcenter_ii and fix Sm2AttestorTests

This commit is contained in:
StellaOps Bot
2025-12-07 22:51:01 +02:00
parent 7c24ed96ee
commit 4b124fb056
3 changed files with 174 additions and 9 deletions

View File

@@ -107,9 +107,9 @@ internal static class Sm2TestKeyFactory
var curve = Org.BouncyCastle.Asn1.GM.GMNamedCurves.GetByName("SM2P256V1");
var domain = new Org.BouncyCastle.Crypto.Parameters.ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H, curve.GetSeed());
var generator = new Org.BouncyCastle.Crypto.Generators.ECKeyPairGenerator("EC");
generator.Init(new Org.BouncyCastle.Crypto.Generators.ECKeyGenerationParameters(domain, new Org.BouncyCastle.Security.SecureRandom()));
generator.Init(new Org.BouncyCastle.Crypto.Parameters.ECKeyGenerationParameters(domain, new Org.BouncyCastle.Security.SecureRandom()));
var pair = generator.GenerateKeyPair();
var privInfo = Org.BouncyCastle.Asn1.Pkcs.PrivateKeyInfoFactory.CreatePrivateKeyInfo(pair.Private);
var privInfo = Org.BouncyCastle.Pkcs.PrivateKeyInfoFactory.CreatePrivateKeyInfo(pair.Private);
var pem = Convert.ToBase64String(privInfo.GetDerEncoded());
var path = System.IO.Path.GetTempFileName();
System.IO.File.WriteAllText(path, "-----BEGIN PRIVATE KEY-----\n" + pem + "\n-----END PRIVATE KEY-----\n");

View File

@@ -1,9 +1,25 @@
using System.Collections;
using System.Globalization;
using System.Linq;
using System.Text.Json;
namespace MongoDB.Bson
{
public enum BsonType
{
Double,
String,
Document,
Array,
Binary,
ObjectId,
Boolean,
DateTime,
Null,
Int32,
Int64
}
public class BsonValue : IEquatable<BsonValue?>
{
protected object? RawValue;
@@ -13,16 +29,38 @@ namespace MongoDB.Bson
RawValue = value;
}
public bool IsString => RawValue is string;
public bool IsBoolean => RawValue is bool;
public bool IsBsonDocument => RawValue is BsonDocument;
public bool IsBsonArray => RawValue is BsonArray;
public virtual BsonType BsonType => RawValue switch
{
null => BsonType.Null,
BsonDocument => BsonType.Document,
BsonArray => BsonType.Array,
string => BsonType.String,
bool => BsonType.Boolean,
int => BsonType.Int32,
long => BsonType.Int64,
double or float or decimal => BsonType.Double,
DateTime or DateTimeOffset => BsonType.DateTime,
ObjectId => BsonType.ObjectId,
byte[] => BsonType.Binary,
Guid => BsonType.String,
_ => BsonType.String
};
public bool IsString => BsonType == BsonType.String;
public bool IsBoolean => BsonType == BsonType.Boolean;
public bool IsBsonDocument => BsonType == BsonType.Document;
public bool IsBsonArray => BsonType == BsonType.Array;
public bool IsBsonNull => BsonType == BsonType.Null;
public bool IsBsonDateTime => BsonType == BsonType.DateTime;
public bool IsInt32 => BsonType == BsonType.Int32;
public bool IsInt64 => BsonType == BsonType.Int64;
public string AsString => RawValue switch
{
null => string.Empty,
string s => s,
Guid g => g.ToString(),
ObjectId o => o.ToString(),
_ => Convert.ToString(RawValue, CultureInfo.InvariantCulture) ?? string.Empty
};
@@ -44,6 +82,17 @@ namespace MongoDB.Bson
_ => 0
};
public int AsInt32 => ToInt32();
public long AsInt64 => RawValue switch
{
long l => l,
int i => i,
double d => (long)d,
string s when long.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out var l) => l,
_ => 0L
};
public Guid AsGuid => RawValue switch
{
Guid g => g,
@@ -58,6 +107,24 @@ namespace MongoDB.Bson
_ => ObjectId.Empty
};
public byte[]? AsByteArray => RawValue as byte[];
public DateTimeOffset AsDateTimeOffset => RawValue switch
{
DateTimeOffset dto => dto.ToUniversalTime(),
DateTime dt => DateTime.SpecifyKind(dt, DateTimeKind.Utc),
string s when DateTimeOffset.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out var dto) => dto.ToUniversalTime(),
_ => DateTimeOffset.MinValue
};
public DateTime ToUniversalTime() => RawValue switch
{
DateTime dt => dt.Kind == DateTimeKind.Utc ? dt : dt.ToUniversalTime(),
DateTimeOffset dto => dto.UtcDateTime,
string s when DateTimeOffset.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out var dto) => dto.UtcDateTime,
_ => DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc)
};
public BsonDocument AsBsonDocument => RawValue as BsonDocument ?? (this as BsonDocument ?? new BsonDocument());
public BsonArray AsBsonArray => RawValue as BsonArray ?? (this as BsonArray ?? new BsonArray());
@@ -69,6 +136,8 @@ namespace MongoDB.Bson
public override bool Equals(object? obj) => obj is BsonValue other && Equals(other);
public override int GetHashCode() => RawValue?.GetHashCode() ?? 0;
public static BsonValue Create(object? value) => BsonDocument.ToBsonValue(value);
public static implicit operator BsonValue(string value) => new(value);
public static implicit operator BsonValue(Guid value) => new(value);
public static implicit operator BsonValue(int value) => new(value);
@@ -76,6 +145,38 @@ namespace MongoDB.Bson
public static implicit operator BsonValue(bool value) => new(value);
public static implicit operator BsonValue(double value) => new(value);
public static implicit operator BsonValue(DateTimeOffset value) => new(value);
public static implicit operator BsonValue(DateTime value) => new(value);
public static implicit operator BsonValue(byte[] value) => new(value);
}
public sealed class BsonNull : BsonValue
{
public static BsonNull Value { get; } = new();
private BsonNull()
: base(null)
{
}
public override BsonType BsonType => BsonType.Null;
}
public sealed class BsonString : BsonValue
{
public BsonString(string value) : base(value)
{
}
}
public sealed class BsonBinaryData : BsonValue
{
public BsonBinaryData(byte[] bytes)
: base(bytes)
{
Bytes = bytes ?? Array.Empty<byte>();
}
public byte[] Bytes { get; }
}
public sealed class BsonDocument : BsonValue, IDictionary<string, BsonValue>
@@ -97,6 +198,27 @@ namespace MongoDB.Bson
}
}
public BsonDocument(IEnumerable<KeyValuePair<string, BsonValue>> values)
: this()
{
foreach (var kvp in values)
{
_values[kvp.Key] = kvp.Value ?? new BsonValue();
}
}
public BsonDocument(string key, BsonValue value)
: this()
{
Add(key, value);
}
public BsonDocument(string key, object? value)
: this()
{
Add(key, value);
}
public int ElementCount => _values.Count;
public BsonValue this[string key]
@@ -117,6 +239,7 @@ namespace MongoDB.Bson
public void Clear() => _values.Clear();
public bool Contains(KeyValuePair<string, BsonValue> item) => _values.Contains(item);
public bool ContainsKey(string key) => _values.ContainsKey(key);
public bool Contains(string key) => ContainsKey(key);
public void CopyTo(KeyValuePair<string, BsonValue>[] array, int arrayIndex) => ((IDictionary<string, BsonValue>)_values).CopyTo(array, arrayIndex);
public IEnumerator<KeyValuePair<string, BsonValue>> GetEnumerator() => _values.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => _values.GetEnumerator();
@@ -126,6 +249,9 @@ namespace MongoDB.Bson
public BsonValue GetValue(string key) => _values[key];
public BsonValue GetValue(string key, BsonValue defaultValue)
=> _values.TryGetValue(key, out var value) ? value : defaultValue;
public BsonDocument DeepClone()
{
var copy = new BsonDocument();
@@ -177,7 +303,7 @@ namespace MongoDB.Bson
return array;
}
internal static BsonValue ToBsonValue(object? value)
public static BsonValue ToBsonValue(object? value)
{
return value switch
{
@@ -190,9 +316,11 @@ namespace MongoDB.Bson
bool b => new BsonValue(b),
double d => new BsonValue(d),
float f => new BsonValue(f),
decimal dec => new BsonValue((double)dec),
DateTime dt => new BsonValue(dt),
DateTimeOffset dto => new BsonValue(dto),
IEnumerable<object?> enumerable => new BsonArray(enumerable.Select(ToBsonValue)),
byte[] bytes => new BsonBinaryData(bytes),
IEnumerable enumerable when value is not string => new BsonArray(enumerable.Cast<object?>().Select(ToBsonValue)),
_ => new BsonValue(value)
};
}
@@ -216,6 +344,15 @@ namespace MongoDB.Bson
_items.AddRange(items);
}
public BsonArray(IEnumerable<object?> items)
: this()
{
foreach (var item in items)
{
Add(item);
}
}
public BsonValue this[int index]
{
get => _items[index];
@@ -227,6 +364,13 @@ namespace MongoDB.Bson
public void Add(BsonValue item) => _items.Add(item ?? new BsonValue());
public void Add(object? item) => _items.Add(BsonDocument.ToBsonValue(item));
public void AddRange(IEnumerable<object?> items)
{
foreach (var item in items)
{
Add(item);
}
}
public void Clear() => _items.Clear();
public bool Contains(BsonValue item) => _items.Contains(item);
public void CopyTo(BsonValue[] array, int arrayIndex) => _items.CopyTo(array, arrayIndex);
@@ -259,6 +403,26 @@ namespace MongoDB.Bson
public override bool Equals(object? obj) => obj is ObjectId other && Equals(other);
public override int GetHashCode() => _value?.GetHashCode(StringComparison.Ordinal) ?? 0;
}
public static class BsonTypeMapper
{
public static object? MapToDotNetValue(BsonValue value)
{
if (value is null) return null;
return value.BsonType switch
{
BsonType.Document => value.AsBsonDocument.ToDictionary(static kvp => kvp.Key, static kvp => MapToDotNetValue(kvp.Value)),
BsonType.Array => value.AsBsonArray.Select(MapToDotNetValue).ToArray(),
BsonType.Null => null,
BsonType.Boolean => value.AsBoolean,
BsonType.Int32 => value.AsInt32,
BsonType.Int64 => value.AsInt64,
BsonType.DateTime => value.ToUniversalTime(),
_ => value.AsString
};
}
}
}
namespace MongoDB.Bson.Serialization.Attributes