|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftCrypto open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the SwiftCrypto project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftCrypto project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import Crypto |
| 16 | +import Foundation |
| 17 | +import SwiftASN1 |
| 18 | + |
| 19 | +@available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, *) |
| 20 | +extension Curve25519.Signing.PrivateKey { |
| 21 | + /// A Distinguished Encoding Rules (DER) encoded representation of the private key. |
| 22 | + public var derRepresentation: Data { |
| 23 | + let pkey = ASN1.PKCS8PrivateKey(algorithm: .ed25519, privateKey: Array(self.rawRepresentation)) |
| 24 | + var serializer = DER.Serializer() |
| 25 | + |
| 26 | + try! serializer.serialize(pkey) |
| 27 | + return Data(serializer.serializedBytes) |
| 28 | + } |
| 29 | + |
| 30 | + /// A Privacy-Enhanced Mail (PEM) representation of the private key. |
| 31 | + public var pemRepresentation: String { |
| 32 | + let pemDocument = ASN1.PEMDocument(type: "PRIVATE KEY", derBytes: self.derRepresentation) |
| 33 | + return pemDocument.pemString |
| 34 | + } |
| 35 | + |
| 36 | + /// Creates a Curve25519 private key for signing from a Privacy-Enhanced Mail |
| 37 | + /// (PEM) representation. |
| 38 | + /// |
| 39 | + /// - Parameters: |
| 40 | + /// - pemRepresentation: A PEM representation of the key. |
| 41 | + public init(pemRepresentation: String) throws { |
| 42 | + let document = try ASN1.PEMDocument(pemString: pemRepresentation) |
| 43 | + self = try .init(derRepresentation: document.derBytes) |
| 44 | + } |
| 45 | + |
| 46 | + /// Creates a Curve25519 private key for signing from a Distinguished Encoding |
| 47 | + /// Rules (DER) encoded representation. |
| 48 | + /// |
| 49 | + /// - Parameters: |
| 50 | + /// - derRepresentation: A DER-encoded representation of the key. |
| 51 | + public init<Bytes: RandomAccessCollection>(derRepresentation: Bytes) throws where Bytes.Element == UInt8 { |
| 52 | + let bytes = Array(derRepresentation) |
| 53 | + let key = try ASN1.PKCS8PrivateKey(derEncoded: bytes) |
| 54 | + self = try .init(rawRepresentation: key.privateKey.bytes) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +@available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, *) |
| 59 | +extension Curve25519.Signing.PublicKey { |
| 60 | + /// A Distinguished Encoding Rules (DER) encoded representation of the public key. |
| 61 | + public var derRepresentation: Data { |
| 62 | + let spki = SubjectPublicKeyInfo(algorithmIdentifier: .ed25519, key: Array(self.rawRepresentation)) |
| 63 | + var serializer = DER.Serializer() |
| 64 | + |
| 65 | + try! serializer.serialize(spki) |
| 66 | + return Data(serializer.serializedBytes) |
| 67 | + } |
| 68 | + |
| 69 | + /// A Privacy-Enhanced Mail (PEM) representation of the public key. |
| 70 | + public var pemRepresentation: String { |
| 71 | + let pemDocument = ASN1.PEMDocument(type: "PUBLIC KEY", derBytes: self.derRepresentation) |
| 72 | + return pemDocument.pemString |
| 73 | + } |
| 74 | + |
| 75 | + /// Creates a Curve25519 public key for signing from a Privacy-Enhanced Mail |
| 76 | + /// (PEM) representation. |
| 77 | + /// |
| 78 | + /// - Parameters: |
| 79 | + /// - pemRepresentation: A PEM representation of the key. |
| 80 | + public init(pemRepresentation: String) throws { |
| 81 | + let document = try ASN1.PEMDocument(pemString: pemRepresentation) |
| 82 | + self = try .init(derRepresentation: document.derBytes) |
| 83 | + } |
| 84 | + |
| 85 | + /// Creates a Curve25519 public key for signing from a Distinguished Encoding |
| 86 | + /// Rules (DER) encoded representation. |
| 87 | + /// |
| 88 | + /// - Parameters: |
| 89 | + /// - derRepresentation: A DER-encoded representation of the key. |
| 90 | + public init<Bytes: RandomAccessCollection>(derRepresentation: Bytes) throws where Bytes.Element == UInt8 { |
| 91 | + let bytes = Array(derRepresentation) |
| 92 | + let spki = try SubjectPublicKeyInfo(derEncoded: bytes) |
| 93 | + guard spki.algorithmIdentifier == .ed25519 else { |
| 94 | + throw CryptoKitASN1Error.invalidASN1Object |
| 95 | + } |
| 96 | + self = try .init(rawRepresentation: spki.key.bytes) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +@available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, *) |
| 101 | +extension Curve25519.KeyAgreement.PrivateKey { |
| 102 | + /// A Distinguished Encoding Rules (DER) encoded representation of the private key. |
| 103 | + public var derRepresentation: Data { |
| 104 | + let pkey = ASN1.PKCS8PrivateKey(algorithm: .x25519, privateKey: Array(self.rawRepresentation)) |
| 105 | + var serializer = DER.Serializer() |
| 106 | + |
| 107 | + // Serializing this key can't throw |
| 108 | + try! serializer.serialize(pkey) |
| 109 | + return Data(serializer.serializedBytes) |
| 110 | + } |
| 111 | + |
| 112 | + /// A Privacy-Enhanced Mail (PEM) representation of the private key. |
| 113 | + public var pemRepresentation: String { |
| 114 | + let pemDocument = ASN1.PEMDocument(type: "PRIVATE KEY", derBytes: self.derRepresentation) |
| 115 | + return pemDocument.pemString |
| 116 | + } |
| 117 | + |
| 118 | + /// Creates a Curve25519 private key for key agreement from a Privacy-Enhanced Mail |
| 119 | + /// (PEM) representation. |
| 120 | + /// |
| 121 | + /// - Parameters: |
| 122 | + /// - pemRepresentation: A PEM representation of the key. |
| 123 | + public init(pemRepresentation: String) throws { |
| 124 | + let document = try ASN1.PEMDocument(pemString: pemRepresentation) |
| 125 | + self = try .init(derRepresentation: document.derBytes) |
| 126 | + } |
| 127 | + |
| 128 | + /// Creates a Curve25519 private key for key agreement from a Distinguished Encoding |
| 129 | + /// Rules (DER) encoded representation. |
| 130 | + /// |
| 131 | + /// - Parameters: |
| 132 | + /// - derRepresentation: A DER-encoded representation of the key. |
| 133 | + public init<Bytes: RandomAccessCollection>(derRepresentation: Bytes) throws where Bytes.Element == UInt8 { |
| 134 | + let bytes = Array(derRepresentation) |
| 135 | + let key = try ASN1.PKCS8PrivateKey(derEncoded: bytes) |
| 136 | + self = try .init(rawRepresentation: key.privateKey.bytes) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +@available(iOS 14.0, macOS 11.0, watchOS 7.0, tvOS 14.0, *) |
| 141 | +extension Curve25519.KeyAgreement.PublicKey { |
| 142 | + /// A Distinguished Encoding Rules (DER) encoded representation of the public key. |
| 143 | + public var derRepresentation: Data { |
| 144 | + let spki = SubjectPublicKeyInfo(algorithmIdentifier: .x25519, key: Array(self.rawRepresentation)) |
| 145 | + var serializer = DER.Serializer() |
| 146 | + |
| 147 | + try! serializer.serialize(spki) |
| 148 | + return Data(serializer.serializedBytes) |
| 149 | + } |
| 150 | + |
| 151 | + /// A Privacy-Enhanced Mail (PEM) representation of the public key. |
| 152 | + public var pemRepresentation: String { |
| 153 | + let pemDocument = ASN1.PEMDocument(type: "PUBLIC KEY", derBytes: self.derRepresentation) |
| 154 | + return pemDocument.pemString |
| 155 | + } |
| 156 | + |
| 157 | + /// Creates a Curve25519 public key for key agreement from a Privacy-Enhanced Mail |
| 158 | + /// (PEM) representation. |
| 159 | + /// |
| 160 | + /// - Parameters: |
| 161 | + /// - pemRepresentation: A PEM representation of the key. |
| 162 | + public init(pemRepresentation: String) throws { |
| 163 | + let document = try ASN1.PEMDocument(pemString: pemRepresentation) |
| 164 | + self = try .init(derRepresentation: document.derBytes) |
| 165 | + } |
| 166 | + |
| 167 | + /// Creates a Curve25519 public key for key agreement from a Distinguished Encoding |
| 168 | + /// Rules (DER) encoded representation. |
| 169 | + /// |
| 170 | + /// - Parameters: |
| 171 | + /// - derRepresentation: A DER-encoded representation of the key. |
| 172 | + public init<Bytes: RandomAccessCollection>(derRepresentation: Bytes) throws where Bytes.Element == UInt8 { |
| 173 | + let bytes = Array(derRepresentation) |
| 174 | + let spki = try SubjectPublicKeyInfo(derEncoded: bytes) |
| 175 | + guard spki.algorithmIdentifier == .x25519 else { |
| 176 | + throw CryptoKitASN1Error.invalidASN1Object |
| 177 | + } |
| 178 | + self = try .init(rawRepresentation: spki.key.bytes) |
| 179 | + } |
| 180 | +} |
0 commit comments