2 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 #pragma ident "%Z%%M% %I% %E% SMI"
9 #include <security/cryptoki.h>
10 #include <security/pkcs11.h>
15 * This routine provides a mapping from Kerberos encryption
16 * and hash types to PKCS#11 encryption and hash types.
19 get_algo(krb5_enctype etype
, KRB5_MECH_TO_PKCS
*algos
)
22 case ENCTYPE_DES_CBC_CRC
:
23 algos
->enc_algo
= CKM_DES_CBC
;
25 algos
->str2key_algo
= 0;
26 algos
->flags
= USE_ENCR
;
28 case ENCTYPE_DES_CBC_MD5
:
29 algos
->enc_algo
= CKM_DES_CBC
;
30 algos
->hash_algo
= CKM_MD5
;
31 algos
->str2key_algo
= 0;
32 algos
->flags
= USE_ENCR
| USE_HASH
;
34 case ENCTYPE_DES_CBC_RAW
:
35 algos
->enc_algo
= CKM_DES_CBC
;
37 algos
->str2key_algo
= 0;
38 algos
->flags
= USE_ENCR
;
40 case ENCTYPE_DES_HMAC_SHA1
:
41 algos
->enc_algo
= CKM_DES_CBC
;
42 algos
->hash_algo
= CKM_SHA_1_HMAC
;
43 algos
->str2key_algo
= 0;
44 algos
->flags
= USE_ENCR
| USE_HASH
;
46 case ENCTYPE_DES3_CBC_SHA1
:
47 algos
->enc_algo
= CKM_DES3_CBC
;
48 algos
->hash_algo
= CKM_SHA_1_HMAC
;
49 algos
->str2key_algo
= 0;
50 algos
->flags
= USE_ENCR
| USE_HASH
;
52 case ENCTYPE_DES3_CBC_RAW
:
53 algos
->enc_algo
= CKM_DES3_CBC
;
55 algos
->str2key_algo
= 0;
56 algos
->flags
= USE_ENCR
;
58 case ENCTYPE_ARCFOUR_HMAC
:
59 case ENCTYPE_ARCFOUR_HMAC_EXP
:
60 algos
->enc_algo
= CKM_RC4
;
61 algos
->hash_algo
= CKM_MD5_HMAC
;
62 algos
->str2key_algo
= 0;
63 algos
->flags
= USE_ENCR
;
65 case ENCTYPE_AES128_CTS_HMAC_SHA1_96
:
66 case ENCTYPE_AES256_CTS_HMAC_SHA1_96
:
67 algos
->enc_algo
= CKM_AES_CBC
;
68 algos
->hash_algo
= CKM_SHA_1_HMAC
;
69 algos
->str2key_algo
= CKM_PKCS5_PBKD2
;
70 algos
->flags
= USE_ENCR
;
73 return (CKR_MECHANISM_INVALID
);
79 * map Kerberos key types to PKCS#11 key type values.
82 get_key_type(krb5_enctype etype
, CK_KEY_TYPE
*keyType
)
85 case ENCTYPE_DES_CBC_CRC
:
86 case ENCTYPE_DES_CBC_MD5
:
87 case ENCTYPE_DES_CBC_RAW
:
88 case ENCTYPE_DES_HMAC_SHA1
:
91 case ENCTYPE_DES3_CBC_SHA1
:
92 case ENCTYPE_DES3_CBC_RAW
:
95 case ENCTYPE_AES128_CTS_HMAC_SHA1_96
:
96 case ENCTYPE_AES256_CTS_HMAC_SHA1_96
:
99 case ENCTYPE_ARCFOUR_HMAC
:
100 case ENCTYPE_ARCFOUR_HMAC_EXP
:
105 /* There's no appropriate error. Just return the general one */
106 return (CKR_GENERAL_ERROR
);
112 * Determine whether the PKCS#11 "slot" supports the necessary
113 * crypto needed for Kerberos functionality.
116 * TRUE = The given slot is OK for Kerberos
117 * FALSE = Not ok, try something else.
120 slot_supports_krb5(CK_SLOT_ID_PTR slotid
)
123 CK_MECHANISM_INFO info
;
125 int enctypes_found
= 0;
126 KRB5_MECH_TO_PKCS algos
;
127 krb5_enctype tempenctype
;
129 for (i
= 0; i
< krb5_enctypes_length
; i
++) {
130 tempenctype
= krb5_enctypes_list
[i
].etype
;
131 if ((rv
= get_algo(tempenctype
, &algos
)) != CKR_OK
) {
132 KRB5_LOG0(KRB5_ERR
, "Failed to get algorithm.");
134 * If the algorithm is not available, disable
135 * this enctype so kerberos doesn't try to use it
138 krb5_enctypes_list
[i
].etype
= -1;
139 krb5_enctypes_list
[i
].in_string
= "<unsupported>";
140 krb5_enctypes_list
[i
].out_string
= "<unsupported>";
143 if (ENC_DEFINED(algos
)) {
144 size_t keysize
, keylength
;
145 rv
= C_GetMechanismInfo(*slotid
, algos
.enc_algo
, &info
);
147 KRB5_LOG1(KRB5_ERR
, "C_GetMechanismInfo failed "
148 "for encr algorith %s: 0x%x\n",
149 krb5_enctypes_list
[i
].in_string
,
154 * If the encryption algorithm is supported,
155 * make sure it supports the correct key sizes.
156 * If not, disable this enctype and continue.
158 keysize
= krb5_enctypes_list
[i
].enc
->keybytes
;
159 keylength
= krb5_enctypes_list
[i
].enc
->keylength
;
161 if (keylength
> info
.ulMaxKeySize
) {
162 krb5_enctypes_list
[i
].etype
= -1;
163 krb5_enctypes_list
[i
].in_string
=
165 krb5_enctypes_list
[i
].out_string
=
169 if (!(info
.flags
& (CKF_ENCRYPT
|CKF_RNG
)))
172 if (HASH_DEFINED(algos
)) {
173 rv
= C_GetMechanismInfo(*slotid
, algos
.hash_algo
,
176 KRB5_LOG1(KRB5_ERR
, "C_GetMechanismInfo failed "
177 "for hash algorithm %s: 0x%x\n",
178 krb5_enctypes_list
[i
].in_string
,
182 if (!(info
.flags
& (CKF_DIGEST
|CKF_SIGN
|CKF_RNG
)))
185 if (algos
.str2key_algo
!= 0) {
186 rv
= C_GetMechanismInfo(*slotid
, algos
.str2key_algo
,
189 KRB5_LOG(KRB5_ERR
, "C_GetMechanismInfo failed "
190 "for str2key algorithm: 0x%x\n", rv
);
197 * If NO enctypes were found to be supported, return FALSE.
199 if (!enctypes_found
) {
201 "No crypto support available from PKCS#11.");