This commit is contained in:
2026-06-15 18:18:16 +08:00
parent 97c9fba14e
commit 2b9f134e5f
4164 changed files with 386922 additions and 79 deletions

View File

@@ -0,0 +1,34 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
#pragma warning disable
using System;
using System.Runtime.Serialization;
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO.Pem
{
[Serializable]
public class PemGenerationException
: Exception
{
public PemGenerationException()
: base()
{
}
public PemGenerationException(string message)
: base(message)
{
}
public PemGenerationException(string message, Exception innerException)
: base(message, innerException)
{
}
protected PemGenerationException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
}
}
#pragma warning restore
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 54711797ae622c84188cfbb9a5cae97a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,64 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
#pragma warning disable
using System;
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO.Pem
{
public class PemHeader
{
private string name;
private string val;
public PemHeader(string name, string val)
{
this.name = name;
this.val = val;
}
public virtual string Name
{
get { return name; }
}
public virtual string Value
{
get { return val; }
}
public override int GetHashCode()
{
return GetHashCode(this.name) + 31 * GetHashCode(this.val);
}
public override bool Equals(object obj)
{
if (obj == this)
return true;
if (!(obj is PemHeader))
return false;
PemHeader other = (PemHeader)obj;
return Org.BouncyCastle.Utilities.Platform.Equals(this.name, other.name)
&& Org.BouncyCastle.Utilities.Platform.Equals(this.val, other.val);
}
private int GetHashCode(string s)
{
if (s == null)
{
return 1;
}
return s.GetHashCode();
}
public override string ToString()
{
return name + ":" + val;
}
}
}
#pragma warning restore
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b892c40614a572f45af1714e78837483
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,49 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
#pragma warning disable
using System;
using System.Collections.Generic;
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO.Pem
{
public class PemObject
: PemObjectGenerator
{
private string type;
private IList<PemHeader> headers;
private byte[] content;
public PemObject(string type, byte[] content)
: this(type, new List<PemHeader>(), content)
{
}
public PemObject(string type, IList<PemHeader> headers, byte[] content)
{
this.type = type;
this.headers = new List<PemHeader>(headers);
this.content = content;
}
public string Type
{
get { return type; }
}
public IList<PemHeader> Headers
{
get { return headers; }
}
public byte[] Content
{
get { return content; }
}
public PemObject Generate()
{
return this;
}
}
}
#pragma warning restore
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4bce1d1ba4bf69e488437b0eb0f8e357
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,17 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
#pragma warning disable
using System;
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO.Pem
{
public interface PemObjectGenerator
{
/// <returns>
/// A <see cref="PemObject"/>
/// </returns>
/// <exception cref="PemGenerationException"></exception>
PemObject Generate();
}
}
#pragma warning restore
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3e16ccade6df2b64db6108492c8f51cb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,21 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
#pragma warning disable
using System;
using System.IO;
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO.Pem
{
public interface PemObjectParser
{
/// <param name="obj">
/// A <see cref="PemObject"/>
/// </param>
/// <returns>
/// An <see cref="object"/>
/// </returns>
/// <exception cref="IOException"></exception>
object ParseObject(PemObject obj);
}
}
#pragma warning restore
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d60aad74ea940284c9e74e43ba83c881
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,377 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
#pragma warning disable
using System;
using System.Collections.Generic;
using System.IO;
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO.Pem
{
public class PemReader
: IDisposable
{
private readonly TextReader reader;
private readonly MemoryStream buffer;
private readonly StreamWriter textBuffer;
private readonly List<int> pushback = new List<int>();
int c = 0;
public PemReader(TextReader reader)
{
this.reader = reader ?? throw new ArgumentNullException(nameof(reader));
this.buffer = new MemoryStream();
this.textBuffer = new StreamWriter(buffer);
}
#region IDisposable
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
reader.Dispose();
}
}
#endregion
public TextReader Reader
{
get { return reader; }
}
/// <returns>
/// A <see cref="PemObject"/>
/// </returns>
/// <exception cref="IOException"></exception>
public PemObject ReadPemObject()
{
//
// Look for BEGIN
//
for (;;)
{
// Seek a leading dash, ignore anything up to that point.
if (!seekDash())
{
// There are no pem objects here.
return null;
}
// consume dash [-----]BEGIN ...
if (!consumeDash())
{
throw new IOException("no data after consuming leading dashes");
}
skipWhiteSpace();
if (!expect("BEGIN"))
{
continue;
}
break;
}
skipWhiteSpace();
//
// Consume type, accepting whitespace
//
if (!bufferUntilStopChar('-',false) )
{
throw new IOException("ran out of data before consuming type");
}
string type = bufferedString().Trim();
// Consume dashes after type.
if (!consumeDash())
{
throw new IOException("ran out of data consuming header");
}
skipWhiteSpace();
//
// Read ahead looking for headers.
// Look for a colon for up to 64 characters, as an indication there might be a header.
//
var headers = new List<PemHeader>();
while (seekColon(64))
{
if (!bufferUntilStopChar(':',false))
{
throw new IOException("ran out of data reading header key value");
}
string key = bufferedString().Trim();
c = Read();
if (c != ':')
{
throw new IOException("expected colon");
}
//
// We are going to look for well formed headers, if they do not end with a "LF" we cannot
// discern where they end.
//
if (!bufferUntilStopChar('\n', false)) // Now read to the end of the line.
{
throw new IOException("ran out of data before consuming header value");
}
skipWhiteSpace();
string value = bufferedString().Trim();
headers.Add(new PemHeader(key,value));
}
//
// Consume payload, ignoring all white space until we encounter a '-'
//
skipWhiteSpace();
if (!bufferUntilStopChar('-',true))
{
throw new IOException("ran out of data before consuming payload");
}
string payload = bufferedString();
// Seek the start of the end.
if (!seekDash())
{
throw new IOException("did not find leading '-'");
}
if (!consumeDash())
{
throw new IOException("no data after consuming trailing dashes");
}
if (!expect("END "+type))
{
throw new IOException("END "+type+" was not found.");
}
if (!seekDash())
{
throw new IOException("did not find ending '-'");
}
// consume trailing dashes.
consumeDash();
return new PemObject(type, headers, Base64.Decode(payload));
}
private string bufferedString()
{
textBuffer.Flush();
string value = Strings.FromUtf8ByteArray(buffer.ToArray());
buffer.Position = 0;
buffer.SetLength(0);
return value;
}
private bool seekDash()
{
c = 0;
while((c = Read()) >=0)
{
if (c == '-')
{
break;
}
}
PushBack(c);
return c == '-';
}
/// <summary>
/// Seek ':" up to the limit.
/// </summary>
/// <param name="upTo"></param>
/// <returns></returns>
private bool seekColon(int upTo)
{
c = 0;
bool colonFound = false;
var read = new List<int>();
for (; upTo>=0 && c >=0; upTo--)
{
c = Read();
read.Add(c);
if (c == ':')
{
colonFound = true;
break;
}
}
while(read.Count>0)
{
PushBack((int)read[read.Count-1]);
read.RemoveAt(read.Count-1);
}
return colonFound;
}
/// <summary>
/// Consume the dashes
/// </summary>
/// <returns></returns>
private bool consumeDash()
{
c = 0;
while ((c = Read()) >= 0)
{
if (c != '-')
{
break;
}
}
PushBack(c);
return c != -1;
}
/// <summary>
/// Skip white space leave char in stream.
/// </summary>
private void skipWhiteSpace()
{
while ((c = Read()) >= 0)
{
if (c > ' ')
{
break;
}
}
PushBack(c);
}
/// <summary>
/// Read forward consuming the expected string.
/// </summary>
/// <param name="value">expected string</param>
/// <returns>false if not consumed</returns>
private bool expect(string value)
{
for (int t=0; t<value.Length; t++)
{
c = Read();
if (c == value[t])
{
continue;
} else
{
return false;
}
}
return true;
}
/// <summary>
/// Consume until dash.
/// </summary>
/// <returns>true if stream end not met</returns>
private bool bufferUntilStopChar(char stopChar, bool skipWhiteSpace)
{
while ((c = Read()) >= 0)
{
if (skipWhiteSpace && c <=' ')
{
continue;
}
if (c != stopChar)
{
textBuffer.Write((char)c);
textBuffer.Flush();
} else
{
PushBack(c);
break;
}
}
return c > -1;
}
private void PushBack(int value)
{
if (pushback.Count == 0)
{
pushback.Add(value);
} else
{
pushback.Insert(0, value);
}
}
private int Read()
{
if (pushback.Count > 0)
{
int i = pushback[0];
pushback.RemoveAt(0);
return i;
}
return reader.Read();
}
}
}
#pragma warning restore
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 09f2d56aaf05d0c4583f2e055172290e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,139 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
#pragma warning disable
using System;
using System.IO;
using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Encoders;
namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO.Pem
{
/**
* A generic PEM writer, based on RFC 1421
*/
public class PemWriter
: IDisposable
{
private const int LineLength = 64;
private readonly TextWriter writer;
private readonly int nlLength;
private char[] buf = new char[LineLength];
/**
* Base constructor.
*
* @param out output stream to use.
*/
public PemWriter(TextWriter writer)
{
this.writer = writer ?? throw new ArgumentNullException(nameof(writer));
this.nlLength = Environment.NewLine.Length;
}
#region IDisposable
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
writer.Dispose();
}
}
#endregion
public TextWriter Writer
{
get { return writer; }
}
/**
* Return the number of bytes or characters required to contain the
* passed in object if it is PEM encoded.
*
* @param obj pem object to be output
* @return an estimate of the number of bytes
*/
public int GetOutputSize(PemObject obj)
{
// BEGIN and END boundaries.
int size = (2 * (obj.Type.Length + 10 + nlLength)) + 6 + 4;
if (obj.Headers.Count > 0)
{
foreach (PemHeader header in obj.Headers)
{
size += header.Name.Length + ": ".Length + header.Value.Length + nlLength;
}
size += nlLength;
}
// base64 encoding
int dataLen = ((obj.Content.Length + 2) / 3) * 4;
size += dataLen + (((dataLen + LineLength - 1) / LineLength) * nlLength);
return size;
}
public void WriteObject(PemObjectGenerator objGen)
{
PemObject obj = objGen.Generate();
WritePreEncapsulationBoundary(obj.Type);
if (obj.Headers.Count > 0)
{
foreach (PemHeader header in obj.Headers)
{
writer.Write(header.Name);
writer.Write(": ");
writer.WriteLine(header.Value);
}
writer.WriteLine();
}
WriteEncoded(obj.Content);
WritePostEncapsulationBoundary(obj.Type);
}
private void WriteEncoded(byte[] bytes)
{
bytes = Base64.Encode(bytes);
for (int i = 0; i < bytes.Length; i += buf.Length)
{
int index = 0;
while (index != buf.Length)
{
if ((i + index) >= bytes.Length)
break;
buf[index] = (char)bytes[i + index];
index++;
}
writer.WriteLine(buf, 0, index);
}
}
private void WritePreEncapsulationBoundary(string type)
{
writer.WriteLine("-----BEGIN " + type + "-----");
}
private void WritePostEncapsulationBoundary(string type)
{
writer.WriteLine("-----END " + type + "-----");
}
}
}
#pragma warning restore
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9bf98bac36d6fa7478399f9c8df01ab7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: