| 1 |
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */ |
| 2 |
/* SPDX-License-Identifier: Unlicense */ |
| 3 |
#include "tomcrypt_private.h" |
| 4 |
|
| 5 |
/** |
| 6 |
@file der_encode_setof.c |
| 7 |
ASN.1 DER, Encode SET OF, Tom St Denis |
| 8 |
*/ |
| 9 |
|
| 10 |
#ifdef LTC_DER |
| 11 |
|
| 12 |
struct edge { |
| 13 |
unsigned char *start; |
| 14 |
unsigned long size; |
| 15 |
}; |
| 16 |
|
| 17 |
#define s_qsort_helper s_setof_qsort_helper |
| 18 |
static int s_qsort_helper(const void *a, const void *b) |
| 19 |
{ |
| 20 |
struct edge *A = (struct edge *)a, *B = (struct edge *)b; |
| 21 |
int r; |
| 22 |
unsigned long x; |
| 23 |
|
| 24 |
/* compare min length */ |
| 25 |
r = XMEMCMP(A->start, B->start, MIN(A->size, B->size)); |
| 26 |
|
| 27 |
if (r == 0 && A->size != B->size) { |
| 28 |
if (A->size > B->size) { |
| 29 |
for (x = B->size; x < A->size; x++) { |
| 30 |
if (A->start[x]) { |
| 31 |
return 1; |
| 32 |
} |
| 33 |
} |
| 34 |
} else { |
| 35 |
for (x = A->size; x < B->size; x++) { |
| 36 |
if (B->start[x]) { |
| 37 |
return -1; |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
return r; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
Encode a SETOF stucture |
| 48 |
@param list The list of items to encode |
| 49 |
@param inlen The number of items in the list |
| 50 |
@param out [out] The destination |
| 51 |
@param outlen [in/out] The size of the output |
| 52 |
@return CRYPT_OK on success |
| 53 |
*/ |
| 54 |
int der_encode_setof(const ltc_asn1_list *list, unsigned long inlen, |
| 55 |
unsigned char *out, unsigned long *outlen) |
| 56 |
{ |
| 57 |
unsigned long x, y, z; |
| 58 |
ptrdiff_t hdrlen; |
| 59 |
int err; |
| 60 |
struct edge *edges; |
| 61 |
unsigned char *ptr, *buf; |
| 62 |
|
| 63 |
/* check that they're all the same type */ |
| 64 |
for (x = 1; x < inlen; x++) { |
| 65 |
if (list[x].type != list[x-1].type) { |
| 66 |
return CRYPT_INVALID_ARG; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/* alloc buffer to store copy of output */ |
| 71 |
buf = XCALLOC(1, *outlen); |
| 72 |
if (buf == NULL) { |
| 73 |
return CRYPT_MEM; |
| 74 |
} |
| 75 |
|
| 76 |
/* encode list */ |
| 77 |
if ((err = der_encode_sequence_ex(list, inlen, buf, outlen, LTC_ASN1_SETOF)) != CRYPT_OK) { |
| 78 |
XFREE(buf); |
| 79 |
return err; |
| 80 |
} |
| 81 |
|
| 82 |
/* allocate edges */ |
| 83 |
edges = XCALLOC(inlen, sizeof(*edges)); |
| 84 |
if (edges == NULL) { |
| 85 |
XFREE(buf); |
| 86 |
return CRYPT_MEM; |
| 87 |
} |
| 88 |
|
| 89 |
/* skip header */ |
| 90 |
ptr = buf + 1; |
| 91 |
|
| 92 |
/* now skip length data */ |
| 93 |
x = *ptr++; |
| 94 |
if (x >= 0x80) { |
| 95 |
ptr += (x & 0x7F); |
| 96 |
} |
| 97 |
|
| 98 |
/* get the size of the static header */ |
| 99 |
hdrlen = ptr - buf; |
| 100 |
|
| 101 |
|
| 102 |
/* scan for edges */ |
| 103 |
x = 0; |
| 104 |
while (ptr < (buf + *outlen)) { |
| 105 |
/* store start */ |
| 106 |
edges[x].start = ptr; |
| 107 |
|
| 108 |
/* skip type */ |
| 109 |
z = 1; |
| 110 |
|
| 111 |
/* parse length */ |
| 112 |
y = ptr[z++]; |
| 113 |
if (y < 128) { |
| 114 |
edges[x].size = y; |
| 115 |
} else { |
| 116 |
y &= 0x7F; |
| 117 |
edges[x].size = 0; |
| 118 |
while (y--) { |
| 119 |
edges[x].size = (edges[x].size << 8) | ((unsigned long)ptr[z++]); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/* skip content */ |
| 124 |
edges[x].size += z; |
| 125 |
ptr += edges[x].size; |
| 126 |
++x; |
| 127 |
} |
| 128 |
|
| 129 |
/* sort based on contents (using edges) */ |
| 130 |
XQSORT(edges, inlen, sizeof(*edges), &s_qsort_helper); |
| 131 |
|
| 132 |
/* copy static header */ |
| 133 |
XMEMCPY(out, buf, hdrlen); |
| 134 |
|
| 135 |
/* copy+sort using edges+indecies to output from buffer */ |
| 136 |
for (y = (unsigned long)hdrlen, x = 0; x < inlen; x++) { |
| 137 |
XMEMCPY(out+y, edges[x].start, edges[x].size); |
| 138 |
y += edges[x].size; |
| 139 |
} |
| 140 |
|
| 141 |
#ifdef LTC_CLEAN_STACK |
| 142 |
zeromem(buf, *outlen); |
| 143 |
#endif |
| 144 |
|
| 145 |
/* free buffers */ |
| 146 |
XFREE(edges); |
| 147 |
XFREE(buf); |
| 148 |
|
| 149 |
return CRYPT_OK; |
| 150 |
} |
| 151 |
|
| 152 |
#undef s_qsort_helper |
| 153 |
|
| 154 |
#endif |