Path: blob/main/crypto/heimdal/lib/asn1/der_length.c
34889 views
/*1* Copyright (c) 1997-2005 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Portions Copyright (c) 2009 Apple Inc. All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10*11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13*14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* 3. Neither the name of the Institute nor the names of its contributors19* may be used to endorse or promote products derived from this software20* without specific prior written permission.21*22* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND23* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE24* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE25* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE26* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS28* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)29* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY31* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF32* SUCH DAMAGE.33*/3435#include "der_locl.h"3637RCSID("$Id$");3839size_t40_heim_len_unsigned (unsigned val)41{42size_t ret = 0;43int last_val_gt_128;4445do {46++ret;47last_val_gt_128 = (val >= 128);48val /= 256;49} while (val);5051if(last_val_gt_128)52ret++;5354return ret;55}5657size_t58_heim_len_unsigned64 (uint64_t val)59{60size_t ret = 0;61int last_val_gt_128;6263do {64++ret;65last_val_gt_128 = (val >= 128);66val /= 256;67} while (val);6869if(last_val_gt_128)70ret++;7172return ret;73}7475size_t76_heim_len_int (int val)77{78unsigned char q;79size_t ret = 0;8081if (val >= 0) {82do {83q = val % 256;84ret++;85val /= 256;86} while(val);87if(q >= 128)88ret++;89} else {90val = ~val;91do {92q = ~(val % 256);93ret++;94val /= 256;95} while(val);96if(q < 128)97ret++;98}99return ret;100}101102size_t103_heim_len_int64 (int64_t val)104{105unsigned char q;106size_t ret = 0;107108if (val >= 0) {109do {110q = val % 256;111ret++;112val /= 256;113} while(val);114if(q >= 128)115ret++;116} else {117val = ~val;118do {119q = ~(val % 256);120ret++;121val /= 256;122} while(val);123if(q < 128)124ret++;125}126return ret;127}128129static size_t130len_oid (const heim_oid *oid)131{132size_t ret = 1;133size_t n;134135for (n = 2; n < oid->length; ++n) {136unsigned u = oid->components[n];137138do {139++ret;140u /= 128;141} while(u > 0);142}143return ret;144}145146size_t147der_length_len (size_t len)148{149if (len < 128)150return 1;151else {152int ret = 0;153do {154++ret;155len /= 256;156} while (len);157return ret + 1;158}159}160161size_t162der_length_tag(unsigned int tag)163{164size_t len = 0;165166if(tag <= 30)167return 1;168while(tag) {169tag /= 128;170len++;171}172return len + 1;173}174175size_t176der_length_integer (const int *data)177{178return _heim_len_int (*data);179}180181size_t182der_length_integer64 (const int64_t *data)183{184return _heim_len_int64 (*data);185}186187size_t188der_length_unsigned (const unsigned *data)189{190return _heim_len_unsigned(*data);191}192193size_t194der_length_unsigned64 (const uint64_t *data)195{196return _heim_len_unsigned64(*data);197}198199size_t200der_length_enumerated (const unsigned *data)201{202return _heim_len_int (*data);203}204205size_t206der_length_general_string (const heim_general_string *data)207{208return strlen(*data);209}210211size_t212der_length_utf8string (const heim_utf8_string *data)213{214return strlen(*data);215}216217size_t218der_length_printable_string (const heim_printable_string *data)219{220return data->length;221}222223size_t224der_length_ia5_string (const heim_ia5_string *data)225{226return data->length;227}228229size_t230der_length_bmp_string (const heim_bmp_string *data)231{232return data->length * 2;233}234235size_t236der_length_universal_string (const heim_universal_string *data)237{238return data->length * 4;239}240241size_t242der_length_visible_string (const heim_visible_string *data)243{244return strlen(*data);245}246247size_t248der_length_octet_string (const heim_octet_string *k)249{250return k->length;251}252253size_t254der_length_heim_integer (const heim_integer *k)255{256if (k->length == 0)257return 1;258if (k->negative)259return k->length + (((~(((unsigned char *)k->data)[0])) & 0x80) ? 0 : 1);260else261return k->length + ((((unsigned char *)k->data)[0] & 0x80) ? 1 : 0);262}263264size_t265der_length_oid (const heim_oid *k)266{267return len_oid (k);268}269270size_t271der_length_generalized_time (const time_t *t)272{273heim_octet_string k;274size_t ret;275276_heim_time2generalizedtime (*t, &k, 1);277ret = k.length;278free(k.data);279return ret;280}281282size_t283der_length_utctime (const time_t *t)284{285heim_octet_string k;286size_t ret;287288_heim_time2generalizedtime (*t, &k, 0);289ret = k.length;290free(k.data);291return ret;292}293294size_t295der_length_boolean (const int *k)296{297return 1;298}299300size_t301der_length_bit_string (const heim_bit_string *k)302{303return (k->length + 7) / 8 + 1;304}305306307