1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * btohex.c - Binary to Hexadecimal string conversion. 30 * 31 * These routines convert binary labels into canonical 32 * hexadecimal representations of the binary form. 33 */ 34 35 #include <stdlib.h> 36 #include <strings.h> 37 #include <tsol/label.h> 38 #include <sys/tsol/label_macro.h> 39 40 /* 0x + Classification + '-' + ll + '-' + Compartments + end of string */ 41 #define _HEX_SIZE 2+(sizeof (Classification_t)*2)+4+\ 42 (sizeof (Compartments_t)*2)+1 43 44 static char hex_buf[_HEX_SIZE]; 45 46 /* 47 * h_alloc - Allocate data storage for a Hexadecimal label string. 48 * 49 * Entry id = Type of label to allocate storage for. 50 * SUN_SL_ID - Sensitivity Label. 51 * SUN_CLR_ID - Clearance. 52 * 53 * Returns NULL, If unable to allocate storage. 54 * Address of buffer. 55 * 56 * Calls malloc; 57 */ 58 59 char * 60 h_alloc(unsigned char id) 61 { 62 size_t size; 63 64 switch (id) { 65 66 case SUN_SL_ID: 67 size = _HEX_SIZE; 68 break; 69 70 case SUN_CLR_ID: 71 size = _HEX_SIZE; 72 break; 73 74 default: 75 return (NULL); 76 } 77 78 return ((char *)malloc(size)); 79 } 80 81 82 /* 83 * h_free - Free a Hexadecimal label string. 84 * 85 * Entry hex = Hexadecimal label string. 86 * 87 * Returns none. 88 * 89 * Calls free. 90 */ 91 92 void 93 h_free(char *hex) 94 { 95 96 if (hex == NULL) 97 return; 98 99 free(hex); 100 } 101 102 103 /* 104 * bsltoh_r - Convert a Sensitivity Label into a Hexadecimal label string. 105 * 106 * Entry label = Sensitivity Label to be translated. 107 * hex = Buffer to place converted label. 108 * len = Length of buffer. 109 * 110 * Returns NULL, If invalid label type. 111 * Address of buffer. 112 * 113 * Calls label_to_str, strncpy. 114 */ 115 116 char * 117 bsltoh_r(const m_label_t *label, char *hex) 118 { 119 char *h; 120 121 if (label_to_str(label, &h, M_INTERNAL, DEF_NAMES) != 0) { 122 free(h); 123 return (NULL); 124 } 125 126 (void) strncpy(hex, (const char *)h, _HEX_SIZE); 127 free(h); 128 return (hex); 129 } 130 131 132 /* 133 * bsltoh - Convert a Sensitivity Label into a Hexadecimal label string. 134 * 135 * Entry label = Sensitivity Label to be translated. 136 * 137 * Returns NULL, If invalid label type. 138 * Address of statically allocated hex label string. 139 * 140 * Calls bsltoh_r. 141 * 142 * Uses hex_buf. 143 */ 144 145 char * 146 bsltoh(const m_label_t *label) 147 { 148 149 return (bsltoh_r(label, hex_buf)); 150 } 151 152 153 /* 154 * bcleartoh_r - Convert a Clearance into a Hexadecimal label string. 155 * 156 * Entry clearance = Clearance to be translated. 157 * hex = Buffer to place converted label. 158 * len = Length of buffer. 159 * 160 * Returns NULL, If invalid label type. 161 * Address of buffer. 162 * 163 * Calls label_to_str, strncpy. 164 */ 165 166 char * 167 bcleartoh_r(const m_label_t *clearance, char *hex) 168 { 169 char *h; 170 171 if (label_to_str(clearance, &h, M_INTERNAL, DEF_NAMES) != 0) { 172 free(h); 173 return (NULL); 174 } 175 176 (void) strncpy(hex, (const char *)h, _HEX_SIZE); 177 free(h); 178 return (hex); 179 } 180 181 182 /* 183 * bcleartoh - Convert a Clearance into a Hexadecimal label string. 184 * 185 * Entry clearance = Clearance to be translated. 186 * 187 * Returns NULL, If invalid label type. 188 * Address of statically allocated hex label string. 189 * 190 * Calls bcleartoh_r. 191 * 192 * Uses hex_buf. 193 */ 194 195 char * 196 bcleartoh(const m_label_t *clearance) 197 { 198 199 return (bcleartoh_r(clearance, hex_buf)); 200 } 201