ThingWorx C SDK
twTls.h
Go to the documentation of this file.
1 /***************************************
2  * Copyright (C) 2015 ThingWorx Inc. *
3  ***************************************/
4 
10 #ifndef TW_TLS_H
11 #define TW_TLS_H
12 
13 #include "twOSPort.h"
14 #include "twErrors.h"
15 
16 #define TW_SUBJECT_CN 0
17 #define TW_SUBJECT_O 1
18 #define TW_SUBJECT_OU 2
19 #define TW_ISSUER_CN 3
20 #define TW_ISSUER_O 4
21 #define TW_ISSUER_OU 5
22 
23 #define TW_READ_TIMEOUT -333
24 
25 #include TW_TLS_INCLUDE
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
34 typedef struct twTlsClient {
36  TW_SSL_CTX * ctx;
37  TW_SSL * ssl;
38  /* void * session; */
39  uint32_t options;
40  char ** x509_data;
41  char * keypasswd;
42  char * read_buf;
43  char selfSignedOk;
44  char validateCert;
45  char isEncrypted;
46  char isEnabled;
48 } twTlsClient;
49 
65 int twTlsClient_Create(const char * host, int16_t port, uint32_t options, twTlsClient ** client);
66 
76 
94 int twTlsClient_ConnectSession(twTlsClient * t, void * sessionId, int sessionLength);
95 
106 int twTlsClient_Reconnect(twTlsClient* t, const char * host, int16_t port);
107 
117 
134 int twTlsClient_Read(twTlsClient * t, char * buf, int len, int timeout);
135 
152 int twTlsClient_Write(twTlsClient * t, char * buf, int len, int timeout);
153 
164 
174 
190 
202 
214 
226 
238 
248 int twTlsClient_UseCertificateFile(twTlsClient * t, const char *file, int type);
249 
259 int twTlsClient_UsePrivateKeyFile(twTlsClient * t, const char *file, int type);
260 
277 int twTlsClient_UseCertificateChainFile(twTlsClient * t, const char *file, int type);
278 
294 int twTlsClient_SetClientCaList(twTlsClient * t, char * caFile);
295 
305 
316 
331 int twTlsClient_SetX509Fields(twTlsClient * t, char * subject_cn, char * subject_o, char * subject_ou,
332  char * issuer_cn, char * issuer_o, char * issuer_ou);
333 
344 
355 
360 #ifndef TW_SHA1_CTX
361 #include "tomcrypt.h"
362 #define TW_SHA1_CTX hash_state
363 #define TW_SHA1_INIT(a) sha1_init(a)
364 #define TW_SHA1_UPDATE(a,b,c) sha1_process(a,b,c)
365 #define TW_SHA1_FINAL(a,b) sha1_done(b,a)
366 #endif
367 
375 void twSHA1_Init(TW_SHA1_CTX * ctx);
376 
386 void twSHA1_Update(TW_SHA1_CTX * ctx, const uint8_t * msg, int len);
387 
396 void twSHA1_Final(uint8_t *digest, TW_SHA1_CTX * ctx);
397 
402 #ifndef TW_MD5_CTX
403 #include "tomcrypt.h"
404 #define TW_MD5_CTX hash_state
405 #define TW_MD5_INIT(a) md5_init(a)
406 #define TW_MD5_UPDATE(a,b,c) md5_process(a,b,c)
407 #define TW_MD5_FINAL(a,b) md5_done(b,a)
408 #endif
409 
417 void twMD5_Init(TW_MD5_CTX *);
418 
428 void twMD5_Update(TW_MD5_CTX *, const uint8_t *msg, int len);
429 
438 void twMD5_Final(uint8_t *digest, TW_MD5_CTX *);
439 
440 #ifdef __cplusplus
441 }
442 #endif
443 
444 #endif
int twTlsClient_Connect(twTlsClient *t)
Connects a twTlsClient::connection (see twSocket_Connect()).
Definition: twTls.c:62
char * read_buf
Definition: twTls.h:42
void twTlsClient_DisableCertValidation(twTlsClient *t)
Sets the twTlsClient to disable certificate validation (see twSocket_DisableCertValidation()).
Definition: twTls.c:278
int twTlsClient_EnableFipsMode(twTlsClient *t)
Enables FIPS mode for a twTlsClient. Wrapper function for TW_ENABLE_FIPS_MODE().
Definition: twTls.c:319
int twTlsClient_UsePrivateKeyFile(twTlsClient *t, const char *file, int type)
Loads the first private key stored in file into twTlsClient::ctx.
Definition: twTls.c:300
ThingWorx C SDK error code definitions.
int twTlsServer_Accept(twTlsClient *t)
Waits for a twTlsClient::ssl to initiate a handshake with the server (see TW_SSL_ACCEPT()).
Definition: twTls.c:170
twSocket * connection
Definition: twTls.h:35
void * twTlsClient_GetSessionId(twTlsClient *t)
Gets the session ID of a twTlsClient.
Definition: twTls.c:268
#define TW_MUTEX
For Linux builds a TW_MUTEX is a pthread_mutex_t.
Definition: twLinux-openssl.h:81
twSocket base type definition.
Definition: twOSPort.h:175
char selfSignedOk
Definition: twTls.h:43
Wrappers for OS-specific functionality.
void twTlsClient_DisableEncryption(twTlsClient *t)
Sets the twTlsClient to disable encryption (see twSocket_DisableEncryption()).
Definition: twTls.c:283
int twTlsClient_Create(const char *host, int16_t port, uint32_t options, twTlsClient **client)
Creates a new twTlsClient structure with the specified settings.
Definition: twTls.c:12
char ** x509_data
Definition: twTls.h:40
int twTlsClient_IsEncrypted(twTlsClient *t)
Checks to see if a twTlsClient is encrypted by getting the value of twTlsClient::isEncrypted.
Definition: twTls.c:324
char validateCert
Definition: twTls.h:44
void twSHA1_Init(TW_SHA1_CTX *ctx)
Initializes an SHA1 context.
Definition: twTls.c:353
void twMD5_Update(TW_MD5_CTX *, const uint8_t *msg, int len)
Add a new entry to an MD5 hash.
Definition: twTls.c:372
char * keypasswd
Definition: twTls.h:41
int twTlsClient_Read(twTlsClient *t, char *buf, int len, int timeout)
Reads len bytes of data from a twTlsClient::connection into buf (see twSocket_Read()).
Definition: twTls.c:196
struct twTlsClient twTlsClient
TLS client structure definition.
void twTlsClient_SetDefaultPasswdCbUserdata(twTlsClient *t, void *u)
Sets the twTlsClient::keypasswd of a twTlsClient to u.
Definition: twTls.c:315
void twSHA1_Final(uint8_t *digest, TW_SHA1_CTX *ctx)
Finalize and get the calculated SHA1 digest.
Definition: twTls.c:361
void twMD5_Final(uint8_t *digest, TW_MD5_CTX *)
Finalize and get the calculated MD5 digest.
Definition: twTls.c:376
int twTlsClient_Close(twTlsClient *t)
Closes a twTlsClient::connection (see twSocket_Close()).
Definition: twTls.c:177
int twTlsServer_Create(twTlsClient *t)
Creates a new #TW_TLS server and associates it with a twTlsClient::ssl.
Definition: twTls.c:153
char isEncrypted
Definition: twTls.h:45
int twTlsClient_Reconnect(twTlsClient *t, const char *host, int16_t port)
Reconnects a twTlsClient::connection (see twSocket_Reconnect()).
Definition: twTls.c:124
void twTlsClient_SetSelfSignedOk(twTlsClient *t)
Sets the twTlsClient to accept self signed certificates (see twSocket_SetSelfSignedOk()).
Definition: twTls.c:273
int twTlsClient_Delete(twTlsClient *t)
Frees all memory associated with a twTlsClient and all of its owned substructures.
Definition: twTls.c:247
#define TW_SHA1_CTX
Definition: twTls.h:362
TW_SSL_CTX * ctx
Definition: twTls.h:36
void twMD5_Init(TW_MD5_CTX *)
Initializes an MD5 context.
Definition: twTls.c:368
uint32_t options
Definition: twTls.h:39
void twSHA1_Update(TW_SHA1_CTX *ctx, const uint8_t *msg, int len)
Add a new entry to an SHA1 hash.
Definition: twTls.c:357
TW_SSL * ssl
Definition: twTls.h:37
int twTlsClient_ValidateCert(twTlsClient *t)
Tells the twTlsClient to validate its twTlsClient::ssl certificate (see TW_VALIDATE_CERT()).
Definition: twTls.c:290
char isEnabled
Definition: twTls.h:46
#define TW_MD5_CTX
Definition: twTls.h:404
TLS client structure definition.
Definition: twTls.h:34
int twTlsClient_ConnectSession(twTlsClient *t, void *sessionId, int sessionLength)
Connects a twTlsClient::connection (see twSocket_Connect()) with some additional session options...
Definition: twTls.c:66
int twTlsClient_SetClientCaList(twTlsClient *t, char *caFile)
Loads a client certificate authority cert chain in file into twTlsClient::ctx.
Definition: twTls.c:310
int twTlsClient_UseCertificateFile(twTlsClient *t, const char *file, int type)
Loads the first certificate stored in file into twTlsClient::ctx.
Definition: twTls.c:295
int twTlsClient_UseCertificateChainFile(twTlsClient *t, const char *file, int type)
Loads the certificate authority cert chain used to validate the server's certificate in file into twT...
Definition: twTls.c:305
TW_MUTEX mtx
Definition: twTls.h:47
int twTlsClient_SetX509Fields(twTlsClient *t, char *subject_cn, char *subject_o, char *subject_ou, char *issuer_cn, char *issuer_o, char *issuer_ou)
Sets the X509 fields of a twTlsClient.
Definition: twTls.c:329
int twTlsClient_Write(twTlsClient *t, char *buf, int len, int timeout)
Writes len bytes of data from buf to a twTlsClient::connection (see twSocket_Write()).
Definition: twTls.c:230