security/nss/lib/util/secasn1u.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 /*
michael@0 6 * Utility routines to complement the ASN.1 encoding and decoding functions.
michael@0 7 */
michael@0 8
michael@0 9 #include "secasn1.h"
michael@0 10
michael@0 11
michael@0 12 /*
michael@0 13 * We have a length that needs to be encoded; how many bytes will the
michael@0 14 * encoding take?
michael@0 15 *
michael@0 16 * The rules are that 0 - 0x7f takes one byte (the length itself is the
michael@0 17 * entire encoding); everything else takes one plus the number of bytes
michael@0 18 * in the length.
michael@0 19 */
michael@0 20 int
michael@0 21 SEC_ASN1LengthLength (unsigned long len)
michael@0 22 {
michael@0 23 int lenlen = 1;
michael@0 24
michael@0 25 if (len > 0x7f) {
michael@0 26 do {
michael@0 27 lenlen++;
michael@0 28 len >>= 8;
michael@0 29 } while (len);
michael@0 30 }
michael@0 31
michael@0 32 return lenlen;
michael@0 33 }
michael@0 34
michael@0 35
michael@0 36 /*
michael@0 37 * XXX Move over (and rewrite as appropriate) the rest of the
michael@0 38 * stuff in dersubr.c!
michael@0 39 */
michael@0 40
michael@0 41
michael@0 42 /*
michael@0 43 * Find the appropriate subtemplate for the given template.
michael@0 44 * This may involve calling a "chooser" function, or it may just
michael@0 45 * be right there. In either case, it is expected to *have* a
michael@0 46 * subtemplate; this is asserted in debug builds (in non-debug
michael@0 47 * builds, NULL will be returned).
michael@0 48 *
michael@0 49 * "thing" is a pointer to the structure being encoded/decoded
michael@0 50 * "encoding", when true, means that we are in the process of encoding
michael@0 51 * (as opposed to in the process of decoding)
michael@0 52 */
michael@0 53 const SEC_ASN1Template *
michael@0 54 SEC_ASN1GetSubtemplate (const SEC_ASN1Template *theTemplate, void *thing,
michael@0 55 PRBool encoding)
michael@0 56 {
michael@0 57 const SEC_ASN1Template *subt = NULL;
michael@0 58
michael@0 59 PORT_Assert (theTemplate->sub != NULL);
michael@0 60 if (theTemplate->sub != NULL) {
michael@0 61 if (theTemplate->kind & SEC_ASN1_DYNAMIC) {
michael@0 62 SEC_ASN1TemplateChooserPtr chooserp;
michael@0 63
michael@0 64 chooserp = *(SEC_ASN1TemplateChooserPtr *) theTemplate->sub;
michael@0 65 if (chooserp) {
michael@0 66 if (thing != NULL)
michael@0 67 thing = (char *)thing - theTemplate->offset;
michael@0 68 subt = (* chooserp)(thing, encoding);
michael@0 69 }
michael@0 70 } else {
michael@0 71 subt = (SEC_ASN1Template*)theTemplate->sub;
michael@0 72 }
michael@0 73 }
michael@0 74 return subt;
michael@0 75 }
michael@0 76
michael@0 77 PRBool SEC_ASN1IsTemplateSimple(const SEC_ASN1Template *theTemplate)
michael@0 78 {
michael@0 79 if (!theTemplate) {
michael@0 80 return PR_TRUE; /* it doesn't get any simpler than NULL */
michael@0 81 }
michael@0 82 /* only templates made of one primitive type or a choice of primitive
michael@0 83 types are considered simple */
michael@0 84 if (! (theTemplate->kind & (~SEC_ASN1_TAGNUM_MASK))) {
michael@0 85 return PR_TRUE; /* primitive type */
michael@0 86 }
michael@0 87 if (!(theTemplate->kind & SEC_ASN1_CHOICE)) {
michael@0 88 return PR_FALSE; /* no choice means not simple */
michael@0 89 }
michael@0 90 while (++theTemplate && theTemplate->kind) {
michael@0 91 if (theTemplate->kind & (~SEC_ASN1_TAGNUM_MASK)) {
michael@0 92 return PR_FALSE; /* complex type */
michael@0 93 }
michael@0 94 }
michael@0 95 return PR_TRUE; /* choice of primitive types */
michael@0 96 }
michael@0 97

mercurial