ASN1Encoder.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //
  2. // CryptoSwift
  3. //
  4. // Copyright (C) Marcin Krzyżanowski <marcin@krzyzanowskim.com>
  5. // This software is provided 'as-is', without any express or implied warranty.
  6. //
  7. // In no event will the authors be held liable for any damages arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
  10. //
  11. // - The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required.
  12. // - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  13. // - This notice may not be removed or altered from any source or binary distribution.
  14. //
  15. // ASN1 Code inspired by Asn1Parser.swift from SwiftyRSA
  16. import Foundation
  17. extension ASN1 {
  18. enum Encoder {
  19. /// Encodes an ASN1Node into it's byte representation
  20. ///
  21. /// - Parameter node: The Node to encode
  22. /// - Returns: The encoded bytes as a UInt8 array
  23. ///
  24. /// - Warning: This ASN.1 encoder has only been tested to work on certain ASN.1 data structures such as DER and PEM files. Before using this encoder for another application, ensure you test it's behavior accordingly.
  25. /// - Warning: This encoder makes no assumptions regarding Integer bit layout and signage. The proper serialization of Integers is left up to the user.
  26. public static func encode(_ node: ASN1.Node) -> [UInt8] {
  27. switch node {
  28. case .integer(let integer):
  29. return IDENTIFIERS.INTERGER.bytes + self.asn1LengthPrefixed(integer.byteArray)
  30. case .bitString(let bits):
  31. return IDENTIFIERS.BITSTRING.bytes + self.asn1LengthPrefixed([0x00] + bits.byteArray)
  32. case .octetString(let octet):
  33. return IDENTIFIERS.OCTETSTRING.bytes + self.asn1LengthPrefixed(octet.byteArray)
  34. case .null:
  35. return IDENTIFIERS.NULL.bytes
  36. case .objectIdentifier(let oid):
  37. return IDENTIFIERS.OBJECTID.bytes + self.asn1LengthPrefixed(oid.byteArray)
  38. case .sequence(let nodes):
  39. return IDENTIFIERS.SEQUENCE.bytes + self.asn1LengthPrefixed( nodes.reduce(into: Array<UInt8>(), { partialResult, node in
  40. partialResult += encode(node)
  41. }))
  42. }
  43. }
  44. /// Calculates and returns the ASN.1 length Prefix for a chunk of data
  45. ///
  46. /// - Parameter bytes: The bytes to be length prefixed
  47. /// - Returns: The ASN.1 length Prefix for this chuck of data (excluding the passed in data)
  48. private static func asn1LengthPrefix(_ bytes: [UInt8]) -> [UInt8] {
  49. if bytes.count >= 0x80 {
  50. var lengthAsBytes = withUnsafeBytes(of: bytes.count.bigEndian, Array<UInt8>.init)
  51. while lengthAsBytes.first == 0 { lengthAsBytes.removeFirst() }
  52. return [0x80 + UInt8(lengthAsBytes.count)] + lengthAsBytes
  53. } else {
  54. return [UInt8(bytes.count)]
  55. }
  56. }
  57. /// Prefixes the provided bytes with the appropriate ASN.1 length prefix
  58. ///
  59. /// - Parameter bytes: The bytes to be length prefixed
  60. /// - Returns: The provided bytes with the appropriate ASN.1 length prefix prepended
  61. private static func asn1LengthPrefixed(_ bytes: [UInt8]) -> [UInt8] {
  62. self.asn1LengthPrefix(bytes) + bytes
  63. }
  64. }
  65. }