Compare commits

...
4 Commits
Author SHA1 Message Date
Cory Benfield da0fe44138 Fixup CryptoExtras RSA public key formats (#151)
Motivation

CryptoExtras RSA public keys support being exported in DER and PEM form.
These exports work great, but it turns out they are inconsistent between
the Darwin and non-Darwin implementations. Darwin platforms would export
the public keys in PKCS1 format, while non-Darwin was exporting them as
SPKI. This isn't great!

Modifications

- Make the two consistent. `.derRepresentation` should export SPKI
    formatted public keys, because that's what it does for all the EC
    keys.
- Add `.pkcs1DERRepresentation` and `.pkcs1PEMRepresentation` for those
    users that require the PKCS1 formatted key.

Result

Key export types are now consistent on all platforms.
2023-03-10 11:38:36 +00:00
Saleem Abdulrasool 80a6e3edc1 build: add ARM64 spelling for Windows (#148)
Windows uses `ARM64` for the `CMAKE_SYSTEM_PROCESSOR` which does not
match the existing spellings.  Add the special case for Darwin while in
the area.
2023-03-02 12:13:00 -08:00
Andrew BarbaandMax Desiatov a1b800581e Support SwiftWasm (#145)
Co-authored-by: Max Desiatov <m_desiatov@apple.com>
2023-02-09 06:04:16 -08:00
Yim Lee 935c74be2f Set up CMake build for _CryptoExtras (#144)
* Set up CMake build for _CryptoExtras

* Add CMakeLists.txt to exclude list
2023-01-26 10:51:20 +00:00
10 changed files with 250 additions and 30 deletions

No files matched your search

+26 -2
View File
@@ -3,7 +3,7 @@
//
// This source file is part of the SwiftCrypto open source project
//
// Copyright (c) 2019 Apple Inc. and the SwiftCrypto project authors
// Copyright (c) 2019-2023 Apple Inc. and the SwiftCrypto project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
@@ -47,6 +47,7 @@ if development {
Platform.linux,
Platform.android,
Platform.windows,
Platform.wasi,
]
dependencies = [
.target(name: "CCryptoBoringSSL", condition: .when(platforms: platforms)),
@@ -78,6 +79,13 @@ let package = Package(
"hash.txt",
"include/boringssl_prefix_symbols_nasm.inc",
"CMakeLists.txt",
/*
* These files are excluded to support WASI libc which doesn't provide <netdb.h>.
* This is safe for all platforms as we do not rely on networking features.
*/
"crypto/bio/connect.c",
"crypto/bio/socket_helper.c",
"crypto/bio/socket.c"
],
cSettings: [
/*
@@ -86,6 +94,11 @@ let package = Package(
* only. Unconditionally define it instead.
*/
.define("WIN32_LEAN_AND_MEAN"),
/*
* These defines are required on Wasm/WASI, to disable use of pthread.
*/
.define("OPENSSL_NO_THREADS_CORRUPT_MEMORY_AND_LEAK_SECRETS_IF_THREADED", .when(platforms: [Platform.wasi])),
.define("OPENSSL_NO_ASM", .when(platforms: [Platform.wasi])),
]
),
.target(
@@ -107,7 +120,18 @@ let package = Package(
],
swiftSettings: swiftSettings
),
.target(name: "_CryptoExtras", dependencies: ["CCryptoBoringSSL", "CCryptoBoringSSLShims", "CryptoBoringWrapper", "Crypto"]),
.target(
name: "_CryptoExtras",
dependencies: [
"CCryptoBoringSSL",
"CCryptoBoringSSLShims",
"CryptoBoringWrapper",
"Crypto"
],
exclude: [
"CMakeLists.txt",
]
),
.target(
name: "CryptoBoringWrapper",
dependencies: [
+2 -1
View File
@@ -2,7 +2,7 @@
##
## This source file is part of the SwiftCrypto open source project
##
## Copyright (c) 2021 Apple Inc. and the SwiftCrypto project authors
## Copyright (c) 2021-2023 Apple Inc. and the SwiftCrypto project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
@@ -16,3 +16,4 @@ add_subdirectory(CCryptoBoringSSL)
add_subdirectory(CCryptoBoringSSLShims)
add_subdirectory(CryptoBoringWrapper)
add_subdirectory(Crypto)
add_subdirectory(_CryptoExtras)
+46
View File
@@ -0,0 +1,46 @@
##===----------------------------------------------------------------------===##
##
## This source file is part of the SwiftCrypto open source project
##
## Copyright (c) 2023 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
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##
add_library(_CryptoExtras
"AES/AES_GCM_SIV.swift"
"AES/Block Function.swift"
"AES/BoringSSL/AES_GCM_SIV_boring.swift"
"RSA/RSA.swift"
"RSA/RSA_boring.swift"
"RSA/RSA_security.swift"
"Util/BoringSSLHelpers.swift"
"Util/CryptoKitErrors_boring.swift"
"Util/DigestType.swift"
"Util/Error.swift"
"Util/PEMDocument.swift"
"Util/RandomBytes.swift")
target_include_directories(_CryptoExtras PRIVATE
$<TARGET_PROPERTY:CCryptoBoringSSL,INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:CCryptoBoringSSLShims,INCLUDE_DIRECTORIES>)
target_link_libraries(_CryptoExtras PUBLIC
$<$<NOT:$<PLATFORM_ID:Darwin>>:Foundation>
Crypto
CryptoBoringWrapper
CCryptoBoringSSL
CCryptoBoringSSLShims)
target_link_options(_CryptoExtras PRIVATE
"$<$<PLATFORM_ID:Darwin>:SHELL:-Xlinker -framework -Xlinker Security>")
set_target_properties(_CryptoExtras PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
set_property(GLOBAL APPEND PROPERTY SWIFT_CRYPTO_EXPORTS _CryptoExtras)
+18
View File
@@ -65,6 +65,14 @@ extension _RSA.Signing {
}
}
public var pkcs1DERRepresentation: Data {
self.backing.pkcs1DERRepresentation
}
public var pkcs1PEMRepresentation: String {
self.backing.pkcs1PEMRepresentation
}
public var derRepresentation: Data {
self.backing.derRepresentation
}
@@ -309,3 +317,13 @@ extension _RSA.Signing {
}
}
}
extension _RSA {
static let PKCS1KeyType = "RSA PRIVATE KEY"
static let PKCS8KeyType = "PRIVATE KEY"
static let PKCS1PublicKeyType = "RSA PUBLIC KEY"
static let SPKIPublicKeyType = "PUBLIC KEY"
}
+22 -6
View File
@@ -29,6 +29,14 @@ internal struct BoringSSLRSAPublicKey {
self.backing = try Backing(derRepresentation: derRepresentation)
}
var pkcs1DERRepresentation: Data {
self.backing.pkcs1DERRepresentation
}
var pkcs1PEMRepresentation: String {
self.backing.pkcs1PEMRepresentation
}
var derRepresentation: Data {
self.backing.derRepresentation
}
@@ -164,6 +172,19 @@ extension BoringSSLRSAPublicKey {
}
}
fileprivate var pkcs1DERRepresentation: Data {
return BIOHelper.withWritableMemoryBIO { bio in
let rc = CCryptoBoringSSL_i2d_RSAPublicKey_bio(bio, self.pointer)
precondition(rc == 1)
return try! Data(copyingMemoryBIO: bio)
}
}
fileprivate var pkcs1PEMRepresentation: String {
return ASN1.PEMDocument(type: _RSA.PKCS1PublicKeyType, derBytes: self.pkcs1DERRepresentation).pemString
}
fileprivate var derRepresentation: Data {
return BIOHelper.withWritableMemoryBIO { bio in
let rc = CCryptoBoringSSL_i2d_RSA_PUBKEY_bio(bio, self.pointer)
@@ -174,12 +195,7 @@ extension BoringSSLRSAPublicKey {
}
fileprivate var pemRepresentation: String {
return BIOHelper.withWritableMemoryBIO { bio in
let rc = CCryptoBoringSSL_PEM_write_bio_RSA_PUBKEY(bio, self.pointer)
precondition(rc == 1)
return try! String(copyingUTF8MemoryBIO: bio)
}
return ASN1.PEMDocument(type: _RSA.SPKIPublicKeyType, derBytes: self.derRepresentation).pemString
}
fileprivate var keySizeInBits: Int {
+88 -9
View File
@@ -42,14 +42,22 @@ internal struct SecurityRSAPublicKey {
self.backing = unwrappedKey
}
var derRepresentation: Data {
var pkcs1DERRepresentation: Data {
var error: Unmanaged<CFError>? = nil
let representation = SecKeyCopyExternalRepresentation(self.backing, &error)
return representation! as Data
}
var pkcs1PEMRepresentation: String {
return ASN1.PEMDocument(type: _RSA.PKCS1PublicKeyType, derBytes: self.pkcs1DERRepresentation).pemString
}
var derRepresentation: Data {
return Data(spkiBytesForPKCS1Bytes: self.pkcs1DERRepresentation)
}
var pemRepresentation: String {
return ASN1.PEMDocument(type: "PUBLIC KEY", derBytes: self.derRepresentation).pemString
return ASN1.PEMDocument(type: _RSA.SPKIPublicKeyType, derBytes: self.derRepresentation).pemString
}
var keySizeInBits: Int {
@@ -66,18 +74,14 @@ internal struct SecurityRSAPublicKey {
internal struct SecurityRSAPrivateKey {
private var backing: SecKey
static let PKCS1KeyType = "RSA PRIVATE KEY"
static let PKCS8KeyType = "PRIVATE KEY"
init(pemRepresentation: String) throws {
let document = try ASN1.PEMDocument(pemString: pemRepresentation)
switch document.type {
case SecurityRSAPrivateKey.PKCS1KeyType:
case _RSA.PKCS1KeyType:
// This is what is expected by Security.framework
self = try .init(derRepresentation: document.derBytes)
case SecurityRSAPrivateKey.PKCS8KeyType:
case _RSA.PKCS8KeyType:
guard let pkcs8Bytes = document.derBytes.pkcs8RSAKeyBytes else {
throw _CryptoRSAError.invalidPEMDocument
}
@@ -138,7 +142,7 @@ internal struct SecurityRSAPrivateKey {
}
var pemRepresentation: String {
return ASN1.PEMDocument(type: SecurityRSAPrivateKey.PKCS1KeyType, derBytes: self.derRepresentation).pemString
return ASN1.PEMDocument(type: _RSA.PKCS1KeyType, derBytes: self.derRepresentation).pemString
}
var keySizeInBits: Int {
@@ -301,6 +305,81 @@ extension Data {
return self.dropFirst(4 + Data.partialPKCS8Prefix.count + 4)
}
// Corresponds to the ASN.1 encoding of the RSA AlgorithmIdentifier:
//
// SEQUENCE of OID (:rsaEncryption) and NULL.
static let rsaAlgorithmIdentifierBytes = Data([
0x30, 0x0D, // SEQUENCE, Length 13
0x06, 0x09, // OID, length 9
0x2A, 0x86 , 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, // 1.2.840.113549.1.1.1 :rsaEncryption
0x05, 0x00, // NULL, length 0
])
fileprivate init(spkiBytesForPKCS1Bytes pkcs1Bytes: Data) {
// This does an ad-hoc SPKI encode. Ideally we'd bring over the entire ASN.1 stack, but it's not worth doing
// for just this one use-case.
let keyLength = (pkcs1Bytes.count + 1)
let bitStringOverhead = 1 + keyLength._bytesNeededToEncodeASN1Length // 1 byte for tag.
let totalLengthOfSequencePayload = Self.rsaAlgorithmIdentifierBytes.count + bitStringOverhead + keyLength
var bytes = Data()
bytes.reserveCapacity(1 + totalLengthOfSequencePayload._bytesNeededToEncodeASN1Length + totalLengthOfSequencePayload)
bytes.append(0x30) // SEQUENCE marker.
bytes.appendAsASN1NodeLength(totalLengthOfSequencePayload)
bytes.append(Self.rsaAlgorithmIdentifierBytes)
bytes.append(0x03) // BITSTRING marker
bytes.appendAsASN1NodeLength(keyLength)
bytes.append(UInt8(0)) // No padding bits
bytes.append(contentsOf: pkcs1Bytes)
self = bytes
}
fileprivate mutating func appendAsASN1NodeLength(_ length: Int) {
let bytesNeeded = length._bytesNeededToEncodeASN1Length
if bytesNeeded == 1 {
self.append(UInt8(length))
} else {
// We first write the number of length bytes
// we need, setting the high bit. Then we write the bytes of the length.
self.append(0x80 | UInt8(bytesNeeded - 1))
for shift in (0..<(bytesNeeded - 1)).reversed() {
// Shift and mask the integer.
self.append(UInt8(truncatingIfNeeded: (length >> (shift * 8))))
}
}
}
}
extension Int {
fileprivate var _bytesNeededToEncodeASN1Length: Int {
// ASN.1 lengths are in two forms. If we can store the length in 7 bits, we should:
// that requires only one byte. Otherwise, we need multiple bytes: work out how many,
// plus one for the length of the length bytes.
if self <= 0x7F {
return 1
} else {
// We need to work out how many bytes we need. There are many fancy bit-twiddling
// ways of doing this, but honestly we don't do this enough to need them, so we'll
// do it the easy way. This math is done on UInt because it makes the shift semantics clean.
// We save a branch here because we can never overflow this addition.
return UInt(self).neededBytes &+ 1
}
}
}
extension UInt {
// Bytes needed to store a given integer in 7 bit bytes.
fileprivate var neededBytes: Int {
let neededBits = self.bitWidth - self.leadingZeroBitCount
return (neededBits + 7) / 8
}
}
#endif
+37 -5
View File
@@ -13,7 +13,7 @@
//===----------------------------------------------------------------------===//
import Foundation
import XCTest
import Crypto
@testable import Crypto
import _CryptoExtras
final class TestRSASigning: XCTestCase {
@@ -569,7 +569,7 @@ final class TestRSASigning: XCTestCase {
XCTAssertThrowsError(try _RSA.Signing.PrivateKey(keySize: .init(bitCount: 1016)))
}
func testParsingPKCS1PublicKeyKeyDER() throws {
func testParsingPKCS1PublicKeyDER() throws {
let pkcs1Key = Data(base64Encoded:
"MIICCgKCAgEAkehUktIKVrGsDSTdxc9EZ3SZKzejfSNwAHG8U9/E+ioSj0t" +
"/EFa9n3Byt2F/yUsPF6c947AEYe7/EZfH9IY+Cvo+XPmT5jR62RRr55yzha" +
@@ -584,10 +584,11 @@ final class TestRSASigning: XCTestCase {
"aG4Nj/QN370EKIf6MzOi5cHkERgWPOGHFrK+ymircxXDpqR+DDeVnWIBqv8" +
"mqYqnK8V0rSS527EPywTEHl7R09XiidnMy/s1Hap0flhFMCAwEAAQ=="
)!
XCTAssertNoThrow(try _RSA.Signing.PublicKey(derRepresentation: pkcs1Key))
let key = try _RSA.Signing.PublicKey(derRepresentation: pkcs1Key)
XCTAssertEqual(pkcs1Key, key.pkcs1DERRepresentation)
}
func testParsingPKCS1PublicKeyKeyPEM() throws {
func testParsingPKCS1PublicKeyPEM() throws {
let pemKey = """
-----BEGIN RSA PUBLIC KEY-----
MIICCgKCAgEAkehUktIKVrGsDSTdxc9EZ3SZKzejfSNwAHG8U9/E+ioSj0t/EFa9
@@ -603,7 +604,38 @@ final class TestRSASigning: XCTestCase {
eVnWIBqv8mqYqnK8V0rSS527EPywTEHl7R09XiidnMy/s1Hap0flhFMCAwEAAQ==
-----END RSA PUBLIC KEY-----
"""
XCTAssertNoThrow(try _RSA.Signing.PublicKey(pemRepresentation: pemKey))
let key = try _RSA.Signing.PublicKey(pemRepresentation: pemKey)
XCTAssertEqual(pemKey, key.pkcs1PEMRepresentation)
}
func testParsingSPKIPublicKeyDER() throws {
let derKey = Data(base64Encoded:
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA509zjqylvktpuN3zMpdw" +
"YwsZ2dp9/cJZ2Krp2EqK+UvMJcp4T3O9rWPMZk1RocQWLpfSwF8jtfyy1OHDQEZh" +
"7UkpnlHmCwlNzzCj+/eaC+JP2Dy6p62nCMonjebPCZ5lhramaO4csrL4bmKdCw5i" +
"XEEaQdwaA8k7Pvv2pkT+X50ZJKBQAaiHo2yRILI5n15UZ4y0fB+HCvA5qebZtkM0" +
"gFqLPxNy1f8oYXuG9KE6sRn/pRwuYuBYD3eAqP6GquO0DkJKmq8RXeewx8ijUBd7" +
"2xiZlbnBZxwvu5eEH5XD9iqf+liS+yA1wORQtQhSwuWApk9acaIP9IjyW2zojAtS" +
"hwIDAQAB"
)!
let key = try _RSA.Signing.PublicKey(derRepresentation: derKey)
XCTAssertEqual(derKey, key.derRepresentation)
}
func testParsingSPKIPublicKeyPEM() throws {
let pemKey = """
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA509zjqylvktpuN3zMpdw
YwsZ2dp9/cJZ2Krp2EqK+UvMJcp4T3O9rWPMZk1RocQWLpfSwF8jtfyy1OHDQEZh
7UkpnlHmCwlNzzCj+/eaC+JP2Dy6p62nCMonjebPCZ5lhramaO4csrL4bmKdCw5i
XEEaQdwaA8k7Pvv2pkT+X50ZJKBQAaiHo2yRILI5n15UZ4y0fB+HCvA5qebZtkM0
gFqLPxNy1f8oYXuG9KE6sRn/pRwuYuBYD3eAqP6GquO0DkJKmq8RXeewx8ijUBd7
2xiZlbnBZxwvu5eEH5XD9iqf+liS+yA1wORQtQhSwuWApk9acaIP9IjyW2zojAtS
hwIDAQAB
-----END PUBLIC KEY-----
"""
let key = try _RSA.Signing.PublicKey(pemRepresentation: pemKey)
XCTAssertEqual(pemKey, key.pemRepresentation)
}
private func testPKCS1Group(_ group: RSAPKCS1TestGroup) throws {
+6 -4
View File
@@ -22,10 +22,12 @@
function(get_swift_host_arch result_var_name)
if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
set("${result_var_name}" "x86_64" PARENT_SCOPE)
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64")
set("${result_var_name}" "aarch64" PARENT_SCOPE)
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "arm64")
set("${result_var_name}" "aarch64" PARENT_SCOPE)
elseif("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "AArch64|aarch64|arm64|ARM64")
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
set("${result_var_name}" "arm64" PARENT_SCOPE)
else()
set("${result_var_name}" "aarch64" PARENT_SCOPE)
endif()
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64")
set("${result_var_name}" "powerpc64" PARENT_SCOPE)
elseif("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64le")
+2 -2
View File
@@ -3,7 +3,7 @@
##
## This source file is part of the SwiftCrypto open source project
##
## Copyright (c) 2019-2021 Apple Inc. and the SwiftCrypto project authors
## Copyright (c) 2019-2023 Apple Inc. and the SwiftCrypto project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
@@ -17,7 +17,7 @@ set -eu
here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
function replace_acceptable_years() {
# this needs to replace all acceptable forms with 'YEARS'
sed -e 's/20[12][890]-20[12][1290]/YEARS/' -e 's/20[12][90123]/YEARS/'
sed -e 's/20[12][8901]-20[12][90123]/YEARS/' -e 's/20[12][90123]/YEARS/'
}
printf "=> Checking for unacceptable language... "
+3 -1
View File
@@ -3,7 +3,7 @@
##
## This source file is part of the SwiftCrypto open source project
##
## Copyright (c) 2021 Apple Inc. and the SwiftCrypto project authors
## Copyright (c) 2021-2023 Apple Inc. and the SwiftCrypto project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
@@ -80,6 +80,8 @@ function update_cmakelists_assembly() {
update_cmakelists_source "CCryptoBoringSSL" "*.c"
update_cmakelists_source "CCryptoBoringSSLShims" "*.c"
update_cmakelists_source "CryptoBoringWrapper" "*.swift"
update_cmakelists_source "Crypto" "*.swift"
update_cmakelists_source "_CryptoExtras" "*.swift"
update_cmakelists_assembly "CCryptoBoringSSL"