1 // Copyright 2012 Nexenta Systems, Inc. All rights reserved.
2 // Copyright (C) 2002 Microsoft Corporation
3 // All rights reserved.
5 // THIS CODE AND INFORMATION IS PROVIDED "AS IS"
6 // WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
7 // OR IMPLIED, INCLUDING BUT NOT LIMITED
8 // TO THE IMPLIED WARRANTIES OF MERCHANTIBILITY
9 // AND/OR FITNESS FOR A PARTICULAR PURPOSE.
12 // Author - Sanj Surati
14 /////////////////////////////////////////////////////////////
18 // SPNEGO Token Parser Header File
20 // Contains the definitions required to properly parse a
21 // SPNEGO token using ASN.1 DER helpers.
23 /////////////////////////////////////////////////////////////
25 #ifndef __SPNEGOPARSE_H__
26 #define __SPNEGOPARSE_H__
29 #if defined(__cplusplus)
34 // Indicates if we copy data when creating a SPNEGO_TOKEN structure or not
35 #define SPNEGO_TOKEN_INTERNAL_COPYPTR 0
36 #define SPNEGO_TOKEN_INTERNAL_COPYDATA 0x1
38 // Internal flag dictates whether or not we will free the binary data when
39 // the SPNEG_TOKEN structure is destroyed
40 #define SPNEGO_TOKEN_INTERNAL_FLAGS_FREEDATA 0x1
43 // Each SPNEGO Token Type can be broken down into a
44 // maximum of 4 separate elements.
47 #define MAX_NUM_TOKEN_ELEMENTS 4
50 // Element offsets in the array
54 #define SPNEGO_INIT_MECHTYPES_ELEMENT 0
55 #define SPNEGO_INIT_REQFLAGS_ELEMENT 1
56 #define SPNEGO_INIT_MECHTOKEN_ELEMENT 2
57 #define SPNEGO_INIT_MECHLISTMIC_ELEMENT 3
60 #define SPNEGO_TARG_NEGRESULT_ELEMENT 0
61 #define SPNEGO_TARG_SUPPMECH_ELEMENT 1
62 #define SPNEGO_TARG_RESPTOKEN_ELEMENT 2
63 #define SPNEGO_TARG_MECHLISTMIC_ELEMENT 3
66 // Defines an individual SPNEGO Token Element.
69 typedef struct SpnegoElement
71 size_t nStructSize
; // Size of the element structure
72 int iElementPresent
; // Is the field present? Must be either
73 // SPNEGO_TOKEN_ELEMENT_UNAVAILABLE or
74 // SPNEGO_TOKEN_ELEMENT_AVAILABLE
76 SPNEGO_ELEMENT_TYPE eElementType
; // The Element Type
78 unsigned char type
; // Data Type
80 unsigned char* pbData
; // Points to actual Data
82 unsigned long nDatalength
; // Actual Data Length
86 // Structure size in case we later choose to extend the structure
87 #define SPNEGO_ELEMENT_SIZE sizeof(SPNEGO_ELEMENT)
90 // Packages a SPNEGO Token Encoding. There are two types of
91 // encodings: NegTokenInit and NegTokenTarg. Each encoding can
92 // contain up to four distinct, optional elements.
95 typedef struct SpnegoToken
97 size_t nStructSize
; // Size of the Token structure
98 unsigned long ulFlags
; // Internal Structure Flags - Reserved!
99 int ucTokenType
; // Token Type - Must be
100 // SPNEGO_TOKEN_INIT or
103 unsigned char* pbBinaryData
; // Points to binary token data
105 unsigned long ulBinaryDataLen
; // Length of the actual binary data
106 int nNumElements
; // Number of elements
107 SPNEGO_ELEMENT aElementArray
[MAX_NUM_TOKEN_ELEMENTS
]; // Holds the elements for the token
110 // Structure size in case we later choose to extend the structure
111 #define SPNEGO_TOKEN_SIZE sizeof(SPNEGO_TOKEN)
114 // Function definitions
117 SPNEGO_TOKEN
* AllocEmptySpnegoToken( unsigned char ucCopyData
, unsigned long ulFlags
,
118 unsigned char * pbTokenData
, unsigned long ulTokenSize
);
119 void FreeSpnegoToken( SPNEGO_TOKEN
* pSpnegoToken
);
120 void InitSpnegoTokenElementArray( SPNEGO_TOKEN
* pSpnegoToken
);
121 int InitSpnegoTokenType( SPNEGO_TOKEN
* pSpnegoToken
, long* pnTokenLength
,
122 long* pnRemainingTokenLength
, unsigned char** ppbFirstElement
);
123 int InitSpnegoTokenElements( SPNEGO_TOKEN
* pSpnegoToken
, unsigned char* pbTokenData
,
124 long nRemainingTokenLength
);
125 int GetSpnegoInitTokenMechList( unsigned char* pbTokenData
, int nMechListLength
,
126 SPNEGO_ELEMENT
* pSpnegoElement
);
127 int InitSpnegoTokenElementFromBasicType( unsigned char* pbTokenData
, int nElementLength
,
128 unsigned char ucExpectedType
,
129 SPNEGO_ELEMENT_TYPE spnegoElementType
,
130 SPNEGO_ELEMENT
* pSpnegoElement
);
131 int InitSpnegoTokenElementFromOID( unsigned char* pbTokenData
, int nElementLength
,
132 SPNEGO_ELEMENT_TYPE spnegoElementType
,
133 SPNEGO_ELEMENT
* pSpnegoElement
);
134 int FindMechOIDInMechList( SPNEGO_ELEMENT
* pSpnegoElement
, SPNEGO_MECH_OID MechOID
,
135 int * piMechTypeIndex
);
136 int ValidateMechList( unsigned char* pbMechListData
, long nBoundaryLength
);
137 int CalculateMinSpnegoInitTokenSize( long nMechTokenLength
, long nMechListMICLength
,
138 SPNEGO_MECH_OID
*mechOid
, int mechOidCnt
, int nReqFlagsAvailable
,
139 long* plTokenSize
, long* plInternalLength
);
140 int CalculateMinSpnegoTargTokenSize( SPNEGO_MECH_OID MechType
, SPNEGO_NEGRESULT spnegoNegResult
,
142 long nMechTokenMIC
, long* pnTokenSize
,
143 long* pnInternalTokenLength
);
144 int CreateSpnegoInitToken( SPNEGO_MECH_OID
*MechTypeList
, long nMechTypes
,
145 unsigned char ucContextFlags
, unsigned char* pbMechToken
,
146 unsigned long ulMechTokenLen
, unsigned char* pbMechListMIC
,
147 unsigned long ulMechListMICLen
, unsigned char* pbTokenData
,
148 long nTokenLength
, long nInternalTokenLength
);
149 int CreateSpnegoTargToken( SPNEGO_MECH_OID MechType
,
150 SPNEGO_NEGRESULT eNegResult
, unsigned char* pbMechToken
,
151 unsigned long ulMechTokenLen
, unsigned char* pbMechListMIC
,
152 unsigned long ulMechListMICLen
, unsigned char* pbTokenData
,
153 long nTokenLength
, long nInternalTokenLength
);
154 int IsValidMechOid( SPNEGO_MECH_OID mechOid
);
155 int IsValidContextFlags( unsigned char ucContextFlags
);
156 int IsValidNegResult( SPNEGO_NEGRESULT negResult
);
157 int IsValidSpnegoToken( SPNEGO_TOKEN
* pSpnegoToken
);
158 int IsValidSpnegoElement( SPNEGO_TOKEN
* pSpnegoToken
,SPNEGO_ELEMENT_TYPE spnegoElement
);
159 int CalculateElementArrayIndex( SPNEGO_TOKEN
* pSpnegoToken
,SPNEGO_ELEMENT_TYPE spnegoElement
);
160 int InitTokenFromBinary( unsigned char ucCopyData
, unsigned long ulFlags
,
161 unsigned char* pbTokenData
, unsigned long ulLength
,
162 SPNEGO_TOKEN
** ppSpnegoToken
);
165 #if defined(__cplusplus)