This commit is contained in:
2020-07-09 08:50:24 +08:00
parent 13d25f4707
commit c523462b82
1818 changed files with 174940 additions and 582 deletions

View File

@@ -0,0 +1,33 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
using System;
namespace Org.BouncyCastle.Utilities.IO.Pem
{
#if !(NETCF_1_0 || NETCF_2_0 || SILVERLIGHT || NETFX_CORE || PORTABLE)
[Serializable]
#endif
public class PemGenerationException
: Exception
{
public PemGenerationException()
: base()
{
}
public PemGenerationException(
string message)
: base(message)
{
}
public PemGenerationException(
string message,
Exception exception)
: base(message, exception)
{
}
}
}
#endif

View File

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

View File

@@ -0,0 +1,59 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
using System;
namespace 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 Platform.Equals(this.name, other.name)
&& Platform.Equals(this.val, other.val);
}
private int GetHashCode(string s)
{
if (s == null)
{
return 1;
}
return s.GetHashCode();
}
}
}
#endif

View File

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

View File

@@ -0,0 +1,51 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
using System;
using System.Collections;
using Org.BouncyCastle.Utilities.Collections;
namespace Org.BouncyCastle.Utilities.IO.Pem
{
public class PemObject
: PemObjectGenerator
{
private string type;
private IList headers;
private byte[] content;
public PemObject(string type, byte[] content)
: this(type, Platform.CreateArrayList(), content)
{
}
public PemObject(String type, IList headers, byte[] content)
{
this.type = type;
this.headers = Platform.CreateArrayList(headers);
this.content = content;
}
public string Type
{
get { return type; }
}
public IList Headers
{
get { return headers; }
}
public byte[] Content
{
get { return content; }
}
public PemObject Generate()
{
return this;
}
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 01648e8319fa246d69d930c059436d58
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)
using System;
namespace Org.BouncyCastle.Utilities.IO.Pem
{
public interface PemObjectGenerator
{
/// <returns>
/// A <see cref="PemObject"/>
/// </returns>
/// <exception cref="PemGenerationException"></exception>
PemObject Generate();
}
}
#endif

View File

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

View File

@@ -0,0 +1,100 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
using System;
using System.Collections;
using System.IO;
using System.Text;
using Org.BouncyCastle.Utilities.Encoders;
namespace Org.BouncyCastle.Utilities.IO.Pem
{
public class PemReader
{
private const string BeginString = "-----BEGIN ";
private const string EndString = "-----END ";
private readonly TextReader reader;
public PemReader(TextReader reader)
{
if (reader == null)
throw new ArgumentNullException("reader");
this.reader = reader;
}
public TextReader Reader
{
get { return reader; }
}
/// <returns>
/// A <see cref="PemObject"/>
/// </returns>
/// <exception cref="IOException"></exception>
public PemObject ReadPemObject()
{
string line = reader.ReadLine();
if (line != null && Platform.StartsWith(line, BeginString))
{
line = line.Substring(BeginString.Length);
int index = line.IndexOf('-');
string type = line.Substring(0, index);
if (index > 0)
return LoadObject(type);
}
return null;
}
private PemObject LoadObject(string type)
{
string endMarker = EndString + type;
IList headers = Platform.CreateArrayList();
StringBuilder buf = new StringBuilder();
string line;
while ((line = reader.ReadLine()) != null
&& Platform.IndexOf(line, endMarker) == -1)
{
int colonPos = line.IndexOf(':');
if (colonPos == -1)
{
buf.Append(line.Trim());
}
else
{
// Process field
string fieldName = line.Substring(0, colonPos).Trim();
if (Platform.StartsWith(fieldName, "X-"))
{
fieldName = fieldName.Substring(2);
}
string fieldValue = line.Substring(colonPos + 1).Trim();
headers.Add(new PemHeader(fieldName, fieldValue));
}
}
if (line == null)
{
throw new IOException(endMarker + " not found");
}
if (buf.Length % 4 != 0)
{
throw new IOException("base64 data appears to be truncated");
}
return new PemObject(type, headers, Base64.Decode(buf.ToString()));
}
}
}
#endif

View File

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

View File

@@ -0,0 +1,124 @@
#if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
using System;
using System.Collections;
using System.IO;
using Org.BouncyCastle.Utilities.Encoders;
namespace Org.BouncyCastle.Utilities.IO.Pem
{
/**
* A generic PEM writer, based on RFC 1421
*/
public class PemWriter
{
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)
{
if (writer == null)
throw new ArgumentNullException("writer");
this.writer = writer;
this.nlLength = Platform.NewLine.Length;
}
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 + "-----");
}
}
}
#endif

View File

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