mirror of
https://github.com/apple/swift-crypto
synced 2026-11-24 04:37:48 +00:00
+6








431013afaf
This change updates Crypto to WWDC25 equivalent code. Each of these changes were individually merged to the WWDC25 branch. This includes: - #359 - #363 - #365 - #367 - #368 - #397 - #403 - #410 - #407 - #401 - #415 --------- Co-authored-by: YourMJK <37852512+YourMJK@users.noreply.github.com> Co-authored-by: Si Beaumont <beaumont@apple.com> Co-authored-by: Rick Newton-Rogers <rnro@apple.com> Co-authored-by: Raphael <josephnoir@users.noreply.github.com> Co-authored-by: Evan Wilde <ewilde@apple.com> Co-authored-by: Tim Condon <0xTim@users.noreply.github.com> Co-authored-by: Gus Cairo <gus.cairo@apple.com> Co-authored-by: George Barnett <gbarnett@apple.com> Co-authored-by: Fabrice de Gans <Steelskin@users.noreply.github.com> Co-authored-by: Wojciech Nagrodzki <278594+wnagrodzki@users.noreply.github.com> Co-authored-by: Jesse L. Zamora <xtremekforever@gmail.com> Co-authored-by: aryan-25 <aryan_shah@apple.com> Co-authored-by: Gwynne Raskind <gwynne@vapor.codes> Co-authored-by: 3405691582 <dsk@google.com> Co-authored-by: Paul Toffoloni <69189821+ptoffy@users.noreply.github.com>
122 lines
4.5 KiB
Swift
122 lines
4.5 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftCrypto open source project
|
|
//
|
|
// Copyright (c) 2019-2021 Apple Inc. and the SwiftCrypto project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftCrypto project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
#if canImport(FoundationEssentials)
|
|
import FoundationEssentials
|
|
#else
|
|
import Foundation
|
|
#endif
|
|
import Crypto
|
|
|
|
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
|
enum ASN1 {
|
|
/// A PEM document is some data, and a discriminator type that is used to advertise the content.
|
|
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
|
struct PEMDocument {
|
|
private static let lineLength = 64
|
|
|
|
var type: String
|
|
|
|
var derBytes: Data
|
|
|
|
init(pemString: String) throws {
|
|
// A PEM document looks like this:
|
|
//
|
|
// -----BEGIN <SOME DISCRIMINATOR>-----
|
|
// <base64 encoded bytes, 64 characters per line>
|
|
// -----END <SOME DISCRIMINATOR>-----
|
|
//
|
|
// This function attempts to parse this string as a PEM document, and returns the discriminator type
|
|
// and the base64 decoded bytes.
|
|
var lines = pemString.split { $0.isNewline }[...]
|
|
guard let first = lines.first, let last = lines.last else {
|
|
throw _CryptoRSAError.invalidPEMDocument
|
|
}
|
|
|
|
guard let discriminator = first.pemStartDiscriminator, discriminator == last.pemEndDiscriminator else {
|
|
throw _CryptoRSAError.invalidPEMDocument
|
|
}
|
|
|
|
// All but the last line must be 64 bytes. The force unwrap is safe because we require the lines to be
|
|
// greater than zero.
|
|
lines = lines.dropFirst().dropLast()
|
|
guard lines.count > 0,
|
|
lines.dropLast().allSatisfy({ $0.utf8.count == PEMDocument.lineLength }),
|
|
lines.last!.utf8.count <= PEMDocument.lineLength else {
|
|
throw _CryptoRSAError.invalidPEMDocument
|
|
}
|
|
|
|
guard let derBytes = Data(base64Encoded: lines.joined()) else {
|
|
throw _CryptoRSAError.invalidPEMDocument
|
|
}
|
|
|
|
self.type = discriminator
|
|
self.derBytes = derBytes
|
|
}
|
|
|
|
init(type: String, derBytes: Data) {
|
|
self.type = type
|
|
self.derBytes = derBytes
|
|
}
|
|
|
|
var pemString: String {
|
|
var encoded = self.derBytes.base64EncodedString()[...]
|
|
let pemLineCount = (encoded.utf8.count + PEMDocument.lineLength) / PEMDocument.lineLength
|
|
var pemLines = [Substring]()
|
|
pemLines.reserveCapacity(pemLineCount + 2)
|
|
|
|
pemLines.append("-----BEGIN \(self.type)-----")
|
|
|
|
while encoded.count > 0 {
|
|
let prefixIndex = encoded.index(encoded.startIndex, offsetBy: PEMDocument.lineLength, limitedBy: encoded.endIndex) ?? encoded.endIndex
|
|
pemLines.append(encoded[..<prefixIndex])
|
|
encoded = encoded[prefixIndex...]
|
|
}
|
|
|
|
pemLines.append("-----END \(self.type)-----")
|
|
|
|
return pemLines.joined(separator: "\n")
|
|
}
|
|
}
|
|
}
|
|
|
|
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
|
|
extension Substring {
|
|
fileprivate var pemStartDiscriminator: String? {
|
|
return self.pemDiscriminator(expectedPrefix: "-----BEGIN ", expectedSuffix: "-----")
|
|
}
|
|
|
|
fileprivate var pemEndDiscriminator: String? {
|
|
return self.pemDiscriminator(expectedPrefix: "-----END ", expectedSuffix: "-----")
|
|
}
|
|
|
|
private func pemDiscriminator(expectedPrefix: String, expectedSuffix: String) -> String? {
|
|
var utf8Bytes = self.utf8[...]
|
|
|
|
// We want to split this sequence into three parts: the prefix, the middle, and the end
|
|
let prefixSize = expectedPrefix.utf8.count
|
|
let suffixSize = expectedSuffix.utf8.count
|
|
|
|
let prefix = utf8Bytes.prefix(prefixSize)
|
|
utf8Bytes = utf8Bytes.dropFirst(prefixSize)
|
|
let suffix = utf8Bytes.suffix(suffixSize)
|
|
utf8Bytes = utf8Bytes.dropLast(suffixSize)
|
|
|
|
guard prefix.elementsEqual(expectedPrefix.utf8), suffix.elementsEqual(expectedSuffix.utf8) else {
|
|
return nil
|
|
}
|
|
|
|
return String(utf8Bytes)
|
|
}
|
|
}
|