Skip to content

Commit 4a37947

Browse files
authored
Merge pull request #3166 from dgarske/csharp_wolfcrypt
wolfCrypt CSharp Wrapper
2 parents bf29b68 + e4f4274 commit 4a37947

46 files changed

Lines changed: 5863 additions & 1008 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: Windows CSharp Build Test
2+
3+
on:
4+
push:
5+
branches: [ 'master', 'main', 'release/**' ]
6+
pull_request:
7+
branches: [ '*' ]
8+
9+
jobs:
10+
build:
11+
12+
runs-on: windows-latest
13+
14+
# This should be a safe limit for the tests to run.
15+
timeout-minutes: 6
16+
17+
env:
18+
# Path to the solution file relative to the root of the project.
19+
SOLUTION_FILE_PATH: wolfssl\wrapper\CSharp\wolfSSL_CSharp.sln
20+
21+
# Configuration type to build.
22+
# You can convert this to a build matrix if you need coverage of multiple configuration types.
23+
# https://docs.github.com/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
24+
BUILD_CONFIGURATION: Debug
25+
BUILD_PLATFORM: x64
26+
27+
steps:
28+
- name: Pull wolfssl
29+
uses: actions/checkout@master
30+
with:
31+
repository: wolfssl/wolfssl
32+
path: wolfssl
33+
34+
- name: Create FIPS stub files (autogen)
35+
working-directory: wolfssl
36+
run: |
37+
echo $null >> wolfcrypt\src\fips.c
38+
echo $null >> wolfcrypt\src\fips_test.c
39+
echo $null >> wolfcrypt\src\wolfcrypt_first.c
40+
echo $null >> wolfcrypt\src\wolfcrypt_last.c
41+
42+
- name: Add MSBuild to PATH
43+
uses: microsoft/setup-msbuild@v1
44+
45+
- name: Build
46+
working-directory: ${{env.GITHUB_WORKSPACE}}
47+
# Add additional options to the MSBuild command line here (like platform or verbosity level).
48+
# See https://docs.microsoft.com/visualstudio/msbuild/msbuild-command-line-reference
49+
run: msbuild /m /p:PlatformToolset=v142 /p:Platform=${{env.BUILD_PLATFORM}} /p:Configuration=${{env.BUILD_CONFIGURATION}} ${{env.SOLUTION_FILE_PATH}}
50+
51+
- name: Run wolfCrypt test
52+
working-directory: ${{env.GITHUB_WORKSPACE}}wolfssl\wrapper\CSharp\Debug\x64\
53+
run: ./wolfCrypt-test.exe
54+
55+
- name: Run wolfSSL client/server example
56+
working-directory: ${{env.GITHUB_WORKSPACE}}wolfssl\wrapper\CSharp\Debug\x64\
57+
run: ./wolfSSL-TLS-Server.exe && sleep 1 & ./wolfSSL-TLS-Client.exe

