a_bitstr.c revision 280297
155714Skris/* crypto/asn1/a_bitstr.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8280297Sjkim *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15280297Sjkim *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22280297Sjkim *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37280297Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40280297Sjkim *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52280297Sjkim *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris#include <stdio.h>
6055714Skris#include "cryptlib.h"
6155714Skris#include <openssl/asn1.h>
6255714Skris
6359191Skrisint ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, unsigned char *d, int len)
64280297Sjkim{
65280297Sjkim    return M_ASN1_BIT_STRING_set(x, d, len);
66280297Sjkim}
6759191Skris
6868651Skrisint i2c_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **pp)
69280297Sjkim{
70280297Sjkim    int ret, j, bits, len;
71280297Sjkim    unsigned char *p, *d;
7255714Skris
73280297Sjkim    if (a == NULL)
74280297Sjkim        return (0);
7555714Skris
76280297Sjkim    len = a->length;
7755714Skris
78280297Sjkim    if (len > 0) {
79280297Sjkim        if (a->flags & ASN1_STRING_FLAG_BITS_LEFT) {
80280297Sjkim            bits = (int)a->flags & 0x07;
81280297Sjkim        } else {
82280297Sjkim            for (; len > 0; len--) {
83280297Sjkim                if (a->data[len - 1])
84280297Sjkim                    break;
85280297Sjkim            }
86280297Sjkim            j = a->data[len - 1];
87280297Sjkim            if (j & 0x01)
88280297Sjkim                bits = 0;
89280297Sjkim            else if (j & 0x02)
90280297Sjkim                bits = 1;
91280297Sjkim            else if (j & 0x04)
92280297Sjkim                bits = 2;
93280297Sjkim            else if (j & 0x08)
94280297Sjkim                bits = 3;
95280297Sjkim            else if (j & 0x10)
96280297Sjkim                bits = 4;
97280297Sjkim            else if (j & 0x20)
98280297Sjkim                bits = 5;
99280297Sjkim            else if (j & 0x40)
100280297Sjkim                bits = 6;
101280297Sjkim            else if (j & 0x80)
102280297Sjkim                bits = 7;
103280297Sjkim            else
104280297Sjkim                bits = 0;       /* should not happen */
105280297Sjkim        }
106280297Sjkim    } else
107280297Sjkim        bits = 0;
108100936Snectar
109280297Sjkim    ret = 1 + len;
110280297Sjkim    if (pp == NULL)
111280297Sjkim        return (ret);
112100936Snectar
113280297Sjkim    p = *pp;
11455714Skris
115280297Sjkim    *(p++) = (unsigned char)bits;
116280297Sjkim    d = a->data;
117280297Sjkim    memcpy(p, d, len);
118280297Sjkim    p += len;
119280297Sjkim    if (len > 0)
120280297Sjkim        p[-1] &= (0xff << bits);
121280297Sjkim    *pp = p;
122280297Sjkim    return (ret);
123280297Sjkim}
12455714Skris
125160814SsimonASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
126280297Sjkim                                     const unsigned char **pp, long len)
127280297Sjkim{
128280297Sjkim    ASN1_BIT_STRING *ret = NULL;
129280297Sjkim    const unsigned char *p;
130280297Sjkim    unsigned char *s;
131280297Sjkim    int i;
13268651Skris
133280297Sjkim    if (len < 1) {
134280297Sjkim        i = ASN1_R_STRING_TOO_SHORT;
135280297Sjkim        goto err;
136280297Sjkim    }
137109998Smarkm
138280297Sjkim    if ((a == NULL) || ((*a) == NULL)) {
139280297Sjkim        if ((ret = M_ASN1_BIT_STRING_new()) == NULL)
140280297Sjkim            return (NULL);
141280297Sjkim    } else
142280297Sjkim        ret = (*a);
14368651Skris
144280297Sjkim    p = *pp;
145280297Sjkim    i = *(p++);
146280297Sjkim    if (i > 7) {
147280297Sjkim        i = ASN1_R_INVALID_BIT_STRING_BITS_LEFT;
148280297Sjkim        goto err;
149280297Sjkim    }
150280297Sjkim    /*
151280297Sjkim     * We do this to preserve the settings.  If we modify the settings, via
152280297Sjkim     * the _set_bit function, we will recalculate on output
153280297Sjkim     */
154280297Sjkim    ret->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear */
155280297Sjkim    ret->flags |= (ASN1_STRING_FLAG_BITS_LEFT | i); /* set */
15655714Skris
157280297Sjkim    if (len-- > 1) {            /* using one because of the bits left byte */
158280297Sjkim        s = (unsigned char *)OPENSSL_malloc((int)len);
159280297Sjkim        if (s == NULL) {
160280297Sjkim            i = ERR_R_MALLOC_FAILURE;
161280297Sjkim            goto err;
162280297Sjkim        }
163280297Sjkim        memcpy(s, p, (int)len);
164280297Sjkim        s[len - 1] &= (0xff << i);
165280297Sjkim        p += len;
166280297Sjkim    } else
167280297Sjkim        s = NULL;
16855714Skris
169280297Sjkim    ret->length = (int)len;
170280297Sjkim    if (ret->data != NULL)
171280297Sjkim        OPENSSL_free(ret->data);
172280297Sjkim    ret->data = s;
173280297Sjkim    ret->type = V_ASN1_BIT_STRING;
174280297Sjkim    if (a != NULL)
175280297Sjkim        (*a) = ret;
176280297Sjkim    *pp = p;
177280297Sjkim    return (ret);
178280297Sjkim err:
179280297Sjkim    ASN1err(ASN1_F_C2I_ASN1_BIT_STRING, i);
180280297Sjkim    if ((ret != NULL) && ((a == NULL) || (*a != ret)))
181280297Sjkim        M_ASN1_BIT_STRING_free(ret);
182280297Sjkim    return (NULL);
183280297Sjkim}
18455714Skris
185280297Sjkim/*
186280297Sjkim * These next 2 functions from Goetz Babin-Ebell <babinebell@trustcenter.de>
18755714Skris */
18855714Skrisint ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value)
189280297Sjkim{
190280297Sjkim    int w, v, iv;
191280297Sjkim    unsigned char *c;
19255714Skris
193280297Sjkim    w = n / 8;
194280297Sjkim    v = 1 << (7 - (n & 0x07));
195280297Sjkim    iv = ~v;
196280297Sjkim    if (!value)
197280297Sjkim        v = 0;
19855714Skris
199280297Sjkim    if (a == NULL)
200280297Sjkim        return 0;
201160814Ssimon
202280297Sjkim    a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear, set on write */
20355714Skris
204280297Sjkim    if ((a->length < (w + 1)) || (a->data == NULL)) {
205280297Sjkim        if (!value)
206280297Sjkim            return (1);         /* Don't need to set */
207280297Sjkim        if (a->data == NULL)
208280297Sjkim            c = (unsigned char *)OPENSSL_malloc(w + 1);
209280297Sjkim        else
210280297Sjkim            c = (unsigned char *)OPENSSL_realloc_clean(a->data,
211280297Sjkim                                                       a->length, w + 1);
212280297Sjkim        if (c == NULL) {
213280297Sjkim            ASN1err(ASN1_F_ASN1_BIT_STRING_SET_BIT, ERR_R_MALLOC_FAILURE);
214280297Sjkim            return 0;
215280297Sjkim        }
216280297Sjkim        if (w + 1 - a->length > 0)
217280297Sjkim            memset(c + a->length, 0, w + 1 - a->length);
218280297Sjkim        a->data = c;
219280297Sjkim        a->length = w + 1;
220280297Sjkim    }
221280297Sjkim    a->data[w] = ((a->data[w]) & iv) | v;
222280297Sjkim    while ((a->length > 0) && (a->data[a->length - 1] == 0))
223280297Sjkim        a->length--;
224280297Sjkim    return (1);
225280297Sjkim}
22655714Skris
22755714Skrisint ASN1_BIT_STRING_get_bit(ASN1_BIT_STRING *a, int n)
228280297Sjkim{
229280297Sjkim    int w, v;
23055714Skris
231280297Sjkim    w = n / 8;
232280297Sjkim    v = 1 << (7 - (n & 0x07));
233280297Sjkim    if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL))
234280297Sjkim        return (0);
235280297Sjkim    return ((a->data[w] & v) != 0);
236280297Sjkim}
23755714Skris
238238405Sjkim/*
239280297Sjkim * Checks if the given bit string contains only bits specified by
240238405Sjkim * the flags vector. Returns 0 if there is at least one bit set in 'a'
241238405Sjkim * which is not specified in 'flags', 1 otherwise.
242238405Sjkim * 'len' is the length of 'flags'.
243238405Sjkim */
244238405Sjkimint ASN1_BIT_STRING_check(ASN1_BIT_STRING *a,
245280297Sjkim                          unsigned char *flags, int flags_len)
246280297Sjkim{
247280297Sjkim    int i, ok;
248280297Sjkim    /* Check if there is one bit set at all. */
249280297Sjkim    if (!a || !a->data)
250280297Sjkim        return 1;
251238405Sjkim
252280297Sjkim    /*
253280297Sjkim     * Check each byte of the internal representation of the bit string.
254280297Sjkim     */
255280297Sjkim    ok = 1;
256280297Sjkim    for (i = 0; i < a->length && ok; ++i) {
257280297Sjkim        unsigned char mask = i < flags_len ? ~flags[i] : 0xff;
258280297Sjkim        /* We are done if there is an unneeded bit set. */
259280297Sjkim        ok = (a->data[i] & mask) == 0;
260280297Sjkim    }
261280297Sjkim    return ok;
262280297Sjkim}
263