| 1 |
/* |
| 2 |
* Copyright (c) 2010 .SE (The Internet Infrastructure Foundation) |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 18 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 20 |
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 22 |
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 23 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 24 |
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
*/ |
| 26 |
|
| 27 |
/***************************************************************************** |
| 28 |
access.cpp |
| 29 |
|
| 30 |
Implements the access rules. |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include "access.h" |
| 34 |
#include <stdlib.h> |
| 35 |
#include <stdio.h> |
| 36 |
|
| 37 |
// Checks if a read operation is allowed on a given object type. |
| 38 |
// |
| 39 |
// Type of session |
| 40 |
// Type of object R/O Public | R/W Public | R/O User | R/W User | R/W SO |
| 41 |
// ------------------------------------------------------------------------------ |
| 42 |
// Public session object OK | OK | OK | OK | OK |
| 43 |
// Private session object UNLI | UNLI | OK | OK | UNLI |
| 44 |
// Public token object OK | OK | OK | OK | OK |
| 45 |
// Private token object UNLI | UNLI | OK | OK | UNLI |
| 46 |
// |
| 47 |
// OK = CKR_OK |
| 48 |
// SRO = CKR_SESSION_READ_ONLY |
| 49 |
// UNLI = CKR_USER_NOT_LOGGED_IN |
| 50 |
|
| 51 |
// Can we do read operations? |
| 52 |
CK_RV haveRead(CK_STATE sessionState, CK_BBOOL /*isTokenObject*/, CK_BBOOL isPrivateObject) |
| 53 |
{ |
| 54 |
switch (sessionState) |
| 55 |
{ |
| 56 |
case CKS_RO_PUBLIC_SESSION: |
| 57 |
case CKS_RW_PUBLIC_SESSION: |
| 58 |
case CKS_RW_SO_FUNCTIONS: |
| 59 |
return isPrivateObject ? CKR_USER_NOT_LOGGED_IN : CKR_OK; |
| 60 |
case CKS_RO_USER_FUNCTIONS: |
| 61 |
case CKS_RW_USER_FUNCTIONS: |
| 62 |
return CKR_OK; |
| 63 |
} |
| 64 |
return CKR_GENERAL_ERROR; // internal error, switch should have covered every state |
| 65 |
} |
| 66 |
|
| 67 |
// Checks if a write operation is allowed on a given object type. |
| 68 |
// |
| 69 |
// Type of session |
| 70 |
// Type of object R/O Public | R/W Public | R/O User | R/W User | R/W SO |
| 71 |
// ------------------------------------------------------------------------------ |
| 72 |
// Public session object OK | OK | OK | OK | OK |
| 73 |
// Private session object UNLI | UNLI | OK | OK | UNLI |
| 74 |
// Public token object SRO | OK | SRO | OK | OK |
| 75 |
// Private token object SRO/UNLI | UNLI | SRO | OK | UNLI |
| 76 |
// |
| 77 |
// OK = CKR_OK |
| 78 |
// SRO = CKR_SESSION_READ_ONLY |
| 79 |
// UNLI = CKR_USER_NOT_LOGGED_IN |
| 80 |
// In the situation where both SRO and UNLI may be returned we favor SRO. |
| 81 |
|
| 82 |
// Can we do write operations? |
| 83 |
CK_RV haveWrite(CK_STATE sessionState, CK_BBOOL isTokenObject, CK_BBOOL isPrivateObject) |
| 84 |
{ |
| 85 |
switch (sessionState) |
| 86 |
{ |
| 87 |
case CKS_RO_PUBLIC_SESSION: |
| 88 |
if (isTokenObject) |
| 89 |
return CKR_SESSION_READ_ONLY; |
| 90 |
else |
| 91 |
return isPrivateObject ? CKR_USER_NOT_LOGGED_IN : CKR_OK; |
| 92 |
case CKS_RW_PUBLIC_SESSION: |
| 93 |
case CKS_RW_SO_FUNCTIONS: |
| 94 |
return isPrivateObject ? CKR_USER_NOT_LOGGED_IN : CKR_OK; |
| 95 |
case CKS_RO_USER_FUNCTIONS: |
| 96 |
return isTokenObject ? CKR_SESSION_READ_ONLY : CKR_OK; |
| 97 |
case CKS_RW_USER_FUNCTIONS: |
| 98 |
return CKR_OK; |
| 99 |
} |
| 100 |
return CKR_GENERAL_ERROR; // internal error, switch should have covered every state |
| 101 |
} |