Added Linux support
This commit is contained in:
parent
05bb768867
commit
6208524c15
|
@ -16,3 +16,20 @@ let package = Package(
|
|||
.testTarget(name: "AWSSignerTests", dependencies: ["AWSSigner"]),
|
||||
]
|
||||
)
|
||||
|
||||
// switch for whether to use CAWSSDKOpenSSL to shim between OpenSSL versions
|
||||
#if os(Linux)
|
||||
let useOpenSSLShim = true
|
||||
#else
|
||||
let useOpenSSLShim = false
|
||||
#endif
|
||||
|
||||
// AWSSDKSwiftCore target
|
||||
let awsSdkSwiftCoreTarget = package.targets.first(where: {$0.name == "AWSSigner"})
|
||||
|
||||
// Decide on where we get our SSL support from. Linux usses NIOSSL to provide SSL. Linux also needs CAWSSDKOpenSSL to shim across different OpenSSL versions for the HMAC functions.
|
||||
if useOpenSSLShim {
|
||||
package.targets.append(.target(name: "CAWSSigner"))
|
||||
awsSdkSwiftCoreTarget?.dependencies.append("CAWSSigner")
|
||||
package.dependencies.append(.package(url: "https://github.com/apple/swift-nio-ssl-support.git", from: "1.0.0"))
|
||||
}
|
||||
|
|
|
@ -7,8 +7,44 @@
|
|||
|
||||
import Foundation
|
||||
|
||||
// Currently only works if CommonCrypto exists. Will look into doing something for Linux later
|
||||
#if canImport(CommonCrypto)
|
||||
// use CAWSSigner if available, otherwise use CommonCrypto
|
||||
// Package.swift includes CAWSSigner target if we are running on Linux
|
||||
#if canImport(CAWSSigner)
|
||||
|
||||
import CAWSSigner
|
||||
|
||||
public func sha256(_ string: String) -> [UInt8] {
|
||||
let bytes = Array(string.utf8)
|
||||
return sha256(bytes)
|
||||
}
|
||||
|
||||
public func sha256(_ bytes: [UInt8]) -> [UInt8] {
|
||||
var hash = [UInt8](repeating: 0, count: Int(SHA256_DIGEST_LENGTH))
|
||||
SHA256(bytes, bytes.count, &hash)
|
||||
return hash
|
||||
}
|
||||
|
||||
public func sha256(_ buffer: UnsafeBufferPointer<UInt8>) -> [UInt8] {
|
||||
var hash = [UInt8](repeating: 0, count: Int(SHA256_DIGEST_LENGTH))
|
||||
SHA256(buffer.baseAddress, buffer.count, &hash)
|
||||
return hash
|
||||
}
|
||||
|
||||
func hmac(string: String, key: [UInt8]) -> [UInt8] {
|
||||
let context = AWS_SIGNER_HMAC_CTX_new()
|
||||
HMAC_Init_ex(context, key, Int32(key.count), EVP_sha256(), nil)
|
||||
|
||||
let bytes = Array(string.utf8)
|
||||
HMAC_Update(context, bytes, bytes.count)
|
||||
var digest = [UInt8](repeating: 0, count: Int(EVP_MAX_MD_SIZE))
|
||||
var length: UInt32 = 0
|
||||
HMAC_Final(context, &digest, &length)
|
||||
AWS_SIGNER_HMAC_CTX_free(context)
|
||||
|
||||
return Array(digest[0..<Int(length)])
|
||||
}
|
||||
|
||||
#elseif canImport(CommonCrypto)
|
||||
|
||||
import CommonCrypto
|
||||
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
//
|
||||
// c_awssdk_openssl.h
|
||||
// AWSSDKSwiftCore
|
||||
//
|
||||
// Created by Adam Fowler on 2019/08/08.
|
||||
//
|
||||
|
||||
#ifndef C_AWSSDK_OPENSSL_H
|
||||
#define C_AWSSDK_OPENSSL_H
|
||||
|
||||
#include <openssl/hmac.h>
|
||||
#include <openssl/md5.h>
|
||||
#include <openssl/sha.h>
|
||||
|
||||
HMAC_CTX *AWS_SIGNER_HMAC_CTX_new();
|
||||
void AWS_SIGNER_HMAC_CTX_free(HMAC_CTX* ctx);
|
||||
|
||||
#endif // C_AWSSDK_OPENSSL_H
|
|
@ -0,0 +1,34 @@
|
|||
//
|
||||
// shims.c
|
||||
// AWSSDKSwiftCore
|
||||
//
|
||||
// Created by Adam Fowler on 2019/08/08.
|
||||
//
|
||||
|
||||
// These are functions that shim over differences in different OpenSSL versions,
|
||||
// which are best handled by using the C preprocessor.
|
||||
#include "c_aws_signer.h"
|
||||
#include <string.h>
|
||||
|
||||
HMAC_CTX *AWS_SIGNER_HMAC_CTX_new() {
|
||||
#if OPENSSL_VERSION_NUMBER < 0x10100000L || (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
|
||||
HMAC_CTX *ctx = OPENSSL_malloc(sizeof(HMAC_CTX));
|
||||
if (ctx != NULL) {
|
||||
HMAC_CTX_init(ctx);
|
||||
}
|
||||
return ctx;
|
||||
#else
|
||||
return HMAC_CTX_new();
|
||||
#endif
|
||||
}
|
||||
|
||||
void AWS_SIGNER_HMAC_CTX_free(HMAC_CTX* ctx) {
|
||||
#if OPENSSL_VERSION_NUMBER < 0x10100000L || (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
|
||||
if (ctx != NULL) {
|
||||
HMAC_CTX_cleanup(ctx);
|
||||
OPENSSL_free(ctx);
|
||||
}
|
||||
#else
|
||||
HMAC_CTX_free(ctx);
|
||||
#endif
|
||||
}
|
Loading…
Reference in New Issue