// SPDX-License-Identifier: GPL-2.0-only /* * Simple encoder primitives for ASN.1 BER/DER/CER * * Copyright (C) 2019 [email protected] */ #include <linux/asn1_encoder.h> #include <linux/bug.h> #include <linux/string.h> #include <linux/module.h> /** * asn1_encode_integer() - encode positive integer to ASN.1 * @data: pointer to the pointer to the data * @end_data: end of data pointer, points one beyond last usable byte in @data * @integer: integer to be encoded * * This is a simplified encoder: it only currently does * positive integers, but it should be simple enough to add the * negative case if a use comes along. */ unsigned char * asn1_encode_integer(unsigned char *data, const unsigned char *end_data, s64 integer) { … } EXPORT_SYMBOL_GPL(…); /* calculate the base 128 digit values setting the top bit of the first octet */ static int asn1_encode_oid_digit(unsigned char **_data, int *data_len, u32 oid) { … } /** * asn1_encode_oid() - encode an oid to ASN.1 * @data: position to begin encoding at * @end_data: end of data pointer, points one beyond last usable byte in @data * @oid: array of oids * @oid_len: length of oid array * * this encodes an OID up to ASN.1 when presented as an array of OID values */ unsigned char * asn1_encode_oid(unsigned char *data, const unsigned char *end_data, u32 oid[], int oid_len) { … } EXPORT_SYMBOL_GPL(…); /** * asn1_encode_length() - encode a length to follow an ASN.1 tag * @data: pointer to encode at * @data_len: pointer to remaining length (adjusted by routine) * @len: length to encode * * This routine can encode lengths up to 65535 using the ASN.1 rules. * It will accept a negative length and place a zero length tag * instead (to keep the ASN.1 valid). This convention allows other * encoder primitives to accept negative lengths as singalling the * sequence will be re-encoded when the length is known. */ static int asn1_encode_length(unsigned char **data, int *data_len, int len) { … } /** * asn1_encode_tag() - add a tag for optional or explicit value * @data: pointer to place tag at * @end_data: end of data pointer, points one beyond last usable byte in @data * @tag: tag to be placed * @string: the data to be tagged * @len: the length of the data to be tagged * * Note this currently only handles short form tags < 31. * * Standard usage is to pass in a @tag, @string and @length and the * @string will be ASN.1 encoded with @tag and placed into @data. If * the encoding would put data past @end_data then an error is * returned, otherwise a pointer to a position one beyond the encoding * is returned. * * To encode in place pass a NULL @string and -1 for @len and the * maximum allowable beginning and end of the data; all this will do * is add the current maximum length and update the data pointer to * the place where the tag contents should be placed is returned. The * data should be copied in by the calling routine which should then * repeat the prior statement but now with the known length. In order * to avoid having to keep both before and after pointers, the repeat * expects to be called with @data pointing to where the first encode * returned it and still NULL for @string but the real length in @len. */ unsigned char * asn1_encode_tag(unsigned char *data, const unsigned char *end_data, u32 tag, const unsigned char *string, int len) { … } EXPORT_SYMBOL_GPL(…); /** * asn1_encode_octet_string() - encode an ASN.1 OCTET STRING * @data: pointer to encode at * @end_data: end of data pointer, points one beyond last usable byte in @data * @string: string to be encoded * @len: length of string * * Note ASN.1 octet strings may contain zeros, so the length is obligatory. */ unsigned char * asn1_encode_octet_string(unsigned char *data, const unsigned char *end_data, const unsigned char *string, u32 len) { … } EXPORT_SYMBOL_GPL(…); /** * asn1_encode_sequence() - wrap a byte stream in an ASN.1 SEQUENCE * @data: pointer to encode at * @end_data: end of data pointer, points one beyond last usable byte in @data * @seq: data to be encoded as a sequence * @len: length of the data to be encoded as a sequence * * Fill in a sequence. To encode in place, pass NULL for @seq and -1 * for @len; then call again once the length is known (still with NULL * for @seq). In order to avoid having to keep both before and after * pointers, the repeat expects to be called with @data pointing to * where the first encode placed it. */ unsigned char * asn1_encode_sequence(unsigned char *data, const unsigned char *end_data, const unsigned char *seq, int len) { … } EXPORT_SYMBOL_GPL(…); /** * asn1_encode_boolean() - encode a boolean value to ASN.1 * @data: pointer to encode at * @end_data: end of data pointer, points one beyond last usable byte in @data * @val: the boolean true/false value */ unsigned char * asn1_encode_boolean(unsigned char *data, const unsigned char *end_data, bool val) { … } EXPORT_SYMBOL_GPL(…); MODULE_DESCRIPTION(…) …; MODULE_LICENSE(…) …;