1*12720SWyllys.Ingersoll@Sun.COM /*
2*12720SWyllys.Ingersoll@Sun.COM * CDDL HEADER START
3*12720SWyllys.Ingersoll@Sun.COM *
4*12720SWyllys.Ingersoll@Sun.COM * The contents of this file are subject to the terms of the
5*12720SWyllys.Ingersoll@Sun.COM * Common Development and Distribution License (the "License").
6*12720SWyllys.Ingersoll@Sun.COM * You may not use this file except in compliance with the License.
7*12720SWyllys.Ingersoll@Sun.COM *
8*12720SWyllys.Ingersoll@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*12720SWyllys.Ingersoll@Sun.COM * or http://www.opensolaris.org/os/licensing.
10*12720SWyllys.Ingersoll@Sun.COM * See the License for the specific language governing permissions
11*12720SWyllys.Ingersoll@Sun.COM * and limitations under the License.
12*12720SWyllys.Ingersoll@Sun.COM *
13*12720SWyllys.Ingersoll@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each
14*12720SWyllys.Ingersoll@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*12720SWyllys.Ingersoll@Sun.COM * If applicable, add the following below this CDDL HEADER, with the
16*12720SWyllys.Ingersoll@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying
17*12720SWyllys.Ingersoll@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner]
18*12720SWyllys.Ingersoll@Sun.COM *
19*12720SWyllys.Ingersoll@Sun.COM * CDDL HEADER END
20*12720SWyllys.Ingersoll@Sun.COM */
21*12720SWyllys.Ingersoll@Sun.COM
22*12720SWyllys.Ingersoll@Sun.COM /*
23*12720SWyllys.Ingersoll@Sun.COM * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24*12720SWyllys.Ingersoll@Sun.COM */
25*12720SWyllys.Ingersoll@Sun.COM
26*12720SWyllys.Ingersoll@Sun.COM /**
27*12720SWyllys.Ingersoll@Sun.COM * \file KMSAgentDataUnitCache.cpp
28*12720SWyllys.Ingersoll@Sun.COM */
29*12720SWyllys.Ingersoll@Sun.COM
30*12720SWyllys.Ingersoll@Sun.COM #include <stdio.h>
31*12720SWyllys.Ingersoll@Sun.COM #include "SYSCommon.h"
32*12720SWyllys.Ingersoll@Sun.COM #include "KMSClientProfile.h"
33*12720SWyllys.Ingersoll@Sun.COM #include "KMSAgentDataUnitCache.h"
34*12720SWyllys.Ingersoll@Sun.COM
CDataUnitCache(int i_iMaxSize)35*12720SWyllys.Ingersoll@Sun.COM CDataUnitCache::CDataUnitCache(int i_iMaxSize)
36*12720SWyllys.Ingersoll@Sun.COM {
37*12720SWyllys.Ingersoll@Sun.COM m_iSize = 0;
38*12720SWyllys.Ingersoll@Sun.COM m_iIndex = 0;
39*12720SWyllys.Ingersoll@Sun.COM m_iMaxSize = i_iMaxSize;
40*12720SWyllys.Ingersoll@Sun.COM m_pCache = 0;
41*12720SWyllys.Ingersoll@Sun.COM
42*12720SWyllys.Ingersoll@Sun.COM K_CreateMutex(&m_Lock);
43*12720SWyllys.Ingersoll@Sun.COM }
44*12720SWyllys.Ingersoll@Sun.COM
~CDataUnitCache()45*12720SWyllys.Ingersoll@Sun.COM CDataUnitCache::~CDataUnitCache()
46*12720SWyllys.Ingersoll@Sun.COM {
47*12720SWyllys.Ingersoll@Sun.COM delete[] m_pCache;
48*12720SWyllys.Ingersoll@Sun.COM K_DestroyMutex(m_Lock);
49*12720SWyllys.Ingersoll@Sun.COM }
50*12720SWyllys.Ingersoll@Sun.COM
Insert(const unsigned char * const i_pDataUnitID,int i_iDataUnitIDMaxLen,const unsigned char * const i_pDataUnitKeyID,int i_iDataUnitKeyIDMaxLen,const utf8char * const i_wsApplianceNetworkAddress)51*12720SWyllys.Ingersoll@Sun.COM bool CDataUnitCache::Insert(
52*12720SWyllys.Ingersoll@Sun.COM const unsigned char* const i_pDataUnitID,
53*12720SWyllys.Ingersoll@Sun.COM int i_iDataUnitIDMaxLen,
54*12720SWyllys.Ingersoll@Sun.COM const unsigned char* const i_pDataUnitKeyID ,
55*12720SWyllys.Ingersoll@Sun.COM int i_iDataUnitKeyIDMaxLen,
56*12720SWyllys.Ingersoll@Sun.COM const utf8char* const i_wsApplianceNetworkAddress )
57*12720SWyllys.Ingersoll@Sun.COM {
58*12720SWyllys.Ingersoll@Sun.COM FATAL_ASSERT( (i_pDataUnitID && i_iDataUnitIDMaxLen == KMS_DATA_UNIT_ID_SIZE) ||
59*12720SWyllys.Ingersoll@Sun.COM (i_pDataUnitKeyID && i_iDataUnitKeyIDMaxLen == KMS_KEY_ID_SIZE));
60*12720SWyllys.Ingersoll@Sun.COM FATAL_ASSERT( i_wsApplianceNetworkAddress &&
61*12720SWyllys.Ingersoll@Sun.COM strlen( i_wsApplianceNetworkAddress ) < KMS_MAX_NETWORK_ADDRESS );
62*12720SWyllys.Ingersoll@Sun.COM
63*12720SWyllys.Ingersoll@Sun.COM Lock();
64*12720SWyllys.Ingersoll@Sun.COM
65*12720SWyllys.Ingersoll@Sun.COM if ( m_pCache == 0 )
66*12720SWyllys.Ingersoll@Sun.COM {
67*12720SWyllys.Ingersoll@Sun.COM m_pCache = new DataUnitCacheEntry[m_iMaxSize];
68*12720SWyllys.Ingersoll@Sun.COM
69*12720SWyllys.Ingersoll@Sun.COM if ( !m_pCache )
70*12720SWyllys.Ingersoll@Sun.COM {
71*12720SWyllys.Ingersoll@Sun.COM // no error logged on out of memory
72*12720SWyllys.Ingersoll@Sun.COM Unlock();
73*12720SWyllys.Ingersoll@Sun.COM #if defined(DEBUG) && defined(METAWARE)
74*12720SWyllys.Ingersoll@Sun.COM log_printf("CDataUnitCache::Insert new DataUnitCacheEntry alloc failure\n");
75*12720SWyllys.Ingersoll@Sun.COM #endif
76*12720SWyllys.Ingersoll@Sun.COM return false;
77*12720SWyllys.Ingersoll@Sun.COM }
78*12720SWyllys.Ingersoll@Sun.COM }
79*12720SWyllys.Ingersoll@Sun.COM
80*12720SWyllys.Ingersoll@Sun.COM if( m_iSize >= m_iMaxSize )
81*12720SWyllys.Ingersoll@Sun.COM {
82*12720SWyllys.Ingersoll@Sun.COM // the cache is full, so reuse an old slot
83*12720SWyllys.Ingersoll@Sun.COM
84*12720SWyllys.Ingersoll@Sun.COM m_iIndex = (m_iIndex + 1) % m_iMaxSize;
85*12720SWyllys.Ingersoll@Sun.COM }
86*12720SWyllys.Ingersoll@Sun.COM else
87*12720SWyllys.Ingersoll@Sun.COM {
88*12720SWyllys.Ingersoll@Sun.COM m_iIndex = m_iSize;
89*12720SWyllys.Ingersoll@Sun.COM
90*12720SWyllys.Ingersoll@Sun.COM m_iSize++;
91*12720SWyllys.Ingersoll@Sun.COM }
92*12720SWyllys.Ingersoll@Sun.COM
93*12720SWyllys.Ingersoll@Sun.COM strncpy( m_pCache[m_iIndex].m_wsApplianceNetworkAddress,
94*12720SWyllys.Ingersoll@Sun.COM i_wsApplianceNetworkAddress,
95*12720SWyllys.Ingersoll@Sun.COM sizeof(m_pCache[m_iIndex].m_wsApplianceNetworkAddress) );
96*12720SWyllys.Ingersoll@Sun.COM m_pCache[m_iIndex].m_wsApplianceNetworkAddress[sizeof(m_pCache[m_iIndex].m_wsApplianceNetworkAddress)-1] = '\0';
97*12720SWyllys.Ingersoll@Sun.COM
98*12720SWyllys.Ingersoll@Sun.COM if ( i_pDataUnitID )
99*12720SWyllys.Ingersoll@Sun.COM {
100*12720SWyllys.Ingersoll@Sun.COM memcpy( m_pCache[m_iIndex].m_aDataUnitID,
101*12720SWyllys.Ingersoll@Sun.COM i_pDataUnitID,
102*12720SWyllys.Ingersoll@Sun.COM i_iDataUnitIDMaxLen );
103*12720SWyllys.Ingersoll@Sun.COM }
104*12720SWyllys.Ingersoll@Sun.COM else
105*12720SWyllys.Ingersoll@Sun.COM {
106*12720SWyllys.Ingersoll@Sun.COM memset( m_pCache[m_iIndex].m_aDataUnitID,0,KMS_DATA_UNIT_ID_SIZE);
107*12720SWyllys.Ingersoll@Sun.COM }
108*12720SWyllys.Ingersoll@Sun.COM
109*12720SWyllys.Ingersoll@Sun.COM if ( i_pDataUnitKeyID )
110*12720SWyllys.Ingersoll@Sun.COM {
111*12720SWyllys.Ingersoll@Sun.COM memcpy( m_pCache[m_iIndex].m_aDataUnitKeyID,
112*12720SWyllys.Ingersoll@Sun.COM i_pDataUnitKeyID,
113*12720SWyllys.Ingersoll@Sun.COM i_iDataUnitKeyIDMaxLen );
114*12720SWyllys.Ingersoll@Sun.COM }
115*12720SWyllys.Ingersoll@Sun.COM else
116*12720SWyllys.Ingersoll@Sun.COM {
117*12720SWyllys.Ingersoll@Sun.COM memset(m_pCache[m_iIndex].m_aDataUnitKeyID,0,KMS_KEY_ID_SIZE);
118*12720SWyllys.Ingersoll@Sun.COM }
119*12720SWyllys.Ingersoll@Sun.COM
120*12720SWyllys.Ingersoll@Sun.COM Unlock();
121*12720SWyllys.Ingersoll@Sun.COM
122*12720SWyllys.Ingersoll@Sun.COM return true;
123*12720SWyllys.Ingersoll@Sun.COM }
124*12720SWyllys.Ingersoll@Sun.COM
GetApplianceByDataUnitID(const unsigned char * const i_pDataUnitID,int i_iDataUnitIDMaxLen,utf8char * const o_wsApplianceNetworkAddress,int i_iMaxApplianceNetworkAddressLen)125*12720SWyllys.Ingersoll@Sun.COM bool CDataUnitCache::GetApplianceByDataUnitID(
126*12720SWyllys.Ingersoll@Sun.COM const unsigned char* const i_pDataUnitID,
127*12720SWyllys.Ingersoll@Sun.COM int i_iDataUnitIDMaxLen,
128*12720SWyllys.Ingersoll@Sun.COM utf8char* const o_wsApplianceNetworkAddress,
129*12720SWyllys.Ingersoll@Sun.COM int i_iMaxApplianceNetworkAddressLen )
130*12720SWyllys.Ingersoll@Sun.COM {
131*12720SWyllys.Ingersoll@Sun.COM FATAL_ASSERT( i_pDataUnitID );
132*12720SWyllys.Ingersoll@Sun.COM FATAL_ASSERT( i_iDataUnitIDMaxLen == KMS_DATA_UNIT_ID_SIZE );
133*12720SWyllys.Ingersoll@Sun.COM FATAL_ASSERT( i_iMaxApplianceNetworkAddressLen <= KMS_MAX_NETWORK_ADDRESS );
134*12720SWyllys.Ingersoll@Sun.COM
135*12720SWyllys.Ingersoll@Sun.COM // assumes o_wsApplianceNetworkAddress points to at least KMS_MAX_NETWORK_ADDRESS
136*12720SWyllys.Ingersoll@Sun.COM
137*12720SWyllys.Ingersoll@Sun.COM Lock();
138*12720SWyllys.Ingersoll@Sun.COM
139*12720SWyllys.Ingersoll@Sun.COM int i;
140*12720SWyllys.Ingersoll@Sun.COM for( i = 0; i < m_iSize; i++ )
141*12720SWyllys.Ingersoll@Sun.COM {
142*12720SWyllys.Ingersoll@Sun.COM if( memcmp(m_pCache[i].m_aDataUnitID, i_pDataUnitID, KMS_DATA_UNIT_ID_SIZE) == 0 )
143*12720SWyllys.Ingersoll@Sun.COM {
144*12720SWyllys.Ingersoll@Sun.COM strncpy( o_wsApplianceNetworkAddress,
145*12720SWyllys.Ingersoll@Sun.COM m_pCache[i].m_wsApplianceNetworkAddress,
146*12720SWyllys.Ingersoll@Sun.COM i_iMaxApplianceNetworkAddressLen );
147*12720SWyllys.Ingersoll@Sun.COM o_wsApplianceNetworkAddress[i_iMaxApplianceNetworkAddressLen-1] = '\0';
148*12720SWyllys.Ingersoll@Sun.COM Unlock();
149*12720SWyllys.Ingersoll@Sun.COM return true;
150*12720SWyllys.Ingersoll@Sun.COM }
151*12720SWyllys.Ingersoll@Sun.COM }
152*12720SWyllys.Ingersoll@Sun.COM
153*12720SWyllys.Ingersoll@Sun.COM Unlock();
154*12720SWyllys.Ingersoll@Sun.COM
155*12720SWyllys.Ingersoll@Sun.COM return false;
156*12720SWyllys.Ingersoll@Sun.COM }
157*12720SWyllys.Ingersoll@Sun.COM
GetApplianceByDataUnitKeyID(const unsigned char * const i_pDataUnitKeyID,int i_iDataUnitKeyIDMaxLen,utf8char * const o_wsApplianceNetworkAddress,int i_iMaxApplianceNetworkAddressLen)158*12720SWyllys.Ingersoll@Sun.COM bool CDataUnitCache::GetApplianceByDataUnitKeyID(
159*12720SWyllys.Ingersoll@Sun.COM const unsigned char* const i_pDataUnitKeyID,
160*12720SWyllys.Ingersoll@Sun.COM int i_iDataUnitKeyIDMaxLen,
161*12720SWyllys.Ingersoll@Sun.COM utf8char* const o_wsApplianceNetworkAddress,
162*12720SWyllys.Ingersoll@Sun.COM int i_iMaxApplianceNetworkAddressLen )
163*12720SWyllys.Ingersoll@Sun.COM {
164*12720SWyllys.Ingersoll@Sun.COM FATAL_ASSERT( i_pDataUnitKeyID );
165*12720SWyllys.Ingersoll@Sun.COM FATAL_ASSERT( i_iDataUnitKeyIDMaxLen == KMS_KEY_ID_SIZE );
166*12720SWyllys.Ingersoll@Sun.COM FATAL_ASSERT( i_iMaxApplianceNetworkAddressLen <= KMS_MAX_NETWORK_ADDRESS );
167*12720SWyllys.Ingersoll@Sun.COM
168*12720SWyllys.Ingersoll@Sun.COM // assumes o_wsApplianceNetworkAddress points to at least KMS_MAX_NETWORK_ADDRESS
169*12720SWyllys.Ingersoll@Sun.COM
170*12720SWyllys.Ingersoll@Sun.COM Lock();
171*12720SWyllys.Ingersoll@Sun.COM
172*12720SWyllys.Ingersoll@Sun.COM int i;
173*12720SWyllys.Ingersoll@Sun.COM for( i = 0; i < m_iSize; i++ )
174*12720SWyllys.Ingersoll@Sun.COM {
175*12720SWyllys.Ingersoll@Sun.COM if( memcmp(m_pCache[i].m_aDataUnitKeyID,
176*12720SWyllys.Ingersoll@Sun.COM i_pDataUnitKeyID, KMS_KEY_ID_SIZE) == 0 )
177*12720SWyllys.Ingersoll@Sun.COM {
178*12720SWyllys.Ingersoll@Sun.COM strncpy( o_wsApplianceNetworkAddress,
179*12720SWyllys.Ingersoll@Sun.COM m_pCache[i].m_wsApplianceNetworkAddress,
180*12720SWyllys.Ingersoll@Sun.COM i_iMaxApplianceNetworkAddressLen );
181*12720SWyllys.Ingersoll@Sun.COM o_wsApplianceNetworkAddress[i_iMaxApplianceNetworkAddressLen-1] = '\0';
182*12720SWyllys.Ingersoll@Sun.COM
183*12720SWyllys.Ingersoll@Sun.COM Unlock();
184*12720SWyllys.Ingersoll@Sun.COM
185*12720SWyllys.Ingersoll@Sun.COM return true;
186*12720SWyllys.Ingersoll@Sun.COM }
187*12720SWyllys.Ingersoll@Sun.COM }
188*12720SWyllys.Ingersoll@Sun.COM
189*12720SWyllys.Ingersoll@Sun.COM Unlock();
190*12720SWyllys.Ingersoll@Sun.COM
191*12720SWyllys.Ingersoll@Sun.COM return false;
192*12720SWyllys.Ingersoll@Sun.COM }
193*12720SWyllys.Ingersoll@Sun.COM
Lock()194*12720SWyllys.Ingersoll@Sun.COM void CDataUnitCache::Lock()
195*12720SWyllys.Ingersoll@Sun.COM {
196*12720SWyllys.Ingersoll@Sun.COM K_LockMutex(m_Lock);
197*12720SWyllys.Ingersoll@Sun.COM }
198*12720SWyllys.Ingersoll@Sun.COM
Unlock()199*12720SWyllys.Ingersoll@Sun.COM void CDataUnitCache::Unlock()
200*12720SWyllys.Ingersoll@Sun.COM {
201*12720SWyllys.Ingersoll@Sun.COM K_UnlockMutex(m_Lock);
202*12720SWyllys.Ingersoll@Sun.COM }
203