10Sstevel@tonic-gate# 20Sstevel@tonic-gate# CDDL HEADER START 30Sstevel@tonic-gate# 40Sstevel@tonic-gate# The contents of this file are subject to the terms of the 5*12575SJan.Pechanec@Sun.COM# Common Development and Distribution License (the "License"). 6*12575SJan.Pechanec@Sun.COM# You may not use this file except in compliance with the License. 70Sstevel@tonic-gate# 80Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate# See the License for the specific language governing permissions 110Sstevel@tonic-gate# and limitations under the License. 120Sstevel@tonic-gate# 130Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate# 190Sstevel@tonic-gate# CDDL HEADER END 200Sstevel@tonic-gate# 21*12575SJan.Pechanec@Sun.COM# Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 220Sstevel@tonic-gate# 230Sstevel@tonic-gate 240Sstevel@tonic-gateThis is an internal library for use only by: 250Sstevel@tonic-gate usr/src/cmd/cmd-crypto 260Sstevel@tonic-gate usr/src/lib/pkcs11 27*12575SJan.Pechanec@Sun.COM usr/src/lib/libkmf 280Sstevel@tonic-gate 290Sstevel@tonic-gateThe library and the header file are installed into the proto area but 300Sstevel@tonic-gateare not included in any pacakges. 310Sstevel@tonic-gate 320Sstevel@tonic-gate 330Sstevel@tonic-gate libcryptoutil Design 340Sstevel@tonic-gate 350Sstevel@tonic-gate1. Introduction 360Sstevel@tonic-gate 370Sstevel@tonic-gateThere are a number of common code components and general utility functions 380Sstevel@tonic-gateneeded that are shared by various userland parts of the crypto framework. 390Sstevel@tonic-gate 400Sstevel@tonic-gateThe originally approved ARC materials (PSARC/2001/488 & PSARC/2001/553) 410Sstevel@tonic-gatedidn't have a library that was included by all user land libraries, 420Sstevel@tonic-gateplugins and commands. 430Sstevel@tonic-gate 440Sstevel@tonic-gateThe solution to this is to follow what other project teams have done in the 450Sstevel@tonic-gatepast and create a project private util library. 460Sstevel@tonic-gate 470Sstevel@tonic-gate2. Contents 480Sstevel@tonic-gate 490Sstevel@tonic-gateAny code that is generic enough to be shared by multiple parts of the 500Sstevel@tonic-gateuser crypto framework is eligible. 510Sstevel@tonic-gate 520Sstevel@tonic-gateThe current contents are: 530Sstevel@tonic-gate 540Sstevel@tonic-gate2.1 Error & Debug Functions 550Sstevel@tonic-gate 560Sstevel@tonic-gate cryptodebug_init(), 570Sstevel@tonic-gate cryptodebug() 580Sstevel@tonic-gate cryptoerror() 590Sstevel@tonic-gate 600Sstevel@tonic-gateThese functions log debug or error information to stderr and/or 610Sstevel@tonic-gatesyslog or a file. Debug is off by default but the code is always 620Sstevel@tonic-gatecompiled in. 630Sstevel@tonic-gate 640Sstevel@tonic-gateThe cryptodebug_init() routine allows the caller to set a message 650Sstevel@tonic-gateprefix for error and debug output. 660Sstevel@tonic-gate 670Sstevel@tonic-gateThe environment variable SUNW_CRYPTO_DEBUG determines wither or not 680Sstevel@tonic-gatedebug output is generated at run time, valid values are "syslog" or "stderr" 690Sstevel@tonic-gate 700Sstevel@tonic-gateFor example elfsign(1) could do: 710Sstevel@tonic-gate 720Sstevel@tonic-gate cryptodebug_init("elfsign"); 730Sstevel@tonic-gate 740Sstevel@tonic-gateand later: 750Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("invalid number of arguments")); 760Sstevel@tonic-gate 770Sstevel@tonic-gateThis would cause an error message on stderr thus: 780Sstevel@tonic-gate 790Sstevel@tonic-gate "elfsign: invalid number of arguments" 800Sstevel@tonic-gate 810Sstevel@tonic-gateThe first argument to cryptoerror is either LOG_STDERR or a syslog(3c) 820Sstevel@tonic-gatepriority. All messages include the PID and are logged at LOG_USER. 830Sstevel@tonic-gate 840Sstevel@tonic-gatefor debug output: 850Sstevel@tonic-gate 860Sstevel@tonic-gate cryptodebug("scmd=request opts=%s", opts); 870Sstevel@tonic-gate 880Sstevel@tonic-gateThis would go to the location defined by $SUNW_CRYPTO_DEBUG, ie 890Sstevel@tonic-gatesyslog, stderr or not be generated at all. 900Sstevel@tonic-gate 910Sstevel@tonic-gate2.2 PKCS#11 Mechanism Type to and from Strings 920Sstevel@tonic-gate 930Sstevel@tonic-gate pkcs11_mech2str() and pkcs11_str2mech() 940Sstevel@tonic-gate 950Sstevel@tonic-gateThese functions use a table built at compile time from the contents of 960Sstevel@tonic-gatethe pkcs11t.h file to map mechanism numbers to the corresponding string 970Sstevel@tonic-gatevalue. 980Sstevel@tonic-gate 990Sstevel@tonic-gatepkcs11_mech2str() returns a pointer to a string that should be free(3c)'d 1000Sstevel@tonic-gateby the caller. 1010Sstevel@tonic-gate 1020Sstevel@tonic-gateConsumers: 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate digest(1), mac(1), encrypt(1), decrypt(1) for translating 1050Sstevel@tonic-gate command line args to mech numbers. They will need to 1060Sstevel@tonic-gate add the "CKM_" prefix before calling pkc11_str2mech() 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate cryptoadm(1m) for output to user, and for storing in pkcs11.conf 1090Sstevel@tonic-gate file. 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate Debug code. 1120Sstevel@tonic-gate 1130Sstevel@tonic-gate2.3 The "pkcs11.conf" configuration file Parsing code. 1140Sstevel@tonic-gate 1150Sstevel@tonic-gateThe "pkcs11.conf" configuration file parsing code and data structures are 1160Sstevel@tonic-gateshared between: 1170Sstevel@tonic-gate cryptoadm(1m), libpkcs11(3crypto). 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate2.3.1 Data Structures: 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate #define MECH_ID_HEX_LEN 11 /* length of mechanism id in hex form */ 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate typedef char libname_t[MAXPATHLEN]; 1240Sstevel@tonic-gate typedef char midstr_t[MECH_ID_HEX_LEN]; 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate /* The policy list for an entry in the config file */ 1270Sstevel@tonic-gate typedef struct umechlist { 1280Sstevel@tonic-gate midstr_t name; 1290Sstevel@tonic-gate struct umechlist *next; 1300Sstevel@tonic-gate } umechlist_t; 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate /* An entry in the pkcs11.conf file */ 1330Sstevel@tonic-gate typedef struct uentry { 1340Sstevel@tonic-gate libname_t name; 1350Sstevel@tonic-gate boolean_t flag_enabledlist; /* TRUE if an enabledlist */ 1360Sstevel@tonic-gate umechlist_t *policylist; /* disabledlist or enabledlist */ 1370Sstevel@tonic-gate int count; 1380Sstevel@tonic-gate } uentry_t; 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate /* The entry list for the entire pkcs11.conf file */ 1410Sstevel@tonic-gate typedef struct uentrylist { 1420Sstevel@tonic-gate uentry_t *pent; 1430Sstevel@tonic-gate struct uentrylist *next; 1440Sstevel@tonic-gate } uentrylist_t; 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate 1470Sstevel@tonic-gate2.3.2 Functions: 1480Sstevel@tonic-gate 1490Sstevel@tonic-gateextern int get_pkcs11conf_info(uentrylist_t **ppliblist); 1500Sstevel@tonic-gate$ 1510Sstevel@tonic-gate Retrieve the user-level provider info from the pkcs11.conf file. 1520Sstevel@tonic-gate If successful, the result is returned from the ppliblist argument. 1530Sstevel@tonic-gate This function returns SUCCESS if successfully done; otherwise it returns 1540Sstevel@tonic-gate FAILURE. The caller should use free_uentrylist() to free the space 1550Sstevel@tonic-gate allocated for "ppliblist". 1560Sstevel@tonic-gate 1570Sstevel@tonic-gateextern umechlist_t *create_umech(char *mechname); 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate Create one item of type umechlist_t with the mechanism name in hex form. 1600Sstevel@tonic-gate A NULL is returned when the input name is NULL or the heap memory is 1610Sstevel@tonic-gate insufficient. The Caller should use free_umechlist() to free the space 1620Sstevel@tonic-gate allocated for the returning data. 1630Sstevel@tonic-gate 1640Sstevel@tonic-gateextern void free_uentrylist(uentrylist_t *ptr); 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate Free space allocated for an pointer to the struct "uentrylist_t". 1670Sstevel@tonic-gate 1680Sstevel@tonic-gateextern void free_uentry(uentry_t *ptr); 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate Free space allocated for an pointer to the struct "uentry_t". 1710Sstevel@tonic-gate 1720Sstevel@tonic-gateextern void free_umechlist(umechlist_t *ptr); 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate Free space allocated for an pointer to the struct "umechlist_t". 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate2.4 PKCS#11 Mechanism Type to key type 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate pkcs11_mech2keytype() 1790Sstevel@tonic-gate 1800Sstevel@tonic-gateThis function is used to get the key type for a mechanism. 1810Sstevel@tonic-gate 1820Sstevel@tonic-gateConsumers: 1830Sstevel@tonic-gate 1840Sstevel@tonic-gate encrypt(1), decrypt(1), and libpkcs11(3crypto) for getting 1850Sstevel@tonic-gate the key type when creating an object for use with a 1860Sstevel@tonic-gate specific mechanism. 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate2.5 PKCS#11 return code to string 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate pkcs11_strerror() 1910Sstevel@tonic-gate 192*12575SJan.Pechanec@Sun.COMThis function returns a string representation of any given PKCS11 return 1930Sstevel@tonic-gatecode. 1940Sstevel@tonic-gate 1950Sstevel@tonic-gateConsumer: 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate encrypt(1) and decrypt(1) uses this function for reporting errors. 1980Sstevel@tonic-gate 199*12575SJan.Pechanec@Sun.COM2.5 PKCS#11 URI parsing code 200*12575SJan.Pechanec@Sun.COM 201*12575SJan.Pechanec@Sun.COM pkcs11_parse_uri() 202*12575SJan.Pechanec@Sun.COM pkcs11_free_uri() 203*12575SJan.Pechanec@Sun.COM 204*12575SJan.Pechanec@Sun.COMThis function parses a PKCS#11 URI and fills up a pkcs11_uri_t structure. It 205*12575SJan.Pechanec@Sun.COMalso reads the PIN if the PKCS#11 URI specifies a passphrase dialog. The 206*12575SJan.Pechanec@Sun.COMpkcs11_uri_t is described in cryptoutil.h, explanation of the return codes for 207*12575SJan.Pechanec@Sun.COMthe pkcs11_parse_uri() function is in the function's comment in pk11_uri.c. The 208*12575SJan.Pechanec@Sun.COMpkcs11_parse_uri() function allocates the URI's fields and the caller is 209*12575SJan.Pechanec@Sun.COMresponsible for calling pkcs11_free_uri() after it's done with the URI 210*12575SJan.Pechanec@Sun.COMstructure. 211*12575SJan.Pechanec@Sun.COM 212*12575SJan.Pechanec@Sun.COMConsumer: 213*12575SJan.Pechanec@Sun.COM 214*12575SJan.Pechanec@Sun.COM SunSSH will use the functions for parsing PKCS#11 URIs. 215*12575SJan.Pechanec@Sun.COM 2160Sstevel@tonic-gate3. Non-Contents 2170Sstevel@tonic-gate 2180Sstevel@tonic-gateCode for cryptographic algorithms does not belong in here. That 2190Sstevel@tonic-gatecomes from usr/src/common/<algorithm> since it is shared between user and 2200Sstevel@tonic-gatekernel. 2210Sstevel@tonic-gate 2220Sstevel@tonic-gatePKCS#11 header files although they are common to various parts of the 2230Sstevel@tonic-gateuser land framework come from usr/src/pkcs11/include 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate4. Interface Taxonomy 2260Sstevel@tonic-gate 2270Sstevel@tonic-gateEverything in this library is Project Private or Internal. The 2280Sstevel@tonic-gateexported symbols will all be marked as SUNWprivate_1.0 in the library 2290Sstevel@tonic-gatespec file. 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate5. Static vs Dynamic 2320Sstevel@tonic-gate 2330Sstevel@tonic-gateThe initial design was to only use a static archive library to avoid 2340Sstevel@tonic-gateexposing a new interface (even though it is all private). However while 2350Sstevel@tonic-gatethis is fine for initial delivery it creates difficulties later with 2360Sstevel@tonic-gatepatching. As such a Dynamic version will be build. 2370Sstevel@tonic-gate 2380Sstevel@tonic-gateLibraries for lint and header files will not be shipped in any Sun packages 2390Sstevel@tonic-gatesince this is all Project Private. Similarly the abi_ file will not be 2400Sstevel@tonic-gateshipped even though a spec file will be used in the source gate. 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate6. Library location 2430Sstevel@tonic-gate 2440Sstevel@tonic-gateAt present all of the consumers of the library are in /usr/ so the 2450Sstevel@tonic-gatelibrary is /usr/lib/{sparcv9}/libcryptoutil.so.1. If kcfd ever moves 2460Sstevel@tonic-gateto /lib/crypto/kcf as a result of PSARC/2002/117 allowing it, then 2470Sstevel@tonic-gatelibcryptoutil needs to move as well. 248