Skip to content

Commit 68bc7aa

Browse files
committed
Merge pull request trusteddomainproject#168 from simplelists/hashtype_parse
Fix verification when key specifies hash algorithm If an ARC key specifies the hash type as SHA256 (i.e. h=sha256) then OpenARC will fail to verify the signature. Whilst the presence of a particular hash type is detected, the type is not set and it defaults to sha1. trusteddomainproject#168
2 parents 7441ad2 + 043a8d1 commit 68bc7aa

File tree

2 files changed

+66
-10
lines changed

2 files changed

+66
-10
lines changed

libopenarc/arc.c

+65-10
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,57 @@ arc_key_hashesok(ARC_LIB *lib, u_char *hashlist)
289289
/* NOTREACHED */
290290
}
291291

292+
/*
293+
** ARC_PARSE_ALGORITHM -- parse an algorithm and set the msg hash and key,
294+
** as well as set the message digest algorithm for
295+
** RSA_verify in the variable nid
296+
**
297+
** Parameters:
298+
** msg -- ARC_MESSAGE handle
299+
** alg -- string containing the algorithm to parse
300+
** nid -- variable to write the message digest algorithm
301+
**
302+
** Return value:
303+
** An ARC_STAT_* constant.
304+
*/
305+
306+
ARC_STAT
307+
arc_parse_algorithm(ARC_MESSAGE *msg, u_char *alg, int *nid)
308+
{
309+
arc_alg_t algtype;
310+
311+
assert(msg != NULL);
312+
assert(nid != NULL);
313+
314+
if (alg == NULL)
315+
{
316+
arc_error(msg, "missing algorithm passed to arc_parse_algorithm");
317+
return ARC_STAT_BADALG;
318+
}
319+
320+
algtype = arc_name_to_code(algorithms, alg);
321+
322+
if (algtype == ARC_SIGN_RSASHA1)
323+
{
324+
msg->arc_hashtype = ARC_HASHTYPE_SHA1;
325+
msg->arc_keytype = ARC_KEYTYPE_RSA;
326+
*nid = NID_sha1;
327+
}
328+
else if (algtype == ARC_SIGN_RSASHA256)
329+
{
330+
msg->arc_hashtype = ARC_HASHTYPE_SHA256;
331+
msg->arc_keytype = ARC_KEYTYPE_RSA;
332+
*nid = NID_sha256;
333+
}
334+
else
335+
{
336+
arc_error(msg, "unknown or invalid algorithm: %s", alg);
337+
return ARC_STAT_BADALG;
338+
}
339+
340+
return ARC_STAT_OK;
341+
}
342+
292343
/*
293344
** ARC_GENAMSHDR -- generate a signature or seal header field
294345
**
@@ -1964,6 +2015,13 @@ arc_validate_msg(ARC_MESSAGE *msg, u_int setnum)
19642015
msg->arc_selector = arc_param_get(kvset, "s");
19652016
msg->arc_domain = arc_param_get(kvset, "d");
19662017

2018+
/* store algorithm in msg, needed for arc_get_key() */
2019+
alg = arc_param_get(kvset, "a");
2020+
status = arc_parse_algorithm(msg, alg, &nid);
2021+
if (status != ARC_STAT_OK)
2022+
// arc_error already set by arc_parse_algorithm()
2023+
return status;
2024+
19672025
/* get the key from DNS (or wherever) */
19682026
status = arc_get_key(msg, FALSE);
19692027
if (status != ARC_STAT_OK)
@@ -2038,11 +2096,6 @@ arc_validate_msg(ARC_MESSAGE *msg, u_int setnum)
20382096
return ARC_STAT_CANTVRFY;
20392097
}
20402098

2041-
alg = arc_param_get(kvset, "a");
2042-
nid = NID_sha1;
2043-
if (alg != NULL && strcmp(alg, "rsa-sha256") == 0)
2044-
nid = NID_sha256;
2045-
20462099
rsastat = RSA_verify(nid, hh, hhlen, sig, siglen, rsa);
20472100

20482101
RSA_free(rsa);
@@ -2118,6 +2171,13 @@ arc_validate_seal(ARC_MESSAGE *msg, u_int setnum)
21182171
msg->arc_selector = arc_param_get(kvset, "s");
21192172
msg->arc_domain = arc_param_get(kvset, "d");
21202173

2174+
/* store algorithm in msg, needed for arc_get_key() */
2175+
alg = arc_param_get(kvset, "a");
2176+
status = arc_parse_algorithm(msg, alg, &nid);
2177+
if (status != ARC_STAT_OK)
2178+
// arc_error already set by arc_parse_algorithm()
2179+
return status;
2180+
21212181
if (msg->arc_selector == NULL)
21222182
{
21232183
arc_error(msg, "seal at i=%u has no selector", setnum);
@@ -2190,11 +2250,6 @@ arc_validate_seal(ARC_MESSAGE *msg, u_int setnum)
21902250
return ARC_STAT_INTERNAL;
21912251
}
21922252

2193-
alg = arc_param_get(kvset, "a");
2194-
nid = NID_sha1;
2195-
if (alg != NULL && strcmp(alg, "rsa-sha256") == 0)
2196-
nid = NID_sha256;
2197-
21982253
rsastat = RSA_verify(nid, sh, shlen, sig, siglen, rsa);
21992254

22002255
RSA_free(rsa);

libopenarc/arc.h

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ typedef int ARC_STAT;
8181
#define ARC_STAT_KEYFAIL 11 /* key retrieval failed */
8282
#define ARC_STAT_MULTIDNSREPLY 12 /* multiple DNS replies */
8383
#define ARC_STAT_SIGGEN 13 /* seal generation failed */
84+
#define ARC_STAT_BADALG 14 /* unknown or invalid algorithm */
8485

8586
/*
8687
** ARC_CHAIN -- chain state

0 commit comments

Comments
 (0)