1 // SPDX-License-Identifier: GPL-2.0-only
3 * Simple encoder primitives for ASN.1 BER/DER/CER
5 * Copyright (C) 2019 James.Bottomley@HansenPartnership.com
8 #include <linux/asn1_encoder.h>
10 #include <linux/string.h>
11 #include <linux/module.h>
14 * asn1_encode_integer() - encode positive integer to ASN.1
15 * @data: pointer to the pointer to the data
16 * @end_data: end of data pointer, points one beyond last usable byte in @data
17 * @integer: integer to be encoded
19 * This is a simplified encoder: it only currently does
20 * positive integers, but it should be simple enough to add the
21 * negative case if a use comes along.
24 asn1_encode_integer(unsigned char *data
, const unsigned char *end_data
,
27 int data_len
= end_data
- data
;
28 unsigned char *d
= &data
[2];
33 "BUG: integer encode only supports positive integers"))
34 return ERR_PTR(-EINVAL
);
39 /* need at least 3 bytes for tag, length and integer encoding */
41 return ERR_PTR(-EINVAL
);
43 /* remaining length where at d (the start of the integer encoding) */
46 data
[0] = _tag(UNIV
, PRIM
, INT
);
52 for (i
= sizeof(integer
); i
> 0 ; i
--) {
53 int byte
= integer
>> (8 * (i
- 1));
55 if (!found
&& byte
== 0)
59 * for a positive number the first byte must have bit
60 * 7 clear in two's complement (otherwise it's a
61 * negative number) so prepend a leading zero if
64 if (!found
&& (byte
& 0x80)) {
66 * no check needed here, we already know we
75 return ERR_PTR(-EINVAL
);
82 data
[1] = d
- data
- 2;
86 EXPORT_SYMBOL_GPL(asn1_encode_integer
);
88 /* calculate the base 128 digit values setting the top bit of the first octet */
89 static int asn1_encode_oid_digit(unsigned char **_data
, int *data_len
, u32 oid
)
91 unsigned char *data
= *_data
;
92 int start
= 7 + 7 + 7 + 7;
105 while (oid
>> start
== 0)
108 while (start
> 0 && *data_len
> 0) {
112 oid
= oid
- (byte
<< start
);
132 * asn1_encode_oid() - encode an oid to ASN.1
133 * @data: position to begin encoding at
134 * @end_data: end of data pointer, points one beyond last usable byte in @data
135 * @oid: array of oids
136 * @oid_len: length of oid array
138 * this encodes an OID up to ASN.1 when presented as an array of OID values
141 asn1_encode_oid(unsigned char *data
, const unsigned char *end_data
,
142 u32 oid
[], int oid_len
)
144 int data_len
= end_data
- data
;
145 unsigned char *d
= data
+ 2;
148 if (WARN(oid_len
< 2, "OID must have at least two elements"))
149 return ERR_PTR(-EINVAL
);
151 if (WARN(oid_len
> 32, "OID is too large"))
152 return ERR_PTR(-EINVAL
);
158 /* need at least 3 bytes for tag, length and OID encoding */
160 return ERR_PTR(-EINVAL
);
162 data
[0] = _tag(UNIV
, PRIM
, OID
);
163 *d
++ = oid
[0] * 40 + oid
[1];
167 for (i
= 2; i
< oid_len
; i
++) {
168 ret
= asn1_encode_oid_digit(&d
, &data_len
, oid
[i
]);
173 data
[1] = d
- data
- 2;
177 EXPORT_SYMBOL_GPL(asn1_encode_oid
);
180 * asn1_encode_length() - encode a length to follow an ASN.1 tag
181 * @data: pointer to encode at
182 * @data_len: pointer to remaining length (adjusted by routine)
183 * @len: length to encode
185 * This routine can encode lengths up to 65535 using the ASN.1 rules.
186 * It will accept a negative length and place a zero length tag
187 * instead (to keep the ASN.1 valid). This convention allows other
188 * encoder primitives to accept negative lengths as singalling the
189 * sequence will be re-encoded when the length is known.
191 static int asn1_encode_length(unsigned char **data
, int *data_len
, int len
)
213 *((*data
)++) = len
& 0xff;
223 *((*data
)++) = (len
>> 8) & 0xff;
224 *((*data
)++) = len
& 0xff;
229 if (WARN(len
> 0xffffff, "ASN.1 length can't be > 0xffffff"))
235 *((*data
)++) = (len
>> 16) & 0xff;
236 *((*data
)++) = (len
>> 8) & 0xff;
237 *((*data
)++) = len
& 0xff;
244 * asn1_encode_tag() - add a tag for optional or explicit value
245 * @data: pointer to place tag at
246 * @end_data: end of data pointer, points one beyond last usable byte in @data
247 * @tag: tag to be placed
248 * @string: the data to be tagged
249 * @len: the length of the data to be tagged
251 * Note this currently only handles short form tags < 31.
253 * Standard usage is to pass in a @tag, @string and @length and the
254 * @string will be ASN.1 encoded with @tag and placed into @data. If
255 * the encoding would put data past @end_data then an error is
256 * returned, otherwise a pointer to a position one beyond the encoding
259 * To encode in place pass a NULL @string and -1 for @len and the
260 * maximum allowable beginning and end of the data; all this will do
261 * is add the current maximum length and update the data pointer to
262 * the place where the tag contents should be placed is returned. The
263 * data should be copied in by the calling routine which should then
264 * repeat the prior statement but now with the known length. In order
265 * to avoid having to keep both before and after pointers, the repeat
266 * expects to be called with @data pointing to where the first encode
267 * returned it and still NULL for @string but the real length in @len.
270 asn1_encode_tag(unsigned char *data
, const unsigned char *end_data
,
271 u32 tag
, const unsigned char *string
, int len
)
273 int data_len
= end_data
- data
;
276 if (WARN(tag
> 30, "ASN.1 tag can't be > 30"))
277 return ERR_PTR(-EINVAL
);
279 if (!string
&& WARN(len
> 127,
280 "BUG: recode tag is too big (>127)"))
281 return ERR_PTR(-EINVAL
);
286 if (!string
&& len
> 0) {
288 * we're recoding, so move back to the start of the
289 * tag and install a dummy length because the real
290 * data_len should be NULL
297 return ERR_PTR(-EINVAL
);
299 *(data
++) = _tagn(CONT
, CONS
, tag
);
301 ret
= asn1_encode_length(&data
, &data_len
, len
);
309 return ERR_PTR(-EINVAL
);
311 memcpy(data
, string
, len
);
316 EXPORT_SYMBOL_GPL(asn1_encode_tag
);
319 * asn1_encode_octet_string() - encode an ASN.1 OCTET STRING
320 * @data: pointer to encode at
321 * @end_data: end of data pointer, points one beyond last usable byte in @data
322 * @string: string to be encoded
323 * @len: length of string
325 * Note ASN.1 octet strings may contain zeros, so the length is obligatory.
328 asn1_encode_octet_string(unsigned char *data
,
329 const unsigned char *end_data
,
330 const unsigned char *string
, u32 len
)
332 int data_len
= end_data
- data
;
338 /* need minimum of 2 bytes for tag and length of zero length string */
340 return ERR_PTR(-EINVAL
);
342 *(data
++) = _tag(UNIV
, PRIM
, OTS
);
345 ret
= asn1_encode_length(&data
, &data_len
, len
);
350 return ERR_PTR(-EINVAL
);
352 memcpy(data
, string
, len
);
357 EXPORT_SYMBOL_GPL(asn1_encode_octet_string
);
360 * asn1_encode_sequence() - wrap a byte stream in an ASN.1 SEQUENCE
361 * @data: pointer to encode at
362 * @end_data: end of data pointer, points one beyond last usable byte in @data
363 * @seq: data to be encoded as a sequence
364 * @len: length of the data to be encoded as a sequence
366 * Fill in a sequence. To encode in place, pass NULL for @seq and -1
367 * for @len; then call again once the length is known (still with NULL
368 * for @seq). In order to avoid having to keep both before and after
369 * pointers, the repeat expects to be called with @data pointing to
370 * where the first encode placed it.
373 asn1_encode_sequence(unsigned char *data
, const unsigned char *end_data
,
374 const unsigned char *seq
, int len
)
376 int data_len
= end_data
- data
;
379 if (!seq
&& WARN(len
> 127,
380 "BUG: recode sequence is too big (>127)"))
381 return ERR_PTR(-EINVAL
);
386 if (!seq
&& len
>= 0) {
388 * we're recoding, so move back to the start of the
389 * sequence and install a dummy length because the
390 * real length should be NULL
397 return ERR_PTR(-EINVAL
);
399 *(data
++) = _tag(UNIV
, CONS
, SEQ
);
402 ret
= asn1_encode_length(&data
, &data_len
, len
);
410 return ERR_PTR(-EINVAL
);
412 memcpy(data
, seq
, len
);
417 EXPORT_SYMBOL_GPL(asn1_encode_sequence
);
420 * asn1_encode_boolean() - encode a boolean value to ASN.1
421 * @data: pointer to encode at
422 * @end_data: end of data pointer, points one beyond last usable byte in @data
423 * @val: the boolean true/false value
426 asn1_encode_boolean(unsigned char *data
, const unsigned char *end_data
,
429 int data_len
= end_data
- data
;
434 /* booleans are 3 bytes: tag, length == 1 and value == 0 or 1 */
436 return ERR_PTR(-EINVAL
);
438 *(data
++) = _tag(UNIV
, PRIM
, BOOL
);
441 asn1_encode_length(&data
, &data_len
, 1);
450 EXPORT_SYMBOL_GPL(asn1_encode_boolean
);
452 MODULE_DESCRIPTION("Simple encoder primitives for ASN.1 BER/DER/CER");
453 MODULE_LICENSE("GPL");