Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
7.7 KiB

  1. #include "stdafx.h"
  2. #ifndef _CHICAGO_
  3. // this file is always compile UNICODE in the library - so the conversions work out right
  4. //
  5. //Local includes
  6. //
  7. #include "certupgr.h"
  8. #define BACKUP_ID 'KRBK'
  9. //------------------------------------------------------------------------------
  10. void ReadWriteDWORD( HANDLE hFile, DWORD *pDword, BOOL fRead );
  11. void ReadWriteString( HANDLE hFile, LPTSTR* ppsz, BOOL fRead );
  12. void ReadWriteBlob( HANDLE hFile, PVOID pBlob, DWORD cbBlob, BOOL fRead );
  13. //-------------------------------------------------------------------------
  14. PCCERT_CONTEXT ImportKRBackupToCAPIStore_A(
  15. PCHAR pszFileName, // path of the file
  16. PCHAR pszPassword, // ANSI password
  17. PCHAR pszCAPIStore, // name of the capi store
  18. BOOL bOverWrite
  19. )
  20. {
  21. PCCERT_CONTEXT pCert = NULL;
  22. // prep the wide strings
  23. PWCHAR pszwFileName = NULL;
  24. PWCHAR pszwCAPIStore = NULL;
  25. DWORD lenFile = (strlen(pszFileName)+1) * sizeof(TCHAR);
  26. DWORD lenStore = (strlen(pszCAPIStore)+1) * sizeof(TCHAR);
  27. pszwFileName = (PWCHAR)GlobalAlloc( GPTR, lenFile );
  28. pszwCAPIStore = (PWCHAR)GlobalAlloc( GPTR, lenStore );
  29. if ( !pszwFileName || !pszwCAPIStore )
  30. goto cleanup;
  31. // convert the strings
  32. MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, pszFileName, -1, pszwFileName, lenFile );
  33. MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, pszCAPIStore, -1, pszwCAPIStore, lenStore );
  34. // do the real call
  35. pCert = ImportKRBackupToCAPIStore_W( pszwFileName, pszPassword, pszwCAPIStore, bOverWrite);
  36. cleanup:
  37. // preserve the last error state
  38. DWORD err = GetLastError();
  39. // clean up the strings
  40. if ( pszwFileName )
  41. GlobalFree( pszwFileName );
  42. if ( pszwCAPIStore )
  43. GlobalFree( pszwCAPIStore );
  44. // reset the last error state
  45. SetLastError( err );
  46. // return the cert
  47. return pCert;
  48. }
  49. //-------------------------------------------------------------------------
  50. // Import old-style keyring backup file
  51. PCCERT_CONTEXT ImportKRBackupToCAPIStore_W(
  52. PWCHAR ptszFileName, // path of the file
  53. PCHAR pszPassword, // ANSI password
  54. PWCHAR pszCAPIStore, // name of the capi store
  55. BOOL bOverWrite
  56. )
  57. {
  58. PCCERT_CONTEXT pCertContext = NULL;
  59. DWORD dword;
  60. LPTSTR psz = NULL;
  61. // prep the file name
  62. HANDLE hFile = NULL;
  63. // This code is originally from KeyRing. The fImport controlled whether it was reading
  64. // or writing the file. In this case, we are always and only reading it. so....
  65. const BOOL fImport = TRUE;
  66. // also, this was a method on a class. The appropriate member variables are now here
  67. PVOID pPrivateKey = NULL;
  68. DWORD cbPrivateKey;
  69. PVOID pCertificate = NULL;
  70. DWORD cbCertificate;
  71. PVOID pRequest = NULL;
  72. DWORD cbRequest = 0;
  73. CString szName;
  74. // open the file
  75. hFile = CreateFile(
  76. ptszFileName, // pointer to name of the file
  77. GENERIC_READ, // access (read-write) mode
  78. FILE_SHARE_READ, // share mode
  79. NULL, // pointer to security attributes
  80. OPEN_EXISTING, // how to create
  81. FILE_ATTRIBUTE_NORMAL, // file attributes
  82. NULL // handle to file with attributes to copy
  83. );
  84. if ( hFile == INVALID_HANDLE_VALUE )
  85. return NULL;
  86. // do the backup id
  87. dword = BACKUP_ID;
  88. ReadWriteDWORD( hFile, &dword, fImport );
  89. // check the backup id
  90. if ( dword != BACKUP_ID )
  91. {
  92. goto cleanup;
  93. }
  94. // start with the name of the key
  95. ReadWriteString( hFile, &psz, fImport );
  96. // we aren't using the name for now, so throw it away.....
  97. if ( psz )
  98. GlobalFree( psz );
  99. psz = NULL;
  100. // now the private key data size
  101. ReadWriteDWORD( hFile, &cbPrivateKey, fImport );
  102. // make a private key data pointer if necessary
  103. if ( fImport && cbPrivateKey )
  104. {
  105. pPrivateKey = GlobalAlloc( GPTR, cbPrivateKey );
  106. if ( !pPrivateKey )
  107. {
  108. goto cleanup;
  109. }
  110. }
  111. // use the private key pointer
  112. if ( cbPrivateKey )
  113. ReadWriteBlob( hFile, pPrivateKey, cbPrivateKey, fImport );
  114. // now the certificate
  115. ReadWriteDWORD( hFile, &cbCertificate, fImport );
  116. // make a data pointer if necessary
  117. if ( fImport && cbCertificate )
  118. {
  119. pCertificate = GlobalAlloc( GPTR, cbCertificate );
  120. if ( !pCertificate )
  121. {
  122. goto cleanup;
  123. }
  124. }
  125. // use the public key pointer
  126. if ( cbCertificate )
  127. ReadWriteBlob( hFile, pCertificate, cbCertificate, fImport );
  128. // now the request - if there is one
  129. ReadWriteDWORD( hFile, &cbRequest, fImport );
  130. // make a data pointer if necessary
  131. if ( fImport && cbRequest )
  132. {
  133. pRequest = GlobalAlloc( GPTR, cbRequest );
  134. if ( !pRequest )
  135. {
  136. goto cleanup;
  137. }
  138. }
  139. // use the request pointer
  140. if ( cbRequest )
  141. ReadWriteBlob( hFile, pRequest, cbRequest, fImport );
  142. // finally, do the CAPI conversion here
  143. pCertContext = CopyKRCertToCAPIStore(
  144. pPrivateKey, cbPrivateKey,
  145. pCertificate, cbCertificate,
  146. pRequest, cbRequest,
  147. pszPassword,
  148. pszCAPIStore,
  149. bOverWrite);
  150. // clean up
  151. cleanup:
  152. if ( hFile )
  153. CloseHandle( hFile );
  154. if ( pPrivateKey )
  155. GlobalFree( pPrivateKey );
  156. if ( pCertificate )
  157. GlobalFree( pCertificate );
  158. if ( pRequest )
  159. GlobalFree( pRequest );
  160. // return the context
  161. return pCertContext;
  162. }
  163. // file utilities
  164. //---------------------------------------------------------------------------
  165. void ReadWriteDWORD( HANDLE hFile, DWORD *pDword, BOOL fRead )
  166. {
  167. // read it or write it
  168. ReadWriteBlob( hFile, pDword, sizeof(DWORD), fRead );
  169. }
  170. //---------------------------------------------------------------------------
  171. // remember - we are only and always reading - never writing.......
  172. void ReadWriteString( HANDLE hFile, LPTSTR* ppsz, BOOL fRead )
  173. {
  174. // get the length of the string
  175. DWORD cbLength = 0;
  176. ReadWriteDWORD(hFile,&cbLength,fRead );
  177. // allocate the buffer for the new string - it is the responsibility
  178. // of the caller to ensure that ppsz is not pointing to something that
  179. // needs to be freed.
  180. if ( fRead )
  181. {
  182. *ppsz = (LPTSTR)GlobalAlloc( GPTR, cbLength+1 );
  183. ASSERT( *ppsz );
  184. if ( !*ppsz )
  185. AfxThrowMemoryException();
  186. }
  187. // read or write the string
  188. ReadWriteBlob(hFile, *ppsz, cbLength+1, fRead);
  189. }
  190. /* #pragma INTRINSA suppress=all */
  191. //---------------------------------------------------------------------------
  192. void ReadWriteBlob( HANDLE hFile, PVOID pBlob, DWORD cbBlob, BOOL fRead )
  193. {
  194. // read it or write it
  195. // - always read it here this isn't keyring anymore
  196. ReadFile(
  197. hFile, // handle of file to read
  198. pBlob, // address of buffer that receives data
  199. cbBlob, // number of bytes to read
  200. &cbBlob, // address of number of bytes read
  201. NULL // address of structure for data
  202. );
  203. }
  204. #endif //_CHICAGO_