PemWriter.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Collections;
  4. using System.IO;
  5. using Org.BouncyCastle.Utilities.Encoders;
  6. namespace Org.BouncyCastle.Utilities.IO.Pem
  7. {
  8. /**
  9. * A generic PEM writer, based on RFC 1421
  10. */
  11. public class PemWriter
  12. {
  13. private const int LineLength = 64;
  14. private readonly TextWriter writer;
  15. private readonly int nlLength;
  16. private char[] buf = new char[LineLength];
  17. /**
  18. * Base constructor.
  19. *
  20. * @param out output stream to use.
  21. */
  22. public PemWriter(TextWriter writer)
  23. {
  24. if (writer == null)
  25. throw new ArgumentNullException("writer");
  26. this.writer = writer;
  27. this.nlLength = Org.BouncyCastle.Utilities.Platform.NewLine.Length;
  28. }
  29. public TextWriter Writer
  30. {
  31. get { return writer; }
  32. }
  33. /**
  34. * Return the number of bytes or characters required to contain the
  35. * passed in object if it is PEM encoded.
  36. *
  37. * @param obj pem object to be output
  38. * @return an estimate of the number of bytes
  39. */
  40. public int GetOutputSize(PemObject obj)
  41. {
  42. // BEGIN and END boundaries.
  43. int size = (2 * (obj.Type.Length + 10 + nlLength)) + 6 + 4;
  44. if (obj.Headers.Count > 0)
  45. {
  46. foreach (PemHeader header in obj.Headers)
  47. {
  48. size += header.Name.Length + ": ".Length + header.Value.Length + nlLength;
  49. }
  50. size += nlLength;
  51. }
  52. // base64 encoding
  53. int dataLen = ((obj.Content.Length + 2) / 3) * 4;
  54. size += dataLen + (((dataLen + LineLength - 1) / LineLength) * nlLength);
  55. return size;
  56. }
  57. public void WriteObject(PemObjectGenerator objGen)
  58. {
  59. PemObject obj = objGen.Generate();
  60. WritePreEncapsulationBoundary(obj.Type);
  61. if (obj.Headers.Count > 0)
  62. {
  63. foreach (PemHeader header in obj.Headers)
  64. {
  65. writer.Write(header.Name);
  66. writer.Write(": ");
  67. writer.WriteLine(header.Value);
  68. }
  69. writer.WriteLine();
  70. }
  71. WriteEncoded(obj.Content);
  72. WritePostEncapsulationBoundary(obj.Type);
  73. }
  74. private void WriteEncoded(byte[] bytes)
  75. {
  76. bytes = Base64.Encode(bytes);
  77. for (int i = 0; i < bytes.Length; i += buf.Length)
  78. {
  79. int index = 0;
  80. while (index != buf.Length)
  81. {
  82. if ((i + index) >= bytes.Length)
  83. break;
  84. buf[index] = (char)bytes[i + index];
  85. index++;
  86. }
  87. writer.WriteLine(buf, 0, index);
  88. }
  89. }
  90. private void WritePreEncapsulationBoundary(string type)
  91. {
  92. writer.WriteLine("-----BEGIN " + type + "-----");
  93. }
  94. private void WritePostEncapsulationBoundary(string type)
  95. {
  96. writer.WriteLine("-----END " + type + "-----");
  97. }
  98. }
  99. }
  100. #endif