Skip to content

Commit 55c10bc

Browse files
committed
hoist oid_from_str() to encode_oid()
1 parent 4053081 commit 55c10bc

File tree

1 file changed

+28
-37
lines changed

1 file changed

+28
-37
lines changed

src/protocols/der/encode.c

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -652,45 +652,16 @@ static ssize_t fr_der_encode_oid_from_value(fr_dbuff_t *dbuff, uint64_t value, u
652652
return fr_dbuff_set(dbuff, &our_dbuff);
653653
}
654654

655-
static ssize_t fr_der_encode_oid_from_str(fr_dbuff_t *dbuff, const char *oid_str)
655+
static ssize_t fr_der_encode_oid(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, UNUSED fr_der_encode_ctx_t *encode_ctx)
656656
{
657-
fr_dbuff_t our_dbuff = FR_DBUFF(dbuff);
657+
fr_dbuff_t our_dbuff = FR_DBUFF(dbuff);
658+
fr_pair_t const *vp;
658659
uint64_t component;
659660
int count = 0;
660661
unsigned long long oid;
661-
char const *start = oid_str;
662+
char const *start;
662663
char *end = NULL;
663664

664-
/*
665-
* Loop until we're done the string.
666-
*/
667-
while (*start) {
668-
ssize_t slen;
669-
670-
/*
671-
* Parse the component.
672-
*/
673-
oid = strtoull(start, &end, 10);
674-
if ((oid == ULLONG_MAX) || (*end && (*end != '.'))) {
675-
fr_strerror_const("Invalid OID");
676-
return -1;
677-
}
678-
679-
slen = fr_der_encode_oid_from_value(&our_dbuff, oid, &component, &count);
680-
if (slen < 0) return -1;
681-
682-
start = end + 1;
683-
}
684-
685-
return fr_dbuff_set(dbuff, &our_dbuff);
686-
}
687-
688-
static ssize_t fr_der_encode_oid(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, UNUSED fr_der_encode_ctx_t *encode_ctx)
689-
{
690-
fr_dbuff_t our_dbuff = FR_DBUFF(dbuff);
691-
fr_pair_t const *vp;
692-
ssize_t slen = 0;
693-
694665
vp = fr_dcursor_current(cursor);
695666
PAIR_VERIFY(vp);
696667
fr_assert(!vp->da->flags.is_raw);
@@ -718,10 +689,30 @@ static ssize_t fr_der_encode_oid(fr_dbuff_t *dbuff, fr_dcursor_t *cursor, UNUSED
718689
* reached by X = 0 and X = 1. 8.19.5 The numerical value of the ith subidentifier, (2 <= i <= N) is
719690
* that of the (i + 1)th object identifier component.
720691
*/
721-
slen = fr_der_encode_oid_from_str(&our_dbuff, vp->vp_strvalue);
722-
if (slen < 0) {
723-
fr_strerror_printf("Failed to encode OID: %s", fr_strerror());
724-
return slen;
692+
693+
694+
/*
695+
* Parse each OID component.
696+
*/
697+
for (start = vp->vp_strvalue; *start != '\0'; start = end + 1) {
698+
ssize_t slen;
699+
700+
/*
701+
* Parse the component.
702+
*/
703+
oid = strtoull(start, &end, 10);
704+
if ((oid == ULLONG_MAX) || (*end && (*end != '.'))) {
705+
fr_strerror_const("Invalid OID");
706+
return -1;
707+
}
708+
709+
slen = fr_der_encode_oid_from_value(&our_dbuff, oid, &component, &count);
710+
if (slen < 0) return -1;
711+
}
712+
713+
if (count <= 2) {
714+
fr_strerror_printf("Invalid OID '%s' - too short", vp->vp_strvalue);
715+
return -1;
725716
}
726717

727718
return fr_dbuff_set(dbuff, &our_dbuff);

0 commit comments

Comments
 (0)