mcapi/crypto.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ enum {
173173
typedef struct CRYPT_AES_CTX {
174174
/* big enough to hold internal, but check on init */
175175
#ifdef WOLF_PRIVATE_KEY_ID
176-
int holder[110];
176+
int holder[114];
177177
#else
178-
int holder[92];
178+
int holder[96];
179179
#endif
180180
} CRYPT_AES_CTX;
181181

src/pk.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ int EncryptDerKey(byte *der, int *derSz, const EVP_CIPHER* cipher,
354354
int ret = 0;
355355
int paddingSz = 0;
356356
word32 idx;
357-
word32 cipherInfoSz;
357+
word32 cipherInfoSz = 0;
358358
#ifdef WOLFSSL_SMALL_STACK
359359
EncryptedInfo* info = NULL;
360360
#else
@@ -3300,7 +3300,7 @@ static int wolfssl_rsa_generate_key_native(WOLFSSL_RSA* rsa, int bits,
33003300
#endif
33013301
int initTmpRng = 0;
33023302
WC_RNG* rng = NULL;
3303-
long en;
3303+
long en = 0;
33043304
#endif
33053305

33063306
(void)cb;

src/ssl.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16138,11 +16138,14 @@ long wolfSSL_set_options(WOLFSSL* ssl, long op)
1613816138
else {
1613916139
/* Only preserve overlapping suites */
1614016140
Suites tmpSuites;
16141-
word16 in, out, haveECDSAsig = 0;
16142-
word16 haveStaticECC = ssl->options.haveStaticECC;
16141+
word16 in, out;
16142+
word16 haveECDSAsig, haveStaticECC;
1614316143
#ifdef NO_RSA
1614416144
haveECDSAsig = 1;
1614516145
haveStaticECC = 1;
16146+
#else
16147+
haveECDSAsig = 0;
16148+
haveStaticECC = ssl->options.haveStaticECC;
1614616149
#endif
1614716150
XMEMSET(&tmpSuites, 0, sizeof(Suites));
1614816151
/* Get all possible ciphers and sigalgs for the version. Following
@@ -21962,9 +21965,9 @@ int set_curves_list(WOLFSSL* ssl, WOLFSSL_CTX *ctx, const char* names,
2196221965
#endif /* HAVE_SUPPORTED_CURVES */
2196321966
}
2196421967

21965-
if (ssl)
21968+
if (ssl != NULL)
2196621969
ssl->disabledCurves = disabled;
21967-
else
21970+
else if (ctx != NULL)
2196821971
ctx->disabledCurves = disabled;
2196921972
ret = WOLFSSL_SUCCESS;
2197021973

tests/api.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27665,7 +27665,7 @@ static int test_wc_EccPrivateKeyToDer(void)
2766527665
byte output[ONEK_BUF];
2766627666
ecc_key eccKey;
2766727667
WC_RNG rng;
27668-
word32 inLen;
27668+
word32 inLen = 0;
2766927669
word32 outLen = 0;
2767027670
int ret;
2767127671

@@ -27681,12 +27681,13 @@ static int test_wc_EccPrivateKeyToDer(void)
2768127681
#endif
2768227682
ExpectIntEQ(ret, 0);
2768327683

27684-
inLen = (word32)sizeof(output);
2768527684
/* Bad Cases */
2768627685
ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, NULL, 0), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
2768727686
ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, output, inLen), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
27688-
ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, NULL, inLen), WC_NO_ERR_TRACE(LENGTH_ONLY_E));
27687+
inLen = wc_EccPrivateKeyToDer(&eccKey, NULL, 0);
27688+
ExpectIntGT(inLen, 0);
2768927689
ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, output, 0), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
27690+
2769027691
/* Good Case */
2769127692
ExpectIntGT(outLen = (word32)wc_EccPrivateKeyToDer(&eccKey, output, inLen), 0);
2769227693

@@ -52516,7 +52517,7 @@ static int test_wolfSSL_ASN1_INTEGER(void)
5251652517
ASN1_INTEGER_free(a);
5251752518
a = NULL;
5251852519

52519-
p = longDer;
52520+
p = invalidLenDer;
5252052521
ExpectNull(d2i_ASN1_INTEGER(NULL, &p, sizeof(invalidLenDer)));
5252152522

5252252523
p = longDer;

