Skip to content

Commit

Permalink
Add support for ModelVersion 3 and resellers
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Svedin committed Nov 2, 2020
1 parent c6b9a59 commit c3b2208
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 9 deletions.
22 changes: 21 additions & 1 deletion include/cryptolens/cryptolens.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ typedef struct cryptolens cryptolens_t;
typedef struct cryptolens_DO cryptolens_DO_t;
typedef struct cryptolens_DOL_entry cryptolens_DOL_entry_t;
typedef struct cryptolens_LK cryptolens_LK_t;
#ifndef CRYPTOLENS_DISABLE_RESELLER
typedef struct cryptolens_RS cryptolens_RS_t;
#endif

#include "error.h"
#include "data_objects.h"
Expand Down Expand Up @@ -41,9 +44,26 @@ struct cryptolens_LK {
long long maxnoofmachines;
void * allowed_machines;
cryptolens_DOL_entry_t * data_objects;
#ifndef CRYPTOLENS_DISABLE_RESELLER
cryptolens_RS_t * reseller;
#endif
long long sign_date;
};

#ifndef CRYPTOLENS_DISABLE_RESELLER
struct cryptolens_RS {
int id;
int invite_id;
int reseller_user_id;
long long created;
char * name;
char * url;
char * email;
char * phone;
char * description;
};
#endif

