17c478bd9Sstevel@tonic-gate# 27c478bd9Sstevel@tonic-gate# CDDL HEADER START 37c478bd9Sstevel@tonic-gate# 47c478bd9Sstevel@tonic-gate# The contents of this file are subject to the terms of the 5*ccd81fddSJan Pechanec# Common Development and Distribution License (the "License"). 6*ccd81fddSJan Pechanec# You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate# 87c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate# and limitations under the License. 127c478bd9Sstevel@tonic-gate# 137c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate# 197c478bd9Sstevel@tonic-gate# CDDL HEADER END 207c478bd9Sstevel@tonic-gate# 21*ccd81fddSJan Pechanec# Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 227c478bd9Sstevel@tonic-gate# 237c478bd9Sstevel@tonic-gate 247c478bd9Sstevel@tonic-gateThis is an internal library for use only by: 257c478bd9Sstevel@tonic-gate usr/src/cmd/cmd-crypto 267c478bd9Sstevel@tonic-gate usr/src/lib/pkcs11 27*ccd81fddSJan Pechanec usr/src/lib/libkmf 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gateThe library and the header file are installed into the proto area but 307c478bd9Sstevel@tonic-gateare not included in any pacakges. 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate libcryptoutil Design 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate1. Introduction 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gateThere are a number of common code components and general utility functions 387c478bd9Sstevel@tonic-gateneeded that are shared by various userland parts of the crypto framework. 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gateThe originally approved ARC materials (PSARC/2001/488 & PSARC/2001/553) 417c478bd9Sstevel@tonic-gatedidn't have a library that was included by all user land libraries, 427c478bd9Sstevel@tonic-gateplugins and commands. 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gateThe solution to this is to follow what other project teams have done in the 457c478bd9Sstevel@tonic-gatepast and create a project private util library. 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate2. Contents 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gateAny code that is generic enough to be shared by multiple parts of the 507c478bd9Sstevel@tonic-gateuser crypto framework is eligible. 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gateThe current contents are: 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate2.1 Error & Debug Functions 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate cryptodebug_init(), 577c478bd9Sstevel@tonic-gate cryptodebug() 587c478bd9Sstevel@tonic-gate cryptoerror() 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gateThese functions log debug or error information to stderr and/or 617c478bd9Sstevel@tonic-gatesyslog or a file. Debug is off by default but the code is always 627c478bd9Sstevel@tonic-gatecompiled in. 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gateThe cryptodebug_init() routine allows the caller to set a message 657c478bd9Sstevel@tonic-gateprefix for error and debug output. 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gateThe environment variable SUNW_CRYPTO_DEBUG determines wither or not 687c478bd9Sstevel@tonic-gatedebug output is generated at run time, valid values are "syslog" or "stderr" 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gateFor example elfsign(1) could do: 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate cryptodebug_init("elfsign"); 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gateand later: 757c478bd9Sstevel@tonic-gate cryptoerror(LOG_STDERR, gettext("invalid number of arguments")); 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gateThis would cause an error message on stderr thus: 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate "elfsign: invalid number of arguments" 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gateThe first argument to cryptoerror is either LOG_STDERR or a syslog(3c) 827c478bd9Sstevel@tonic-gatepriority. All messages include the PID and are logged at LOG_USER. 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gatefor debug output: 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate cryptodebug("scmd=request opts=%s", opts); 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gateThis would go to the location defined by $SUNW_CRYPTO_DEBUG, ie 897c478bd9Sstevel@tonic-gatesyslog, stderr or not be generated at all. 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate2.2 PKCS#11 Mechanism Type to and from Strings 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate pkcs11_mech2str() and pkcs11_str2mech() 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gateThese functions use a table built at compile time from the contents of 967c478bd9Sstevel@tonic-gatethe pkcs11t.h file to map mechanism numbers to the corresponding string 977c478bd9Sstevel@tonic-gatevalue. 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gatepkcs11_mech2str() returns a pointer to a string that should be free(3c)'d 1007c478bd9Sstevel@tonic-gateby the caller. 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gateConsumers: 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate digest(1), mac(1), encrypt(1), decrypt(1) for translating 1057c478bd9Sstevel@tonic-gate command line args to mech numbers. They will need to 1067c478bd9Sstevel@tonic-gate add the "CKM_" prefix before calling pkc11_str2mech() 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate cryptoadm(1m) for output to user, and for storing in pkcs11.conf 1097c478bd9Sstevel@tonic-gate file. 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate Debug code. 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate2.3 The "pkcs11.conf" configuration file Parsing code. 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gateThe "pkcs11.conf" configuration file parsing code and data structures are 1167c478bd9Sstevel@tonic-gateshared between: 1177c478bd9Sstevel@tonic-gate cryptoadm(1m), libpkcs11(3crypto). 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate2.3.1 Data Structures: 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate #define MECH_ID_HEX_LEN 11 /* length of mechanism id in hex form */ 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate typedef char libname_t[MAXPATHLEN]; 1247c478bd9Sstevel@tonic-gate typedef char midstr_t[MECH_ID_HEX_LEN]; 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate /* The policy list for an entry in the config file */ 1277c478bd9Sstevel@tonic-gate typedef struct umechlist { 1287c478bd9Sstevel@tonic-gate midstr_t name; 1297c478bd9Sstevel@tonic-gate struct umechlist *next; 1307c478bd9Sstevel@tonic-gate } umechlist_t; 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate /* An entry in the pkcs11.conf file */ 1337c478bd9Sstevel@tonic-gate typedef struct uentry { 1347c478bd9Sstevel@tonic-gate libname_t name; 1357c478bd9Sstevel@tonic-gate boolean_t flag_enabledlist; /* TRUE if an enabledlist */ 1367c478bd9Sstevel@tonic-gate umechlist_t *policylist; /* disabledlist or enabledlist */ 1377c478bd9Sstevel@tonic-gate int count; 1387c478bd9Sstevel@tonic-gate } uentry_t; 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate /* The entry list for the entire pkcs11.conf file */ 1417c478bd9Sstevel@tonic-gate typedef struct uentrylist { 1427c478bd9Sstevel@tonic-gate uentry_t *pent; 1437c478bd9Sstevel@tonic-gate struct uentrylist *next; 1447c478bd9Sstevel@tonic-gate } uentrylist_t; 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate2.3.2 Functions: 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gateextern int get_pkcs11conf_info(uentrylist_t **ppliblist); 1507c478bd9Sstevel@tonic-gate$ 1517c478bd9Sstevel@tonic-gate Retrieve the user-level provider info from the pkcs11.conf file. 1527c478bd9Sstevel@tonic-gate If successful, the result is returned from the ppliblist argument. 1537c478bd9Sstevel@tonic-gate This function returns SUCCESS if successfully done; otherwise it returns 1547c478bd9Sstevel@tonic-gate FAILURE. The caller should use free_uentrylist() to free the space 1557c478bd9Sstevel@tonic-gate allocated for "ppliblist". 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gateextern umechlist_t *create_umech(char *mechname); 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate Create one item of type umechlist_t with the mechanism name in hex form. 1607c478bd9Sstevel@tonic-gate A NULL is returned when the input name is NULL or the heap memory is 1617c478bd9Sstevel@tonic-gate insufficient. The Caller should use free_umechlist() to free the space 1627c478bd9Sstevel@tonic-gate allocated for the returning data. 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gateextern void free_uentrylist(uentrylist_t *ptr); 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate Free space allocated for an pointer to the struct "uentrylist_t". 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gateextern void free_uentry(uentry_t *ptr); 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate Free space allocated for an pointer to the struct "uentry_t". 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gateextern void free_umechlist(umechlist_t *ptr); 1737c478bd9Sstevel@tonic-gate 1747c478bd9Sstevel@tonic-gate Free space allocated for an pointer to the struct "umechlist_t". 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate2.4 PKCS#11 Mechanism Type to key type 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate pkcs11_mech2keytype() 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gateThis function is used to get the key type for a mechanism. 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gateConsumers: 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate encrypt(1), decrypt(1), and libpkcs11(3crypto) for getting 1857c478bd9Sstevel@tonic-gate the key type when creating an object for use with a 1867c478bd9Sstevel@tonic-gate specific mechanism. 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate2.5 PKCS#11 return code to string 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate pkcs11_strerror() 1917c478bd9Sstevel@tonic-gate 192*ccd81fddSJan PechanecThis function returns a string representation of any given PKCS11 return 1937c478bd9Sstevel@tonic-gatecode. 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gateConsumer: 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate encrypt(1) and decrypt(1) uses this function for reporting errors. 1987c478bd9Sstevel@tonic-gate 199*ccd81fddSJan Pechanec2.5 PKCS#11 URI parsing code 200*ccd81fddSJan Pechanec 201*ccd81fddSJan Pechanec pkcs11_parse_uri() 202*ccd81fddSJan Pechanec pkcs11_free_uri() 203*ccd81fddSJan Pechanec 204*ccd81fddSJan PechanecThis function parses a PKCS#11 URI and fills up a pkcs11_uri_t structure. It 205*ccd81fddSJan Pechanecalso reads the PIN if the PKCS#11 URI specifies a passphrase dialog. The 206*ccd81fddSJan Pechanecpkcs11_uri_t is described in cryptoutil.h, explanation of the return codes for 207*ccd81fddSJan Pechanecthe pkcs11_parse_uri() function is in the function's comment in pk11_uri.c. The 208*ccd81fddSJan Pechanecpkcs11_parse_uri() function allocates the URI's fields and the caller is 209*ccd81fddSJan Pechanecresponsible for calling pkcs11_free_uri() after it's done with the URI 210*ccd81fddSJan Pechanecstructure. 211*ccd81fddSJan Pechanec 212*ccd81fddSJan PechanecConsumer: 213*ccd81fddSJan Pechanec 214*ccd81fddSJan Pechanec SunSSH will use the functions for parsing PKCS#11 URIs. 215*ccd81fddSJan Pechanec 2167c478bd9Sstevel@tonic-gate3. Non-Contents 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gateCode for cryptographic algorithms does not belong in here. That 2197c478bd9Sstevel@tonic-gatecomes from usr/src/common/<algorithm> since it is shared between user and 2207c478bd9Sstevel@tonic-gatekernel. 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gatePKCS#11 header files although they are common to various parts of the 2237c478bd9Sstevel@tonic-gateuser land framework come from usr/src/pkcs11/include 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate4. Interface Taxonomy 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gateEverything in this library is Project Private or Internal. The 2287c478bd9Sstevel@tonic-gateexported symbols will all be marked as SUNWprivate_1.0 in the library 2297c478bd9Sstevel@tonic-gatespec file. 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate5. Static vs Dynamic 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gateThe initial design was to only use a static archive library to avoid 2347c478bd9Sstevel@tonic-gateexposing a new interface (even though it is all private). However while 2357c478bd9Sstevel@tonic-gatethis is fine for initial delivery it creates difficulties later with 2367c478bd9Sstevel@tonic-gatepatching. As such a Dynamic version will be build. 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gateLibraries for lint and header files will not be shipped in any Sun packages 2397c478bd9Sstevel@tonic-gatesince this is all Project Private. Similarly the abi_ file will not be 2407c478bd9Sstevel@tonic-gateshipped even though a spec file will be used in the source gate. 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate6. Library location 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gateAt present all of the consumers of the library are in /usr/ so the 2457c478bd9Sstevel@tonic-gatelibrary is /usr/lib/{sparcv9}/libcryptoutil.so.1. If kcfd ever moves 2467c478bd9Sstevel@tonic-gateto /lib/crypto/kcf as a result of PSARC/2002/117 allowing it, then 2477c478bd9Sstevel@tonic-gatelibcryptoutil needs to move as well. 248