summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1 (follow)
Commit message (Collapse)AuthorAgeFilesLines
...
* Use ASN1_INTEGER to parse/build (Z)LONG_itjsing2022-07-021-69/+67
| | | | | | | Rather than having yet another (broken) ASN.1 INTEGER content builder and parser, use {c2i,i2c}_ASN1_INTEGER(). ok beck@
* whitespace nittb2022-06-301-2/+2
|
* Refactor asn1 time parsing to use CBS - enforce valid times in ASN.1 parsing.beck2022-06-293-68/+155
| | | | | | | | While we're here enforce valid days for months and leap years. Inspired by same in boringssl. ok jsing@
* Negate unsigned then cast to signed.jsing2022-06-281-2/+2
| | | | | | | | | Avoid undefined behaviour by negating the unsigned value, before casting to int64_t, rather than casting to int64_t then negating. Fixes oss-fuzz #48499 ok tb@
* Add new time manipulation funcitons that OpenSSL has exposed thatbeck2022-06-273-24/+86
| | | | | | | | the world seems to be using. Symbols.list changes and exposure to wait for minor bump ok jsing@ jca@
* Prepare to provide EVP_PKEY_security_bits()tb2022-06-272-2/+10
| | | | | | | This also provides a pkey_security_bits member to the PKEY ASN.1 methods and a corresponding setter EVP_PKEY_asn1_set_security_bits(). ok beck jsing
* Provide and use long_{get,set}()jsing2022-06-261-11/+35
| | | | | | | | | Apparently at some point a LONG_it was misaligned - provide and use long_{get,set}() so that we always memcpy() rather than doing it some times but not others. While here provide long_clear() rather than abusing and reusing long_free(). ok tb@
* Use ints for boolean values.jsing2022-06-251-31/+31
| | | | | | | Switch to using ints for boolean values and use 0 or 1 for constructed, rather than using 0 the ASN.1 tag encoded value (1 << 5). ok tb@
* Reuse ASN1_INTEGER functions for ASN1_ENUMERATED_{get,set}()jsing2022-06-252-56/+59
| | | | | | | Instead of having a separate get/set implementation, reuse the ASN1_INTEGER code. Also prepare to provide ASN1_ENUMERATED_{get,set}_int64(). ok beck@ tb@
* Rewrite ASN1_INTEGER_{get,set}() using CBS/CBBjsing2022-06-254-65/+197
| | | | | | In the process, prepare to provide ASN1_INTEGER_{get,set}_{u,}int64(). ok beck@ tb@
* Simplify ASN1_INTEGER_cmp()jsing2022-06-251-16/+9
| | | | ok beck@ tb@
* Clean up ASN1_item_sign_ctx() a littletb2022-05-241-25/+38
| | | | | | | | | | | | | Instead of inl, outl, and outll, use in_len, out_len, and buf_out_len. Use the appropriate types for them. Check return values properly, check for overflow. Remove some unnecessary casts and add some for readability. Use asn1_abs_set_unused_bits() instead of inlining it. This removes the last direct consumer of ASN1_STRING_FLAG_BITS_LEFT outside of asn1/a_bitstr.c. The flag is still mentioned in x509/x509_addr.c but that will hopefully go away soon. tweaks/ok jsing
* Rewrite X509_PUBKEY_set0_param() to use asn1_abs_set_unused_bits()tb2022-05-241-10/+8
| | | | | | | This streamlines the logic and uses ASN1_STRING_set0() and asn1_abs_set_unused_bits() instead of inlining them. ok jsing
* Use asn1_abs_set_unused_bits() in asn1_str2type()tb2022-05-241-5/+6
| | | | ok jsing
* Remove some unhelpful comments and spell NULL correctly.jsing2022-05-211-10/+4
|
* Factor out ASN1_ITYPE_EXTERN handling.jsing2022-05-211-15/+32
| | | | | | | Factor out the ef->asn1_ex_d2i() callback handling - this allows us to pull out all of the related variables into a self-contained function. ok tb@
* Use 'at' for ASN1_TEMPLATE variable names rather than 'tt'.jsing2022-05-211-52/+58
| | | | | | | | | Also use array indexes for it->templates, rather than trying to be extra clever in for loops (suggested by tb@ during a review). No functional change. ok tb@
* Remove _ex_ from all of the internal function names.jsing2022-05-211-35/+35
| | | | | | | | | It no longer makes sense to have "extended" versions of functions internally. No functional change. ok tb@
* Drop *out == NULL check in ASN1_STRING_to_UTF8()tb2022-05-201-2/+6
| | | | | | | | | Unfortunately, several things in the ecosystem depend on the existing API behavior of being able to pass in an uninitialized pointer on the stack: haproxy, grpc, mongo-tools and others show up on the first two pages of Debian codesearch. ok jsing
* Reorder functions within file.jsing2022-05-191-624/+608
| | | | | | | | Order functions by use, moving public API to the bottom and utility functions to the top. This makes the code more logical/readable, plus we can remove all except one of the static function prototypes. No functional change.
* Rewrite the asn1_template_*() functions with CBS.jsing2022-05-191-218/+168
| | | | | | | | | | Rewrite the asn1_template_*() functions with CBS, readable variable names and free then alloc. This was the last caller of asn1_check_eoc() and asn1_check_tag(), hence remove them and rename the _cbs suffixed versions in their place. ok tb@
* Refactor asn1_d2i_ex_primitive()jsing2022-05-171-98/+146
| | | | | | | | | | Split the object content handling off into asn1_d2i_ex_primitive_content(), move the handling ov V_ASN1_ANY into asn1_d2i_ex_any() and move the MSTRING handling into asn1_d2i_ex_mstring(). This way we parse the header once (rather than twice for ANY and MSTRING), then process the content, while also avoiding complex special cases in a single code path. ok tb@
* Refactor ASN.1 template functions before rewriting.jsing2022-05-171-133/+145
| | | | | | | | | | | Change asn1_template_ex_d2i() so that we short circuit in the no explicit tagging case. Split out the SET OF/SEQUENCE OF handling from asn1_template_noexp_d2i() into a asn1_template_stack_of_d2i() function and simplify the remaining code. ok tb@
* Expose asn1_abs_set_unused_bits() in asn1_locl.htb2022-05-172-3/+4
| | | | | | Should have been part of a previous commit. ok jsing
* Clean up and fix ASN1_STRING_to_UTF8()tb2022-05-161-12/+23
| | | | | | | | | Instead of using a temporary variable on the stack, we can use the usual Henson mechanism for allocating the struct. Make the function single exit and throw an error instead of crashing or leaking if out is NULL or *out is non-NULL. tweaks/ok jsing
* Remove a stray spacetb2022-05-161-2/+2
|
* Avoid use of uninitialized in ASN1_STRING_to_UTF8()tb2022-05-161-4/+3
| | | | | | | | | | | | A long standing failure to initialize a struct on the stack fully was exposed by a recent refactoring. Fortunately, the uninitialized 'flag' member is only used to decide whether or not to call freezero(NULL, 0), so it is completely harmless. This is a first trivial fix, a better version will be landed separately with regress. Reported by Steffen Jaeckel, GH #760 ok beck
* Rewrite asn1_item_ex_d2i_sequence() using CBS and readable variable names.jsing2022-05-161-131/+113
| | | | | | Now that combine no longer exists, we can also free and reallocate. ok tb@
* Fix d2i_ASN1_OBJECT()tb2022-05-131-2/+2
| | | | | | | | | Due to a confusion of two CBS, the API would incorrectly advance the *der_in pointer, resulting in a DER parse failure. Issue reported by Aram Sargsyan ok jsing
* Call the ASN1_OP_D2I_PRE callback after ASN1_item_ex_new().jsing2022-05-121-10/+8
| | | | | | | | In asn1_item_ex_d2i_choice(), only call the ASN1_OP_D2I_PRE callback after allocation has occurred via ASN1_item_ex_new(). This matches the sequence handling code and the documentation. Discussed with tb@
* Rename asn1_enc_free() to asn1_enc_cleanup().jsing2022-05-123-6/+6
| | | | | | | This function does not actually free an ASN1_ENCODING, which are embedded in a struct. Name suggested by tb@
* Rewrite asn1_item_ex_d2i_choice() using CBS.jsing2022-05-121-73/+54
| | | | | | Now that combine no longer exists, we can also free and reallocate. ok tb@
* Rewrite asn1_enc_save() using CBS.jsing2022-05-123-46/+69
| | | | | | Rework and clean up other asn1_enc_* related functions while here. ok tb@
* Use freezero() with ASN1_ENCODING.jsing2022-05-121-3/+3
| | | | | | | | While ASN1_ENCODING is currently only used with types that should only contain public information, we assume that ASN.1 may contain sensitive information, hence use freezero() here instead of free(). ok deraadt@ tb@
* Remove ASN1_AFLG_BROKEN.jsing2022-05-122-19/+3
| | | | | | | | This workaround was used by ASN1_BROKEN_SEQUENCE, which existed for NETSCAPE_ENCRYPTED_PKEY. Remove the workaround since the only consumer has already been removed. ok tb@
* Rewrite asn1_item_ex_d2i() using CBS.jsing2022-05-101-58/+125
| | | | | | | | This requires a few wrappers to call into some non-CBS functions, however we can now remove the asn1_d2i_ex_primitive() wrapper as there are no longer any non-CBS callers. ok tb@
* Remove ASN.1 combining.jsing2022-05-105-82/+37
| | | | | | | | This was an option used to combine ASN.1 into a single structure, which was only ever used by DSAPublicKey and X509_ATTRIBUTE. Since they no longer use it we can mop this up and simplify all of the related code. ok tb@
* Simplify X509_ATTRIBUTE ASN.1 encoding.jsing2022-05-092-64/+17
| | | | | | | | | | | For some unknown historical reason, X509_ATTRIBUTE allows for a single ASN.1 value or an ASN.1 SET OF, rather than requiring an ASN.1 SET OF. Simplify encoding and remove support for single values - this is similar to OpenSSL e20b57270dec. This removes the last use of COMBINE in the ASN.1 decoder. ok tb@
* Split asn1_item_ex_d2i() into three.jsing2022-05-071-223/+287
| | | | | | | | Factor out the handling of CHOICE and SEQUENCE into their own functions. This reduces complexity, reduces indentation and will allow for further clean up. ok beck@ tb@
* Rewrite asn1_d2i_ex_primitive() with CBS.jsing2022-05-071-72/+86
| | | | ok tb@
* Refactor asn1_ex_c2i()jsing2022-05-071-39/+59
| | | | | | | | | | | | | | | The asn1_ex_c2i() function currently handles the V_ASN1_ANY case inline, which means there multiple special cases, with pointer fudging and restoring. Instead, split asn1_ex_c2i() into three functions - one that only handles storage into a primitive type (asn1_ex_c2i_primitive()), one that handles the V_ASN1_ANY case (asn1_ex_c2i_any()) and calls asn1_ex_c2i_primitive() with the correct pointer and an asn1_ex_c2i() that handles the custom functions case, before dispatching to asn1_ex_c2i_any() or asn1_ex_c2i_primitive(), as appropriate. This results in cleaner and simpler code. With input from and ok tb@
* Use size_t for ASN.1 lengths.jsing2022-05-054-16/+20
| | | | | | | | Change asn1_get_length_cbs() and asn1_get_object_cbs() to handle and return a length as a size_t rather than a uint32_t. This makes it simpler and less error prone in the callers. Suggested by and ok tb@
* Rewrite asn1_collect() and asn1_find_end() with CBS.jsing2022-05-041-77/+80
| | | | | | Use more readable variable and arguments names in the process. ok tb@
* Avoid special handling of definite length before calling asn1_find_end().jsing2022-05-041-10/+4
| | | | | | | | The asn1_find_end() function handles definite length ASN.1, which means that there is no point in the only caller having code to explicitly handle definite length - it can just call the function. ok tb@
* Change asn1_check_tag_cbs() out_len from long to size_t.jsing2022-05-041-4/+10
| | | | | | | | The long vs size_t checks can be handled in the asn1_check_tag() wrapper and this will help to avoid propagating long vs size_t issues into new code. ok tb@
* Rewrite asn1_check_tlen() using CBS.jsing2022-04-283-63/+113
| | | | | | | | | | | | | | | Rather than calling asn1_get_object_cbs(), call asn1_get_identifier_cbs(), then immediately proceed with the tag number and tag class check. Only if that succeeds (or it is not required) do we call asn1_get_length_cbs(). This avoids incurring the overhead of decoding the length in the case where the tag number and tag class do not match. While here rename asn1_check_tlen() to asn1_check_tag() - while we decode the length, what we are normally checking is the tag number and tag class. Also rename the arguments for readability. For now the argument types and encoding remain unchanged. ok inoguchi@ tb@
* Refactor ASN1_TIME_adj_internal()tb2022-04-281-84/+82
| | | | | | | | | | | | | | | | | | ASN1_TIME_adj_internal() does some strange dances with remembering allocations in a boolean and using strlen(p) to deduce what happened inside *_string_from_tm(). It also (mis)translates a NULL p to an illegal time value error. This can be streamlined by converting directly from a struct tm into an ASN1_TIME and setting the errors when they occur instead of trying to deduce them from a NULL return. This is made a bit uglier than necessary due to the reuse-or-allocate semantics of the public API. At the cost of a little code duplication, ASN1_TIME_adj_internal() becomes very easy and ASN1_TIME_to_generalizedtime() is also simplified somewhat. ok inoguchi jsing
* Decode via c2i_ASN1_INTEGER_cbs() from asn1_ex_c2i().jsing2022-04-272-5/+5
|
* Rewrite c2i_ASN1_INTEGER() using CBS.jsing2022-04-271-84/+129
| | | | | | | | This also makes validation stricter and inline with X.690 - we now reject zero length inputs (rather than treating them as zero values) and enforce minimal encoding. ok tb@
* Remove the ASN.1 decoder tag/length cache (TLC).jsing2022-04-271-90/+37
| | | | | | | | | | | | | | | | | | | | | | | | | | Currently, every time an ASN.1 identifier and length is decoded it is stored in a tag/length cache for potential reuse. However, the only time this is actually of benefit is when decoding CHOICE or SEQUENCE with OPTIONAL fields (or MSTRING and ANY due to less than ideal implementation). For CHOICE and SEQUENCE with OPTIONAL fields the current code attempts to decode the first option and if that fails, it moves onto the next option and attempts to decode it, repeating until it succeeds (or runs out of options). There are a number of problems with the cache. Firstly, it adds complexity to the ASN.1 decoder since it has to be passed up and down through the various layers. Secondly, there is nothing that keeps the cached data in synchronisation with the input stream. This makes it fragile and a potential security risk. Thirdly, the type is in the public headers and API, meaning that we cannot readily change the types or fields to improve the code. Testing also suggests that in typical decoding cases we actually get a small performance increase by removing the cache. There are also several other options that would improve decoding performance, which we can visit once we have simpler and more robust code. ok beck@ inoguchi@ tb@