|
| 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 SwiftASN1 |
| 17 | + |
| 18 | +@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *) |
| 19 | +extension ASN1 { |
| 20 | + // A PKCS#8 private key is one of two formats, depending on the version: |
| 21 | + // |
| 22 | + // For PKCS#8 we need the following for the private key: |
| 23 | + // |
| 24 | + // PrivateKeyInfo ::= SEQUENCE { |
| 25 | + // version Version, |
| 26 | + // privateKeyAlgorithm PrivateKeyAlgorithmIdentifier, |
| 27 | + // privateKey PrivateKey, |
| 28 | + // attributes [0] IMPLICIT Attributes OPTIONAL } |
| 29 | + // |
| 30 | + // Version ::= INTEGER |
| 31 | + // |
| 32 | + // PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier |
| 33 | + // |
| 34 | + // PrivateKey ::= OCTET STRING |
| 35 | + // |
| 36 | + // Attributes ::= SET OF Attribute |
| 37 | + // |
| 38 | + // We disregard the attributes because we don't support them anyway. |
| 39 | + @available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *) |
| 40 | + struct PKCS8PrivateKey: DERImplicitlyTaggable { |
| 41 | + static var defaultIdentifier: ASN1Identifier { |
| 42 | + .sequence |
| 43 | + } |
| 44 | + |
| 45 | + var algorithm: RFC8410AlgorithmIdentifier |
| 46 | + |
| 47 | + var privateKey: ASN1OctetString |
| 48 | + |
| 49 | + init(derEncoded rootNode: ASN1Node, withIdentifier identifier: ASN1Identifier) throws { |
| 50 | + self = try DER.sequence(rootNode, identifier: identifier) { nodes in |
| 51 | + let version = try Int(derEncoded: &nodes) |
| 52 | + guard version == 0 || version == 1 else { |
| 53 | + throw ASN1Error.invalidASN1Object(reason: "Version number mismatch") |
| 54 | + } |
| 55 | + |
| 56 | + let algorithm = try ASN1.RFC8410AlgorithmIdentifier(derEncoded: &nodes) |
| 57 | + let privateKeyBytes = try ASN1OctetString(derEncoded: &nodes) |
| 58 | + |
| 59 | + // We ignore the attributes |
| 60 | + _ = try DER.optionalExplicitlyTagged(&nodes, tagNumber: 0, tagClass: .contextSpecific) { _ in } |
| 61 | + |
| 62 | + let privateKeyNode = try DER.parse(privateKeyBytes.bytes) |
| 63 | + let privateKey = try ASN1OctetString(derEncoded: privateKeyNode) |
| 64 | + |
| 65 | + return try .init(algorithm: algorithm, privateKey: privateKey) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private init(algorithm: ASN1.RFC8410AlgorithmIdentifier, privateKey: ASN1OctetString) throws { |
| 70 | + self.privateKey = privateKey |
| 71 | + self.algorithm = algorithm |
| 72 | + } |
| 73 | + |
| 74 | + init(algorithm: ASN1.RFC8410AlgorithmIdentifier, privateKey: [UInt8]) { |
| 75 | + self.algorithm = algorithm |
| 76 | + self.privateKey = ASN1OctetString(contentBytes: privateKey[...]) |
| 77 | + } |
| 78 | + |
| 79 | + func serialize(into coder: inout DER.Serializer, withIdentifier identifier: ASN1Identifier) throws { |
| 80 | + try coder.appendConstructedNode(identifier: identifier) { coder in |
| 81 | + try coder.serialize(0) |
| 82 | + try coder.serialize(self.algorithm) |
| 83 | + |
| 84 | + // Here's a weird one: we recursively serialize the private key, and then turn the bytes into an octet string. |
| 85 | + var subCoder = DER.Serializer() |
| 86 | + try subCoder.serialize(self.privateKey) |
| 87 | + let serializedKey = ASN1OctetString(contentBytes: subCoder.serializedBytes[...]) |
| 88 | + |
| 89 | + try coder.serialize(serializedKey) |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments