summaryrefslogtreecommitdiff
path: root/view/kernelcache/core/transformers/libDER/DER_Encode.c
blob: 9b6946175a0855d0472ca721ee6e7670fe75ee42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
 * Copyright (c) 2005-2007,2011,2014 Apple Inc. All Rights Reserved.
 *
 * @APPLE_LICENSE_HEADER_START@
 * 
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this
 * file.
 * 
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 * 
 * @APPLE_LICENSE_HEADER_END@
 */


/*
 * DER_Encode.h - DER encoding routines
 *
 */

#include <libDER/DER_Encode.h>
#include <libDER/asn1Types.h>
#include <libDER/libDER_config.h>
#include <libDER/DER_Decode.h>

#ifndef	DER_ENCODE_ENABLE
#error Please define DER_ENCODE_ENABLE.
#endif

#if		DER_ENCODE_ENABLE

/* calculate size of encoded tag */
static DERSize DERLengthOfTag(
	DERTag tag)
{
	DERSize rtn = 1;

    tag &= ASN1_TAGNUM_MASK;
    if (tag >= 0x1F) {
        /* Shift 7-bit digits out of the tag integer until it's zero. */
        while(tag != 0) {
            rtn++;
            tag >>= 7;
        }
    }

    return rtn;
}

/* encode tag */
static DERReturn DEREncodeTag(
	DERTag tag,
	DERByte *buf,		/* encoded length goes here */
	DERSize *inOutLen)	/* IN/OUT */
{
    DERSize outLen = DERLengthOfTag(tag);
    DERTag tagNumber = tag & ASN1_TAGNUM_MASK;
    DERByte tag1 = (tag >> (sizeof(DERTag) * 8 - 8)) & 0xE0;

	if(outLen > *inOutLen) {
		return DR_BufOverflow;
	}

    if(outLen == 1) {
		/* short form */
		*buf = tag1 | tagNumber;
	}
	else {
        /* long form */
        DERByte *tagBytes = buf + outLen;	// l.s. digit of tag
        *buf = tag1 | 0x1F;                 // tag class / method indicator
        *--tagBytes = tagNumber & 0x7F;
        tagNumber >>= 7;
        while(tagNumber != 0) {
            *--tagBytes = (tagNumber & 0x7F) | 0x80;
            tagNumber >>= 7;
        }
    }
	*inOutLen = outLen;
	return DR_Success;
}

/* calculate size of encoded length */
DERSize DERLengthOfLength(
	DERSize length)
{
	DERSize rtn;
	
	if(length < 0x80) {
		/* short form length */
		return 1;
	}
	
	/* long form - one length-of-length byte plus length bytes */
	rtn = 1;
	while(length != 0) {
		rtn++;
		length >>= 8;
	}
	return rtn;
}

/* encode length */
DERReturn DEREncodeLength(
	DERSize length,
	DERByte *buf,		/* encoded length goes here */
	DERSize *inOutLen)	/* IN/OUT */
{
	DERByte *lenBytes;
	DERSize outLen = DERLengthOfLength(length);
	
	if(outLen > *inOutLen) {
		return DR_BufOverflow;
	}
	
	if(length < 0x80) {
		/* short form */
		*buf = (DERByte)length;
		*inOutLen = 1;
		return DR_Success;
	}
	
	/* long form */
	*buf = (outLen - 1) | 0x80;		// length of length, long form indicator
	lenBytes = buf + outLen - 1;	// l.s. digit of length 
	while(length != 0) {
		*lenBytes-- = (DERByte)length;
		length >>= 8;
	}
	*inOutLen = outLen;
	return DR_Success;
}

DERSize DERLengthOfItem(
	DERTag tag,
	DERSize length)
{
    return DERLengthOfTag(tag) + DERLengthOfLength(length) + length;
}

DERReturn DEREncodeItem(
	DERTag tag,
	DERSize length,
    const DERByte *src,
	DERByte *derOut,	/* encoded item goes here */
	DERSize *inOutLen)	/* IN/OUT */
{
	DERReturn		drtn;
	DERSize			itemLen;
	DERByte			*currPtr = derOut;
	DERSize         bytesLeft = DERLengthOfItem(tag, length);
	if(bytesLeft > *inOutLen) {
		return DR_BufOverflow;
	}
	*inOutLen = bytesLeft;

	/* top level tag */
	itemLen = bytesLeft;
	drtn = DEREncodeTag(tag, currPtr, &itemLen);
	if(drtn) {
		return drtn;
	}
	currPtr += itemLen;
	bytesLeft -= itemLen;
	itemLen = bytesLeft;
	drtn = DEREncodeLength(length, currPtr, &itemLen);
	if(drtn) {
		return drtn;
	}
	currPtr += itemLen;
	bytesLeft -= itemLen;
	DERMemmove(currPtr, src, length);

    (void) bytesLeft;
    
	return DR_Success;
}

static /* calculate the content length of an encoded sequence */
DERSize DERContentLengthOfEncodedSequence(
	const void			*src,		/* generally a ptr to a struct full of 
									 *    DERItems */
	DERShort			numItems,	/* size of itemSpecs[] */
	const DERItemSpec	*itemSpecs)
{
	DERSize contentLen = 0;
	unsigned dex;
	DERSize thisContentLen;
	
	/* find length of each item */
	for(dex=0; dex<numItems; dex++) {
		const DERItemSpec *currItemSpec = &itemSpecs[dex];
		DERShort currOptions = currItemSpec->options;
		const DERByte *byteSrc = (const DERByte *)src + currItemSpec->offset;
		const DERItem *itemSrc = (const DERItem *)byteSrc;

		if(currOptions & DER_ENC_WRITE_DER) {
			/* easy case - no encode */
			contentLen += itemSrc->length;
			continue;
		}
		
        if ((currOptions & DER_DEC_OPTIONAL) && itemSrc->length == 0) {
            /* If an optional item isn't present we don't encode a
               tag and len. */
            continue;
        }

		/* 
		 * length of this item = 
		 *   tag (one byte) +
		 *   length of length +
		 *   content length +
		 *   optional zero byte for signed integer
		 */
		contentLen += DERLengthOfTag(currItemSpec->tag);
		
		/* check need for pad byte before calculating lengthOfLength... */
		thisContentLen = itemSrc->length;
		if((currOptions & DER_ENC_SIGNED_INT) &&
		   (itemSrc->length != 0)) {
			if(itemSrc->data[0] & 0x80) {
				/* insert zero keep it positive */
				thisContentLen++;
			}
		}
		contentLen += DERLengthOfLength(thisContentLen);
		contentLen += thisContentLen;
	}
	return contentLen;
}

DERReturn DEREncodeSequence(
	DERTag				topTag,		/* ASN1_CONSTR_SEQUENCE, ASN1_CONSTR_SET */
	const void			*src,		/* generally a ptr to a struct full of 
									 *    DERItems */
	DERShort			numItems,	/* size of itemSpecs[] */
	const DERItemSpec	*itemSpecs,
	DERByte				*derOut,	/* encoded data written here */
	DERSize				*inOutLen)	/* IN/OUT */
{
	const DERByte	*endPtr = derOut + *inOutLen;
	DERByte			*currPtr = derOut;
	DERSize			bytesLeft = *inOutLen;
	DERSize			contentLen;
	DERReturn		drtn;
	DERSize			itemLen;
	unsigned		dex;
	
	/* top level tag */
	itemLen = bytesLeft;
    drtn = DEREncodeTag(topTag, currPtr, &itemLen);
	if(drtn) {
		return drtn;
	}
	currPtr += itemLen;
	bytesLeft -= itemLen;
	if(currPtr >= endPtr) {	
		return DR_BufOverflow;
	}
	
	/* content length */
	contentLen = DERContentLengthOfEncodedSequence(src, numItems, itemSpecs);	
	itemLen = bytesLeft;
	drtn = DEREncodeLength(contentLen, currPtr, &itemLen);
	if(drtn) {
		return drtn;
	}
	currPtr += itemLen;
	bytesLeft -= itemLen;
	if(currPtr + contentLen > endPtr) {
		return DR_BufOverflow;
	}
	/* we don't have to check for overflow any more */
	
	/* grind thru the items */
	for(dex=0; dex<numItems; dex++) {
		const DERItemSpec *currItemSpec = &itemSpecs[dex];
		DERShort currOptions = currItemSpec->options;
		const DERByte *byteSrc = (const DERByte *)src + currItemSpec->offset;
		const DERItem *itemSrc = (const DERItem *)byteSrc;
		int prependZero = 0;
		
		if(currOptions & DER_ENC_WRITE_DER) {
			/* easy case */
			DERMemmove(currPtr, itemSrc->data, itemSrc->length);
			currPtr += itemSrc->length;
			bytesLeft -= itemSrc->length;
			continue;
		}

        if ((currOptions & DER_DEC_OPTIONAL) && itemSrc->length == 0) {
            /* If an optional item isn't present we skip it. */
            continue;
        }

        /* encode one item: first the tag */
        itemLen = bytesLeft;
        drtn = DEREncodeTag(currItemSpec->tag, currPtr, &itemLen);
        if(drtn) {
            return drtn;
        }
        currPtr += itemLen;
        bytesLeft -= itemLen;
		
		/* do we need to prepend a zero to content? */
		contentLen = itemSrc->length;
		if((currOptions & DER_ENC_SIGNED_INT) &&
		   (itemSrc->length != 0)) {
			if(itemSrc->data[0] & 0x80) {
				/* insert zero keep it positive */
				contentLen++;
				prependZero = 1;
			}
		}

		/* encode content length */
		itemLen = bytesLeft;
		drtn = DEREncodeLength(contentLen, currPtr, &itemLen);
		if(drtn) {
			return drtn;
		}
		currPtr += itemLen;
		bytesLeft -= itemLen;
		
		/* now the content, with possible leading zero added */
		if(prependZero) {
			*currPtr++ = 0;
			bytesLeft--;
		}
		DERMemmove(currPtr, itemSrc->data, itemSrc->length);
		currPtr += itemSrc->length;
		bytesLeft -= itemSrc->length;
	}
	*inOutLen = (currPtr - derOut);
	return DR_Success;
}

/* calculate the length of an encoded sequence. */
DERSize DERLengthOfEncodedSequence(
    DERTag				topTag,
	const void			*src,		/* generally a ptr to a struct full of 
									 *    DERItems */
	DERShort			numItems,	/* size of itemSpecs[] */
	const DERItemSpec	*itemSpecs)
{
	DERSize contentLen = DERContentLengthOfEncodedSequence(
		src, numItems, itemSpecs);
		
	return DERLengthOfTag(topTag) +
		DERLengthOfLength(contentLen) +
		contentLen;
}

#endif	/* DER_ENCODE_ENABLE */