cryptlib  3.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros
key_attr.c
Go to the documentation of this file.
1 /****************************************************************************
2 * *
3 * cryptlib Keyset Attribute Routines *
4 * Copyright Peter Gutmann 1995-2007 *
5 * *
6 ****************************************************************************/
7 
8 #include <stdio.h>
9 #include <stdarg.h>
10 #include "crypt.h"
11 #ifdef INC_ALL
12  #include "keyset.h"
13 #else
14  #include "keyset/keyset.h"
15 #endif /* Compiler-specific includes */
16 
17 #ifdef USE_KEYSETS
18 
19 /****************************************************************************
20 * *
21 * Utility Functions *
22 * *
23 ****************************************************************************/
24 
25 /* Exit after setting extended error information */
26 
28 static int exitError( INOUT KEYSET_INFO *keysetInfoPtr,
29  IN_ATTRIBUTE const CRYPT_ATTRIBUTE_TYPE errorLocus,
30  IN_ENUM( CRYPT_ERRTYPE ) const CRYPT_ERRTYPE_TYPE errorType,
31  IN_ERROR const int status )
32  {
33  assert( isWritePtr( keysetInfoPtr, sizeof( KEYSET_INFO ) ) );
34 
35  REQUIRES( isAttribute( errorLocus ) || \
36  isInternalAttribute( errorLocus ) );
37  REQUIRES( errorType > CRYPT_ERRTYPE_NONE && \
38  errorType < CRYPT_ERRTYPE_LAST );
39  REQUIRES( cryptStatusError( status ) );
40 
41  setErrorInfo( keysetInfoPtr, errorLocus, errorType );
42  return( status );
43  }
44 
46 static int exitErrorNotFound( INOUT KEYSET_INFO *keysetInfoPtr,
47  IN_ATTRIBUTE const CRYPT_ATTRIBUTE_TYPE errorLocus )
48  {
49  assert( isWritePtr( keysetInfoPtr, sizeof( KEYSET_INFO ) ) );
50 
51  REQUIRES( isAttribute( errorLocus ) || \
52  isInternalAttribute( errorLocus ) );
53 
54  return( exitError( keysetInfoPtr, errorLocus, CRYPT_ERRTYPE_ATTR_ABSENT,
56  }
57 
59 static int exitErrorIncomplete( INOUT KEYSET_INFO *keysetInfoPtr,
60  IN_ATTRIBUTE const CRYPT_ATTRIBUTE_TYPE errorLocus )
61  {
62  assert( isWritePtr( keysetInfoPtr, sizeof( KEYSET_INFO ) ) );
63 
64  REQUIRES( isAttribute( errorLocus ) || \
65  isInternalAttribute( errorLocus ) );
66 
67  return( exitError( keysetInfoPtr, errorLocus, CRYPT_ERRTYPE_ATTR_PRESENT,
69  }
70 
71 /****************************************************************************
72 * *
73 * Get Attributes *
74 * *
75 ****************************************************************************/
76 
77 /* Get a numeric/boolean attribute */
78 
79 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
80 int getKeysetAttribute( INOUT KEYSET_INFO *keysetInfoPtr,
81  OUT_INT_Z int *valuePtr,
83  {
84  assert( isWritePtr( keysetInfoPtr, sizeof( KEYSET_INFO ) ) );
85  assert( isWritePtr( valuePtr, sizeof( int ) ) );
86 
87  REQUIRES( isAttribute( attribute ) || \
88  isInternalAttribute( attribute ) );
89 
90  /* Clear return value */
91  *valuePtr = 0;
92 
93  switch( attribute )
94  {
96  *valuePtr = keysetInfoPtr->errorType;
97  return( CRYPT_OK );
98 
100  *valuePtr = keysetInfoPtr->errorLocus;
101  return( CRYPT_OK );
102  }
103 
104  retIntError();
105  }
106 
107 /* Get a string attribute */
108 
109 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
110 int getKeysetAttributeS( INOUT KEYSET_INFO *keysetInfoPtr,
112  IN_ATTRIBUTE const CRYPT_ATTRIBUTE_TYPE attribute )
113  {
114  assert( isWritePtr( keysetInfoPtr, sizeof( KEYSET_INFO ) ) );
115  assert( isWritePtr( msgData, sizeof( MESSAGE_DATA ) ) );
116 
117  REQUIRES( isAttribute( attribute ) || \
118  isInternalAttribute( attribute ) );
119 
120  switch( attribute )
121  {
123  {
124 #ifdef USE_ERRMSGS
125  ERROR_INFO *errorInfo = &keysetInfoPtr->errorInfo;
126 
127  if( errorInfo->errorStringLength > 0 )
128  {
129  return( attributeCopy( msgData, errorInfo->errorString,
130  errorInfo->errorStringLength ) );
131  }
132 #endif /* USE_ERRMSGS */
133  return( exitErrorNotFound( keysetInfoPtr,
135  }
136 
137  case CRYPT_IATTRIBUTE_CONFIGDATA:
138  case CRYPT_IATTRIBUTE_USERINDEX:
139  case CRYPT_IATTRIBUTE_USERINFO:
140  case CRYPT_IATTRIBUTE_TRUSTEDCERT:
141  case CRYPT_IATTRIBUTE_TRUSTEDCERT_NEXT:
142  REQUIRES( keysetInfoPtr->type == KEYSET_FILE && \
143  keysetInfoPtr->subType == KEYSET_SUBTYPE_PKCS15 );
144 
145  /* It's encoded cryptlib-specific data, fetch it from to the
146  keyset */
147  return( keysetInfoPtr->getSpecialItemFunction( keysetInfoPtr,
148  attribute, msgData->data, msgData->length,
149  &msgData->length ) );
150 
151  }
152 
153  retIntError();
154  }
155 
156 /****************************************************************************
157 * *
158 * Set Attributes *
159 * *
160 ****************************************************************************/
161 
162 /* Set a numeric/boolean attribute */
163 
165 int setKeysetAttribute( INOUT KEYSET_INFO *keysetInfoPtr,
166  IN_INT_Z const int value,
167  IN_ATTRIBUTE const CRYPT_ATTRIBUTE_TYPE attribute )
168  {
169  assert( isWritePtr( keysetInfoPtr, sizeof( KEYSET_INFO ) ) );
170 
171  REQUIRES( value >= 0 && value < MAX_INTLENGTH );
172  REQUIRES( isAttribute( attribute ) || \
173  isInternalAttribute( attribute ) );
174 
175  switch( attribute )
176  {
177  case CRYPT_IATTRIBUTE_HWSTORAGE:
178  REQUIRES( keysetInfoPtr->type == KEYSET_FILE && \
179  keysetInfoPtr->subType == KEYSET_SUBTYPE_PKCS15 );
180 
181  return( keysetInfoPtr->setSpecialItemFunction( keysetInfoPtr,
182  CRYPT_IATTRIBUTE_HWSTORAGE,
183  &value, sizeof( int ) ) );
184  }
185 
186  retIntError();
187  }
188 
189 /* Set a string attribute */
190 
191 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
192 int setKeysetAttributeS( INOUT KEYSET_INFO *keysetInfoPtr,
193  IN_BUFFER( dataLength ) const void *data,
194  IN_LENGTH const int dataLength,
195  IN_ATTRIBUTE const CRYPT_ATTRIBUTE_TYPE attribute )
196  {
197  int status;
198 
199  assert( isWritePtr( keysetInfoPtr, sizeof( KEYSET_INFO ) ) );
200  assert( isReadPtr( data, dataLength ) );
201 
202  REQUIRES( dataLength > 0 && dataLength < MAX_INTLENGTH );
203  REQUIRES( isAttribute( attribute ) || \
204  isInternalAttribute( attribute ) );
205 
206  switch( attribute )
207  {
208  case CRYPT_KEYINFO_QUERY:
210  {
211  REQUIRES( keysetInfoPtr->type == KEYSET_DBMS );
212  REQUIRES( keysetInfoPtr->setSpecialItemFunction != NULL );
213  REQUIRES( keysetInfoPtr->isBusyFunction != NULL );
214 
215  /* If we're in the middle of an existing query the user needs to
216  cancel it before starting another one */
217  if( keysetInfoPtr->isBusyFunction( keysetInfoPtr ) && \
218  ( dataLength != 6 || strCompare( data, "cancel", 6 ) ) )
219  return( exitErrorIncomplete( keysetInfoPtr, attribute ) );
220 
221  /* Send the query to the data source */
222  return( keysetInfoPtr->setSpecialItemFunction( keysetInfoPtr,
223  attribute, data, dataLength ) );
224  }
225 
226  case CRYPT_IATTRIBUTE_CONFIGDATA:
227  case CRYPT_IATTRIBUTE_USERINDEX:
228  case CRYPT_IATTRIBUTE_USERID:
229  case CRYPT_IATTRIBUTE_USERINFO:
230  REQUIRES( keysetInfoPtr->type == KEYSET_FILE && \
231  keysetInfoPtr->subType == KEYSET_SUBTYPE_PKCS15 );
232 
233  /* It's encoded cryptlib-specific data, pass it through to the
234  keyset */
235  status = keysetInfoPtr->setSpecialItemFunction( keysetInfoPtr,
236  attribute, data, dataLength );
237  if( cryptStatusOK( status ) && \
238  attribute != CRYPT_IATTRIBUTE_USERID )
239  {
240  /* The update succeeded, remember that the data in the keyset
241  has changed unless it's a userID that just modifies
242  existing data */
243  keysetInfoPtr->flags |= KEYSET_DIRTY;
244  keysetInfoPtr->flags &= ~KEYSET_EMPTY;
245  }
246  return( status );
247  }
248 
249  retIntError();
250  }
251 
252 #endif /* USE_KEYSETS */