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