1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* tests/asn.1/utility.c */
3 /*
4  * Copyright (C) 1994 by the Massachusetts Institute of Technology.
5  * All rights reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  */
26 
27 #include "utility.h"
28 #include "krb5.h"
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <ctype.h>
32 
33 krb5int_access acc;
34 
35 char hexchar (const unsigned int digit);
36 
37 void *
ealloc(size_t size)38 ealloc(size_t size)
39 {
40     void *ptr = calloc(1, size);
41 
42     if (ptr == NULL)
43         abort();
44     return ptr;
45 }
46 
47 char *
estrdup(const char * str)48 estrdup(const char *str)
49 {
50     char *newstr = strdup(str);
51 
52     if (newstr == NULL)
53         abort();
54     return newstr;
55 }
56 
57 void
asn1_krb5_data_unparse(const krb5_data * code,char ** s)58 asn1_krb5_data_unparse(const krb5_data *code, char **s)
59 {
60     if (*s != NULL) free(*s);
61 
62     if (code==NULL) {
63         *s = estrdup("<NULL>");
64     } else if (code->data == NULL || ((int) code->length) <= 0) {
65         *s = estrdup("<EMPTY>");
66     } else {
67         unsigned int i;
68 
69         *s = ealloc(3 * code->length);
70         for (i = 0; i < code->length; i++) {
71             (*s)[3*i] = hexchar((unsigned char) (((code->data)[i]&0xF0)>>4));
72             (*s)[3*i+1] = hexchar((unsigned char) ((code->data)[i]&0x0F));
73             (*s)[3*i+2] = ' ';
74         }
75         (*s)[3*(code->length)-1] = '\0';
76     }
77 }
78 
79 char
hexchar(const unsigned int digit)80 hexchar(const unsigned int digit)
81 {
82     if (digit<=9)
83         return '0'+digit;
84     else if (digit<=15)
85         return 'A'+digit-10;
86     else
87         return 'X';
88 }
89 
90 void
krb5_data_parse(krb5_data * d,const char * s)91 krb5_data_parse(krb5_data *d, const char *s)
92 {
93     d->length = strlen(s);
94     d->data = ealloc(d->length);
95     memcpy(d->data, s, d->length);
96 }
97 
98 krb5_error_code
krb5_data_hex_parse(krb5_data * d,const char * s)99 krb5_data_hex_parse(krb5_data *d, const char *s)
100 {
101     int lo;
102     long v;
103     const char *cp;
104     char *dp;
105     char buf[2];
106 
107     d->data = ealloc(strlen(s) / 2 + 1);
108     d->length = 0;
109     buf[1] = '\0';
110     for (lo = 0, dp = d->data, cp = s; *cp; cp++) {
111         if (*cp < 0)
112             return ASN1_PARSE_ERROR;
113         else if (isspace((unsigned char) *cp))
114             continue;
115         else if (isxdigit((unsigned char) *cp)) {
116             buf[0] = *cp;
117             v = strtol(buf, NULL, 16);
118         } else
119             return ASN1_PARSE_ERROR;
120         if (lo) {
121             *dp++ |= v;
122             lo = 0;
123         } else {
124             *dp = v << 4;
125             lo = 1;
126         }
127     }
128 
129     d->length = dp - d->data;
130     return 0;
131 }
132 
133 void
init_access(const char * progname)134 init_access(const char *progname)
135 {
136     krb5_error_code ret;
137     ret = krb5int_accessor(&acc, KRB5INT_ACCESS_VERSION);
138     if (ret) {
139         com_err(progname, ret, "while initializing accessor");
140         exit(1);
141     }
142 }
143