Commit fc9dfbe8 authored by filatov's avatar filatov
Browse files

Support for Macro substitution in cert generation

Add default ITS AID names
parent 6f2205ed
Loading
Loading
Loading
Loading
+7 −3
Original line number Original line Diff line number Diff line
@@ -255,7 +255,11 @@ int main(int argc, char ** argv)
		cxml_handler_add_default_entities(NULL);
		cxml_handler_add_default_entities(NULL);


		fprintf(stdout, "\t<signer type =\"%s\"", ENUMSTRING(*p, _signer_types));
		fprintf(stdout, "\t<signer type =\"%s\"", ENUMSTRING(*p, _signer_types));
		if (*p == si_digest || *p == si_digest_with_other_algorithm){
		if (*p == si_self){
			p++;
			fprintf(stdout, "/>\n");
		}
		else if (*p == si_digest || *p == si_digest_with_other_algorithm){
			if (*p == si_digest_with_other_algorithm){
			if (*p == si_digest_with_other_algorithm){
				p++;
				p++;
				fprintf(stdout, " algorithm =\"%s\"", ENUMSTRING(*p, _pk_algorithms));
				fprintf(stdout, " algorithm =\"%s\"", ENUMSTRING(*p, _pk_algorithms));
@@ -269,7 +273,7 @@ int main(int argc, char ** argv)
		}
		}
		else{
		else{
			fprintf(stdout, ">\n");
			fprintf(stdout, ">\n");
			if (*p == si_certificate_chain || *p >= si_other){
			if (*p == si_certificate_chain || *p == si_other){
				const char * b = ++p;
				const char * b = ++p;
				length = (uint32_t)cintx_read(&p, e, NULL);
				length = (uint32_t)cintx_read(&p, e, NULL);
				print_x(stdout, b, length);
				print_x(stdout, b, length);
+28 −0
Original line number Original line Diff line number Diff line
@@ -65,6 +65,34 @@ char * cstrcpy(char * dst, const char * src)
	return dst + len;
	return dst + len;
}
}


char * cstrchr(const char * str, int c)
{
	char * ret = (char*)0;
	if (str) {
		if (c == 0) ret = cstrend(str);
		else{
			ret = strchr(str, c);
			if (!ret) ret = cstrend(str);
		}
	}
	return ret;
}

char * cstrrchr(const char * str, int c)
{
	char * ret = (char*)0;
	if (str) {
		if (c == 0) ret = cstrend(str);
		else{
			ret = strrchr(str, c);
			if (!ret) ret = cstrend(str);
		}
	}
	return ret;
}

char * cstrrchr(const char * str, int c);

/* copy up to maxsize characters from src to dst and return pointer to the next byte after the end */ 
/* copy up to maxsize characters from src to dst and return pointer to the next byte after the end */ 
char * cstrncpy(char * dst, int maxsize, const char * src)
char * cstrncpy(char * dst, int maxsize, const char * src)
{
{
+3 −0
Original line number Original line Diff line number Diff line
@@ -39,6 +39,9 @@ char * cvstrncpy(char * dst, int maxsize, const char * ptr, ...);
int cstrequal(const char * s1, const char * s2);
int cstrequal(const char * s1, const char * s2);
char* cstrisprefix(const char * str, const char * prefix);
char* cstrisprefix(const char * str, const char * prefix);


char * cstrchr(const char * str, int c);
char * cstrrchr(const char * str, int c);

/* allocate copy of the str */ 
/* allocate copy of the str */ 
char * cstralloc(int size);
char * cstralloc(int size);
char * cstrdup(const char * str);
char * cstrdup(const char * str);
+93 −19
Original line number Original line Diff line number Diff line
@@ -15,6 +15,8 @@
#include "../cshared/copts.h"
#include "../cshared/copts.h"
#include "../cshared/cserialize.h"
#include "../cshared/cserialize.h"
#include "../cshared/cstr.h"
#include "../cshared/cstr.h"
#include "../cshared/cring.h"
#include "../cshared/cmem.h"


#include "ecc_api.h"
#include "ecc_api.h"
#include "mkgmtime.h"
#include "mkgmtime.h"
@@ -258,6 +260,7 @@ static const char * _verificationKey = NULL;
static const char * _decriptionKey   = NULL;
static const char * _decriptionKey   = NULL;
static const char * _keyPath = NULL;
static const char * _keyPath = NULL;
static int          _reuseKeys = 0;
static int          _reuseKeys = 0;
static int          _force = 0;


static long         _refLat = 0;
static long         _refLat = 0;
static long         _refLon = 0;
static long         _refLon = 0;
@@ -304,6 +307,63 @@ static int _refPoint_option(const copt_t * opt, const char * option, const copt_
	return -1;
	return -1;
}
}


typedef struct macro_item macro_item;
struct macro_item {
	cring_t _ring;
	const char* name;
	const char* value;
};
static macro_item _default_macro[] = {
	{ { NULL, NULL }, "CAM", "36" },
	{ { NULL, NULL }, "DENM", "37" },
	{ { NULL, NULL }, "GENERIC", "38" },
	{ { NULL, NULL }, "TLM", "137" },
	{ { NULL, NULL }, "SPAT", "137" },
	{ { NULL, NULL }, "RLT", "138" },
	{ { NULL, NULL }, "MAP", "138" },
	{ { NULL, NULL }, "MAP", "138" },
	{ { NULL, NULL }, "IVI", "139" },
	{ { NULL, NULL }, "TLC", "140" },
	{ { NULL, NULL }, "GN-MGMT", "141" }
};

static cring_t _macro = { &_macro, &_macro };

static int _addMacro_option(const copt_t * opt, const char * option, const copt_value_t * value)
{
	macro_item * m;
	const char * n = value->v_str;
	char * v = strchr(n, ':');
	if (v){
		*(v++) = 0;
		if (0 == *v) v = NULL;
	}
	for (m = cring_first_cast(_macro, macro_item);
		&m->_ring != &_macro && !cstrequal(m->name, n);
		m = cring_next_cast(m, macro_item));
	if (&m->_ring == &_macro){
		m = cnew(macro_item);
		cring_init(&m->_ring);
		cring_enqueue(&_macro, &m->_ring);
		m->name = n;
	}
	m->value = v;
	return 0;
}

static const char * macro_lookup(const char* n)
{
	macro_item * m;
	for (m = cring_first_cast(_macro, macro_item);
		&m->_ring != &_macro;
		m = cring_next_cast(m, macro_item)) {
		if (cstrequal(m->name, n)){
			return m->value;
		}
	}
	return NULL;
}

static const char * _o_formats[] = {
static const char * _o_formats[] = {
	"bin", "hex", "pem", NULL
	"bin", "hex", "pem", NULL
};
};
@@ -316,13 +376,14 @@ static copt_t options [] = {
	{ "S", "certs", COPT_STR, (void*)&_searchPath, "Certificates search path [Output path by default]" },
	{ "S", "certs", COPT_STR, (void*)&_searchPath, "Certificates search path [Output path by default]" },
	{ "K", "keys",  COPT_STR,  (void*)&_keyPath,    "Private key storage path [Output path by default]" },
	{ "K", "keys",  COPT_STR,  (void*)&_keyPath,    "Private key storage path [Output path by default]" },
	{ "R", "reuse", COPT_BOOL, (void*)&_reuseKeys, "Reuse existing key pair [regenerate by default]" },
	{ "R", "reuse", COPT_BOOL, (void*)&_reuseKeys, "Reuse existing key pair [regenerate by default]" },
	{ "f", "force", COPT_BOOL, (void*)&_force, "Force regenerate existing certificate and keys" },
	{ "n", "name", COPT_STR, (void*)&_certName, "Certificate name (take from profile by default)" },
	{ "n", "name", COPT_STR, (void*)&_certName, "Certificate name (take from profile by default)" },
	{ "v", "vkey", COPT_STR, (void*)&_verificationKey, "Verification public key (generate key pair by default)" },
	{ "v", "vkey", COPT_STR, (void*)&_verificationKey, "Verification public key (generate key pair by default)" },
	{ "e", "ekey", COPT_STR, (void*)&_decriptionKey, "Decription public key (generate key pair if neccessary)" },
	{ "e", "ekey", COPT_STR, (void*)&_decriptionKey, "Decription public key (generate key pair if neccessary)" },
	{ "s", "signer", COPT_STR, (void*)&_signerName, "Signer certificate name [take from profile by default]" },
	{ "s", "signer", COPT_STR, (void*)&_signerName, "Signer certificate name [take from profile by default]" },
	{ "t", "reftime",     COPT_STR | COPT_CALLBACK, (void*)&_time_option, "Reference UTC time point (YYY-DD-MM) [current date]" },
	{ "t", "reftime",     COPT_STR | COPT_CALLBACK, (void*)&_time_option, "Reference UTC time point (YYY-DD-MM) [current date]" },
	{ "l", "reflocation", COPT_STR | COPT_CALLBACK, (void*)&_refPoint_option, "Reference location in form <lat>:<lon> [0.0:0.0]" },
	{ "l", "reflocation", COPT_STR | COPT_CALLBACK, (void*)&_refPoint_option, "Reference location in form <lat>:<lon> [0.0:0.0]" },
	//	{ "c", "chain",   COPT_BOOL , &_createChain,     "Produce a text file for ITS Test Suite" },
	{ "m", "macro", COPT_STR | COPT_CALLBACK, (void*)&_addMacro_option, "Add macro in form of name:value to process AID, SSP, etc." },
	{ NULL, NULL,     COPT_END,  NULL, NULL }
	{ NULL, NULL,     COPT_END,  NULL, NULL }
};
};


@@ -338,6 +399,11 @@ int main(int argc, char ** argv)
	// set default time to the begining of this year
	// set default time to the begining of this year
	_setup_default_time();
	_setup_default_time();


	// init default macroses
	for (rc = 0; rc < sizeof(_default_macro) / sizeof(_default_macro[0]); rc++){
		cring_enqueue(&_macro, &_default_macro[rc]._ring);
	}

	argc = coptions(argc, argv, COPT_HELP_NOVALUES , options);
	argc = coptions(argc, argv, COPT_HELP_NOVALUES , options);


    if(argc < 2){
    if(argc < 2){
@@ -412,7 +478,7 @@ static int root_certificate_tag (cxml_handler_t* const _h, cxml_tag_t * const ta
		}
		}
		h->ptr = h->buf = malloc(4096);
		h->ptr = h->buf = malloc(4096);
		h->end = h->buf + 4096;
		h->end = h->buf + 4096;
		if (cxml_tag_attr_boolean(tag, "keep-existing")){
		if (!_force && cxml_tag_attr_boolean(tag, "keep-existing")){
			FILE * f;
			FILE * f;
			char * path = h->ptr;
			char * path = h->ptr;
			cvstrncpy(path, h->end - path, _outPath, "/", _certName, ".crt", NULL);
			cvstrncpy(path, h->end - path, _outPath, "/", _certName, ".crt", NULL);
@@ -990,14 +1056,18 @@ static int attribute_aid_tag (cxml_handler_t* const _h, cxml_tag_t * cons
			uint32_t n;
			uint32_t n;
			while(cisspace(*v))v++;
			while(cisspace(*v))v++;
			if (!cisdigit(*v)){
			if (!cisdigit(*v)){
				// look in the aid map
				const char * v1 = macro_lookup(v);
				if (!v1){
					fprintf(stderr, "ERROR: Invalid AID '%s'\n", v);
					fprintf(stderr, "ERROR: Invalid AID '%s'\n", v);
				rc = -1;
					return -1;
			}else{
				}
				v = v1;
			}
			n = strtoul(v, NULL, 0);
			n = strtoul(v, NULL, 0);
			cintx_write(n, &h->ptr, h->end, &rc);
			cintx_write(n, &h->ptr, h->end, &rc);
		}
		}
	}
	}
	}
	return rc;
	return rc;
}
}


@@ -1014,14 +1084,18 @@ static int attribute_ssp_tag (cxml_handler_t* const _h, cxml_tag_t * cons
		}
		}
		while(cisspace(*v))v++;
		while(cisspace(*v))v++;
		if (!cisdigit(*v)){
		if (!cisdigit(*v)){
			// look in the aid map
			const char * v1 = macro_lookup(v);
			if (!v1){
				fprintf(stderr, "ERROR: Invalid AID '%s' in SSP\n", v);
				fprintf(stderr, "ERROR: Invalid AID '%s' in SSP\n", v);
				return -1;
				return -1;
			}
			}
			v = v1;
		}
		n = strtoul(v, NULL, 0);
		n = strtoul(v, NULL, 0);
		cintx_write(n, &h->ptr, h->end, &rc);
		cintx_write(n, &h->ptr, h->end, &rc);
		bookmark_position(h, tag);
		bookmark_position(h, tag);
	}else{
	}else{
		// write ssp buffer data
		apply_bookmark_size(h, tag);
		apply_bookmark_size(h, tag);
	}
	}
	return rc;
	return rc;