Make it compile and run on windows
diff --git a/dumper/dumper.c b/dumper/dumper.c
index ce00eb0..926cc59 100644
--- a/dumper/dumper.c
+++ b/dumper/dumper.c
@@ -84,7 +84,7 @@
NULL, CN_CBOR_ARRAY, 0, &Key, 1, 0
};
-FOO HeaderMap[26] = {
+FOO HeaderMap[27] = {
{ "alg", CN_CBOR_UINT, 1, AlgorithmMap, _countof(AlgorithmMap), 0 },
{ "crit", CN_CBOR_UINT, 2, NULL, 0, 0 },
{ "content type", CN_CBOR_UINT, 3, NULL, 0, 0},
@@ -92,6 +92,7 @@
{ "iv", CN_CBOR_UINT, 5, NULL, 0, 0 },
{ "partial iv", CN_CBOR_UINT, 6, NULL, 0, 0 },
{ "countersign", CN_CBOR_UINT, 7, Signer, 5, 0 },
+ { "op time", CN_CBOR_INT, 8, NULL, 0, 0 },
{ "ephemeral", CN_CBOR_INT, -1, KeyMap, _countof(KeyMap), 50},
{"salt", CN_CBOR_INT, -20, NULL, 0, 50 },
{ "U identity", CN_CBOR_INT, -21, NULL, 0, 50 },
@@ -149,38 +150,53 @@
FOO SignBody[5] = {
{ "protected", CN_CBOR_BYTES, 0, &RecurseHeaderMap, 1, 0 },
{ "unprotected", CN_CBOR_MAP, 0, HeaderMap, _countof(HeaderMap), 0 },
- { "payload", CN_CBOR_BYTES, 0, NULL },
- { "signatures", CN_CBOR_ARRAY, 0, &Signers, 1 }
+ { "payload", CN_CBOR_BYTES, 0, NULL, 0, 0 },
+ { "signatures", CN_CBOR_ARRAY, 0, &Signers, 1, 0 }
};
FOO EnvelopedMessage = {
- NULL, CN_CBOR_ARRAY, 0, EncryptedBody, _countof(EncryptedBody)
+ NULL, CN_CBOR_ARRAY, 0, EncryptedBody, _countof(EncryptedBody), 0
};
FOO SignedMessage = {
- NULL, CN_CBOR_ARRAY, 0, SignBody, _countof(SignBody)
+ NULL, CN_CBOR_ARRAY, 0, SignBody, _countof(SignBody), 0
};
FOO MacMessage = {
- NULL, CN_CBOR_ARRAY, 0, MacBody, _countof(MacBody)
+ NULL, CN_CBOR_ARRAY, 0, MacBody, _countof(MacBody), 0
};
FOO EncryptedMessage = {
- NULL, CN_CBOR_ARRAY, 0, EncryptedBody, _countof(EncryptedBody)-1
+ NULL, CN_CBOR_ARRAY, 0, EncryptedBody, _countof(EncryptedBody)-1, 0
};
FOO EncryptedMessageWithTag = {
- NULL, CN_CBOR_TAG, 997, &EncryptedMessage, 1
+ NULL, CN_CBOR_TAG, 997, &EncryptedMessage, 1, 0
};
FOO EnvelopedMessageWithTag = {
- NULL, CN_CBOR_TAG, 998, &EnvelopedMessage, 1
+ NULL, CN_CBOR_TAG, 998, &EnvelopedMessage, 1, 0
};
FOO SignedMessageWithTag = {
- NULL, CN_CBOR_TAG, 999, &SignedMessage, 1
+ NULL, CN_CBOR_TAG, 999, &SignedMessage, 1, 0
};
FOO MacMessageWithTag = {
- NULL, CN_CBOR_TAG, 996, &MacMessage, 1
+ NULL, CN_CBOR_TAG, 996, &MacMessage, 1, 0
+};
+
+FOO Mac0Body[4] = {
+ { "protected", CN_CBOR_BYTES, 0, &RecurseHeaderMap, 1, 0 },
+ { "unprotected", CN_CBOR_MAP, 0, HeaderMap, _countof(HeaderMap), 0 },
+ { "payload", CN_CBOR_BYTES, 0, NULL, 0, 0 },
+ { "tag", CN_CBOR_BYTES, 0, NULL, 0, 0 }
+};
+
+FOO Mac0Message = {
+ NULL, CN_CBOR_ARRAY, 0, Mac0Body, _countof(Mac0Body), 0
+};
+
+FOO Mac0MessageWithTag = {
+ NULL, CN_CBOR_TAG, 995, &Mac0Message, 1, 0
};
int WrapLineAt = 0;
@@ -195,26 +211,26 @@
va_start(args, format);
vsprintf(buffer, format, args);
if (WrapLineAt == 0) {
- fprintf(fp, buffer);
+ fprintf(fp, "%s", buffer);
return;
}
strcat_s(OutputBuffer, sizeof(OutputBuffer), buffer);
- while (iRet = strchr(OutputBuffer, '\n')) {
+ while ((iRet = strchr(OutputBuffer, '\n'))) {
char * t = OutputBuffer;
*iRet = 0;
while (strlen(t) > WrapLineAt) {
char x = t[WrapLineAt];
t[WrapLineAt] = 0;
- fprintf(fp, t);
+ fprintf(fp, "%s", t);
fprintf(fp, "\n");
t[WrapLineAt] = x;
t += WrapLineAt;
}
- fprintf(fp, t);
+ fprintf(fp, "%s", t);
fprintf(fp, "\n");
strcpy(OutputBuffer, iRet + 1);
}
@@ -260,7 +276,7 @@
}
}
-void DumpTree(const cn_cbor * cbor, FILE * out, FOO *pFOO, int depth, int fField, int fValue, int fInComment)
+void DumpTree(const cn_cbor * cbor, FILE * out, const FOO *pFOO, int depth, int fField, int fValue, int fInComment)
{
int i;
int i2;
@@ -409,10 +425,23 @@
case CN_CBOR_TEXT:
WrapPrintF(out, "\"");
- for (i = 0; i < cbor->length; i++) WrapPrintF(out, "%c", cbor->v.str[i]);
+ for (i = 0; i < cbor->length; i++) {
+ if (fInComment && (cbor->v.str[i] == '/')) {
+ WrapPrintF(out, "%c", cbor->v.str[i]);
+ }
+ WrapPrintF(out, "%c", cbor->v.str[i]);
+ }
WrapPrintF(out, "\"");
break;
+ case CN_CBOR_TRUE:
+ WrapPrintF(out, "true");
+ break;
+
+ case CN_CBOR_FALSE:
+ WrapPrintF(out, "false");
+ break;
+
default:
WrapPrintF(out, "##");
break;
@@ -429,7 +458,6 @@
size_t cb = 0;
byte rgb[2048];
size_t cbIn;
- int msg_type;
int forXML = false;
FOO * root = NULL;
@@ -449,6 +477,7 @@
else if (strcmp(&argv[i][1], "type=envelope") == 0) root = &EnvelopedMessage;
else if (strcmp(&argv[i][1], "type=signed") == 0) root = &SignedMessage;
else if (strcmp(&argv[i][1], "type=mac") == 0) root = &MacMessage;
+ else if (strcmp(&argv[i][1], "type=mac0") == 0) root = &Mac0Message;
else if (strcmp(&argv[i][1], "type=keyset") == 0) root = &KeySet;
else if (strcmp(&argv[i][1], "type=key") == 0) root = &Key;
else PrintUsage();
@@ -526,6 +555,7 @@
case 992: root = &EnvelopedMessageWithTag; break;
case 993: root = &EncryptedMessageWithTag; break;
case 994: root = &MacMessageWithTag; break;
+ case 997: root = &Mac0MessageWithTag; break;
}
}
}
diff --git a/src/Cose.c b/src/Cose.c
index 550fbf6..f68a5e7 100644
--- a/src/Cose.c
+++ b/src/Cose.c
@@ -36,12 +36,16 @@
CHECK_CONDITION_CBOR(pobj->m_cbor != NULL, errState);
pobj->m_ownMsg = 1;
+#ifdef TAG_IN_ARRAY
if (msgType > 0) {
cn_cbor * cn = cn_cbor_int_create(msgType, CBOR_CONTEXT_PARAM_COMMA &errState);
CHECK_CONDITION_CBOR(cn != NULL, errState);
CHECK_CONDITION_CBOR(cn_cbor_array_append(pobj->m_cbor, cn, &errState), errState);
pobj->m_msgType = msgType;
}
+#else
+ pobj->m_msgType = msgType;
+#endif
pobj->m_unprotectMap = cn_cbor_map_create(CBOR_CONTEXT_PARAM_COMMA &errState);
CHECK_CONDITION_CBOR(pobj->m_unprotectMap != NULL, errState);
@@ -125,7 +129,7 @@
}
-HCOSE COSE_Decode(const byte * rgbData, int cbData, int * ptype, CBOR_CONTEXT_COMMA cose_errback * perr)
+HCOSE COSE_Decode(const byte * rgbData, int cbData, int * ptype, COSE_object_type struct_type, CBOR_CONTEXT_COMMA cose_errback * perr)
{
cn_cbor * cbor = NULL;
const cn_cbor * pType = NULL;
@@ -157,38 +161,40 @@
CHECK_CONDITION(((pType != NULL) && (pType->type == CN_CBOR_UINT)), COSE_ERR_INVALID_PARAMETER);
*ptype = pType->v.sint;
#else // ! TAG_IN_ARRAY
- if (cbor->type != CN_CBOR_TAG) {
- FAIL_CONDITION(COSE_ERR_INVALID_PARAMETER);
+
+ if (cbor->type == CN_CBOR_TAG) {
+ if (struct_type != 0) {
+ CHECK_CONDITION(struct_type == cbor->v.uint, COSE_ERR_INVALID_PARAMETER);
+ }
+ else struct_type = cbor->v.uint;
+
+ *ptype = struct_type;
+
+ cbor = cbor->first_child;
}
- switch (cbor->v.uint) {
- case 998:
- *ptype = MSG_TYPE_ENCRYPT;
- break;
+ else {
+ *ptype = struct_type;
+ }
- default:
- FAIL_CONDITION(COSE_ERR_INVALID_PARAMETER);
- }
-
- cbor = cbor->first_child;
- CHECK_CONDITION(cbor->type == CN_CBOR_ARRAY, COSE_ERR_INVALID_PARAMETER);
+ CHECK_CONDITION(cbor->type == CN_CBOR_ARRAY, COSE_ERR_INVALID_PARAMETER);
#endif // TAG_IN_ARRAY
switch (*ptype) {
- case MSG_TYPE_ENCRYPT:
+ case COSE_enveloped_object:
h = (HCOSE)_COSE_Encrypt_Init_From_Object(cbor, NULL, CBOR_CONTEXT_PARAM_COMMA perr);
if (h == NULL) {
goto errorReturn;
}
break;
- case MSG_TYPE_SIGN:
+ case COSE_sign_object:
h = (HCOSE)_COSE_Sign_Init_From_Object(cbor, NULL, CBOR_CONTEXT_PARAM_COMMA perr);
if (h == NULL) {
goto errorReturn;
}
break;
- case MSG_TYPE_MAC:
+ case COSE_mac_object:
h = (HCOSE)_COSE_Mac_Init_From_Object(cbor, NULL, CBOR_CONTEXT_PARAM_COMMA perr);
if (h == NULL) {
goto errorReturn;
@@ -311,8 +317,12 @@
#endif // USE_CBOR_CONTEXT
#ifdef USE_ARRAY
+#ifdef TAG_IN_ARRAY
pProtected = cn_cbor_index(pMessage->m_cbor, INDEX_PROTECTED + (pMessage->m_msgType == 0 ? 0 : 1));
#else
+ pProtected = cn_cbor_index(pMessage->m_cbor, INDEX_PROTECTED);
+#endif
+#else
pProtected = cn_cbor_mapget_int(pMessage->m_cbor, COSE_Header_Protected);
#endif
if ((pProtected != NULL) &&(pProtected->type != CN_CBOR_INVALID)) {
@@ -347,13 +357,17 @@
bool _COSE_array_replace(COSE * pMessage, cn_cbor * cb_value, int index, CBOR_CONTEXT_COMMA cn_cbor_errback * errp)
{
+#ifdef TAG_IN_ARRAY
if (pMessage->m_msgType != 0) index += 1;
+#endif
return cn_cbor_array_replace(pMessage->m_cbor, cb_value, index, CBOR_CONTEXT_PARAM_COMMA errp);
}
cn_cbor * _COSE_arrayget_int(COSE * pMessage, int index)
{
+#ifdef TAG_IN_ARRAY
if (pMessage->m_msgType != 0) index += 1;
+#endif
return cn_cbor_index(pMessage->m_cbor, index);
}
diff --git a/src/Encrypt.c b/src/Encrypt.c
index dd69af1..51c94ee 100644
--- a/src/Encrypt.c
+++ b/src/Encrypt.c
@@ -33,7 +33,7 @@
return NULL;
}
- if (!_COSE_Init(&pobj->m_message, MSG_TYPE_ENCRYPT, CBOR_CONTEXT_PARAM_COMMA perror)) {
+ if (!_COSE_Init(&pobj->m_message, COSE_enveloped_object, CBOR_CONTEXT_PARAM_COMMA perror)) {
COSE_Encrypt_Free((HCOSE_ENCRYPT)pobj);
return NULL;
}
@@ -158,7 +158,7 @@
return NULL;
}
- if (!_COSE_Init(&pobj->m_encrypt.m_message, MSG_TYPE_NONE, CBOR_CONTEXT_PARAM_COMMA perror)) {
+ if (!_COSE_Init(&pobj->m_encrypt.m_message, COSE_unknown_object, CBOR_CONTEXT_PARAM_COMMA perror)) {
goto error;
}
@@ -397,7 +397,7 @@
cn_Alg = _COSE_map_get_int(&pcose->m_message, COSE_Header_Algorithm, COSE_BOTH, perr);
if (cn_Alg == NULL) goto errorReturn;
- CHECK_CONDITION((cn_Alg->type != CN_CBOR_UINT) && (cn_Alg->type != CN_CBOR_INT), COSE_ERR_INVALID_PARAMETER);
+ CHECK_CONDITION((cn_Alg->type == CN_CBOR_UINT) || (cn_Alg->type == CN_CBOR_INT), COSE_ERR_INVALID_PARAMETER);
alg = (int) cn_Alg->v.uint;
// Get the key size
diff --git a/src/MacMessage.c b/src/MacMessage.c
index 3bc42e8..df94aa7 100644
--- a/src/MacMessage.c
+++ b/src/MacMessage.c
@@ -24,7 +24,7 @@
COSE_MacMessage * pobj = (COSE_MacMessage *)COSE_CALLOC(1, sizeof(COSE_MacMessage), context);
CHECK_CONDITION(pobj != NULL, COSE_ERR_OUT_OF_MEMORY);
- if (!_COSE_Init(&pobj->m_message, MSG_TYPE_MAC, CBOR_CONTEXT_PARAM_COMMA perr)) {
+ if (!_COSE_Init(&pobj->m_message, COSE_mac_object, CBOR_CONTEXT_PARAM_COMMA perr)) {
goto errorReturn;
}
@@ -137,7 +137,7 @@
pobj = (COSE_RecipientInfo *)COSE_CALLOC(1, sizeof(COSE_RecipientInfo), context);
CHECK_CONDITION(pobj != NULL, COSE_ERR_OUT_OF_MEMORY);
- if (!_COSE_Init(&pobj->m_encrypt.m_message, MSG_TYPE_NONE, CBOR_CONTEXT_PARAM_COMMA perr)) {
+ if (!_COSE_Init(&pobj->m_encrypt.m_message, COSE_unknown_object, CBOR_CONTEXT_PARAM_COMMA perr)) {
goto errorReturn;
}
diff --git a/src/Sign.c b/src/Sign.c
index 8c44704..3bd06fe 100644
--- a/src/Sign.c
+++ b/src/Sign.c
@@ -19,7 +19,7 @@
return NULL;
}
- if (!_COSE_Init(&pobj->m_message, MSG_TYPE_SIGN, CBOR_CONTEXT_PARAM_COMMA perror)) {
+ if (!_COSE_Init(&pobj->m_message, COSE_sign_object, CBOR_CONTEXT_PARAM_COMMA perror)) {
COSE_Sign_Free((HCOSE_SIGN)pobj);
return NULL;
}
@@ -160,7 +160,7 @@
pSigner = (COSE_SignerInfo *)COSE_CALLOC(1, sizeof(COSE_SignerInfo), context);
CHECK_CONDITION(pSigner != NULL, COSE_ERR_OUT_OF_MEMORY);
- if (!_COSE_Init(&pSigner->m_message, MSG_TYPE_NONE, CBOR_CONTEXT_PARAM_COMMA perr)) goto errorReturn;
+ if (!_COSE_Init(&pSigner->m_message, COSE_unknown_object, CBOR_CONTEXT_PARAM_COMMA perr)) goto errorReturn;
cbor = cn_cbor_mapget_int(pkey, COSE_Key_ID);
if (cbor != NULL) {
diff --git a/src/cose.h b/src/cose.h
index 137b8ed..8aa37f1 100644
--- a/src/cose.h
+++ b/src/cose.h
@@ -39,9 +39,19 @@
cose_error err;
} cose_errback;
+typedef enum {
+ COSE_unknown_object = 0,
+ COSE_sign_object = 991,
+ COSE_sign1_object = 997,
+ COSE_enveloped_object = 992,
+ COSE_encrypted_object = 993,
+ COSE_mac_object = 994,
+ COSE_mac0_object = 996
+} COSE_object_type;
+
// Generic functions for the COSE library
-HCOSE COSE_Decode(const byte * rgbData, int cbData, int * type, CBOR_CONTEXT_COMMA cose_errback * perr); // Decode the object
+HCOSE COSE_Decode(const byte * rgbData, int cbData, int * type, COSE_object_type struct_type, CBOR_CONTEXT_COMMA cose_errback * perr); // Decode the object
size_t COSE_Encode(HCOSE msg, byte * rgb, int ib, size_t cb);
cn_cbor * COSE_get_cbor(HCOSE hmsg);
diff --git a/src/cose_int.h b/src/cose_int.h
index 807666b..8e69839 100644
--- a/src/cose_int.h
+++ b/src/cose_int.h
@@ -166,11 +166,6 @@
//// Defines on message types
-#define MSG_TYPE_NONE 0
-#define MSG_TYPE_SIGN 1
-#define MSG_TYPE_ENCRYPT 2
-#define MSG_TYPE_MAC 3
-
#define COSE_Header_Protected 99
#define COSE_Header_Unprotected 98
#define COSE_Header_Type 97
diff --git a/test/test.c b/test/test.c
index 25212cc..f600a85 100644
--- a/test/test.c
+++ b/test/test.c
@@ -54,7 +54,7 @@
/* */
int typ;
- hEncObj = (HCOSE_MAC) COSE_Decode(rgb, (int) cb, &typ, NULL, NULL);
+ hEncObj = (HCOSE_MAC) COSE_Decode(rgb, (int) cb, &typ, COSE_mac_object, NULL, NULL);
int iRecipient = 0;
do {
@@ -165,7 +165,7 @@
/* */
int typ;
- hEncObj = (HCOSE_ENCRYPT) COSE_Decode(rgb, (int) cb, &typ, NULL, NULL);
+ hEncObj = (HCOSE_ENCRYPT) COSE_Decode(rgb, (int) cb, &typ, COSE_enveloped_object, NULL, NULL);
int iRecipient = 0;
do {