wolfcrypt/src/aes.c

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10539,6 +10539,7 @@ int wc_Gmac(const byte* key, word32 keySz, byte* iv, word32 ivSz,
1053910539
if (ret == 0)
1054010540
ret = wc_AesGcmEncrypt_ex(aes, NULL, NULL, 0, iv, ivSz,
1054110541
authTag, authTagSz, authIn, authInSz);
10542+
aes->isAllocated = 0;
1054210543
wc_AesFree(aes);
1054310544
}
1054410545
ForceZero(aes, sizeof *aes);
@@ -10580,6 +10581,8 @@ int wc_GmacVerify(const byte* key, word32 keySz,
1058010581
if (ret == 0)
1058110582
ret = wc_AesGcmDecrypt(aes, NULL, NULL, 0, iv, ivSz,
1058210583
authTag, authTagSz, authIn, authInSz);
10584+
10585+
aes->isAllocated = 0;
1058310586
wc_AesFree(aes);
1058410587
}
1058510588
ForceZero(aes, sizeof *aes);
@@ -11296,6 +11299,20 @@ int wc_AesCcmEncrypt_ex(Aes* aes, byte* out, const byte* in, word32 sz,
1129611299

1129711300
#endif /* HAVE_AESCCM */
1129811301

11302+
Aes* wc_AesNew(void* heap, int devId)
11303+
{
11304+
Aes* aes = (Aes*)XMALLOC(sizeof(Aes), heap, DYNAMIC_TYPE_AES);
11305+
if (aes != NULL) {
11306+
if (wc_AesInit(aes, heap, devId) != 0) {
11307+
XFREE(aes, heap, DYNAMIC_TYPE_AES);
11308+
aes = NULL;
11309+
}
11310+
else {
11311+
aes->isAllocated = 1;
11312+
}
11313+
}
11314+
return aes;
11315+
}
1129911316

1130011317
/* Initialize Aes for use with async hardware */
1130111318
int wc_AesInit(Aes* aes, void* heap, int devId)
@@ -11305,6 +11322,7 @@ int wc_AesInit(Aes* aes, void* heap, int devId)
1130511322
if (aes == NULL)
1130611323
return BAD_FUNC_ARG;
1130711324

11325+
aes->isAllocated = 0;
1130811326
aes->heap = heap;
1130911327
aes->rounds = 0;
1131011328

@@ -11430,11 +11448,18 @@ int wc_AesInit_Label(Aes* aes, const char* label, void* heap, int devId)
1143011448
/* Free Aes from use with async hardware */
1143111449
void wc_AesFree(Aes* aes)
1143211450
{
11433-
if (aes == NULL)
11451+
void* heap;
11452+
byte isAllocated;
11453+
11454+
if (aes == NULL) {
1143411455
return;
11456+
}
11457+
11458+
heap = aes->heap;
11459+
isAllocated = aes->isAllocated;
1143511460

1143611461
#ifdef WC_DEBUG_CIPHER_LIFECYCLE
11437-
(void)wc_debug_CipherLifecycleFree(&aes->CipherLifecycleTag, aes->heap, 1);
11462+
(void)wc_debug_CipherLifecycleFree(&aes->CipherLifecycleTag, heap, 1);
1143811463
#endif
1143911464

1144011465
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
@@ -11472,7 +11497,7 @@ void wc_AesFree(Aes* aes)
1147211497
#endif
1147311498
#if defined(WOLFSSL_AESGCM_STREAM) && defined(WOLFSSL_SMALL_STACK) && \
1147411499
!defined(WOLFSSL_AESNI)
11475-
XFREE(aes->streamData, aes->heap, DYNAMIC_TYPE_AES);
11500+
XFREE(aes->streamData, heap, DYNAMIC_TYPE_AES);
1147611501
aes->streamData = NULL;
1147711502
#endif
1147811503

@@ -11499,6 +11524,11 @@ void wc_AesFree(Aes* aes)
1149911524
#ifdef WOLFSSL_CHECK_MEM_ZERO
1150011525
wc_MemZero_Check(aes, sizeof(Aes));
1150111526
#endif
11527+
11528+
if (isAllocated) {
11529+
XFREE(aes, heap, DYNAMIC_TYPE_AES);
11530+
}
11531+
1150211532
}
1150311533

1150411534
int wc_AesGetKeySize(Aes* aes, word32* keySize)
@@ -14003,6 +14033,13 @@ static WARN_UNUSED_RESULT int AesSivCipher(
1400314033
}
1400414034
}
1400514035

14036+
#ifndef WOLFSSL_SMALL_STACK
14037+
/* make aes has heap hint and isAllocated initialized for cleanup below */
14038+
if (ret != 0) {
14039+
XMEMSET(aes, 0, sizeof(Aes));
14040+
}
14041+
#endif
14042+
1400614043
if (ret == 0 && dataSz > 0) {
1400714044
sivTmp[12] &= 0x7f;
1400814045
sivTmp[8] &= 0x7f;
@@ -14032,10 +14069,15 @@ static WARN_UNUSED_RESULT int AesSivCipher(
1403214069
}
1403314070
}
1403414071

14035-
wc_AesFree(aes);
1403614072
#ifdef WOLFSSL_SMALL_STACK
14037-
XFREE(aes, NULL, DYNAMIC_TYPE_AES);
14073+
if (aes != NULL)
1403814074
#endif
14075+
{
14076+
wc_AesFree(aes);
14077+
#ifdef WOLFSSL_SMALL_STACK
14078+
XFREE(aes, NULL, DYNAMIC_TYPE_AES);
14079+
#endif
14080+
}
1403914081

