Skip to content

Commit daa737d

Browse files
authored
[C][Client] Support SSL client authentication for the c client (#5719)
* [C][Client] Support SSL client authentication * [C][Client] Support SSL client authentication, update sample
1 parent 861fcce commit daa737d

File tree

4 files changed

+132
-32
lines changed

4 files changed

+132
-32
lines changed

modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ apiClient_t *apiClient_create() {
1313
curl_global_init(CURL_GLOBAL_ALL);
1414
apiClient_t *apiClient = malloc(sizeof(apiClient_t));
1515
apiClient->basePath = strdup("{{{basePath}}}");
16-
apiClient->caPath = NULL;
16+
apiClient->sslConfig = NULL;
1717
apiClient->dataReceived = NULL;
1818
apiClient->response_code = 0;
1919
{{#hasAuthMethods}}
@@ -35,7 +35,7 @@ apiClient_t *apiClient_create() {
3535
}
3636

3737
apiClient_t *apiClient_create_with_base_path(const char *basePath
38-
, const char *caPath
38+
, sslConfig_t *sslConfig
3939
{{#hasAuthMethods}}
4040
{{#authMethods}}
4141
{{#isApiKey}}
@@ -52,10 +52,10 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath
5252
apiClient->basePath = strdup("{{{basePath}}}");
5353
}
5454

55-
if(caPath){
56-
apiClient->caPath = strdup(caPath);
55+
if(sslConfig){
56+
apiClient->sslConfig = sslConfig;
5757
}else{
58-
apiClient->caPath = NULL;
58+
apiClient->sslConfig = NULL;
5959
}
6060

6161
apiClient->dataReceived = NULL;
@@ -92,9 +92,6 @@ void apiClient_free(apiClient_t *apiClient) {
9292
if(apiClient->basePath) {
9393
free(apiClient->basePath);
9494
}
95-
if(apiClient->caPath) {
96-
free(apiClient->caPath);
97-
}
9895
{{#hasAuthMethods}}
9996
{{#authMethods}}
10097
{{#isBasic}}
@@ -132,6 +129,33 @@ void apiClient_free(apiClient_t *apiClient) {
132129
curl_global_cleanup();
133130
}
134131

132+
sslConfig_t *sslConfig_create(const char *clientCertFile, const char *clientKeyFile, const char *CACertFile, int insecureSkipTlsVerify) {
133+
sslConfig_t *sslConfig = calloc(1, sizeof(sslConfig_t));
134+
if ( clientCertFile ) {
135+
sslConfig->clientCertFile = strdup(clientCertFile);
136+
}
137+
if ( clientKeyFile ) {
138+
sslConfig->clientKeyFile = strdup(clientKeyFile);
139+
}
140+
if ( CACertFile ) {
141+
sslConfig->CACertFile = strdup(CACertFile);
142+
}
143+
sslConfig->insecureSkipTlsVerify = insecureSkipTlsVerify;
144+
}
145+
146+
void sslConfig_free(sslConfig_t *sslConfig) {
147+
if ( sslConfig->clientCertFile ) {
148+
free(sslConfig->clientCertFile);
149+
}
150+
if ( sslConfig->clientKeyFile ) {
151+
free(sslConfig->clientKeyFile);
152+
}
153+
if ( sslConfig->CACertFile ){
154+
free(sslConfig->CACertFile);
155+
}
156+
free(sslConfig);
157+
}
158+
135159
void replaceSpaceWithPlus(char *stringToProcess) {
136160
for(int i = 0; i < strlen(stringToProcess); i++) {
137161
if(stringToProcess[i] == ' ') {
@@ -388,13 +412,27 @@ void apiClient_invoke(apiClient_t *apiClient,
388412
}
389413
}
390414

391-
if( strstr(apiClient->basePath, "https") != NULL ){
392-
if (apiClient->caPath) {
393-
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, true);
394-
curl_easy_setopt(handle, CURLOPT_CAINFO, apiClient->caPath);
415+
if ( strstr(apiClient->basePath, "https") != NULL ) {
416+
if ( apiClient->sslConfig ) {
417+
if( apiClient->sslConfig->clientCertFile ) {
418+
curl_easy_setopt(handle, CURLOPT_SSLCERT, apiClient->sslConfig->clientCertFile);
419+
}
420+
if( apiClient->sslConfig->clientKeyFile ) {
421+
curl_easy_setopt(handle, CURLOPT_SSLKEY, apiClient->sslConfig->clientKeyFile);
422+
}
423+
if( apiClient->sslConfig->CACertFile ) {
424+
curl_easy_setopt(handle, CURLOPT_CAINFO, apiClient->sslConfig->CACertFile);
425+
}
426+
if ( 1 == apiClient->sslConfig->insecureSkipTlsVerify ) {
427+
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0L);
428+
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0L);
429+
} else {
430+
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 1L);
431+
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 2L);
432+
}
395433
} else {
396-
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, false);
397-
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, false);
434+
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0L);
435+
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0L);
398436
}
399437
}
400438

modules/openapi-generator/src/main/resources/C-libcurl/apiClient.h.mustache

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,17 @@
99
#include "../include/list.h"
1010
#include "../include/keyValuePair.h"
1111

12+
typedef struct sslConfig_t {
13+
char *clientCertFile; /* client certificate */
14+
char *clientKeyFile; /* client private key */
15+
char *CACertFile; /* CA certificate */
16+
int insecureSkipTlsVerify ; /* 0 -- verify server certificate */
17+
/* 1 -- skip ssl verify for server certificate */
18+
} sslConfig_t;
19+
1220
typedef struct apiClient_t {
1321
char *basePath;
14-
char *caPath;
22+
sslConfig_t *sslConfig;
1523
void *dataReceived;
1624
long response_code;
1725
{{#hasAuthMethods}}
@@ -39,7 +47,7 @@ typedef struct binary_t
3947
apiClient_t* apiClient_create();
4048

4149
apiClient_t* apiClient_create_with_base_path(const char *basePath
42-
, const char *caPath
50+
, sslConfig_t *sslConfig
4351
{{#hasAuthMethods}}
4452
{{#authMethods}}
4553
{{#isApiKey}}
@@ -53,6 +61,10 @@ void apiClient_free(apiClient_t *apiClient);
5361

5462
void apiClient_invoke(apiClient_t *apiClient,char* operationParameter, list_t *queryParameters, list_t *headerParameters, list_t *formParameters,list_t *headerType,list_t *contentType, char *bodyParameters, char *requestType);
5563

64+
sslConfig_t *sslConfig_create(const char *clientCertFile, const char *clientKeyFile, const char *CACertFile, int insecureSkipTlsVerify);
65+
66+
void sslConfig_free(sslConfig_t *sslConfig);
67+
5668
char *strReplace(char *orig, char *rep, char *with);
5769

5870
char *base64encode(const void *b64_encode_this, int encode_this_many_bytes);

samples/client/petstore/c/include/apiClient.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,17 @@
99
#include "../include/list.h"
1010
#include "../include/keyValuePair.h"
1111

12+
typedef struct sslConfig_t {
13+
char *clientCertFile; /* client certificate */
14+
char *clientKeyFile; /* client private key */
15+
char *CACertFile; /* CA certificate */
16+
int insecureSkipTlsVerify ; /* 0 -- verify server certificate */
17+
/* 1 -- skip ssl verify for server certificate */
18+
} sslConfig_t;
19+
1220
typedef struct apiClient_t {
1321
char *basePath;
14-
char *caPath;
22+
sslConfig_t *sslConfig;
1523
void *dataReceived;
1624
long response_code;
1725
list_t *apiKeys;
@@ -27,14 +35,18 @@ typedef struct binary_t
2735
apiClient_t* apiClient_create();
2836

2937
apiClient_t* apiClient_create_with_base_path(const char *basePath
30-
, const char *caPath
38+
, sslConfig_t *sslConfig
3139
, list_t *apiKeys
3240
);
3341

3442
void apiClient_free(apiClient_t *apiClient);
3543

3644
void apiClient_invoke(apiClient_t *apiClient,char* operationParameter, list_t *queryParameters, list_t *headerParameters, list_t *formParameters,list_t *headerType,list_t *contentType, char *bodyParameters, char *requestType);
3745

46+
sslConfig_t *sslConfig_create(const char *clientCertFile, const char *clientKeyFile, const char *CACertFile, int insecureSkipTlsVerify);
47+
48+
void sslConfig_free(sslConfig_t *sslConfig);
49+
3850
char *strReplace(char *orig, char *rep, char *with);
3951

4052
char *base64encode(const void *b64_encode_this, int encode_this_many_bytes);

samples/client/petstore/c/src/apiClient.c

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ apiClient_t *apiClient_create() {
1313
curl_global_init(CURL_GLOBAL_ALL);
1414
apiClient_t *apiClient = malloc(sizeof(apiClient_t));
1515
apiClient->basePath = strdup("http://petstore.swagger.io/v2");
16-
apiClient->caPath = NULL;
16+
apiClient->sslConfig = NULL;
1717
apiClient->dataReceived = NULL;
1818
apiClient->response_code = 0;
1919
apiClient->apiKeys = NULL;
@@ -23,7 +23,7 @@ apiClient_t *apiClient_create() {
2323
}
2424

2525
apiClient_t *apiClient_create_with_base_path(const char *basePath
26-
, const char *caPath
26+
, sslConfig_t *sslConfig
2727
, list_t *apiKeys
2828
) {
2929
curl_global_init(CURL_GLOBAL_ALL);
@@ -34,10 +34,10 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath
3434
apiClient->basePath = strdup("http://petstore.swagger.io/v2");
3535
}
3636

37-
if(caPath){
38-
apiClient->caPath = strdup(caPath);
37+
if(sslConfig){
38+
apiClient->sslConfig = sslConfig;
3939
}else{
40-
apiClient->caPath = NULL;
40+
apiClient->sslConfig = NULL;
4141
}
4242

4343
apiClient->dataReceived = NULL;
@@ -62,9 +62,6 @@ void apiClient_free(apiClient_t *apiClient) {
6262
if(apiClient->basePath) {
6363
free(apiClient->basePath);
6464
}
65-
if(apiClient->caPath) {
66-
free(apiClient->caPath);
67-
}
6865
if(apiClient->apiKeys) {
6966
listEntry_t *listEntry = NULL;
7067
list_ForEach(listEntry, apiClient->apiKeys) {
@@ -86,6 +83,33 @@ void apiClient_free(apiClient_t *apiClient) {
8683
curl_global_cleanup();
8784
}
8885

86+
sslConfig_t *sslConfig_create(const char *clientCertFile, const char *clientKeyFile, const char *CACertFile, int insecureSkipTlsVerify) {
87+
sslConfig_t *sslConfig = calloc(1, sizeof(sslConfig_t));
88+
if ( clientCertFile ) {
89+
sslConfig->clientCertFile = strdup(clientCertFile);
90+
}
91+
if ( clientKeyFile ) {
92+
sslConfig->clientKeyFile = strdup(clientKeyFile);
93+
}
94+
if ( CACertFile ) {
95+
sslConfig->CACertFile = strdup(CACertFile);
96+
}
97+
sslConfig->insecureSkipTlsVerify = insecureSkipTlsVerify;
98+
}
99+
100+
void sslConfig_free(sslConfig_t *sslConfig) {
101+
if ( sslConfig->clientCertFile ) {
102+
free(sslConfig->clientCertFile);
103+
}
104+
if ( sslConfig->clientKeyFile ) {
105+
free(sslConfig->clientKeyFile);
106+
}
107+
if ( sslConfig->CACertFile ){
108+
free(sslConfig->CACertFile);
109+
}
110+
free(sslConfig);
111+
}
112+
89113
void replaceSpaceWithPlus(char *stringToProcess) {
90114
for(int i = 0; i < strlen(stringToProcess); i++) {
91115
if(stringToProcess[i] == ' ') {
@@ -342,13 +366,27 @@ void apiClient_invoke(apiClient_t *apiClient,
342366
}
343367
}
344368

345-
if( strstr(apiClient->basePath, "https") != NULL ){
346-
if (apiClient->caPath) {
347-
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, true);
348-
curl_easy_setopt(handle, CURLOPT_CAINFO, apiClient->caPath);
369+
if ( strstr(apiClient->basePath, "https") != NULL ) {
370+
if ( apiClient->sslConfig ) {
371+
if( apiClient->sslConfig->clientCertFile ) {
372+
curl_easy_setopt(handle, CURLOPT_SSLCERT, apiClient->sslConfig->clientCertFile);
373+
}
374+
if( apiClient->sslConfig->clientKeyFile ) {
375+
curl_easy_setopt(handle, CURLOPT_SSLKEY, apiClient->sslConfig->clientKeyFile);
376+
}
377+
if( apiClient->sslConfig->CACertFile ) {
378+
curl_easy_setopt(handle, CURLOPT_CAINFO, apiClient->sslConfig->CACertFile);
379+
}
380+
if ( 1 == apiClient->sslConfig->insecureSkipTlsVerify ) {
381+
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0L);
382+
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0L);
383+
} else {
384+
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 1L);
385+
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 2L);
386+
}
349387
} else {
350-
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, false);
351-
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, false);
388+
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0L);
389+
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0L);
352390
}
353391
}
354392

0 commit comments

Comments
 (0)