2N/A/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2N/A/*
2N/A * src/lib/krb5/asn.1/asn1_make.c
2N/A *
2N/A * Copyright 1994 by the Massachusetts Institute of Technology.
2N/A * All Rights Reserved.
2N/A *
2N/A * Export of this software from the United States of America may
2N/A * require a specific license from the United States Government.
2N/A * It is the responsibility of any person or organization contemplating
2N/A * export to obtain such a license before exporting.
2N/A *
2N/A * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
2N/A * distribute this software and its documentation for any purpose and
2N/A * without fee is hereby granted, provided that the above copyright
2N/A * notice appear in all copies and that both that copyright notice and
2N/A * this permission notice appear in supporting documentation, and that
2N/A * the name of M.I.T. not be used in advertising or publicity pertaining
2N/A * to distribution of the software without specific, written prior
2N/A * permission. Furthermore if you modify this software you must label
2N/A * your software as modified software and not distribute it in such a
2N/A * fashion that it might be confused with the original M.I.T. software.
2N/A * M.I.T. makes no representations about the suitability of
2N/A * this software for any purpose. It is provided "as is" without express
2N/A * or implied warranty.
2N/A */
2N/A
2N/A#include "asn1_make.h"
2N/A
2N/Aasn1_error_code
2N/Aasn1_make_etag(asn1buf *buf, asn1_class asn1class, asn1_tagnum tagnum,
2N/A unsigned int in_len, unsigned int *retlen)
2N/A{
2N/A return asn1_make_tag(buf,asn1class,CONSTRUCTED,tagnum,in_len,retlen);
2N/A}
2N/A
2N/Aasn1_error_code
2N/Aasn1_make_tag(asn1buf *buf, asn1_class asn1class,
2N/A asn1_construction construction, asn1_tagnum tagnum,
2N/A unsigned int in_len, unsigned int *retlen)
2N/A{
2N/A asn1_error_code retval;
2N/A unsigned int sumlen=0, length;
2N/A
2N/A if (tagnum > ASN1_TAGNUM_MAX) return ASN1_OVERFLOW;
2N/A
2N/A retval = asn1_make_length(buf,in_len, &length);
2N/A if (retval) return retval;
2N/A sumlen += length;
2N/A retval = asn1_make_id(buf,asn1class,construction,tagnum,&length);
2N/A if (retval) return retval;
2N/A sumlen += length;
2N/A
2N/A *retlen = sumlen;
2N/A return 0;
2N/A}
2N/A
2N/Aasn1_error_code
2N/Aasn1_make_length(asn1buf *buf, const unsigned int in_len, unsigned int *retlen)
2N/A{
2N/A asn1_error_code retval;
2N/A
2N/A if (in_len < 128) {
2N/A retval = asn1buf_insert_octet(buf, (asn1_octet)(in_len&0x7F));
2N/A if (retval) return retval;
2N/A *retlen = 1;
2N/A } else {
2N/A int in_copy=in_len, length=0;
2N/A
2N/A while (in_copy != 0) {
2N/A retval = asn1buf_insert_octet(buf, (asn1_octet)(in_copy&0xFF));
2N/A if (retval) return retval;
2N/A in_copy = in_copy >> 8;
2N/A length++;
2N/A }
2N/A retval = asn1buf_insert_octet(buf, (asn1_octet) (0x80 | (asn1_octet)(length&0x7F)));
2N/A if (retval) return retval;
2N/A length++;
2N/A *retlen = length;
2N/A }
2N/A
2N/A return 0;
2N/A}
2N/A
2N/Aasn1_error_code
2N/Aasn1_make_id(asn1buf *buf, asn1_class asn1class,
2N/A asn1_construction construction, asn1_tagnum tagnum,
2N/A unsigned int *retlen)
2N/A{
2N/A asn1_error_code retval;
2N/A
2N/A if (tagnum < 31) {
2N/A retval = asn1buf_insert_octet(buf, (asn1_octet) (asn1class | construction |
2N/A (asn1_octet)tagnum));
2N/A if (retval) return retval;
2N/A *retlen = 1;
2N/A } else {
2N/A asn1_tagnum tagcopy = tagnum;
2N/A int length = 0;
2N/A
2N/A retval = asn1buf_insert_octet(buf, (asn1_octet)(tagcopy&0x7F));
2N/A if (retval) return retval;
2N/A tagcopy >>= 7;
2N/A length++;
2N/A
2N/A for (; tagcopy != 0; tagcopy >>= 7) {
2N/A retval = asn1buf_insert_octet(buf, (asn1_octet) (0x80 | (asn1_octet)(tagcopy&0x7F)));
2N/A if (retval) return retval;
2N/A length++;
2N/A }
2N/A
2N/A retval = asn1buf_insert_octet(buf, (asn1_octet) (asn1class | construction | 0x1F));
2N/A if (retval) return retval;
2N/A length++;
2N/A *retlen = length;
2N/A }
2N/A
2N/A return 0;
2N/A}
2N/A
2N/Aasn1_error_code
2N/Aasn1_make_sequence(asn1buf *buf, const unsigned int seq_len,
2N/A unsigned int *retlen)
2N/A{
2N/A asn1_error_code retval;
2N/A unsigned int len, sum=0;
2N/A
2N/A retval = asn1_make_length(buf,seq_len,&len);
2N/A if (retval) return retval;
2N/A sum += len;
2N/A retval = asn1_make_id(buf,UNIVERSAL,CONSTRUCTED,ASN1_SEQUENCE,&len);
2N/A if (retval) return retval;
2N/A sum += len;
2N/A
2N/A *retlen = sum;
2N/A return 0;
2N/A}
2N/A
2N/Aasn1_error_code
2N/Aasn1_make_set(asn1buf *buf, const unsigned int set_len, unsigned int *retlen)
2N/A{
2N/A asn1_error_code retval;
2N/A unsigned int len, sum=0;
2N/A
2N/A retval = asn1_make_length(buf,set_len,&len);
2N/A if (retval) return retval;
2N/A sum += len;
2N/A retval = asn1_make_id(buf,UNIVERSAL,CONSTRUCTED,ASN1_SET,&len);
2N/A if (retval) return retval;
2N/A sum += len;
2N/A
2N/A *retlen = sum;
2N/A return 0;
2N/A}
2N/A
2N/Aasn1_error_code
2N/Aasn1_make_string(asn1buf *buf, const unsigned int length, const char *string,
2N/A int *retlen)
2N/A{
2N/A asn1_error_code retval;
2N/A
2N/A retval = asn1buf_insert_charstring(buf,length,string);
2N/A if (retval) return retval;
2N/A
2N/A *retlen = length;
2N/A return 0;
2N/A}