diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2021-07-08 17:05:55 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2021-07-08 17:05:55 +0700 |
commit | 4206023cd1ba392562ecc3d9b3f37f7e1b456d76 (patch) | |
tree | 12b659481c05c9e93ff7dfd863e2b996cf793b52 /crypto/src/asn1/BerOctetString.cs | |
parent | Check key sizes strictly (diff) | |
download | BouncyCastle.NET-ed25519-4206023cd1ba392562ecc3d9b3f37f7e1b456d76.tar.xz |
ASN.1 updates from bc-java
Diffstat (limited to 'crypto/src/asn1/BerOctetString.cs')
-rw-r--r-- | crypto/src/asn1/BerOctetString.cs | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/crypto/src/asn1/BerOctetString.cs b/crypto/src/asn1/BerOctetString.cs index f41aaf6a5..2b4de4b2f 100644 --- a/crypto/src/asn1/BerOctetString.cs +++ b/crypto/src/asn1/BerOctetString.cs @@ -1,5 +1,6 @@ using System; using System.Collections; +using System.Diagnostics; using System.IO; using Org.BouncyCastle.Utilities; @@ -22,15 +23,36 @@ namespace Org.BouncyCastle.Asn1 return new BerOctetString(v); } - private static byte[] ToBytes(Asn1OctetString[] octs) + internal static byte[] FlattenOctetStrings(Asn1OctetString[] octetStrings) { - MemoryStream bOut = new MemoryStream(); - foreach (Asn1OctetString o in octs) + int count = octetStrings.Length; + switch (count) { - byte[] octets = o.GetOctets(); - bOut.Write(octets, 0, octets.Length); + case 0: + return EmptyOctets; + case 1: + return octetStrings[0].str; + default: + { + int totalOctets = 0; + for (int i = 0; i < count; ++i) + { + totalOctets += octetStrings[i].str.Length; + } + + byte[] str = new byte[totalOctets]; + int pos = 0; + for (int i = 0; i < count; ++i) + { + byte[] octets = octetStrings[i].str; + Array.Copy(octets, 0, str, pos, octets.Length); + pos += octets.Length; + } + + Debug.Assert(pos == totalOctets); + return str; + } } - return bOut.ToArray(); } private static Asn1OctetString[] ToOctetStringArray(IEnumerable e) @@ -71,7 +93,7 @@ namespace Org.BouncyCastle.Asn1 } public BerOctetString(Asn1OctetString[] octs, int chunkSize) - : this(ToBytes(octs), octs, chunkSize) + : this(FlattenOctetStrings(octs), octs, chunkSize) { } |