Files
swift-crypto/Sources/CryptoExtras/Util/SubjectPublicKeyInfo.swift
Paul Toffoloni 87a9e066d9 Add PEM/DER APIs for Curve25519 (#419)
Add PEM/DER APIs for Curve25519

### Checklist
- [x] I've run tests to see all new and existing tests pass
- [x] I've followed the code style of the rest of the project
- [x] I've read the [Contribution Guidelines](CONTRIBUTING.md)
- [x] I've updated the documentation if necessary

#### If you've made changes to `gyb` files
- [ ] I've run `./scripts/generate_boilerplate_files_with_gyb.sh` and
included updated generated files in a commit of this pull request

### Motivation:

Closes #82 
Creating Ed25519/X25519 keys using PEM/DER and also generating their
PEM/DER representations can be handy

### Modifications:

Added 
- `init(pemRepresentation:)` and the `pemRepresentation` computed
property
- `init(derRepresentation:)` and the `derRepresentation` computed
property

to `Curve25519.{Signing,KeyAgreement}.{Private,Public}Key`

### Result:

Curve25519 can now be created with and can produce PEM/DER
representations
2025-10-23 12:35:45 +01:00

135 lines
4.9 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftCrypto open source project
//
// Copyright (c) 2024 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
//
//===----------------------------------------------------------------------===//
import SwiftASN1
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
struct SubjectPublicKeyInfo: DERImplicitlyTaggable, Hashable {
static var defaultIdentifier: ASN1Identifier {
.sequence
}
var algorithmIdentifier: RFC5480AlgorithmIdentifier
var key: ASN1BitString
init(derEncoded rootNode: ASN1Node, withIdentifier identifier: ASN1Identifier) throws {
// The SPKI block looks like this:
//
// SubjectPublicKeyInfo ::= SEQUENCE {
// algorithm AlgorithmIdentifier,
// subjectPublicKey BIT STRING
// }
self = try DER.sequence(rootNode, identifier: identifier) { nodes in
let algorithmIdentifier = try RFC5480AlgorithmIdentifier(derEncoded: &nodes)
let key = try ASN1BitString(derEncoded: &nodes)
return SubjectPublicKeyInfo(algorithmIdentifier: algorithmIdentifier, key: key)
}
}
private init(algorithmIdentifier: RFC5480AlgorithmIdentifier, key: ASN1BitString) {
self.algorithmIdentifier = algorithmIdentifier
self.key = key
}
internal init(algorithmIdentifier: RFC5480AlgorithmIdentifier, key: [UInt8]) {
self.algorithmIdentifier = algorithmIdentifier
self.key = ASN1BitString(bytes: key[...])
}
func serialize(into coder: inout DER.Serializer, withIdentifier identifier: ASN1Identifier) throws {
try coder.appendConstructedNode(identifier: identifier) { coder in
try coder.serialize(self.algorithmIdentifier)
try coder.serialize(self.key)
}
}
}
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
struct RFC5480AlgorithmIdentifier: DERImplicitlyTaggable, Hashable {
static var defaultIdentifier: ASN1Identifier {
.sequence
}
var algorithm: ASN1ObjectIdentifier
var parameters: ASN1Any?
init(algorithm: ASN1ObjectIdentifier, parameters: ASN1Any?) {
self.algorithm = algorithm
self.parameters = parameters
}
init(derEncoded rootNode: ASN1Node, withIdentifier identifier: ASN1Identifier) throws {
// The AlgorithmIdentifier block looks like this.
//
// AlgorithmIdentifier ::= SEQUENCE {
// algorithm OBJECT IDENTIFIER,
// parameters ANY DEFINED BY algorithm OPTIONAL
// }
//
// ECParameters ::= CHOICE {
// namedCurve OBJECT IDENTIFIER
// -- implicitCurve NULL
// -- specifiedCurve SpecifiedECDomain
// }
//
// We don't bother with helpers: we just try to decode it directly.
self = try DER.sequence(rootNode, identifier: identifier) { nodes in
let algorithmOID = try ASN1ObjectIdentifier(derEncoded: &nodes)
let parameters = nodes.next().map { ASN1Any(derEncoded: $0) }
return .init(algorithm: algorithmOID, parameters: parameters)
}
}
func serialize(into coder: inout DER.Serializer, withIdentifier identifier: ASN1Identifier) throws {
try coder.appendConstructedNode(identifier: identifier) { coder in
try coder.serialize(self.algorithm)
if let parameters = self.parameters {
try coder.serialize(parameters)
}
}
}
}
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension SubjectPublicKeyInfo {
static func stripRsaPssParameters(derEncoded: [UInt8]) throws -> [UInt8] {
guard var spki = try? SubjectPublicKeyInfo(derEncoded: derEncoded),
spki.algorithmIdentifier.algorithm == .AlgorithmIdentifier.rsaPSS
else {
// If it's neither a SPKI nor a PSS key, we don't have to modify it.
return derEncoded
}
spki.algorithmIdentifier.algorithm = .AlgorithmIdentifier.rsaEncryption
spki.algorithmIdentifier.parameters = try ASN1Any(erasing: ASN1Null())
var serializer = DER.Serializer()
try serializer.serialize(spki)
return serializer.serializedBytes
}
}
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, macCatalyst 13, visionOS 1.0, *)
extension RFC5480AlgorithmIdentifier {
static let ed25519 = RFC5480AlgorithmIdentifier(algorithm: .AlgorithmIdentifier.idEd25519, parameters: nil)
static let x25519 = RFC5480AlgorithmIdentifier(algorithm: .AlgorithmIdentifier.idX25519, parameters: nil)
}