ThingWorx C SDK
twTls.h
Go to the documentation of this file.
1 /***************************************
2  * Copyright 2017, PTC, 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 {
37  TW_SSL * ssl;
38  /* void * session; */
39  uint32_t options;
40  char ** x509_data;
41  twPasswdCallbackFunction keypasswdCallback;
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, char * caPath);
295 
304 void twTlsClient_SetDefaultPasswdCb(twTlsClient * t, twPasswdCallbackFunction u);
305 
320 int twTlsClient_SetX509Fields(twTlsClient * t, char * subject_cn, char * subject_o, char * subject_ou,
321  char * issuer_cn, char * issuer_o, char * issuer_ou);
322 
333 
344 
349 #ifndef TW_SHA1_CTX
350 #include "tomcrypt.h"
351 #define TW_SHA1_CTX hash_state
352 #define TW_SHA1_INIT(a) sha1_init(a)
353 #define TW_SHA1_UPDATE(a,b,c) sha1_process(a,b,c)
354 #define TW_SHA1_FINAL(a,b) sha1_done(b,a)
355 #endif
356 
364 void twSHA1_Init(TW_SHA1_CTX * ctx);
365 
375 void twSHA1_Update(TW_SHA1_CTX * ctx, const uint8_t * msg, int len);
376 
385 void twSHA1_Final(uint8_t *digest, TW_SHA1_CTX * ctx);
386 
391 #ifndef TW_MD5_CTX
392 #include "tomcrypt.h"
393 #define TW_MD5_CTX hash_state
394 #define TW_MD5_INIT(a) md5_init(a)
395 #define TW_MD5_UPDATE(a,b,c) md5_process(a,b,c)
396 #define TW_MD5_FINAL(a,b) md5_done(b,a)
397 #endif
398 
406 void twMD5_Init(TW_MD5_CTX *);
407 
417 void twMD5_Update(TW_MD5_CTX *, const uint8_t *msg, int len);
418 
427 void twMD5_Final(uint8_t *digest, TW_MD5_CTX *);
428 
429 #ifdef __cplusplus
430 }
431 #endif
432 
433 #endif
int twTlsClient_Connect(twTlsClient *t)
Connects a twTlsClient::connection (see twSocket_Connect()).
Definition: twTls.c:75
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:291
int twTlsClient_UsePrivateKeyFile(twTlsClient *t, const char *file, int type)
Loads the first private key stored in file into twTlsClient::ctx.
Definition: twTls.c:313
void twTlsClient_SetDefaultPasswdCb(twTlsClient *t, twPasswdCallbackFunction u)
Sets the twTlsClient::keypasswdCallback of a twTlsClient to u.
Definition: twTls.c:333
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:184
twSocket * connection
Definition: twTls.h:35
void * twTlsClient_GetSessionId(twTlsClient *t)
Gets the session ID of a twTlsClient.
Definition: twTls.c:281
#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:174
char selfSignedOk
Definition: twTls.h:43
Wrappers for OS-specific functionality.
#define TW_SSL
The base SSL structure for your SSL library.
Definition: twTemplateSSL.h:21
void twTlsClient_DisableEncryption(twTlsClient *t)
Sets the twTlsClient to disable encryption (see twSocket_DisableEncryption()).
Definition: twTls.c:296
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:14
char ** x509_data
Definition: twTls.h:40
#define TW_SSL_CTX
The SSL context structure for your SSL library.
Definition: twTemplateSSL.h:38
int twTlsClient_IsEncrypted(twTlsClient *t)
Checks to see if a twTlsClient is encrypted by getting the value of twTlsClient::isEncrypted.
Definition: twTls.c:339
char validateCert
Definition: twTls.h:44
void twSHA1_Init(TW_SHA1_CTX *ctx)
Initializes an SHA1 context.
Definition: twTls.c:377
void twMD5_Update(TW_MD5_CTX *, const uint8_t *msg, int len)
Add a new entry to an MD5 hash.
Definition: twTls.c:396
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:210
struct twTlsClient twTlsClient
TLS client structure definition.
void twSHA1_Final(uint8_t *digest, TW_SHA1_CTX *ctx)
Finalize and get the calculated SHA1 digest.
Definition: twTls.c:385
twPasswdCallbackFunction keypasswdCallback
Definition: twTls.h:41
void twMD5_Final(uint8_t *digest, TW_MD5_CTX *)
Finalize and get the calculated MD5 digest.
Definition: twTls.c:400
int twTlsClient_Close(twTlsClient *t)
Closes a twTlsClient::connection (see twSocket_Close()).
Definition: twTls.c:191
int twTlsServer_Create(twTlsClient *t)
Creates a new #TW_TLS server and associates it with a twTlsClient::ssl.
Definition: twTls.c:166
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:137
void twTlsClient_SetSelfSignedOk(twTlsClient *t)
Sets the twTlsClient to accept self signed certificates (see twSocket_SetSelfSignedOk()).
Definition: twTls.c:286
int twTlsClient_Delete(twTlsClient *t)
Frees all memory associated with a twTlsClient and all of its owned substructures.
Definition: twTls.c:261
#define TW_SHA1_CTX
Definition: twTls.h:351
TW_SSL_CTX * ctx
Definition: twTls.h:36
int twTlsClient_SetClientCaList(twTlsClient *t, char *caFile, char *caPath)
Loads a client certificate authority cert chain in file into twTlsClient::ctx.
Definition: twTls.c:328
void twMD5_Init(TW_MD5_CTX *)
Initializes an MD5 context.
Definition: twTls.c:392
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:381
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:303
char isEnabled
Definition: twTls.h:46
#define TW_MD5_CTX
Definition: twTls.h:393
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:79
int twTlsClient_UseCertificateFile(twTlsClient *t, const char *file, int type)
Loads the first certificate stored in file into twTlsClient::ctx.
Definition: twTls.c:308
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:323
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:344
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:244
Definition: gzappend.c:170