Source code of Windows XP (NT5)
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.

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