Commit fc9dfbe8 authored by filatov's avatar filatov

Support for Macro substitution in cert generation

Add default ITS AID names
parent 6f2205ed
......@@ -255,7 +255,11 @@ int main(int argc, char ** argv)
cxml_handler_add_default_entities(NULL);
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){
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;
}
......
......@@ -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)
{
......
......@@ -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);
......
......@@ -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"
......@@ -143,7 +145,7 @@ static const cxml_taghandler_t h_attribute[] = {
{ "assurance", attribute_assurance_tag, NULL, NULL },
{ "aid", attribute_aid_tag, NULL, NULL },
{ "ssp", attribute_ssp_tag, attribute_ssp_text, NULL },
{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
};
......@@ -316,13 +376,14 @@ static copt_t options [] = {
{ "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]" },
{ "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 <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 }
};
......@@ -338,6 +399,11 @@ 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,15 +1055,19 @@ 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)){
if (!cisdigit(*v)){
// look in the aid map
const char * v1 = macro_lookup(v);
if (!v1){
fprintf(stderr, "ERROR: Invalid AID '%s'\n", v);
rc = -1;
}else{
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)){
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);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment