Skip to content

Commit

Permalink
Merge pull request #101 from vapor/c-namespace
Browse files Browse the repository at this point in the history
add c namespace
  • Loading branch information
tanner0101 authored Aug 15, 2019
2 parents b75ca4b + 2e7599a commit 3a9ada8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
31 changes: 20 additions & 11 deletions Sources/CJWTKitCrypto/c_jwtkit_crypto.c
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
#include "include/c_jwtkit_crypto.h"

#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
EVP_MD_CTX *EVP_MD_CTX_new(void) {
EVP_MD_CTX *jwtkit_EVP_MD_CTX_new(void) {
return EVP_MD_CTX_create();
};

void EVP_MD_CTX_free(EVP_MD_CTX *ctx) {
void jwtkit_EVP_MD_CTX_free(EVP_MD_CTX *ctx) {
EVP_MD_CTX_cleanup(ctx);
free(ctx);
};

int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d) {
r->n = n;
r->e = e;
r->d = d;
return 0;
};

HMAC_CTX *HMAC_CTX_new(void) {
HMAC_CTX *jwtkit_HMAC_CTX_new(void) {
HMAC_CTX *ptr = malloc(sizeof(HMAC_CTX));
HMAC_CTX_init(ptr);
return ptr;
};

void HMAC_CTX_free(HMAC_CTX *ctx) {
void jwtkit_HMAC_CTX_free(HMAC_CTX *ctx) {
HMAC_CTX_cleanup(ctx);
free(ctx);
};
#else
EVP_MD_CTX *jwtkit_EVP_MD_CTX_new(void) {
return EVP_MD_CTX_new();
};

void jwtkit_EVP_MD_CTX_free(EVP_MD_CTX *ctx) {
EVP_MD_CTX_free(ctx);
};

HMAC_CTX *jwtkit_HMAC_CTX_new(void) {
return HMAC_CTX_new();
};

void jwtkit_HMAC_CTX_free(HMAC_CTX *ctx) {
HMAC_CTX_free(ctx);
};
#endif
11 changes: 4 additions & 7 deletions Sources/CJWTKitCrypto/include/c_jwtkit_crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@
#include <openssl/pkcs12.h>
#include <openssl/x509v3.h>

#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
EVP_MD_CTX *EVP_MD_CTX_new(void);
void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d);
HMAC_CTX *HMAC_CTX_new(void);
void HMAC_CTX_free(HMAC_CTX *ctx);
#endif
EVP_MD_CTX *jwtkit_EVP_MD_CTX_new(void);
void jwtkit_EVP_MD_CTX_free(EVP_MD_CTX *ctx);
HMAC_CTX *jwtkit_HMAC_CTX_new(void);
void jwtkit_HMAC_CTX_free(HMAC_CTX *ctx);

#endif
4 changes: 2 additions & 2 deletions Sources/JWTKit/JWTSigner+HMAC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ private struct HMACSigner: JWTAlgorithm {
func sign<Plaintext>(_ plaintext: Plaintext) throws -> [UInt8]
where Plaintext: DataProtocol
{
let context = HMAC_CTX_new()
defer { HMAC_CTX_free(context) }
let context = jwtkit_HMAC_CTX_new()
defer { jwtkit_HMAC_CTX_free(context) }

guard self.key.withUnsafeBytes({
return HMAC_Init_ex(context, $0.baseAddress?.assumingMemoryBound(to: UInt8.self), Int32($0.count), convert(self.algorithm), nil)
Expand Down
4 changes: 2 additions & 2 deletions Sources/JWTKit/OpenSSLSigner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ extension OpenSSLSigner {
func digest<Plaintext>(_ plaintext: Plaintext) throws -> [UInt8]
where Plaintext: DataProtocol
{
let context = EVP_MD_CTX_new()
defer { EVP_MD_CTX_free(context) }
let context = jwtkit_EVP_MD_CTX_new()
defer { jwtkit_EVP_MD_CTX_free(context) }

guard EVP_DigestInit_ex(context, convert(self.algorithm), nil) == 1 else {
throw JWTError.signingAlgorithmFailure(OpenSSLError.digestInitializationFailure)
Expand Down

0 comments on commit 3a9ada8

Please sign in to comment.