xref: /titanic_44/usr/src/lib/libsmbfs/smb/spnegoparse.h (revision 1e4c938b57d1656808e4112127ff1dce3eba5314)
1 // Copyright (C) 2002 Microsoft Corporation
2 // All rights reserved.
3 //
4 // THIS CODE AND INFORMATION IS PROVIDED "AS IS"
5 // WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
6 // OR IMPLIED, INCLUDING BUT NOT LIMITED
7 // TO THE IMPLIED WARRANTIES OF MERCHANTIBILITY
8 // AND/OR FITNESS FOR A PARTICULAR PURPOSE.
9 //
10 // Date    - 10/08/2002
11 // Author  - Sanj Surati
12 
13 /////////////////////////////////////////////////////////////
14 //
15 // SPNEGOPARSE.H
16 //
17 // SPNEGO Token Parser Header File
18 //
19 // Contains the definitions required to properly parse a
20 // SPNEGO token using ASN.1 DER helpers.
21 //
22 /////////////////////////////////////////////////////////////
23 
24 #pragma ident	"%Z%%M%	%I%	%E% SMI"
25 
26 #ifndef __SPNEGOPARSE_H__
27 #define __SPNEGOPARSE_H__
28 
29 // C++ Specific
30 #if defined(__cplusplus)
31 extern "C"
32 {
33 #endif
34 
35 // Indicates if we copy data when creating a SPNEGO_TOKEN structure or not
36 #define SPNEGO_TOKEN_INTERNAL_COPYPTR           0
37 #define SPNEGO_TOKEN_INTERNAL_COPYDATA          0x1
38 
39 // Internal flag dictates whether or not we will free the binary data when
40 // the SPNEG_TOKEN structure is destroyed
41 #define  SPNEGO_TOKEN_INTERNAL_FLAGS_FREEDATA   0x1
42 
43    //
44 // Each SPNEGO Token Type can be broken down into a
45 // maximum of 4 separate elements.
46 //
47 
48 #define  MAX_NUM_TOKEN_ELEMENTS  4
49 
50 //
51 // Element offsets in the array
52 //
53 
54 // INIT elements
55 #define  SPNEGO_INIT_MECHTYPES_ELEMENT    0
56 #define  SPNEGO_INIT_REQFLAGS_ELEMENT     1
57 #define  SPNEGO_INIT_MECHTOKEN_ELEMENT    2
58 #define  SPNEGO_INIT_MECHLISTMIC_ELEMENT  3
59 
60 // Response elements
61 #define  SPNEGO_TARG_NEGRESULT_ELEMENT    0
62 #define  SPNEGO_TARG_SUPPMECH_ELEMENT     1
63 #define  SPNEGO_TARG_RESPTOKEN_ELEMENT    2
64 #define  SPNEGO_TARG_MECHLISTMIC_ELEMENT  3
65 
66 //
67 // Defines an individual SPNEGO Token Element.
68 //
69 
70 typedef struct SpnegoElement
71 {
72    size_t                nStructSize;        // Size of the element structure
73    int                   iElementPresent;    // Is the field present?  Must be either
74                                              // SPNEGO_TOKEN_ELEMENT_UNAVAILABLE or
75                                              // SPNEGO_TOKEN_ELEMENT_AVAILABLE
76 
77    SPNEGO_ELEMENT_TYPE   eElementType;       // The Element Type
78 
79    unsigned char         type;               // Data Type
80 
81    unsigned char*        pbData;             // Points to actual Data
82 
83    unsigned long         nDatalength;        // Actual Data Length
84 
85 } SPNEGO_ELEMENT;
86 
87 // Structure size in case we later choose to extend the structure
88 #define  SPNEGO_ELEMENT_SIZE sizeof(SPNEGO_ELEMENT)
89 
90 //
91 // Packages a SPNEGO Token Encoding.  There are two types of
92 // encodings: NegTokenInit and NegTokenTarg.  Each encoding can
93 // contain up to four distinct, optional elements.
94 //
95 
96 typedef struct SpnegoToken
97 {
98    size_t            nStructSize;                              // Size of the Token structure
99    unsigned long     ulFlags;                                  // Internal Structure Flags - Reserved!
100    int               ucTokenType;                              // Token Type - Must be
101                                                                // SPNEGO_TOKEN_INIT or
102                                                                // SPNEGO_TOKEN_TARG
103 
104    unsigned char*    pbBinaryData;                             // Points to binary token data
105 
106    unsigned long     ulBinaryDataLen;                          // Length of the actual binary data
107    int               nNumElements;                             // Number of elements
108    SPNEGO_ELEMENT    aElementArray [MAX_NUM_TOKEN_ELEMENTS];   // Holds the elements for the token
109 } SPNEGO_TOKEN;
110 
111 // Structure size in case we later choose to extend the structure
112 #define  SPNEGO_TOKEN_SIZE sizeof(SPNEGO_TOKEN)
113 
114 //
115 // Function definitions
116 //
117 
118 SPNEGO_TOKEN* AllocEmptySpnegoToken( unsigned char ucCopyData, unsigned long ulFlags,
119                                     unsigned char * pbTokenData, unsigned long ulTokenSize );
120 void FreeSpnegoToken( SPNEGO_TOKEN* pSpnegoToken );
121 void InitSpnegoTokenElementArray( SPNEGO_TOKEN* pSpnegoToken );
122 int InitSpnegoTokenType( SPNEGO_TOKEN* pSpnegoToken, long* pnTokenLength,
123                            long* pnRemainingTokenLength, unsigned char** ppbFirstElement );
124 int InitSpnegoTokenElements( SPNEGO_TOKEN* pSpnegoToken, unsigned char* pbTokenData,
125                            long nRemainingTokenLength  );
126 int GetSpnegoInitTokenMechList( unsigned char* pbTokenData, int nMechListLength,
127                                  SPNEGO_ELEMENT* pSpnegoElement );
128 int InitSpnegoTokenElementFromBasicType( unsigned char* pbTokenData, int nElementLength,
129                                           unsigned char ucExpectedType,
130                                           SPNEGO_ELEMENT_TYPE spnegoElementType,
131                                           SPNEGO_ELEMENT* pSpnegoElement );
132 int InitSpnegoTokenElementFromOID( unsigned char* pbTokenData, int nElementLength,
133                                    SPNEGO_ELEMENT_TYPE spnegoElementType,
134                                    SPNEGO_ELEMENT* pSpnegoElement );
135 int FindMechOIDInMechList( SPNEGO_ELEMENT* pSpnegoElement, SPNEGO_MECH_OID MechOID,
136                            int * piMechTypeIndex );
137 int ValidateMechList( unsigned char* pbMechListData, long nBoundaryLength );
138 int CalculateMinSpnegoInitTokenSize( long nMechTokenLength, long nMechListMICLength,
139                                     SPNEGO_MECH_OID mechOid, int nReqFlagsAvailable,
140                                     long* plTokenSize, long* plInternalLength );
141 int CalculateMinSpnegoTargTokenSize( SPNEGO_MECH_OID MechType, SPNEGO_NEGRESULT spnegoNegResult,
142                                     long nMechTokenLen,
143                                     long nMechTokenMIC, long* pnTokenSize,
144                                     long* pnInternalTokenLength );
145 int CreateSpnegoInitToken( SPNEGO_MECH_OID MechType,
146           unsigned char ucContextFlags, unsigned char* pbMechToken,
147           unsigned long ulMechTokenLen, unsigned char* pbMechListMIC,
148           unsigned long ulMechListMICLen, unsigned char* pbTokenData,
149           long nTokenLength, long nInternalTokenLength );
150 int CreateSpnegoTargToken( SPNEGO_MECH_OID MechType,
151           SPNEGO_NEGRESULT eNegResult, unsigned char* pbMechToken,
152           unsigned long ulMechTokenLen, unsigned char* pbMechListMIC,
153           unsigned long ulMechListMICLen, unsigned char* pbTokenData,
154           long nTokenLength, long nInternalTokenLength );
155 int IsValidMechOid( SPNEGO_MECH_OID mechOid );
156 int IsValidContextFlags( unsigned char ucContextFlags );
157 int IsValidNegResult( SPNEGO_NEGRESULT negResult );
158 int IsValidSpnegoToken( SPNEGO_TOKEN* pSpnegoToken );
159 int IsValidSpnegoElement( SPNEGO_TOKEN* pSpnegoToken,SPNEGO_ELEMENT_TYPE spnegoElement );
160 int CalculateElementArrayIndex( SPNEGO_TOKEN* pSpnegoToken,SPNEGO_ELEMENT_TYPE spnegoElement );
161 int InitTokenFromBinary( unsigned char ucCopyData, unsigned long ulFlags,
162                         unsigned char* pbTokenData, unsigned long ulLength,
163                         SPNEGO_TOKEN** ppSpnegoToken );
164 
165    // C++ Specific
166 #if defined(__cplusplus)
167 }
168 #endif
169 
170 #endif
171