-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support loading RSAPSS public keys with parameters #268
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2ec52ab
Support loading RSAPSS public keys with parameters
gautierdelorme a92b3d9
address review + reset algorithm as well
gautierdelorme 8bfd22d
run update_cmakelists.sh
gautierdelorme 2992ad1
attempt to fix the cmake build
gautierdelorme 4555c1f
include FetchContent
gautierdelorme 40a8450
early return for non-PSS keys
gautierdelorme 7170c47
simplify code
gautierdelorme File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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.md for the list of SwiftCrypto project authors | ||
// | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add the license header that is present in other files and remove this header? |
||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import SwiftASN1 | ||
|
||
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) | ||
} | ||
} | ||
} | ||
|
||
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) | ||
} | ||
} | ||
} | ||
} | ||
|
||
extension SubjectPublicKeyInfo { | ||
static func stripRsaPssParameters(derEncoded: [UInt8]) throws -> [UInt8] { | ||
guard var spki = try? SubjectPublicKeyInfo(derEncoded: derEncoded) else { | ||
// If it's not a SPKI then it can't be a PSS key so we just return it. | ||
return derEncoded | ||
} | ||
|
||
guard spki.algorithmIdentifier.algorithm == .AlgorithmIdentifier.rsaPSS else { | ||
// If it's not 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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a request for changes in this PR, but can you file an issue that asks us to move all our RSA key parsing into Swift ASN1? This is a pragmatic change for now, but as we're already parsing the key we should probably just stick with that now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#269