Newer
Older
if(cisdigit(*e)){
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
p = e;
continue;
}
break;
}
if(*v == '-'){
ret = _defaultTime - ret;
}else{
ret = _defaultTime + ret;
}
}
else{
// next try load as integer seconds since epoch
ret = strtoul(v, &e, 0);
if (ret == ULONG_MAX || *e){
ret = 0;
//next try to convert ISO text representation
memset(&tm, 0, sizeof(tm));
if (3 == sscanf(v, "%d-%d-%d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday)){
tm.tm_mon--; // STARTED FROM 0
if (tm.tm_year > 500){
tm.tm_year -= 1900;
}
ret = mkitstime32(&tm);
if (ret == (time_t)-1) {
fprintf(stderr, "%s: Date format specification error. Use YYY-MM-DD\n", v);
ret = 0;
}
}
}
}
return ret;
}
static int validity_restriction_tag (cxml_handler_t* const _h, cxml_tag_t * const tag)
{
int rc = 0;
cert_cxml_handler_t * h = (cert_cxml_handler_t *)_h;
if (cxml_tag_is_open(tag)){
int vr_type = -1;
int nTime;
const char * v = cxml_tag_attr_value(tag, "type");
const char *start, *end, *duration;
if(NULL == v){
fprintf(stderr, "ERROR: Restriction shall have a type.\n");
return -1;
}
while(cisspace(*v))v++;
if(0 == strcmp("time", v)){
start = cxml_tag_attr_value(tag, "start");
end = cxml_tag_attr_value(tag, "end");
duration = cxml_tag_attr_value(tag, "duration");
if(end && *end){
vr_type = (start && *start) ? 1 : 0;
}else if(start && *start && duration && *duration){
vr_type = 2;
}else{
fprintf(stderr, "ERROR: Either end or start and duration shall be specified for time restriction.\n");
return -1;
}
}else if(0 == strcmp("region", v)){
vr_type = 3;
}else if(cisdigit(*v)){
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
vr_type=strtoul(v, NULL,0);
}else{
fprintf(stderr, "%s: Unknown validity restriction type.\n", v);
return -1;
}
h->vr_type = vr_type;
cint8_write(vr_type, &h->ptr, h->end, &rc);
// save time restrictions
switch(vr_type){
case 1: /* time_start_and_end */
nTime = _convert_time(start);
cint32_write(nTime, &h->ptr, h->end, &rc);
case 0: /* time_end */
nTime = _convert_time(end);
cint32_write(nTime, &h->ptr, h->end, &rc);
break;
case 2: /* time_start_and_duration */
nTime = _convert_time(start);
cint32_write(nTime, &h->ptr, h->end, &rc);
nTime = _convert_time(duration);
cint32_write(nTime, &h->ptr, h->end, &rc);
break;
case 3: /* region */
break;
default: // opaque
bookmark_position(h, tag); // for opaque data
}
}else{
if(h->vr_type > 3){
apply_bookmark_size(h, tag);
}
h->vr_type = -1;
}
return 0;
}
static int region_none_tag (cxml_handler_t* const _h, cxml_tag_t * const tag)
{
if (cxml_tag_is_open(tag)){
cert_cxml_handler_t * h = (cert_cxml_handler_t *)_h;
return cint8_write(0, &h->ptr, h->end, NULL);
}
return 0;
}
static int region_circle_tag (cxml_handler_t* const _h, cxml_tag_t * const tag)
{
int rc = 0;
cert_cxml_handler_t * h = (cert_cxml_handler_t *)_h;
if (cxml_tag_is_open(tag)){
//region type
if(0 == cint8_write(1, &h->ptr, h->end, NULL)){
//latitude and longitude
rc = location_tag (_h, tag);
if(0 == rc){
//radius
uint32_t n;
const char * v = cxml_tag_attr_value(tag, "radius");
if(NULL == v){
fprintf(stderr, "ERROR: radius shall be specified for circle.\n");
return -1;
}
n = strtoul(v, &e, 0);
if( (e[0] == 'k' || e[0] == 'K') && (e[1] == 'm' || e[1] == 'M') )
n *= 1000;
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
if(n > 0xFFFF){
fprintf(stderr, "ERROR: %ul: radius is too big.\n", n);
return -1;
}
cint16_write(n, &h->ptr, h->end, &rc);
}
}
}
return rc;
}
static int region_rectangle_tag (cxml_handler_t* const _h, cxml_tag_t * const tag)
{
int rc;
cert_cxml_handler_t * h = (cert_cxml_handler_t *)_h;
if (cxml_tag_is_open(tag)){
// region type
rc = cint8_write(2, &h->ptr, h->end, NULL);
if(0 == rc){
bookmark_position(h, tag);
}
}else{
rc = apply_bookmark_size(h, tag);
}
return rc;
}
static int region_polygon_tag (cxml_handler_t* const _h, cxml_tag_t * const tag)
{
int rc;
cert_cxml_handler_t * h = (cert_cxml_handler_t *)_h;
if (cxml_tag_is_open(tag)){
// region type
rc = cint8_write(3, &h->ptr, h->end, NULL);
if(0 == rc){
bookmark_position(h, tag);
}
}else{
rc = apply_bookmark_size(h, tag);
}
return rc;
}
static int location_tag (cxml_handler_t* const _h, cxml_tag_t * const tag)
{
int rc = 0;
if (cxml_tag_is_open(tag)){
int32_t lat, lon;
long double d;
cert_cxml_handler_t * h = (cert_cxml_handler_t *)_h;
const char * v = cxml_tag_attr_value(tag, "latitude");
if(v == NULL){
fprintf(stderr, "ERROR: Latitude shall be specified for location.\n");
return -1;
}
e = NULL; d = strtold(v, &e);
if(e[0] == 'm' || (e[0] == 'k' && e[1] == 'm')){
if(e[0] == 'k') d*=1000.0;
d = d * _latTMDPerMetter;
}else{
if (d <= 90.0 && d >= -90.0) d *= 10000000.0; // degree
}
lat = (int32_t)floorl(d+_refLat);
v = cxml_tag_attr_value(tag, "longitude");
if(v == NULL){
fprintf(stderr, "ERROR: Longitude shall be specified for location.\n");
return -1;
}
e = NULL; d = strtold(v, &e);
if(e[0] == 'm' || (e[0] == 'k' && e[1] == 'm')){
if(e[0] == 'k') d*=1000.0;
// convert metters to degree
d = d * _lonTMDPerMetter;
}else{
if (d <= 180.0 && d >= -180.0) d *= 10000000.0; // degree
}
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
lon = (int32_t)floorl(d + _refLon);
cint32_write(lat, &h->ptr, h->end, &rc);
cint32_write(lon, &h->ptr, h->end, &rc);
}
return rc;
}
static const char * _id_dictionaries[] = {
"iso_3166_1",
"un_stats",
};
static int region_id_tag (cxml_handler_t* const _h, cxml_tag_t * const tag)
{
int rc = 0;
cert_cxml_handler_t * h = (cert_cxml_handler_t *)_h;
if (cxml_tag_is_open(tag)){
int value = 0;
unsigned int uvalue = 0;
const char * v;
// region type
rc = cint8_write(4, &h->ptr, h->end, NULL);
// region dictionary. use 0 by default
v = cxml_tag_attr_value(tag, "dictionary");
if(v == NULL)v = cxml_tag_attr_value(tag, "dict");
if(v){
value = STR2ENUM(_id_dictionaries, v);
if(value<0){
fprintf(stderr, "%s: Unknown dictionary type\n", v);
return -1;
}
}
if(cint8_write(value, &h->ptr, h->end, NULL)){
return -1;
}
v = cxml_tag_attr_value(tag, "id");
if(v == NULL){
fprintf(stderr, "ERROR: Region identifier must be set\n");
return -1;
}
while(cisspace(*v))v++;
uvalue = strtoul(v, NULL, 0);
if(uvalue > 0xFFFF){
fprintf(stderr, "%s: Invalid region identifier\n", v);
return -1;
}
if(cint16_write(uvalue, &h->ptr, h->end, NULL)){
return -1;
}
uvalue = 0;
v = cxml_tag_attr_value(tag, "local");
if(v){
while(cisspace(*v))v++;
uvalue = strtoul(v, NULL, 0);
if(!cisdigit(*v) || uvalue > 0xFFFF){
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
fprintf(stderr, "%s: Invalid region identifier\n", v);
return -1;
}
}
cintx_write(uvalue, &h->ptr, h->end, &rc);
if(rc) return -1;
}
return rc;
}
static const char * _signature_algorithms[] = {
"ecdsa_nistp256_with_sha256",
};
static int certificate_signature_tag (cxml_handler_t* const _h, cxml_tag_t * const tag)
{
int rc = 0;
cert_cxml_handler_t * h = (cert_cxml_handler_t *)_h;
if (cxml_tag_is_open(tag)){
void * key;
int alg = 0;
if(h->signer_type == 0){
// self signed certificate
key = h->verificationKey;
if(!key){
fprintf(stderr, "ERROR: Verification key attribute was not provided for self-signed certificate\n");
return -1;
}
}else{
const char * v = cxml_tag_attr_value(tag, "algorithm");
if (v){
alg = STR2ENUM(_signature_algorithms, v);
if (alg < 0){
fprintf(stderr, "%s: Unknown signature algorithm\n", v);
return -1;
}
}
if (h->signer == NULL){
fprintf(stderr, "ERROR: Signer certificate name shall be provided\n");
return -1;
}
// load signer certificate
int plen = strlen(_searchPath) + strlen(h->signer);
char * path = malloc(plen + 16);
cvstrncpy(path, plen + 16, _searchPath, "/", h->signer, ".vkey", NULL);
key = ecc_api_key_private_load(path, alg);
if (key == NULL){
fprintf(stderr, "%s: Could not load issuing private key\n", path);
free(path);
return -1;
}
}
cint8_write(alg, &h->ptr, h->end, &rc);
rc = ecc_sign(key, h->buf, h->ptr - h->buf - 1, &h->ptr, h->end - h->ptr);
}
return rc;
}
static int _Begin_Tag(cxml_handler_t* const h, cxml_tag_t * const tag)
{
fprintf(stderr, "WARNING: %s: Unknown tag", tag->name);
return 0;
}
static int _End_Tag(cxml_handler_t* const h, cxml_tag_t * const tag)
{
return 0;
}
static int _Text(cxml_handler_t* const h, char * const text, int length)
{return 0;}