diff --git a/tools/itscertgen/checker/certinfo.c b/tools/itscertgen/checker/certinfo.c index ad1fb8de29fb97cb999da770c5c7fc2f20771e3c..e5ac2441ba72bfad9de1523b18822d7bf5f0bcf1 100644 --- a/tools/itscertgen/checker/certinfo.c +++ b/tools/itscertgen/checker/certinfo.c @@ -255,7 +255,11 @@ int main(int argc, char ** argv) cxml_handler_add_default_entities(NULL); fprintf(stdout, "\t\n"); + } + else if (*p == si_digest || *p == si_digest_with_other_algorithm){ if (*p == si_digest_with_other_algorithm){ p++; fprintf(stdout, " algorithm =\"%s\"", ENUMSTRING(*p, _pk_algorithms)); @@ -269,13 +273,13 @@ int main(int argc, char ** argv) } else{ fprintf(stdout, ">\n"); - if (*p == si_certificate_chain || *p >= si_other){ + if (*p == si_certificate_chain || *p == si_other){ const char * b = ++p; length = (uint32_t)cintx_read(&p, e, NULL); print_x(stdout, b, length); p += length; } - else{ + else { fprintf(stderr, "Unsupported signer type: %d\n", ENUMSTRING(*p, _signer_types)); return -1; } diff --git a/tools/itscertgen/cshared/cstr.c b/tools/itscertgen/cshared/cstr.c index bd8835f5171f69898b6026449025290c19efd63c..6ad050bf3ad04fe82515a39ae36e9e758fc1434d 100644 --- a/tools/itscertgen/cshared/cstr.c +++ b/tools/itscertgen/cshared/cstr.c @@ -65,6 +65,34 @@ char * cstrcpy(char * dst, const char * src) 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 */ char * cstrncpy(char * dst, int maxsize, const char * src) { diff --git a/tools/itscertgen/cshared/cstr.h b/tools/itscertgen/cshared/cstr.h index 430858ec04797919c161af4be1a510bb7b088f1d..d7688e4593952244b442ef4357b8bb9641b1ae05 100644 --- a/tools/itscertgen/cshared/cstr.h +++ b/tools/itscertgen/cshared/cstr.h @@ -39,6 +39,9 @@ char * cvstrncpy(char * dst, int maxsize, const char * ptr, ...); int cstrequal(const char * s1, const char * s2); 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 */ char * cstralloc(int size); char * cstrdup(const char * str); diff --git a/tools/itscertgen/generator/certgen.c b/tools/itscertgen/generator/certgen.c index 84d3214a348b5b05251eb10c9197607fe0a1004e..e3258c2cb1c6cd7c865c20b6cd1ac16e3674ef67 100644 --- a/tools/itscertgen/generator/certgen.c +++ b/tools/itscertgen/generator/certgen.c @@ -15,6 +15,8 @@ #include "../cshared/copts.h" #include "../cshared/cserialize.h" #include "../cshared/cstr.h" +#include "../cshared/cring.h" +#include "../cshared/cmem.h" #include "ecc_api.h" #include "mkgmtime.h" @@ -141,9 +143,9 @@ static const cxml_taghandler_t h_attribute[] = { { "ecc-point", attribute_eccpoint_tag, attribute_eccpoint_text, NULL }, { "ecc_point", attribute_eccpoint_tag, attribute_eccpoint_text, NULL }, { "assurance", attribute_assurance_tag, NULL, NULL }, - { "aid", attribute_aid_tag, NULL, NULL }, - { "ssp", attribute_ssp_tag, attribute_ssp_text, NULL }, - {NULL} + { "aid", attribute_aid_tag, NULL, NULL }, + { "ssp", attribute_ssp_tag, attribute_ssp_text, NULL }, + { NULL } }; static int subject_attribute_tag (cxml_handler_t* const h, cxml_tag_t * const tag); @@ -258,6 +260,7 @@ static const char * _verificationKey = NULL; static const char * _decriptionKey = NULL; static const char * _keyPath = NULL; static int _reuseKeys = 0; +static int _force = 0; static long _refLat = 0; static long _refLon = 0; @@ -304,6 +307,63 @@ static int _refPoint_option(const copt_t * opt, const char * option, const copt_ 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[] = { "bin", "hex", "pem", NULL }; @@ -315,14 +375,15 @@ static copt_t options [] = { { "O", "format", COPT_STRENUM, (void*)_o_formats, "Output format (bin|hex|pem)[binary 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]" }, - { "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)" }, { "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)" }, { "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]" }, { "l", "reflocation", COPT_STR | COPT_CALLBACK, (void*)&_refPoint_option, "Reference location in form : [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 } }; @@ -337,7 +398,12 @@ int main(int argc, char ** argv) // set default time to the begining of this year _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); 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->end = h->buf + 4096; - if (cxml_tag_attr_boolean(tag, "keep-existing")){ + if (!_force && cxml_tag_attr_boolean(tag, "keep-existing")){ FILE * f; char * path = h->ptr; cvstrncpy(path, h->end - path, _outPath, "/", _certName, ".crt", NULL); @@ -977,7 +1043,7 @@ static int attribute_assurance_tag (cxml_handler_t* const _h, cxml_tag_t * cons return rc; } -static int attribute_aid_tag (cxml_handler_t* const _h, cxml_tag_t * const tag) +static int attribute_aid_tag(cxml_handler_t* const _h, cxml_tag_t * const tag) { int rc = 0; cert_cxml_handler_t * h = (cert_cxml_handler_t *)_h; @@ -989,13 +1055,17 @@ static int attribute_aid_tag (cxml_handler_t* const _h, cxml_tag_t * cons }else{ uint32_t n; while(cisspace(*v))v++; - if(!cisdigit(*v)){ - fprintf(stderr, "ERROR: Invalid AID '%s'\n", v); - rc = -1; - }else{ - n = strtoul(v, NULL, 0); - cintx_write(n, &h->ptr, h->end, &rc); + if (!cisdigit(*v)){ + // look in the aid map + const char * v1 = macro_lookup(v); + if (!v1){ + fprintf(stderr, "ERROR: Invalid AID '%s'\n", v); + return -1; + } + v = v1; } + n = strtoul(v, NULL, 0); + cintx_write(n, &h->ptr, h->end, &rc); } } return rc; @@ -1013,15 +1083,19 @@ static int attribute_ssp_tag (cxml_handler_t* const _h, cxml_tag_t * cons return -1; } while(cisspace(*v))v++; - if(!cisdigit(*v)){ - fprintf(stderr, "ERROR: Invalid AID '%s' in SSP\n", v); - return -1; + 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); + return -1; + } + v = v1; } n = strtoul(v, NULL, 0); cintx_write(n, &h->ptr, h->end, &rc); bookmark_position(h, tag); }else{ - // write ssp buffer data apply_bookmark_size(h, tag); } return rc; @@ -1030,7 +1104,7 @@ static int attribute_ssp_tag (cxml_handler_t* const _h, cxml_tag_t * cons static int attribute_ssp_text(cxml_handler_t* const _h, char * const text, int length) { int rc=0; - if(text && length){ + if (text && length){ cert_cxml_handler_t * h = (cert_cxml_handler_t *)_h; rc = cbuf_write(text, length, &h->ptr, h->end, NULL); }