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
59325337Sjkim#include <limits.h>
6055714Skris#include <stdio.h>
6155714Skris#include "cryptlib.h"
6255714Skris#include <openssl/asn1.h>
6355714Skris
6459191Skrisint ASN1_BIT_STRING_set(ASN1_BIT_STRING *x, unsigned char *d, int len)
65280297Sjkim{
66280297Sjkim    return M_ASN1_BIT_STRING_set(x, d, len);
67280297Sjkim}
6859191Skris
6968651Skrisint i2c_ASN1_BIT_STRING(ASN1_BIT_STRING *a, unsigned char **pp)
70280297Sjkim{
71280297Sjkim    int ret, j, bits, len;
72280297Sjkim    unsigned char *p, *d;
7355714Skris
74280297Sjkim    if (a == NULL)
75280297Sjkim        return (0);
7655714Skris
77280297Sjkim    len = a->length;
7855714Skris
79280297Sjkim    if (len > 0) {
80280297Sjkim        if (a->flags & ASN1_STRING_FLAG_BITS_LEFT) {
81280297Sjkim            bits = (int)a->flags & 0x07;
82280297Sjkim        } else {
83280297Sjkim            for (; len > 0; len--) {
84280297Sjkim                if (a->data[len - 1])
85280297Sjkim                    break;
86280297Sjkim            }
87280297Sjkim            j = a->data[len - 1];
88280297Sjkim            if (j & 0x01)
89280297Sjkim                bits = 0;
90280297Sjkim            else if (j & 0x02)
91280297Sjkim                bits = 1;
92280297Sjkim            else if (j & 0x04)
93280297Sjkim                bits = 2;
94280297Sjkim            else if (j & 0x08)
95280297Sjkim                bits = 3;
96280297Sjkim            else if (j & 0x10)
97280297Sjkim                bits = 4;
98280297Sjkim            else if (j & 0x20)
99280297Sjkim                bits = 5;
100280297Sjkim            else if (j & 0x40)
101280297Sjkim                bits = 6;
102280297Sjkim            else if (j & 0x80)
103280297Sjkim                bits = 7;
104280297Sjkim            else
105280297Sjkim                bits = 0;       /* should not happen */
106280297Sjkim        }
107280297Sjkim    } else
108280297Sjkim        bits = 0;
109100936Snectar
110280297Sjkim    ret = 1 + len;
111280297Sjkim    if (pp == NULL)
112280297Sjkim        return (ret);
113100936Snectar
114280297Sjkim    p = *pp;
11555714Skris
116280297Sjkim    *(p++) = (unsigned char)bits;
117280297Sjkim    d = a->data;
118325335Sjkim    if (len > 0) {
119325335Sjkim        memcpy(p, d, len);
120325335Sjkim        p += len;
121280297Sjkim        p[-1] &= (0xff << bits);
122325335Sjkim    }
123280297Sjkim    *pp = p;
124280297Sjkim    return (ret);
125280297Sjkim}
12655714Skris
127160814SsimonASN1_BIT_STRING *c2i_ASN1_BIT_STRING(ASN1_BIT_STRING **a,
128280297Sjkim                                     const unsigned char **pp, long len)
129280297Sjkim{
130280297Sjkim    ASN1_BIT_STRING *ret = NULL;
131280297Sjkim    const unsigned char *p;
132280297Sjkim    unsigned char *s;
133280297Sjkim    int i;
13468651Skris
135280297Sjkim    if (len < 1) {
136280297Sjkim        i = ASN1_R_STRING_TOO_SHORT;
137280297Sjkim        goto err;
138280297Sjkim    }
139109998Smarkm
140325337Sjkim    if (len > INT_MAX) {
141325337Sjkim        i = ASN1_R_STRING_TOO_LONG;
142325337Sjkim        goto err;
143325337Sjkim    }
144325337Sjkim
145280297Sjkim    if ((a == NULL) || ((*a) == NULL)) {
146280297Sjkim        if ((ret = M_ASN1_BIT_STRING_new()) == NULL)
147280297Sjkim            return (NULL);
148280297Sjkim    } else
149280297Sjkim        ret = (*a);
15068651Skris
151280297Sjkim    p = *pp;
152280297Sjkim    i = *(p++);
153280297Sjkim    if (i > 7) {
154280297Sjkim        i = ASN1_R_INVALID_BIT_STRING_BITS_LEFT;
155280297Sjkim        goto err;
156280297Sjkim    }
157280297Sjkim    /*
158280297Sjkim     * We do this to preserve the settings.  If we modify the settings, via
159280297Sjkim     * the _set_bit function, we will recalculate on output
160280297Sjkim     */
161280297Sjkim    ret->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear */
162280297Sjkim    ret->flags |= (ASN1_STRING_FLAG_BITS_LEFT | i); /* set */
16355714Skris
164280297Sjkim    if (len-- > 1) {            /* using one because of the bits left byte */
165280297Sjkim        s = (unsigned char *)OPENSSL_malloc((int)len);
166280297Sjkim        if (s == NULL) {
167280297Sjkim            i = ERR_R_MALLOC_FAILURE;
168280297Sjkim            goto err;
169280297Sjkim        }
170280297Sjkim        memcpy(s, p, (int)len);
171280297Sjkim        s[len - 1] &= (0xff << i);
172280297Sjkim        p += len;
173280297Sjkim    } else
174280297Sjkim        s = NULL;
17555714Skris
176280297Sjkim    ret->length = (int)len;
177280297Sjkim    if (ret->data != NULL)
178280297Sjkim        OPENSSL_free(ret->data);
179280297Sjkim    ret->data = s;
180280297Sjkim    ret->type = V_ASN1_BIT_STRING;
181280297Sjkim    if (a != NULL)
182280297Sjkim        (*a) = ret;
183280297Sjkim    *pp = p;
184280297Sjkim    return (ret);
185280297Sjkim err:
186280297Sjkim    ASN1err(ASN1_F_C2I_ASN1_BIT_STRING, i);
187280297Sjkim    if ((ret != NULL) && ((a == NULL) || (*a != ret)))
188280297Sjkim        M_ASN1_BIT_STRING_free(ret);
189280297Sjkim    return (NULL);
190280297Sjkim}
19155714Skris
192280297Sjkim/*
193280297Sjkim * These next 2 functions from Goetz Babin-Ebell <babinebell@trustcenter.de>
19455714Skris */
19555714Skrisint ASN1_BIT_STRING_set_bit(ASN1_BIT_STRING *a, int n, int value)
196280297Sjkim{
197280297Sjkim    int w, v, iv;
198280297Sjkim    unsigned char *c;
19955714Skris
200280297Sjkim    w = n / 8;
201280297Sjkim    v = 1 << (7 - (n & 0x07));
202280297Sjkim    iv = ~v;
203280297Sjkim    if (!value)
204280297Sjkim        v = 0;
20555714Skris
206280297Sjkim    if (a == NULL)
207280297Sjkim        return 0;
208160814Ssimon
209280297Sjkim    a->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); /* clear, set on write */
21055714Skris
211280297Sjkim    if ((a->length < (w + 1)) || (a->data == NULL)) {
212280297Sjkim        if (!value)
213280297Sjkim            return (1);         /* Don't need to set */
214280297Sjkim        if (a->data == NULL)
215280297Sjkim            c = (unsigned char *)OPENSSL_malloc(w + 1);
216280297Sjkim        else
217280297Sjkim            c = (unsigned char *)OPENSSL_realloc_clean(a->data,
218280297Sjkim                                                       a->length, w + 1);
219280297Sjkim        if (c == NULL) {
220280297Sjkim            ASN1err(ASN1_F_ASN1_BIT_STRING_SET_BIT, ERR_R_MALLOC_FAILURE);
221280297Sjkim            return 0;
222280297Sjkim        }
223280297Sjkim        if (w + 1 - a->length > 0)
224280297Sjkim            memset(c + a->length, 0, w + 1 - a->length);
225280297Sjkim        a->data = c;
226280297Sjkim        a->length = w + 1;
227280297Sjkim    }
228280297Sjkim    a->data[w] = ((a->data[w]) & iv) | v;
229280297Sjkim    while ((a->length > 0) && (a->data[a->length - 1] == 0))
230280297Sjkim        a->length--;
231280297Sjkim    return (1);
232280297Sjkim}
23355714Skris
23455714Skrisint ASN1_BIT_STRING_get_bit(ASN1_BIT_STRING *a, int n)
235280297Sjkim{
236280297Sjkim    int w, v;
23755714Skris
238280297Sjkim    w = n / 8;
239280297Sjkim    v = 1 << (7 - (n & 0x07));
240280297Sjkim    if ((a == NULL) || (a->length < (w + 1)) || (a->data == NULL))
241280297Sjkim        return (0);
242280297Sjkim    return ((a->data[w] & v) != 0);
243280297Sjkim}
24455714Skris
245238405Sjkim/*
246280297Sjkim * Checks if the given bit string contains only bits specified by
247238405Sjkim * the flags vector. Returns 0 if there is at least one bit set in 'a'
248238405Sjkim * which is not specified in 'flags', 1 otherwise.
249238405Sjkim * 'len' is the length of 'flags'.
250238405Sjkim */
251238405Sjkimint ASN1_BIT_STRING_check(ASN1_BIT_STRING *a,
252280297Sjkim                          unsigned char *flags, int flags_len)
253280297Sjkim{
254280297Sjkim    int i, ok;
255280297Sjkim    /* Check if there is one bit set at all. */
256280297Sjkim    if (!a || !a->data)
257280297Sjkim        return 1;
258238405Sjkim
259280297Sjkim    /*
260280297Sjkim     * Check each byte of the internal representation of the bit string.
261280297Sjkim     */
262280297Sjkim    ok = 1;
263280297Sjkim    for (i = 0; i < a->length && ok; ++i) {
264280297Sjkim        unsigned char mask = i < flags_len ? ~flags[i] : 0xff;
265280297Sjkim        /* We are done if there is an unneeded bit set. */
266280297Sjkim        ok = (a->data[i] & mask) == 0;
267280297Sjkim    }
268280297Sjkim    return ok;
269280297Sjkim}
270