#ifdef __cplusplus
extern "C" {
#endif
Expand All @@ -67,7 +87,7 @@ cryptolens_LK_t *
cryptolens_handle_activate_response(
cryptolens_error_t *,
cryptolens_signature_verifier_t *,
char const*
char const*
);

cryptolens_LK_t *
Expand Down
25 changes: 23 additions & 2 deletions src/cryptolens.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
#include "cryptolens/internal/decode_base64.h"
#include "cryptolens/cryptolens.h"

#ifndef CRYPTOLENS_ACTIVATE_MODEL_VERSION
#define CRYPTOLENS_ACTIVATE_MODEL_VERSION "3"
#endif

#ifndef CRYPTOLENS_ACTIVATE_FLOATING_MODEL_VERSION
#define CRYPTOLENS_ACTIVATE_FLOATING_MODEL_VERSION "3"
#endif

cryptolens_t *
cryptolens_init(
cryptolens_error_t * e
Expand Down Expand Up @@ -56,6 +64,17 @@ cryptolens_LK_destroy(
{
if (license_key == NULL) { return; }

#ifndef CRYPTOLENS_DISABLE_RESELLER
if (license_key->reseller) {
free(license_key->reseller->name);
free(license_key->reseller->url);
free(license_key->reseller->email);
free(license_key->reseller->phone);
free(license_key->reseller->description);
}
free(license_key->reseller);
#endif

cryptolens_DOL_destroy(license_key->data_objects);

free(license_key);
Expand Down Expand Up @@ -252,7 +271,7 @@ cryptolens_IN_deactivate(
cryptolens_RHP_add_argument(e, r, "v", "1");

response = cryptolens_RHP_perform(e, r);

cryptolens_RP_parse_deactivate_response(e, NULL, response);

goto end;
Expand Down Expand Up @@ -290,7 +309,7 @@ cryptolens_IN_deactivate_floating(
cryptolens_RHP_add_argument(e, r, "v", "1");

response = cryptolens_RHP_perform(e, r);

cryptolens_RP_parse_deactivate_floating_response(e, NULL, response);

goto end;
Expand Down Expand Up @@ -329,6 +348,7 @@ cryptolens_IN_activate(
cryptolens_RHP_add_argument(e, r, "Sign", "true");
cryptolens_RHP_add_argument(e, r, "MachineCode", machine_code);
// add_argument(e, r, "FieldsToReturn", "0");
cryptolens_RHP_add_argument(e, r, "ModelVersion", CRYPTOLENS_ACTIVATE_MODEL_VERSION);
cryptolens_RHP_add_argument(e, r, "SignMethod", "1");
cryptolens_RHP_add_argument(e, r, "v", "1");

Expand Down Expand Up @@ -375,6 +395,7 @@ cryptolens_IN_activate_floating(
cryptolens_RHP_add_argument(e, r, "Sign", "true");
cryptolens_RHP_add_argument(e, r, "MachineCode", machine_code);
// add_argument(e, r, "FieldsToReturn", "0");
cryptolens_RHP_add_argument(e, r, "ModelVersion", CRYPTOLENS_ACTIVATE_FLOATING_MODEL_VERSION);
cryptolens_RHP_add_argument(e, r, "SignMethod", "1");
cryptolens_RHP_add_argument(e, r, "FloatingTimeInterval", floating_interval);
cryptolens_RHP_add_argument(e, r, "v", "1");
Expand Down
4 changes: 2 additions & 2 deletions src/request_handler_curl.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ cryptolens_RH_new(cryptolens_error_t * e)
o = malloc(sizeof(cryptolens_RH_t));
if (!o) { cryptolens_set_error(e, CRYPTOLENS_ES_RH, CRYPTOLENS_ER_ALLOC_FAILED, 1); goto error; }

o->curl = curl_easy_init();
o->curl = curl_easy_init();
if (!o->curl) { cryptolens_set_error(e, CRYPTOLENS_ES_RH, 2, 0); goto error; }

goto end;

error:
if (o) {
curl_easy_cleanup(o->curl);
curl_easy_cleanup(o->curl);
}

free(o);
Expand Down
130 changes: 127 additions & 3 deletions src/response_parser_cJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ cryptolens_RP_parse_DO_list(
data_objects = cJSON_GetObjectItemCaseSensitive(json, "dataObjects");
if (data_objects == NULL || !cJSON_IsArray(data_objects)) { cryptolens_set_error(e, CRYPTOLENS_ES_RP, 14, 0); goto error; }

list = parse_DO_list(e, data_objects);
list = parse_DO_list(e, data_objects);

goto end;

Expand Down Expand Up @@ -491,6 +491,115 @@ cryptolens_RP_parse_DO_additive(
cJSON_Delete(json);
}

#ifndef CRYPTOLENS_DISABLE_RESELLER
static
cryptolens_RS_t *
parse_RS(
cryptolens_error_t * e,
cJSON * json
)
{
cryptolens_RS_t * reseller = NULL;
cJSON * field = NULL;
size_t m = 0;

if (cryptolens_check_error(e)) { goto end; }

if (!cJSON_IsObject(json)) { goto end; }

reseller = malloc(sizeof(cryptolens_RS_t));
if (!reseller) { cryptolens_set_error(e, 123, 3, 0); goto error; }

reseller->id = 0;
reseller->invite_id = 0;
reseller->reseller_user_id = 0;
reseller->created = 0;
reseller->name = NULL;
reseller->url = NULL;
reseller->email = NULL;
reseller->phone = NULL;
reseller->description = NULL;

field = cJSON_GetObjectItem(json, "Id");
if (field == NULL || !cJSON_IsNumber(field)) { cryptolens_set_error(e, 123, 4, 0); goto error; }
reseller->id = field->valueint;

field = cJSON_GetObjectItem(json, "InviteId");
if (field == NULL || !cJSON_IsNumber(field)) { cryptolens_set_error(e, 123, 4, 0); goto error; }
reseller->id = field->valueint;

field = cJSON_GetObjectItem(json, "ResellerUserId");
if (field == NULL || !cJSON_IsNumber(field)) { cryptolens_set_error(e, 123, 4, 0); goto error; }
reseller->id = field->valueint;

field = cJSON_GetObjectItem(json, "Created");
if (field == NULL || !cJSON_IsNumber(field)) { cryptolens_set_error(e, 123, 11, 0); goto error; }
reseller->created = field->valuedouble;

field = cJSON_GetObjectItem(json, "Name");
if (field == NULL || !cJSON_IsString(field)) { reseller->name = calloc(1, 1); }
else {
m = strlen(field->valuestring);
reseller->name = malloc(m+1);
if (!reseller->name) { cryptolens_set_error(e, 123, 6, 0); goto error; }
strlcpy(reseller->name, field->valuestring, m+1);
}

field = cJSON_GetObjectItem(json, "Url");
if (field == NULL || !cJSON_IsString(field)) { reseller->url = calloc(1, 1); }
else {
m = strlen(field->valuestring);
reseller->url = malloc(m+1);
if (!reseller->url) { cryptolens_set_error(e, 123, 8, 0); goto error; }
strlcpy(reseller->url, field->valuestring, m+1);
}

field = cJSON_GetObjectItem(json, "Email");
if (field == NULL || !cJSON_IsString(field)) { reseller->email = calloc(1, 1); }
else {
m = strlen(field->valuestring);
reseller->email = malloc(m+1);
if (!reseller->email) { cryptolens_set_error(e, 123, 8, 0); goto error; }
strlcpy(reseller->email, field->valuestring, m+1);
}

field = cJSON_GetObjectItem(json, "Phone");
if (field == NULL || !cJSON_IsString(field)) { reseller->phone = calloc(1, 1); }
else {
m = strlen(field->valuestring);
reseller->phone = malloc(m+1);
if (!reseller->phone) { cryptolens_set_error(e, 123, 8, 0); goto error; }
strlcpy(reseller->phone, field->valuestring, m+1);
}

field = cJSON_GetObjectItem(json, "Description");
if (field == NULL || !cJSON_IsString(field)) { reseller->description = calloc(1, 1); }
else {
m = strlen(field->valuestring);
reseller->description = malloc(m+1);
if (!reseller->description) { cryptolens_set_error(e, 123, 10, 0); goto error; }
strlcpy(reseller->description, field->valuestring, m+1);
}

goto end;

error:
if (reseller) {
free(reseller->name);
free(reseller->url);
free(reseller->email);
free(reseller->phone);
free(reseller->description);
}

free(reseller);
reseller = NULL;

end:
return reseller;
}
#endif

static
void
LK_set_feature(
Expand Down Expand Up @@ -533,6 +642,16 @@ cryptolens_RP_parse_license_key(
license_key = (cryptolens_LK_t *)malloc(sizeof(cryptolens_LK_t));
if (license_key == NULL) { cryptolens_set_error(e, CRYPTOLENS_ES_RP, 31, 0); goto error; }

#ifndef CRYPTOLENS_DISABLE_RESELLER
license_key->key = NULL;
license_key->notes = NULL;
license_key->customer = NULL;
license_key->activated_machines = NULL;
license_key->allowed_machines = NULL;
license_key->data_objects = NULL;
license_key->reseller = NULL;
#endif

field = cJSON_GetObjectItemCaseSensitive(json, "Expires");
if (field == NULL || !cJSON_IsNumber(field)) { cryptolens_set_error(e, CRYPTOLENS_ES_RP, 32, 0); goto error; }
license_key->expires = field->valuedouble;
Expand Down Expand Up @@ -574,11 +693,16 @@ cryptolens_RP_parse_license_key(
license_key->data_objects = parse_DO_list(e, field);
if(cryptolens_check_error(e)) { goto error; }

#ifndef CRYPTOLENS_DISABLE_RESELLER
field = cJSON_GetObjectItem(json, "Reseller");
license_key->reseller = parse_RS(e, field);
if(cryptolens_check_error(e)) { goto error; }
#endif

goto end;

error:
// TODO: When we add more fields to license_key we need to free those with allocs
free(license_key);
cryptolens_LK_destroy(license_key);
license_key = NULL;

end:
Expand Down
2 changes: 1 addition & 1 deletion src/signature_verifier_cryptoapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ cryptolens_SV_init(
cryptolens_signature_verifier_t* o = NULL;

if (cryptolens_check_error(e)) { goto error; }

o = (cryptolens_signature_verifier_t*)malloc(sizeof(cryptolens_signature_verifier_t));
if (o == NULL) { cryptolens_set_error(e, CRYPTOLENS_ES_SV, CRYPTOLENS_ER_ALLOC_FAILED, 1); goto error; }

Expand Down

0 comments on commit c3b2208

Please sign in to comment.