a_bool.c revision 296465
1139823Simp/* crypto/asn1/a_bool.c */
221259Swollman/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
321259Swollman * All rights reserved.
421259Swollman *
521259Swollman * This package is an SSL implementation written
621259Swollman * by Eric Young (eay@cryptsoft.com).
721259Swollman * The implementation was written so as to conform with Netscapes SSL.
821259Swollman *
921259Swollman * This library is free for commercial and non-commercial use as long as
1021259Swollman * the following conditions are aheared to.  The following conditions
1121259Swollman * apply to all code found in this distribution, be it the RC4, RSA,
1221259Swollman * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1321259Swollman * included with this distribution is covered by the same copyright terms
1421259Swollman * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1521259Swollman *
1621259Swollman * Copyright remains Eric Young's, and as such any Copyright notices in
1721259Swollman * the code are not to be removed.
1821259Swollman * If this package is used in a product, Eric Young should be given attribution
1921259Swollman * as the author of the parts of the library used.
2021259Swollman * This can be in the form of a textual message at program startup or
2121259Swollman * in documentation (online or textual) provided with the package.
2221259Swollman *
2321259Swollman * Redistribution and use in source and binary forms, with or without
2421259Swollman * modification, are permitted provided that the following conditions
2521259Swollman * are met:
2621259Swollman * 1. Redistributions of source code must retain the copyright
2721259Swollman *    notice, this list of conditions and the following disclaimer.
2821259Swollman * 2. Redistributions in binary form must reproduce the above copyright
2921259Swollman *    notice, this list of conditions and the following disclaimer in the
3050477Speter *    documentation and/or other materials provided with the distribution.
3121259Swollman * 3. All advertising materials mentioning features or use of this software
3221259Swollman *    must display the following acknowledgement:
3321259Swollman *    "This product includes cryptographic software written by
3421259Swollman *     Eric Young (eay@cryptsoft.com)"
3521259Swollman *    The word 'cryptographic' can be left out if the rouines from the library
3621259Swollman *    being used are not cryptographic related :-).
3721259Swollman * 4. If you include any Windows specific code (or a derivative thereof) from
3821259Swollman *    the apps directory (application code) you must include an acknowledgement:
3921259Swollman *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4021259Swollman *
4121259Swollman * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4221259Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4321259Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4421259Swollman * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4521259Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4621259Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4721259Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4821259Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4921259Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5021259Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51108533Sschweikh * SUCH DAMAGE.
5221259Swollman *
5321259Swollman * The licence and distribution terms for any publically available version or
5421259Swollman * derivative of this code cannot be changed.  i.e. this code cannot simply be
5521259Swollman * copied and put under another distribution licence
56108533Sschweikh * [including the GNU Public Licence.]
5721259Swollman */
5821259Swollman
5921259Swollman#include <stdio.h>
6021259Swollman#include "cryptlib.h"
61257244Sglebius#include <openssl/asn1t.h>
62257244Sglebius
6321259Swollmanint i2d_ASN1_BOOLEAN(int a, unsigned char **pp)
64142215Sglebius{
65228571Sglebius    int r;
66155051Sglebius    unsigned char *p;
67257244Sglebius
68193731Szec    r = ASN1_object_size(0, 1, V_ASN1_BOOLEAN);
69266974Smarcel    if (pp == NULL)
70270874Sglebius        return (r);
7121259Swollman    p = *pp;
7269224Sjlemon
73257244Sglebius    ASN1_put_object(&p, 0, 1, V_ASN1_BOOLEAN, V_ASN1_UNIVERSAL);
74186207Skmacy    *(p++) = (unsigned char)a;
75195699Srwatson    *pp = p;
7669224Sjlemon    return (r);
77256525Sglebius}
7874914Sjhb
79257244Sglebiusint d2i_ASN1_BOOLEAN(int *a, const unsigned char **pp, long length)
80186199Skmacy{
81196481Srwatson    int ret = -1;
82257244Sglebius    const unsigned char *p;
8369152Sjlemon    long len;
84121816Sbrooks    int inf, tag, xclass;
85121816Sbrooks    int i = 0;
86281613Sglebius
87130416Smlaier    p = *pp;
8860938Sjake    inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
8960938Sjake    if (inf & 0x80) {
9072084Sphk        i = ASN1_R_BAD_OBJECT_HEADER;
91159781Smlaier        goto err;
9221259Swollman    }
93240112Smelifaro
94240099Smelifaro    if (tag != V_ASN1_BOOLEAN) {
95240099Smelifaro        i = ASN1_R_EXPECTING_A_BOOLEAN;
96291292Sae        goto err;
97291292Sae    }
98291292Sae
99291292Sae    if (len != 1) {
100291292Sae        i = ASN1_R_BOOLEAN_IS_WRONG_LENGTH;
101291292Sae        goto err;
102291292Sae    }
103291292Sae    ret = (int)*(p++);
104240112Smelifaro    if (a != NULL)
105240099Smelifaro        (*a) = ret;
106270870Sglebius    *pp = p;
107272244Sglebius    return (ret);
108270870Sglebius err:
109270870Sglebius    ASN1err(ASN1_F_D2I_ASN1_BOOLEAN, i);
110270870Sglebius    return (ret);
111270870Sglebius}
112270870Sglebius