1*7c478bd9Sstevel@tonic-gate# 2*7c478bd9Sstevel@tonic-gate# CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate# 4*7c478bd9Sstevel@tonic-gate# The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate# Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate# (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate# with the License. 8*7c478bd9Sstevel@tonic-gate# 9*7c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate# and limitations under the License. 13*7c478bd9Sstevel@tonic-gate# 14*7c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate# 20*7c478bd9Sstevel@tonic-gate# CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate# 22*7c478bd9Sstevel@tonic-gate# Copyright 2004 Sun Microsystems, Inc. All rights reserved. 23*7c478bd9Sstevel@tonic-gate# Use is subject to license terms. 24*7c478bd9Sstevel@tonic-gate# 25*7c478bd9Sstevel@tonic-gate# ident "%Z%%M% %I% %E% SMI" 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gateThis is an internal library for use only by: 28*7c478bd9Sstevel@tonic-gate usr/src/cmd/cmd-crypto 29*7c478bd9Sstevel@tonic-gate usr/src/lib/pkcs11 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gateThe library and the header file are installed into the proto area but 32*7c478bd9Sstevel@tonic-gateare not included in any pacakges. 33*7c478bd9Sstevel@tonic-gate 34*7c478bd9Sstevel@tonic-gate 35*7c478bd9Sstevel@tonic-gate libcryptoutil Design 36*7c478bd9Sstevel@tonic-gate 37*7c478bd9Sstevel@tonic-gate1. Introduction 38*7c478bd9Sstevel@tonic-gate 39*7c478bd9Sstevel@tonic-gateThere are a number of common code components and general utility functions 40*7c478bd9Sstevel@tonic-gateneeded that are shared by various userland parts of the crypto framework. 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gateThe originally approved ARC materials (PSARC/2001/488 & PSARC/2001/553) 43*7c478bd9Sstevel@tonic-gatedidn't have a library that was included by all user land libraries, 44*7c478bd9Sstevel@tonic-gateplugins and commands. 45*7c478bd9Sstevel@tonic-gate 46*7c478bd9Sstevel@tonic-gateThe solution to this is to follow what other project teams have done in the 47*7c478bd9Sstevel@tonic-gatepast and create a project private util library. 48*7c478bd9Sstevel@tonic-gate 49*7c478bd9Sstevel@tonic-gate2. Contents 50*7c478bd9Sstevel@tonic-gate 51*7c478bd9Sstevel@tonic-gateAny code that is generic enough to be shared by multiple parts of the 52*7c478bd9Sstevel@tonic-gateuser crypto framework is eligible. 53*7c478bd9Sstevel@tonic-gate 54*7c478bd9Sstevel@tonic-gateThe current contents are: 55*7c478bd9Sstevel@tonic-gate 56*7c478bd9Sstevel@tonic-gate2.1 Error & Debug Functions 57*7c478bd9Sstevel@tonic-gate 58*7c478bd9Sstevel@tonic-gate cryptodebug_init(), 59*7c478bd9Sstevel@tonic-gate cryptodebug() 60*7c478bd9Sstevel@tonic-gate cryptoerror() 61*7c478bd9Sstevel@tonic-gate 62*7c478bd9Sstevel@tonic-gateThese functions log debug or error information to stderr and/or 63*7c478bd9Sstevel@tonic-gatesyslog or a file. Debug is off by default but the code is always 64*7c478bd9Sstevel@tonic-gatecompiled in. 65*7c478bd9Sstevel@tonic-gate 66*7c478bd9Sstevel@tonic-gateThe cryptodebug_init() routine allows the caller to set a message 67*7c478bd9Sstevel@tonic-gateprefix for error and debug output. 68*7c478bd9Sstevel@tonic-gate 69*7c478bd9Sstevel@tonic-gateThe environment variable SUNW_CRYPTO_DEBUG determines wither or not 70*7c478bd9Sstevel@tonic-gatedebug output is generated at run time, valid values are "syslog" or "stderr" 71*7c478bd9Sstevel@tonic-gate 72*7c478bd9Sstevel@tonic-gateFor example elfsign(1) could do: 73*7c478bd9Sstevel@tonic-gate 74*7c478bd9Sstevel@tonic-gate cryptodebug_init("elfsign"); 75*7c478bd9Sstevel@tonic-gate 76*7c478bd9Sstevel@tonic-gateand later: 77*7c478bd9Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("invalid number of arguments")); 78*7c478bd9Sstevel@tonic-gate 79*7c478bd9Sstevel@tonic-gateThis would cause an error message on stderr thus: 80*7c478bd9Sstevel@tonic-gate 81*7c478bd9Sstevel@tonic-gate "elfsign: invalid number of arguments" 82*7c478bd9Sstevel@tonic-gate 83*7c478bd9Sstevel@tonic-gateThe first argument to cryptoerror is either LOG_STDERR or a syslog(3c) 84*7c478bd9Sstevel@tonic-gatepriority. All messages include the PID and are logged at LOG_USER. 85*7c478bd9Sstevel@tonic-gate 86*7c478bd9Sstevel@tonic-gatefor debug output: 87*7c478bd9Sstevel@tonic-gate 88*7c478bd9Sstevel@tonic-gate cryptodebug("scmd=request opts=%s", opts); 89*7c478bd9Sstevel@tonic-gate 90*7c478bd9Sstevel@tonic-gateThis would go to the location defined by $SUNW_CRYPTO_DEBUG, ie 91*7c478bd9Sstevel@tonic-gatesyslog, stderr or not be generated at all. 92*7c478bd9Sstevel@tonic-gate 93*7c478bd9Sstevel@tonic-gate2.2 PKCS#11 Mechanism Type to and from Strings 94*7c478bd9Sstevel@tonic-gate 95*7c478bd9Sstevel@tonic-gate pkcs11_mech2str() and pkcs11_str2mech() 96*7c478bd9Sstevel@tonic-gate 97*7c478bd9Sstevel@tonic-gateThese functions use a table built at compile time from the contents of 98*7c478bd9Sstevel@tonic-gatethe pkcs11t.h file to map mechanism numbers to the corresponding string 99*7c478bd9Sstevel@tonic-gatevalue. 100*7c478bd9Sstevel@tonic-gate 101*7c478bd9Sstevel@tonic-gatepkcs11_mech2str() returns a pointer to a string that should be free(3c)'d 102*7c478bd9Sstevel@tonic-gateby the caller. 103*7c478bd9Sstevel@tonic-gate 104*7c478bd9Sstevel@tonic-gateConsumers: 105*7c478bd9Sstevel@tonic-gate 106*7c478bd9Sstevel@tonic-gate digest(1), mac(1), encrypt(1), decrypt(1) for translating 107*7c478bd9Sstevel@tonic-gate command line args to mech numbers. They will need to 108*7c478bd9Sstevel@tonic-gate add the "CKM_" prefix before calling pkc11_str2mech() 109*7c478bd9Sstevel@tonic-gate 110*7c478bd9Sstevel@tonic-gate cryptoadm(1m) for output to user, and for storing in pkcs11.conf 111*7c478bd9Sstevel@tonic-gate file. 112*7c478bd9Sstevel@tonic-gate 113*7c478bd9Sstevel@tonic-gate Debug code. 114*7c478bd9Sstevel@tonic-gate 115*7c478bd9Sstevel@tonic-gate2.3 The "pkcs11.conf" configuration file Parsing code. 116*7c478bd9Sstevel@tonic-gate 117*7c478bd9Sstevel@tonic-gateThe "pkcs11.conf" configuration file parsing code and data structures are 118*7c478bd9Sstevel@tonic-gateshared between: 119*7c478bd9Sstevel@tonic-gate cryptoadm(1m), libpkcs11(3crypto). 120*7c478bd9Sstevel@tonic-gate 121*7c478bd9Sstevel@tonic-gate2.3.1 Data Structures: 122*7c478bd9Sstevel@tonic-gate 123*7c478bd9Sstevel@tonic-gate #define MECH_ID_HEX_LEN 11 /* length of mechanism id in hex form */ 124*7c478bd9Sstevel@tonic-gate 125*7c478bd9Sstevel@tonic-gate typedef char libname_t[MAXPATHLEN]; 126*7c478bd9Sstevel@tonic-gate typedef char midstr_t[MECH_ID_HEX_LEN]; 127*7c478bd9Sstevel@tonic-gate 128*7c478bd9Sstevel@tonic-gate /* The policy list for an entry in the config file */ 129*7c478bd9Sstevel@tonic-gate typedef struct umechlist { 130*7c478bd9Sstevel@tonic-gate midstr_t name; 131*7c478bd9Sstevel@tonic-gate struct umechlist *next; 132*7c478bd9Sstevel@tonic-gate } umechlist_t; 133*7c478bd9Sstevel@tonic-gate 134*7c478bd9Sstevel@tonic-gate /* An entry in the pkcs11.conf file */ 135*7c478bd9Sstevel@tonic-gate typedef struct uentry { 136*7c478bd9Sstevel@tonic-gate libname_t name; 137*7c478bd9Sstevel@tonic-gate boolean_t flag_enabledlist; /* TRUE if an enabledlist */ 138*7c478bd9Sstevel@tonic-gate umechlist_t *policylist; /* disabledlist or enabledlist */ 139*7c478bd9Sstevel@tonic-gate int count; 140*7c478bd9Sstevel@tonic-gate } uentry_t; 141*7c478bd9Sstevel@tonic-gate 142*7c478bd9Sstevel@tonic-gate /* The entry list for the entire pkcs11.conf file */ 143*7c478bd9Sstevel@tonic-gate typedef struct uentrylist { 144*7c478bd9Sstevel@tonic-gate uentry_t *pent; 145*7c478bd9Sstevel@tonic-gate struct uentrylist *next; 146*7c478bd9Sstevel@tonic-gate } uentrylist_t; 147*7c478bd9Sstevel@tonic-gate 148*7c478bd9Sstevel@tonic-gate 149*7c478bd9Sstevel@tonic-gate2.3.2 Functions: 150*7c478bd9Sstevel@tonic-gate 151*7c478bd9Sstevel@tonic-gateextern int get_pkcs11conf_info(uentrylist_t **ppliblist); 152*7c478bd9Sstevel@tonic-gate$ 153*7c478bd9Sstevel@tonic-gate Retrieve the user-level provider info from the pkcs11.conf file. 154*7c478bd9Sstevel@tonic-gate If successful, the result is returned from the ppliblist argument. 155*7c478bd9Sstevel@tonic-gate This function returns SUCCESS if successfully done; otherwise it returns 156*7c478bd9Sstevel@tonic-gate FAILURE. The caller should use free_uentrylist() to free the space 157*7c478bd9Sstevel@tonic-gate allocated for "ppliblist". 158*7c478bd9Sstevel@tonic-gate 159*7c478bd9Sstevel@tonic-gateextern umechlist_t *create_umech(char *mechname); 160*7c478bd9Sstevel@tonic-gate 161*7c478bd9Sstevel@tonic-gate Create one item of type umechlist_t with the mechanism name in hex form. 162*7c478bd9Sstevel@tonic-gate A NULL is returned when the input name is NULL or the heap memory is 163*7c478bd9Sstevel@tonic-gate insufficient. The Caller should use free_umechlist() to free the space 164*7c478bd9Sstevel@tonic-gate allocated for the returning data. 165*7c478bd9Sstevel@tonic-gate 166*7c478bd9Sstevel@tonic-gateextern void free_uentrylist(uentrylist_t *ptr); 167*7c478bd9Sstevel@tonic-gate 168*7c478bd9Sstevel@tonic-gate Free space allocated for an pointer to the struct "uentrylist_t". 169*7c478bd9Sstevel@tonic-gate 170*7c478bd9Sstevel@tonic-gateextern void free_uentry(uentry_t *ptr); 171*7c478bd9Sstevel@tonic-gate 172*7c478bd9Sstevel@tonic-gate Free space allocated for an pointer to the struct "uentry_t". 173*7c478bd9Sstevel@tonic-gate 174*7c478bd9Sstevel@tonic-gateextern void free_umechlist(umechlist_t *ptr); 175*7c478bd9Sstevel@tonic-gate 176*7c478bd9Sstevel@tonic-gate Free space allocated for an pointer to the struct "umechlist_t". 177*7c478bd9Sstevel@tonic-gate 178*7c478bd9Sstevel@tonic-gate2.4 PKCS#11 Mechanism Type to key type 179*7c478bd9Sstevel@tonic-gate 180*7c478bd9Sstevel@tonic-gate pkcs11_mech2keytype() 181*7c478bd9Sstevel@tonic-gate 182*7c478bd9Sstevel@tonic-gateThis function is used to get the key type for a mechanism. 183*7c478bd9Sstevel@tonic-gate 184*7c478bd9Sstevel@tonic-gateConsumers: 185*7c478bd9Sstevel@tonic-gate 186*7c478bd9Sstevel@tonic-gate encrypt(1), decrypt(1), and libpkcs11(3crypto) for getting 187*7c478bd9Sstevel@tonic-gate the key type when creating an object for use with a 188*7c478bd9Sstevel@tonic-gate specific mechanism. 189*7c478bd9Sstevel@tonic-gate 190*7c478bd9Sstevel@tonic-gate2.5 PKCS#11 return code to string 191*7c478bd9Sstevel@tonic-gate 192*7c478bd9Sstevel@tonic-gate pkcs11_strerror() 193*7c478bd9Sstevel@tonic-gate 194*7c478bd9Sstevel@tonic-gateThis function returnes a string representation of any given PKCS11 return 195*7c478bd9Sstevel@tonic-gatecode. 196*7c478bd9Sstevel@tonic-gate 197*7c478bd9Sstevel@tonic-gateConsumer: 198*7c478bd9Sstevel@tonic-gate 199*7c478bd9Sstevel@tonic-gate encrypt(1) and decrypt(1) uses this function for reporting errors. 200*7c478bd9Sstevel@tonic-gate 201*7c478bd9Sstevel@tonic-gate3. Non-Contents 202*7c478bd9Sstevel@tonic-gate 203*7c478bd9Sstevel@tonic-gateCode for cryptographic algorithms does not belong in here. That 204*7c478bd9Sstevel@tonic-gatecomes from usr/src/common/<algorithm> since it is shared between user and 205*7c478bd9Sstevel@tonic-gatekernel. 206*7c478bd9Sstevel@tonic-gate 207*7c478bd9Sstevel@tonic-gatePKCS#11 header files although they are common to various parts of the 208*7c478bd9Sstevel@tonic-gateuser land framework come from usr/src/pkcs11/include 209*7c478bd9Sstevel@tonic-gate 210*7c478bd9Sstevel@tonic-gate4. Interface Taxonomy 211*7c478bd9Sstevel@tonic-gate 212*7c478bd9Sstevel@tonic-gateEverything in this library is Project Private or Internal. The 213*7c478bd9Sstevel@tonic-gateexported symbols will all be marked as SUNWprivate_1.0 in the library 214*7c478bd9Sstevel@tonic-gatespec file. 215*7c478bd9Sstevel@tonic-gate 216*7c478bd9Sstevel@tonic-gate5. Static vs Dynamic 217*7c478bd9Sstevel@tonic-gate 218*7c478bd9Sstevel@tonic-gateThe initial design was to only use a static archive library to avoid 219*7c478bd9Sstevel@tonic-gateexposing a new interface (even though it is all private). However while 220*7c478bd9Sstevel@tonic-gatethis is fine for initial delivery it creates difficulties later with 221*7c478bd9Sstevel@tonic-gatepatching. As such a Dynamic version will be build. 222*7c478bd9Sstevel@tonic-gate 223*7c478bd9Sstevel@tonic-gateLibraries for lint and header files will not be shipped in any Sun packages 224*7c478bd9Sstevel@tonic-gatesince this is all Project Private. Similarly the abi_ file will not be 225*7c478bd9Sstevel@tonic-gateshipped even though a spec file will be used in the source gate. 226*7c478bd9Sstevel@tonic-gate 227*7c478bd9Sstevel@tonic-gate6. Library location 228*7c478bd9Sstevel@tonic-gate 229*7c478bd9Sstevel@tonic-gateAt present all of the consumers of the library are in /usr/ so the 230*7c478bd9Sstevel@tonic-gatelibrary is /usr/lib/{sparcv9}/libcryptoutil.so.1. If kcfd ever moves 231*7c478bd9Sstevel@tonic-gateto /lib/crypto/kcf as a result of PSARC/2002/117 allowing it, then 232*7c478bd9Sstevel@tonic-gatelibcryptoutil needs to move as well. 233