1404014082
return ret;
1404114083
}

wolfcrypt/src/asn.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22659,7 +22659,7 @@ static int DecodeCertReq(DecodedCert* cert, int* criticalExt)
2265922659
{
2266022660
DECL_ASNGETDATA(dataASN, certReqASN_Length);
2266122661
int ret = 0;
22662-
byte version;
22662+
byte version = 0;
2266322663
word32 idx;
2266422664

2266522665
CALLOC_ASNGETDATA(dataASN, certReqASN_Length, ret, cert->heap);
@@ -23552,7 +23552,7 @@ int wc_CertGetPubKey(const byte* cert, word32 certSz,
2355223552
const unsigned char** pubKey, word32* pubKeySz)
2355323553
{
2355423554
int ret = 0;
23555-
int l;
23555+
int l = 0;
2355623556
word32 o = 0;
2355723557
int i;
2355823558
static DecodeInstr ops[] = {
@@ -27841,7 +27841,7 @@ static int SetCertificatePolicies(byte *output,
2784127841
byte oid[MAX_OID_SZ];
2784227842
word32 oidSz;
2784327843
word32 sz = 0;
27844-
int piSz;
27844+
int piSz = 0;
2784527845

2784627846
if ((input == NULL) || (nb_certpol > MAX_CERTPOL_NB)) {
2784727847
ret = BAD_FUNC_ARG;
@@ -30399,8 +30399,8 @@ int AddSignature(byte* buf, int bodySz, const byte* sig, int sigSz,
3039930399
return (int)(idx + seqSz);
3040030400
#else
3040130401
DECL_ASNSETDATA(dataASN, sigASN_Length);
30402-
word32 seqSz;
30403-
int sz;
30402+
word32 seqSz = 0;
30403+
int sz = 0;
3040430404
int ret = 0;
3040530405

3040630406
CALLOC_ASNSETDATA(dataASN, sigASN_Length, ret, NULL);
@@ -35106,6 +35106,7 @@ int wc_BuildEccKeyDer(ecc_key* key, byte* output, word32 *inLen,
3510635106

3510735107
/* Write a Private ecc key, including public to DER format,
3510835108
* length on success else < 0 */
35109+
/* Note: use wc_EccKeyDerSize to get length only */
3510935110
WOLFSSL_ABI
3511035111
int wc_EccKeyToDer(ecc_key* key, byte* output, word32 inLen)
3511135112
{
@@ -35117,10 +35118,7 @@ int wc_EccKeyToDer(ecc_key* key, byte* output, word32 inLen)
3511735118
int wc_EccKeyDerSize(ecc_key* key, int pub)
3511835119
{
3511935120
word32 sz = 0;
35120-
int ret;
35121-
35122-
ret = wc_BuildEccKeyDer(key, NULL, &sz, pub, 1);
35123-
35121+
int ret = wc_BuildEccKeyDer(key, NULL, &sz, pub, 1);
3512435122
if (ret != WC_NO_ERR_TRACE(LENGTH_ONLY_E)) {
3512535123
return ret;
3512635124
}
@@ -35131,7 +35129,11 @@ int wc_EccKeyDerSize(ecc_key* key, int pub)
3513135129
* length on success else < 0 */
3513235130
int wc_EccPrivateKeyToDer(ecc_key* key, byte* output, word32 inLen)
3513335131
{
35134-
return wc_BuildEccKeyDer(key, output, &inLen, 0, 1);
35132+
int ret = wc_BuildEccKeyDer(key, output, &inLen, 0, 1);
35133+
if (ret == WC_NO_ERR_TRACE(LENGTH_ONLY_E)) {
35134+
return (int)inLen;
35135+
}
35136+
return ret;
3513535137
}
3513635138

3513735139
#ifdef HAVE_PKCS8
@@ -35673,7 +35675,7 @@ int SetAsymKeyDer(const byte* privKey, word32 privKeyLen,
3567335675
word32 idx = 0, seqSz, verSz, algoSz, privSz, pubSz = 0, sz;
3567435676
#else
3567535677
DECL_ASNSETDATA(dataASN, edKeyASN_Length);
35676-
int sz;
35678+
int sz = 0;
3567735679
#endif
3567835680

3567935681
/* validate parameters */

0 commit comments

Comments
 (0)