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.
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.
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]
23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
27 * \file KMSAgentChallenge.cpp
30 #include "KMSAgentChallenge.h"
31 #include "KMSAgentCryptoUtilities.h"
32 #include "KMSAgentStringUtilities.h"
33 #include "ApplianceParameters.h"
34 #include "SYSCommon.h"
36 extern "C" int Log2(char* msg1
,
45 * ComputeChallengeResponse
47 bool ComputeChallengeResponse(
48 const unsigned char* i_pAuthenticationSecret
,
49 int i_iAuthenticationSecretLength
,
50 const unsigned char* i_pRootCACertificate
,
51 int i_iRootCACertificateLength
,
52 const unsigned char* i_pAuthenticationChallenge
,
53 int i_iAuthenticationChallengeLength
,
54 unsigned char* o_pAuthenticationChallengeResponse
,
55 int i_iAuthenticationChallengeResponseLength
)
61 Log2 ("KMSAgent_LoadProfile::ComputeChallengeResponse", "Entered");
63 FATAL_ASSERT( i_iAuthenticationChallengeResponseLength
== HMAC_LENGTH
);
65 // challenge response is HMAC-SHA1( RootCACertificate ||
66 // AuthenticationChallenge, AuthenticationSecret )
67 const unsigned char* aBuffersToHMAC
[2];
68 int aBuffersToHMACSize
[2];
70 aBuffersToHMAC
[0] = i_pRootCACertificate
;
71 aBuffersToHMACSize
[0] = i_iRootCACertificateLength
;
73 aBuffersToHMAC
[1] = i_pAuthenticationChallenge
;
74 aBuffersToHMACSize
[1] = i_iAuthenticationChallengeLength
;
80 i_pAuthenticationSecret
,
81 i_iAuthenticationSecretLength
,
82 o_pAuthenticationChallengeResponse
);
84 #if defined(METAWARE) && defined(DEBUG)
87 j
+=snprintf(outmsg
+j
, OUTMSG_SIZE
,
89 i_iAuthenticationSecretLength
);
91 for (int i
=0 ; i
< i_iAuthenticationSecretLength
; i
++)
93 j
+=snprintf(outmsg
+j
, OUTMSG_SIZE
,
95 i_pAuthenticationSecret
[i
]);
97 snprintf(outmsg
+j
, OUTMSG_SIZE
, "\n");
99 Log2("Secret = ",outmsg
);
102 #if defined(METAWARE) && defined(DEBUG)
105 j
+=snprintf(outmsg
+j
, OUTMSG_SIZE
,
107 i_iRootCACertificateLength
);
109 for (i
=0 ; i
< i_iRootCACertificateLength
; i
++)
111 j
+=snprintf(outmsg
+j
, OUTMSG_SIZE
,
113 i_pRootCACertificate
[i
]);
115 snprintf(outmsg
+j
, OUTMSG_SIZE
, "\n");
117 Log2("i_pRootCACertificate = ",outmsg
);
120 #if defined(METAWARE) && defined(DEBUG)
123 j
+=snprintf(outmsg
+j
, OUTMSG_SIZE
,
125 i_iAuthenticationChallengeLength
);
127 for (i
=0 ; i
< i_iAuthenticationChallengeLength
; i
++)
129 j
+=snprintf(outmsg
+j
, OUTMSG_SIZE
,
131 i_pAuthenticationChallenge
[i
]);
133 snprintf(outmsg
+j
, OUTMSG_SIZE
, "\n");
135 Log2("i_pAuthenticationChallenge = ",outmsg
);
138 #if defined(METAWARE) && defined(DEBUG)
141 j
+=snprintf(outmsg
+j
, OUTMSG_SIZE
,
143 i_iAuthenticationChallengeResponseLength
);
145 for (i
=0 ; i
< i_iAuthenticationChallengeResponseLength
; i
++)
147 j
+=snprintf(outmsg
+j
, OUTMSG_SIZE
,
149 o_pAuthenticationChallengeResponse
[i
]);
151 snprintf(outmsg
+j
, OUTMSG_SIZE
, "\n");
153 Log2("o_pAuthenticationChallengeResponse = ",outmsg
);
162 * ComputeEntityHashedPassphraseAndAuthenticationSecret
164 bool ComputeEntityHashedPassphraseAndAuthenticationSecret(
165 const char* i_sPassphrase
,
166 char* const o_sHexHashedPassphrase
,
167 int* const o_piAuthenticationHashIterationCount
,
168 char* const o_sHexAuthenticationSecret
)
170 // HashedPassphrase is SHA1( Passphrase-UTF-8 )
171 // Using UTF-8 ensures the same result on different platforms with
172 // different wide character representations.
173 // This hashed passphrase value is used to wrap entity
174 // private key materials.
175 #if defined(METAWARE) && defined(DEBUG)
176 Log2 ("KMSAgent_LoadProfile::ComputeEntityHashedPassphraseAndAuthenticationSecret",
180 unsigned char aHashedPassphrase
[HASH_LENGTH
];
182 memset(aHashedPassphrase
, 0, HASH_LENGTH
);
184 if ( strlen(i_sPassphrase
) > 0 )
187 (unsigned char*)i_sPassphrase
,
188 strlen(i_sPassphrase
),
195 ConvertBinaryToUTF8HexString( o_sHexHashedPassphrase
,
199 // HexAuthenticationSecret is SHA1( SHA1( ... ( SHA1(
200 // HashedPassphrase ) ) ) The number of iterations is time bounded
201 // at 1/10 of a second, and also bounded by fixed minimum and
202 // maximum values (to prevent too weak of a computation and to
203 // prevent a DoS, respectively). This value is used as the shared
204 // secret in challenge-response authentication exchanges.
206 *o_piAuthenticationHashIterationCount
= 0;
208 unsigned long iStartTickCount
= K_GetTickCount();
210 while ( *o_piAuthenticationHashIterationCount
<
211 MAX_AUTHENTICATION_ITERATION_COUNT
212 && ( *o_piAuthenticationHashIterationCount
<
213 MIN_AUTHENTICATION_ITERATION_COUNT
215 AUTHENTICATION_ITERATION_TIME_IN_MILLISECONDS
>
226 (*o_piAuthenticationHashIterationCount
)++;
229 ConvertBinaryToUTF8HexString( o_sHexAuthenticationSecret
,
230 aHashedPassphrase
, HASH_LENGTH
);
232 #if defined(METAWARE) && defined(DEBUG)
233 snprintf(outmsg
, OUTMSG_SIZE
,
234 "o_sHexAuthenticationSecret=%x o_piAuth..."
235 "= %x aHashedPassphrase=%s\n",
236 o_sHexAuthenticationSecret
,
237 *o_piAuthenticationHashIterationCount
,
239 Log2("ComputeEntityHashedPassphraseAndAuthenticationSecret ",
247 * ComputeFixedEntityHashedPassphraseAndAuthenticationSecret
249 bool ComputeFixedEntityHashedPassphraseAndAuthenticationSecret(
250 const char* i_sPassphrase
,
251 char* const o_sHexHashedPassphrase
,
252 int i_iAuthenticationHashIterationCount
,
253 char* const o_sHexAuthenticationSecret
)
255 // compute same values as
256 // ComputeEntityHashedPassphraseAndAuthenticationSecret, except
257 // iteration count is fixed
258 #if defined(METAWARE) && defined(DEBUG)
259 Log2 ("KMSAgent_LoadProfile::"
260 "ComputeFixedEntityHashedPassphraseAndAuthenticationSecret", "Entered");
263 // detect attempts to cause weak computation or DoS attack
264 if ( i_iAuthenticationHashIterationCount
<
265 MIN_AUTHENTICATION_ITERATION_COUNT
||
266 i_iAuthenticationHashIterationCount
>
267 MAX_AUTHENTICATION_ITERATION_COUNT
)
273 unsigned char aHashedPassphrase
[HASH_LENGTH
];
275 memset(aHashedPassphrase
, 0, HASH_LENGTH
);
277 if ( strlen(i_sPassphrase
) > 0 )
280 (unsigned char*)i_sPassphrase
,
281 strlen(i_sPassphrase
),
288 ConvertBinaryToUTF8HexString( o_sHexHashedPassphrase
,
289 aHashedPassphrase
, HASH_LENGTH
);
292 for ( i
= 0; i
< i_iAuthenticationHashIterationCount
; i
++ )
303 ConvertBinaryToUTF8HexString( o_sHexAuthenticationSecret
,
304 aHashedPassphrase
, HASH_LENGTH
);
306 #if defined(METAWARE) && defined(DEBUG)
307 snprintf(outmsg
, OUTMSG_SIZE
,
309 i_iAuthenticationHashIterationCount
);
311 Log2("ComputeEntityHashedPassphraseAndAuthenticationSecret ",