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.

971 lines
35 KiB

  1. <%@ CODEPAGE=65001 'UTF-8%>
  2. <%' certsces.asp - (CERT)srv web - (S)mart (C)ard (E)nrollment (S)tation
  3. ' Copyright (C) Microsoft Corporation, 1998 - 1999 %>
  4. <!-- #include FILE=certdat.inc -->
  5. <HTML>
  6. <Head>
  7. <Meta HTTP-Equiv="Content-Type" Content="text/html; charset=UTF-8">
  8. <Title>Microsoft Smart Card Enrollment Station</Title>
  9. <Script Language="VBScript">
  10. Option Explicit
  11. '---------------------------------------------------------------------
  12. ' Page global constants and variables
  13. ' page state constants
  14. Const e_ControlLoading=-1
  15. Const e_PageLoading=0
  16. Const e_PageDead=1
  17. Const e_PagePreEnroll=2
  18. Const e_PageEnrolling=3
  19. Const e_PagePostEnrollOK=4
  20. Const e_PagePostEnrollError=5
  21. ' special return value for GetTemplateList and UpdateCSPList
  22. Const ERROR_NOITEMS=-1
  23. Const ERROR_NOMATCHEDCSP=-2
  24. ' flag constants for SCrdEnrl
  25. ' flags for getCertTemplateCount and enumCertTemplateName
  26. Const SCARD_ENROLL_ALL_CERT_TEMPLATE=0 'default
  27. Const SCARD_ENROLL_USER_CERT_TEMPLATE=1
  28. Const SCARD_ENROLL_MACHINE_CERT_TEMPLATE=2
  29. Const SCARD_ENROLL_ENTERPRISE_CERT_TEMPLATE=&H08
  30. Const SCARD_ENROLL_CROSS_CERT_TEMPLATE=&H20
  31. Const SCARD_ENROLL_OFFLINE_CERT_TEMPLATE=&H10
  32. ' flags for enumCertTemplateName, getCertTemplateName and setCertTemplateName
  33. Const SCARD_ENROLL_CERT_TEMPLATE_REAL_NAME=0 ' default
  34. Const SCARD_ENROLL_CERT_TEMPLATE_DISPLAY_NAME=4
  35. ' flags for enumCAName, getCAName and setCAName
  36. Const SCARD_ENROLL_CA_REAL_NAME=0 'default
  37. Const SCARD_ENROLL_CA_MACHINE_NAME=1
  38. Const SCARD_ENROLL_CA_DISPLAY_NAME=2
  39. Const SCARD_ENROLL_CA_UNIQUE_NAME=3 'machineName\realName
  40. ' flags for getSigningCertificateName, getEnrolledCertificateName
  41. Const SCARD_ENROLL_DISPLAY_CERT=0
  42. Const SCARD_ENROLL_NO_DISPLAY_CERT=1
  43. ' flags for setUserName and getUserName
  44. Const SCARD_ENROLL_SAM_COMPATIBLE_NAME=0 ' default
  45. Const SCARD_ENROLL_UPN_NAME=1
  46. ' flags for setSigningCertificate and selectSigningCertificate
  47. ' Const SCARD_SELECT_TEMPLATENAME = 0
  48. Const SCARD_SELECT_EKU = 1
  49. Const FLAGS_NONE=0
  50. ' the state of fields that can be invalid
  51. Dim g_bUserNameBad, g_bSigningCertBad, g_bTemplateBad, g_bCSPBad, g_fNewStation
  52. ' error string to display for e_PageDead and e_PagePostEnrollError
  53. Dim g_sPageError
  54. ' Strings to be localized
  55. Const L_DownloadingControl_Message="Downloading ActiveX control..."
  56. Const L_BadCPU_ErrorMessage="""Your CPU ("" + sError + "") is not supported.""" ' sError will be replaced w/ cpu type
  57. Const L_ControlLoadFailed_ErrorMessage="The proper version of the ActiveX control failed to download and install. You may not have sufficient permissions. Please ask your system administrator for assistance."
  58. Const L_ControlLoadFailedEx_ErrorMessage="""An unexpected error ("" + sError + "") occurred while downloading and installing the proper version of the ActiveX control. Please ask your system administrator for assistance."""
  59. Const L_PageLoading_Message="One moment please. Retrieving CSP list, certificate template list, and CA list."
  60. Const L_ControlLoading_Message="One moment please. Loading ActiveX control."
  61. Const L_PageDead_ErrorMessage="""An unexpected fatal error has occurred: "" + sError"
  62. Const L_MustSelect001_Message="Please select a user to enroll."
  63. Const L_MustSelect010_Message="Please select a signing certificate."
  64. Const L_MustSelect011_Message="Please select a user to enroll and a signing certificate."
  65. Const L_MustSelect100_Message="Please select a template which has a CA."
  66. Const L_MustSelect101_Message="Please select a user to enroll and a template which has a CA."
  67. Const L_MustSelect110_Message="Please select a signing certificate and a template which has a CA."
  68. Const L_MustSelect111_Message="Please select a user to enroll and a signing certificate and a template which has a CA."
  69. Const L_ReadyToEnroll_Message="Please insert the user's smart card into a reader and then press 'Enroll'."
  70. Const L_PageEnrolling_Message="Please wait while the user is enrolled..."
  71. Const L_PagePostEnrollOK_Message="The smart card is ready. Please press 'View Certificate' to make sure the certificate contains the correct personal information about the user."
  72. Const L_PagePostEnrollError_ErrorMessage="""An unexpected error occurred. Error: "" + sError" '"""The smart card enrollment failed: "" + sError"
  73. Const L_IntErrBadStatus_ErrorMessage="!! Internal error !! - unknown page status"
  74. Const L_Unexpected_ErrorMessage="Unexpected Error"
  75. Const L_RetrvTemplateList_Message="Retrieving template list..."
  76. Const L_RetrvTemplateList_ErrorMessage="""An error ocurred while retrieving the template list. Error: "" + sError"
  77. Const L_NoTemplates_ErrorMessage="(No templates found!)"
  78. Const L_NoTemplatesLong_ErrorMessage="No templates could be found. There are no CAs from which you have permission to request a certificate, or an error occurred while accessing the Active Directory."
  79. Const L_RetrvCSPList_Message="Retrieving CSP list..."
  80. Const L_RetrvCSPList_ErrorMessage="""An error occurred while retrieving the CSP list. Error: "" + sError"
  81. Const L_NoCSPs_ErrorMessage="(No CSPs found!)"
  82. Const L_NoMatchedCSP_ErrorMessage="(No matched CSPs!)"
  83. Const L_NoMatchedCSP_Warningmessage="The CSPs supported by the current template is not found on this computer. Select different templates or use template management snapin to add CSPs installed on this computer to the template."
  84. Const L_RetrvCAList_Message="Retrieving CA list..."
  85. Const L_NoCAs_ErrorMessage="(No CA available for this template)"
  86. Const L_NoCertSelected_Message="(No certificate selected)"
  87. Const L_SelectSignCert_ErrorMessage="""An unexpected error occurred while selecting the signing certificate. Error: "" + sError"
  88. Const L_SelectUser_ErrorMessage="""An unexpected error occurred while selecting the user. Error: "" + sError"
  89. Const L_NoUserSelected_Message="(No user selected)"
  90. Const L_IntErrBadData_ErrorMessage="!! Internal Error !! - called enroll with invalid data"
  91. Const L_IntErrSCrdEnrlError_ErrorMessage="!! Internal Error !! - the template, CA, or CSP was rejected"
  92. Const L_Enrolling_Message="Enrolling..."
  93. Const L_EnrlErrNotEnoughReaders_ErrorMessage="You do not have enough smart card readers installed on this system. You must have one smart card available after the signing certificate is selected."
  94. Const L_EnrlErrInsertCard_ErrorMessage="Please insert the user's smart card."
  95. Const L_EnrlErrRemoveCard_ErrorMessage="The smart card has been removed, so that further communication is not possible."
  96. Const L_EnrlErrWrongCards_ErrorMessage="Too many smart cards of the same type are inserted. Please insert only one user smart card and try again."
  97. Const L_EnrlErrCryptoError_ErrorMessage="An error has occur while performing a cryptographic operation on the smart card. If this problem persists, try using a different smart card."
  98. Const L_EnrlErrNoSignCert_ErrorMessage="Cannot find the administrator signing smart card. Please insert the administrator smart card."
  99. Const L_EnrlErrCardCSPMismatch_ErrorMessage="The user smart card you inserted does not match the selected cryptographic service provider (CSP). Please insert a different smart card or select the appropriate CSP."
  100. Const L_EnrlErrCantEncode_ErrorMessage="The user's e-mail address cannot be encoded. The e-mail address may not contain extended characters."
  101. Const L_EnrlErrUnexpected_ErrorMessage="""An unexpected error occurred. Error: "" + sError"
  102. '---------------------------------------------------------------------
  103. ' Set the dynamic status message and the state of the buttons, etc.
  104. Sub LoadControl(sContinueCmd)
  105. Dim sControlFileName, sCPU, sControl, chQuote, sUserAgent
  106. ' determine the file name from the CPU type.
  107. sCPU=LCase(navigator.cpuClass)
  108. If 0<>strComp("x86", sCPU) And 0<>strComp("ia64", sCPU) Then
  109. AbortPage evalErrorMessage(L_BadCPU_ErrorMessage, sCPU)
  110. Exit Sub
  111. End If
  112. sUserAgent = navigator.userAgent
  113. If 0 = InStr(sUserAgent, "Windows NT 5.1") Then
  114. sCPU = "w2k"
  115. 'w2k or lower
  116. g_fNewStation = False
  117. End If
  118. ' load the control
  119. chQuote=chr(34)
  120. sControl="<Object " & vbNewline _
  121. & " ClassID=" & chQuote & "clsid:80CB7887-20DE-11D2-8D5C-00C04FC29D45" & chQuote & vbNewline _
  122. & " CodeBase=" & chQuote & "/CertControl/" + sCPU + "/scrdenrl.dll#Version=<%=sScrdEnrlVersion%>" & chQuote & vbNewline _
  123. & " ID=SCrdEnrl " & vbNewline _
  124. & "></Object>"
  125. 'Alert "About to create:" & vbNewline & sControl
  126. spnSCrdEnrl.innerHTML=sControl
  127. ' begin polling to see if the control is loaded
  128. setTimeout "LoadControlPhase2(" & chQuote & sContinueCmd & chQuote & ")", 1
  129. End Sub
  130. '---------------------------------------------------------------------
  131. ' Wait until the corntrol is loaded
  132. Function LoadControlPhase2(sContinueCmd)
  133. ' continued from above
  134. Dim chQuote, nResult, sErrorNumber, sErrorMessage
  135. chQuote=chr(34)
  136. 'Alert document.SCrdEnrl.readyState
  137. ' is the control loaded?
  138. If 4<>document.SCrdEnrl.readyState Then ' 4=READYSTATE_COMPLETE
  139. ' no, show a message and wait a while
  140. ShowTransientMessage(L_DownloadingControl_Message)
  141. setTimeout "LoadControlPhase2(" & chQuote & sContinueCmd & chQuote & ")", 500
  142. Else
  143. ' yes, hide the message and continue.
  144. HideTransientMessage
  145. ' smoke test the control
  146. nResult=ConfirmSCrdEnrlLoaded
  147. If 0<>nResult Then
  148. If 438=nResult Then
  149. sErrorMessage=L_ControlLoadFailed_ErrorMessage
  150. Else
  151. sErrorNumber="0x" & Hex(nResult)
  152. sErrorMessage=evalErrorMessage(L_ControlLoadFailedEx_ErrorMessage, sErrorNumber)
  153. End If
  154. AbortPage sErrorMessage
  155. Exit Function
  156. End If
  157. execScript sContinueCmd, "VBScript"
  158. End If
  159. End Function
  160. '-----------------------------------------------------------------
  161. ' Test to make sure SCrdEnrl loaded properly by calling a method on it.
  162. ' For best results, the method we call should only be available in the
  163. ' most recent version of the control, however any method will detect
  164. ' failure to create the object.
  165. Function ConfirmSCrdEnrlLoaded()
  166. On Error Resume Next
  167. Dim nTest
  168. nTest=document.SCrdEnrl.CSPCount
  169. ConfirmSCrdEnrlLoaded=Err.Number
  170. End Function
  171. '---------------------------------------------------------------------
  172. ' Set the dynamic status message and the state of the buttons, etc.
  173. Sub ChangePageStatusTo(eStatus)
  174. ' by default, hide everything
  175. spnRetry.style.display="none"
  176. spnViewCert.style.display="none"
  177. spnNewUser.style.display="none"
  178. spnEnroll.style.display="none"
  179. document.UIForm.btnSelectSigningCert.disabled=True
  180. document.UIForm.btnSelectUserName.disabled=True
  181. document.UIForm.btnRetry.disabled=True
  182. document.UIForm.btnViewCert.disabled=True
  183. document.UIForm.btnNewUser.disabled=True
  184. document.UIForm.btnEnroll.disabled=True
  185. document.UIForm.lbCertTemplate.disabled=True
  186. document.UIForm.lbCA.disabled=True
  187. document.UIForm.lbCSP.disabled=True
  188. If e_PageLoading=eStatus Then
  189. spnStatus.innerText=L_PageLoading_Message
  190. ElseIf e_ControlLoading=eStatus Then
  191. spnStatus.innerText=L_ControlLoading_Message
  192. ElseIf e_PageDead=eStatus Then
  193. spnStatus.innerText=evalErrorMessage(L_PageDead_ErrorMessage, g_sPageError)
  194. ElseIf e_PagePreEnroll=eStatus Then
  195. ' enable all the controls
  196. document.UIForm.lbCertTemplate.disabled=False
  197. If False=g_bTemplateBad Then
  198. document.UIForm.lbCA.disabled=False ' don't enable the CA box if there are no CAs
  199. End If
  200. document.UIForm.lbCSP.disabled=False
  201. document.UIForm.btnSelectSigningCert.disabled=False
  202. document.UIForm.btnSelectUserName.disabled=False
  203. ' set the status based upon what the user still must select
  204. If True=g_bUserNameBad Or True=g_bSigningCertBad Or True=g_bTemplateBad Or True=g_bCSPBad Then
  205. If True=g_bTemplateBad Then
  206. If True=g_bSigningCertBad Then
  207. If True=g_bUserNameBad Then
  208. spnStatus.innerText=L_MustSelect111_Message
  209. Else
  210. spnStatus.innerText=L_MustSelect110_Message
  211. End If
  212. Else
  213. If True=g_bUserNameBad Then
  214. spnStatus.innerText=L_MustSelect101_Message
  215. Else
  216. spnStatus.innerText=L_MustSelect100_Message
  217. End If
  218. End If
  219. ElseIf True = g_bCSPBad Then
  220. spnStatus.innerText=L_NoMatchedCSP_Warningmessage
  221. Else
  222. If True=g_bSigningCertBad Then
  223. If True=g_bUserNameBad Then
  224. spnStatus.innerText=L_MustSelect011_Message
  225. Else
  226. spnStatus.innerText=L_MustSelect010_Message
  227. End If
  228. Else
  229. spnStatus.innerText=L_MustSelect001_Message
  230. End If
  231. End If
  232. Else
  233. spnStatus.innerText=L_ReadyToEnroll_Message
  234. spnEnroll.style.display=""
  235. document.UIForm.btnEnroll.disabled=False
  236. End If
  237. ElseIf e_PageEnrolling=eStatus Then
  238. spnStatus.innerText=L_PageEnrolling_Message
  239. ElseIf e_PagePostEnrollOK=eStatus Then
  240. spnStatus.innerText=L_PagePostEnrollOK_Message
  241. spnViewCert.style.display=""
  242. document.UIForm.btnViewCert.disabled=False
  243. spnNewUser.style.display=""
  244. document.UIForm.btnNewUser.disabled=False
  245. ElseIf e_PagePostEnrollError=eStatus Then
  246. ' enable all the controls
  247. document.UIForm.lbCertTemplate.disabled=False
  248. If False=g_bTemplateBad Then
  249. document.UIForm.lbCA.disabled=False ' don't enable the CA box if there are no CAs
  250. End If
  251. document.UIForm.lbCSP.disabled=False
  252. document.UIForm.btnSelectSigningCert.disabled=False
  253. document.UIForm.btnSelectUserName.disabled=False
  254. spnStatus.innerText=evalErrorMessage(L_PagePostEnrollError_ErrorMessage, g_sPageError)
  255. spnRetry.style.display=""
  256. document.UIForm.btnRetry.disabled=False
  257. spnNewUser.style.display=""
  258. document.UIForm.btnNewUser.disabled=False
  259. Else
  260. spnStatus.innerText=L_IntErrBadStatus_ErrorMessage
  261. End If
  262. End Sub
  263. Const SCARD_CTINFO_CSPLIST_FIRST=8
  264. Const SCARD_CTINFO_CSPLIST_NEXT=9
  265. '---------------------------------------------------------------------
  266. ' Populate the template list
  267. Function GetTemplateList
  268. On Error Resume Next
  269. Const SCARD_CTINFO_EXT_OID=3
  270. Const SCARD_CTINFO_ENROLLMENTFLAGS=11
  271. Const SCARD_CTINFO_RA_SIGNATURES=13
  272. Const CT_FLAG_PEND_ALL_REQUESTS=&H00000002
  273. Dim nIndex, sRealName, sDisplayName, nTemplateCount, oElem, bDefaultSet, nRequestedTemplateFlags
  274. Dim sCTEOid, nPendingFlags, fShowTemplate
  275. Dim sSmartCardCSPs, sCSP
  276. ShowTransientMessage L_RetrvTemplateList_Message
  277. nRequestedTemplateFlags=SCARD_ENROLL_USER_CERT_TEMPLATE Or SCARD_ENROLL_ENTERPRISE_CERT_TEMPLATE Or SCARD_ENROLL_CROSS_CERT_TEMPLATE
  278. ' get the number of available templates
  279. nTemplateCount=document.SCrdEnrl.getCertTemplateCount(nRequestedTemplateFlags)
  280. If 0<>Err.Number Then
  281. ' unexpected error
  282. GetTemplateList=Err.Number
  283. AddOption document.UIForm.lbCertTemplate, "(" & L_Unexpected_ErrorMessage & " 0x" & HEX(Err.Number) & ")", ""
  284. document.UIForm.lbCertTemplate.selectedIndex=0
  285. HideTransientMessage
  286. Exit Function
  287. ElseIf 0=nTemplateCount Then
  288. ' No templates found
  289. GetTemplateList=ERROR_NOITEMS 'our own error number
  290. AddOption document.UIForm.lbCertTemplate, L_NoTemplates_ErrorMessage, ""
  291. document.UIForm.lbCertTemplate.selectedIndex=0
  292. HideTransientMessage
  293. Exit Function
  294. End If
  295. If g_fNewStation Then
  296. 'get list of smart card csps
  297. For nIndex=1 To document.SCrdEnrl.CSPCount
  298. If 1 = nIndex Then
  299. sSmartCardCSPs = document.SCrdEnrl.enumCSPName(nIndex-1, FLAGS_NONE)
  300. End If
  301. If 1 <> nIndex Then
  302. sSmartCardCSPs = sSmartCardCSPs & "?" & document.SCrdEnrl.enumCSPName(nIndex-1, FLAGS_NONE)
  303. End If
  304. Next
  305. End If
  306. ' set the default template to be the first one which has a CA
  307. bDefaultSet=False
  308. ' loop over all the available templates and add them to the list box
  309. For nIndex=1 To nTemplateCount
  310. fShowTemplate = True
  311. ' add this template to the list box
  312. sRealName = document.SCrdEnrl.enumCertTemplateName(nIndex-1, nRequestedTemplateFlags Or SCARD_ENROLL_CERT_TEMPLATE_REAL_NAME)
  313. 'check to see if V2 template
  314. sCTEOid = document.ScrdEnrl.getCertTemplateInfo(sRealName, SCARD_CTINFO_EXT_OID)
  315. If "" <> sCTEOid Then
  316. 'check to see if pending
  317. nPendingFlags = document.ScrdEnrl.getCertTemplateInfo(sRealName, SCARD_CTINFO_ENROLLMENTFLAGS)
  318. nPendingFlags = CT_FLAG_PEND_ALL_REQUESTS And nPendingFlags
  319. If 0 <> nPendingFlags Then
  320. 'don't teake pending template
  321. fShowTemplate = False
  322. End If
  323. If True=fShowTemplate Then
  324. If 1 <> document.ScrdEnrl.getCertTemplateInfo(sRealName, SCARD_CTINFO_RA_SIGNATURES) Then
  325. fShowTemplate = False
  326. End If
  327. End If
  328. End If
  329. If g_fNewStation Then
  330. sCSP = Empty
  331. sCSP = document.SCrdEnrl.getCertTemplateInfo(sRealName, SCARD_CTINFO_CSPLIST_FIRST)
  332. If True = fShowTemplate And Not IsEmpty(sCSP) Then
  333. fShowTemplate = False
  334. 'check to see if any matched CSP
  335. While Not fShowTemplate And Not IsEmpty(sCSP)
  336. If 0 <> InStr(sSmartCardCSPs, sCSP) Then
  337. fShowTemplate = True
  338. End If
  339. If False = fShowTemplate Then
  340. sCSP = Empty
  341. sCSP = document.SCrdEnrl.getCertTemplateInfo(sRealName, SCARD_CTINFO_CSPLIST_NEXT)
  342. End If
  343. Wend
  344. End If
  345. End If
  346. If True = fShowTemplate Then
  347. sDisplayName=document.SCrdEnrl.enumCertTemplateName(nIndex-1, nRequestedTemplateFlags Or SCARD_ENROLL_CERT_TEMPLATE_DISPLAY_NAME)
  348. 'Alert "r:" & sRealName & " d:" & sDisplay Name
  349. AddOption document.UIForm.lbCertTemplate, sDisplayName, sRealName
  350. ' if we haven't set the default and this template has a CA, make it the default
  351. If False=bDefaultSet And 0<>document.SCrdEnrl.getCACount(sRealName) Then
  352. document.UIForm.lbCertTemplate.selectedIndex=nIndex-1
  353. bDefaultSet=True
  354. End If
  355. End If
  356. Next
  357. 'just select the first template
  358. document.UIForm.lbCertTemplate.selectedIndex=0
  359. ' we were successful
  360. GetTemplateList=0
  361. HideTransientMessage
  362. End Function
  363. '---------------------------------------------------------------------
  364. ' Populate the CSP list
  365. Function UpdateCSPList
  366. On Error Resume Next
  367. Dim nIndex, sName, nCSPCount, oElem
  368. Dim nListLength, sTemplateSupportedCSPs, sCSP, sCurrentTemplate
  369. 'init
  370. g_bCSPBad = False
  371. ShowTransientMessage L_RetrvCSPList_Message
  372. 'remove the current csp list
  373. nListLength=document.UIForm.lbCSP.Options.length
  374. For nIndex=1 To nListLength
  375. ' note that we keep deleting element 0 since
  376. ' the other options are automatically moved up one
  377. document.UIForm.lbCSP.Options.remove(0)
  378. Next
  379. ' get the number of available CSPs
  380. nCSPCount=document.SCrdEnrl.CSPCount
  381. If 0<>Err.Number Then
  382. ' unexpected error
  383. g_bCSPBad = True
  384. UpdateCSPList=Err.Number
  385. AddOption document.UIForm.lbCSP, "(" & L_Unexpected_ErrorMessage & " 0x" & HEX(Err.Number) & ")", ""
  386. document.UIForm.lbCSP.selectedIndex=0
  387. HideTransientMessage
  388. Exit Function
  389. ElseIf 0=nCSPCount Then
  390. ' No CSPs found
  391. g_bCSPBad = True
  392. UpdateCSPList=ERROR_NOITEMS
  393. AddOption document.UIForm.lbCSP, L_NoCSPs_ErrorMessage, ""
  394. document.UIForm.lbCSP.selectedIndex=0
  395. HideTransientMessage
  396. Exit Function
  397. End If
  398. If g_fNewStation Then
  399. 'template change, update csp list
  400. sTemplateSupportedCSPs = Empty
  401. sCurrentTemplate=document.UIForm.lbCertTemplate.value
  402. 'get csp list separated by ?
  403. sCSP = document.SCrdEnrl.getCertTemplateInfo(sCurrentTemplate, SCARD_CTINFO_CSPLIST_FIRST)
  404. While Not IsEmpty(sCSP)
  405. If IsEmpty(sTemplateSupportedCSPs) Then
  406. sTemplateSupportedCSPs = sCSP
  407. Else
  408. sTemplateSupportedCSPs = sTemplateSupportedCSPs & "?" & sCSP
  409. End If
  410. sCSP = Empty
  411. sCSP = document.SCrdEnrl.getCertTemplateInfo(sCurrentTemplate, SCARD_CTINFO_CSPLIST_NEXT)
  412. Wend
  413. End If
  414. ' loop over all the available CSPs and add them to the list box
  415. For nIndex=1 To nCSPCount
  416. sName=document.SCrdEnrl.enumCSPName(nIndex-1, FLAGS_NONE)
  417. If Not g_fNewStation Then
  418. AddOption document.UIForm.lbCSP, sName, sName
  419. End If
  420. If g_fNewStation Then
  421. If IsEmpty(sTemplateSupportedCSPs) Then
  422. AddOption document.UIForm.lbCSP, sName, sName
  423. End If
  424. If Not IsEmpty(sTemplateSupportedCSPs) Then
  425. If 0 <> InStr(stemplateSupportedCSPs, sName) Then
  426. AddOption document.UIForm.lbCSP, sName, sName
  427. End If
  428. End If
  429. End If
  430. Next
  431. If 0 = document.UIForm.lbCSP.Options.length Then
  432. ' No macthed CSP
  433. g_bCSPBad = True
  434. UpdateCSPList = ERROR_NOMATCHEDCSP
  435. AddOption document.UIForm.lbCSP, L_NoMatchedCSP_ErrorMessage, ""
  436. document.UIForm.lbCSP.selectedIndex = 0
  437. HideTransientMessage
  438. Exit Function
  439. End If
  440. 'set the default CSP selection
  441. document.UIForm.lbCSP.selectedIndex=0
  442. ' we were successful
  443. UpdateCSPList=0
  444. HideTransientMessage
  445. End Function
  446. '---------------------------------------------------------------------
  447. ' Update the CA list based upon the currently selected cert template
  448. Sub HandleTemplateChange
  449. On Error Resume Next
  450. Dim sCurrentTemplate, nListLength, nIndex, nCACount, sUniqueName, sDisplayName
  451. Dim nResult
  452. ShowTransientMessage L_RetrvCAList_Message
  453. sCurrentTemplate=document.UIForm.lbCertTemplate.value
  454. ' delete the current CA selection list
  455. nListLength=document.UIForm.lbCA.Options.length
  456. For nIndex=1 To nListLength
  457. ' note that we keep deleting element 0 since
  458. ' the other options are automatically moved up one
  459. document.UIForm.lbCA.Options.remove(0)
  460. Next
  461. 'update the CA list based on the CertType
  462. nCACount=document.SCrdEnrl.getCACount(sCurrentTemplate)
  463. If 0=nCACount Then
  464. AddOption document.UIForm.lbCA, L_NoCAs_ErrorMessage, ""
  465. g_bTemplateBad=True
  466. Else
  467. ' loop over all the available CAs and add them to the list box
  468. For nIndex=1 To nCACount
  469. sUniqueName=document.SCrdEnrl.enumCAName(nIndex-1, SCARD_ENROLL_CA_UNIQUE_NAME, sCurrentTemplate)
  470. sDisplayName=document.SCrdEnrl.enumCAName(nIndex-1, SCARD_ENROLL_CA_DISPLAY_NAME, sCurrentTemplate)
  471. 'alert "r:" & sRealName & " d:" & sDisplayName
  472. AddOption document.UIForm.lbCA, sDisplayName, sUniqueName
  473. Next
  474. g_bTemplateBad=False
  475. End If
  476. 'set the default CertType selection
  477. document.UIForm.lbCA.selectedIndex=0
  478. ' get the CSP list
  479. nResult=UpdateCSPList()
  480. ' make sure focus stays put
  481. document.UIForm.lbCertTemplate.focus
  482. If ERROR_NOMATCHEDCSP = nResult Then
  483. 'don't abort the page
  484. nResult = 0
  485. End If
  486. If 0<>nResult Then
  487. AbortPage evalErrorMessage(L_RetrvCSPList_ErrorMessage, "(0x" & HEX(nResult) & ")")
  488. Exit Sub
  489. End If
  490. HideTransientMessage
  491. ' refresh the state - may now be able to enroll
  492. ChangePageStatusTo e_PagePreEnroll
  493. End Sub
  494. '---------------------------------------------------------------------
  495. ' Create a new select option add it to the list box
  496. Sub AddOption(lbTarget, sText, sValue)
  497. Dim oElem
  498. Set oElem=document.createElement("Option")
  499. oElem.text=sText
  500. oElem.value=sValue
  501. lbTarget.Options.Add oElem
  502. End Sub
  503. '---------------------------------------------------------------------
  504. ' Indicate a catastrophic error and don't let the user do anything further
  505. Sub AbortPage(sMessage)
  506. g_sPageError=sMessage
  507. ChangePageStatusTo e_PageDead
  508. Alert sMessage
  509. End Sub
  510. '---------------------------------------------------------------------
  511. ' Show a transient message
  512. Sub ShowTransientMessage(sMessage)
  513. window.status=sMessage
  514. End Sub
  515. '---------------------------------------------------------------------
  516. ' Hide the last transient message
  517. Sub HideTransientMessage
  518. window.status=""
  519. End Sub
  520. '---------------------------------------------------------------------
  521. ' Set up everything after the page loads
  522. Sub PostLoad
  523. ' set the page status message and disable the controls
  524. ChangePageStatusTo e_ControlLoading
  525. ' set the page-global variables
  526. g_bUserNameBad=True
  527. g_bSigningCertBad=True
  528. g_bTemplateBad=True
  529. g_bCSPBad = True
  530. g_fNewStation = True
  531. ' load the ActiveX control
  532. LoadControl "PostLoadPhase2()"
  533. End Sub
  534. '---------------------------------------------------------------------
  535. ' set the status message and continue setting up
  536. Sub PostLoadPhase2
  537. ' set the page status message and disable the controls
  538. ChangePageStatusTo e_PageLoading
  539. ' allow IE to update the screen
  540. setTimeout "PostLoadPhase3", 1
  541. End Sub
  542. '---------------------------------------------------------------------
  543. ' Set up the CSP list and the template list
  544. Sub PostLoadPhase3
  545. On Error Resume Next
  546. Dim nResult
  547. ' get the template list
  548. nResult=GetTemplateList()
  549. If 0<>nResult Then
  550. If ERROR_NOITEMS=nResult Then
  551. AbortPage L_NoTemplatesLong_ErrorMessage
  552. Else
  553. AbortPage evalErrorMessage(L_RetrvTemplateList_ErrorMessage, "(0x" & HEX(nResult) & ")")
  554. End If
  555. Exit Sub
  556. End If
  557. ' update the CA list to reflect the currently selected template
  558. HandleTemplateChange
  559. ' set the default signing certificate
  560. If 0 = InStr(navigator.userAgent, "Windows NT 5.1") Then
  561. document.SCrdEnrl.setSigningCertificate FLAGS_NONE, "EnrollmentAgent"
  562. Else
  563. document.SCrdEnrl.setSigningCertificate SCARD_SELECT_EKU, "1.3.6.1.4.1.311.20.2.1"
  564. End If
  565. ' ignore errors: if no default signing certificate, the user must pick one.
  566. Err.Clear
  567. ' make the page reflect the default signing certificate
  568. document.UIForm.tbSigningCert.value=document.SCrdEnrl.getSigningCertificateName(SCARD_ENROLL_NO_DISPLAY_CERT)
  569. g_bSigningCertBad=False
  570. If 0<>Err.Number Or ""=document.UIForm.tbSigningCert.value Then
  571. document.UIForm.tbSigningCert.value=L_NoCertSelected_Message
  572. g_bSigningCertBad=True
  573. End If
  574. ' finished setting up, enable controls
  575. ChangePageStatusTo e_PagePreEnroll
  576. End Sub
  577. '---------------------------------------------------------------------
  578. ' Select a signing certificate
  579. Sub SelectSigningCert
  580. On Error Resume Next
  581. ' ask SCrdEnrl to throw up UI to pick a signing cert
  582. If 0 = InStr(navigator.userAgent, "Windows NT 5.1") Then
  583. document.SCrdEnrl.selectSigningCertificate FLAGS_NONE, "EnrollmentAgent"
  584. Else
  585. document.SCrdEnrl.selectSigningCertificate SCARD_SELECT_EKU, "1.3.6.1.4.1.311.20.2.1"
  586. End If
  587. If 0<>Err.Number Then
  588. Alert evalErrorMessage(L_SelectSignCert_ErrorMessage, "(0x" & HEX(Err.Number) & ")")
  589. End If
  590. ' make the page reflect what the user picked
  591. document.UIForm.tbSigningCert.value=document.SCrdEnrl.getSigningCertificateName(SCARD_ENROLL_NO_DISPLAY_CERT)
  592. g_bSigningCertBad=False
  593. If 0<>Err.Number Or ""=document.UIForm.tbSigningCert.value Then
  594. document.UIForm.tbSigningCert.value=L_NoCertSelected_Message
  595. g_bSigningCertBad=True
  596. End If
  597. ' refresh the state - may now be able to enroll
  598. ChangePageStatusTo e_PagePreEnroll
  599. End Sub
  600. '---------------------------------------------------------------------
  601. ' Select a user name
  602. Sub SelectUserName
  603. On Error Resume Next
  604. ' ask SCrdEnrl to throw up UI to pick a user
  605. document.SCrdEnrl.selectUserName(FLAGS_NONE)
  606. If 0<>Err.Number Then
  607. Alert evalErrorMessage(L_SelectUser_ErrorMessage, "(0x" & HEX(Err.Number) & ")")
  608. End If
  609. ' make the page reflect what the user picked
  610. document.UIForm.tbUserName.value=document.SCrdEnrl.getUserName(SCARD_ENROLL_UPN_NAME)
  611. If 0<>Err.Number Then
  612. 'If we can not get the UPN name, get the SAM compatible name
  613. Err.Clear
  614. document.UIForm.tbUserName.value=document.SCrdEnrl.getUserName(SCARD_ENROLL_SAM_COMPATIBLE_NAME)
  615. End If
  616. g_bUserNameBad=False
  617. If 0<>Err.Number Or ""=document.UIForm.tbUserName.value Then
  618. document.UIForm.tbUserName.value=L_NoUserSelected_Message
  619. g_bUserNameBad=True
  620. End If
  621. ' refresh the state - may now be able to enroll
  622. ChangePageStatusTo e_PagePreEnroll
  623. End Sub
  624. '---------------------------------------------------------------------
  625. ' Verify all the user input and begin the enrollment process
  626. Sub Enroll
  627. On Error Resume Next
  628. Dim sTemplateName, sCAName, sCSPName, UserName, LenResult
  629. ' check to make sure all the fields are OK
  630. If True=g_bUserNameBad Or True=g_bSigningCertBad Or True=g_bTemplateBad Or True = g_bCSPBad Then
  631. AbortPage L_IntErrBadData_ErrorMessage
  632. Exit Sub
  633. End If
  634. ' tell SCrdEnrl what the user picked from the list boxes
  635. ' these should all be OK, since SCrdEnrl gave us these options
  636. ' set the template
  637. sTemplateName=document.UIForm.lbCertTemplate.value
  638. document.SCrdEnrl.setCertTemplateName SCARD_ENROLL_CERT_TEMPLATE_REAL_NAME, sTemplateName
  639. ' set the CA name
  640. sCAName=document.UIForm.lbCA.value
  641. document.SCrdEnrl.setCAName SCARD_ENROLL_CA_UNIQUE_NAME, sTemplateName, sCAName
  642. ' set the CSP
  643. sCSPName=document.UIForm.lbCSP.value
  644. document.SCrdEnrl.CSPName=sCSPName
  645. ' make sure SCrdEnrl is still happy so far
  646. If 0<>Err.Number Then
  647. AbortPage L_IntErrSCrdEnrlError_ErrorMessage
  648. Exit Sub
  649. End If
  650. ' signing cert is already set
  651. ' user name is already set
  652. ' everything looks great
  653. ' change the status
  654. ChangePageStatusTo e_PageEnrolling
  655. ShowTransientMessage L_Enrolling_Message
  656. ' give IE time to repaint the screen, then continue enrolling
  657. setTimeout "EnrollPhase2", 1
  658. End Sub
  659. '---------------------------------------------------------------------
  660. ' Finish the enrollment process
  661. Sub EnrollPhase2
  662. On Error Resume Next
  663. Const CR_DISP_ISSUED = 3
  664. Dim EnrollStatus
  665. Dim EnrollErr, sErrDescription
  666. ' actually do the enrollment
  667. document.SCrdEnrl.enroll(FLAGS_NONE)
  668. EnrollErr = Err.Number
  669. If 0 <> EnrollErr Then
  670. sErrDescription = Err.Description
  671. End If
  672. EnrollStatus = document.ScrdEnrl.EnrollmentStatus
  673. ' check for errors
  674. If 0=EnrollErr And CR_DISP_ISSUED=EnrollStatus Then
  675. ' No errors. Yay!
  676. ChangePageStatusTo e_PagePostEnrollOK
  677. HideTransientMessage
  678. Else
  679. ' ERROR:
  680. ' determine what went wrong, by known error numbers
  681. Dim sError
  682. sError=Hex(EnrollErr)
  683. If 0=strComp(sError, "80100017") Then
  684. g_sPageError=L_EnrlErrNotEnoughReaders_ErrorMessage
  685. ElseIf 0=strComp(sError, "8010000C") Then
  686. g_sPageError=L_EnrlErrInsertCard_ErrorMessage
  687. ElseIf 0=strComp(sError, "80070004") Then
  688. g_sPageError=L_EnrlErrWrongCards_ErrorMessage
  689. ElseIf 0=strComp(sError, "8010001C") Then
  690. g_sPageError=L_EnrlErrCryptoError_ErrorMessage
  691. ElseIf 0=strComp(sError, "8010002C") Then
  692. g_sPageError=L_EnrlErrNoSignCert_ErrorMessage
  693. ElseIf 0=strComp(sError, "8010000F") Then
  694. g_sPageError=L_EnrlErrCardCSPMismatch_ErrorMessage
  695. ElseIf 0=strComp(sError, "80100069") Then
  696. g_sPageError=L_EnrlErrRemoveCard_ErrorMessage
  697. ElseIf 0=strComp(sError, "80092022") Then
  698. g_sPageError=L_EnrlErrCantEncode_ErrorMessage
  699. Else
  700. g_sPageError=evalErrorMessage(L_EnrlErrUnexpected_ErrorMessage, "(0x" & sError & ")." & vbNewLine & sErrDescription)
  701. End If
  702. ' Throw an alert box and change the page to show the error
  703. ChangePageStatusTo e_PagePostEnrollError
  704. Alert g_sPageError
  705. HideTransientMessage
  706. End If
  707. End Sub
  708. '---------------------------------------------------------------------
  709. ' View the enrolled certificate
  710. Sub ViewCert
  711. On Error Resume Next
  712. document.SCrdEnrl.getEnrolledCertificateName(SCARD_ENROLL_DISPLAY_CERT)
  713. End Sub
  714. '---------------------------------------------------------------------
  715. ' Reset a new user
  716. Sub NewUser
  717. On Error Resume Next
  718. ' get rid of the old user name
  719. document.SCrdEnrl.resetUser()
  720. document.UIForm.tbUserName.value=L_NoUserSelected_Message
  721. g_bUserNameBad=True
  722. ' refresh the state
  723. ChangePageStatusTo e_PagePreEnroll
  724. End Sub
  725. </Script>
  726. <Script Language="JavaScript">
  727. //--------------------------------------------------------------------
  728. // perform substitution on the error string, because VBScript cannot
  729. function evalErrorMessage(sMessage, sError) {
  730. return eval(sMessage);
  731. }
  732. </Script>
  733. </Head>
  734. <Body Language="VBScript" OnLoad="PostLoad" BgColor=#FFFFFF Link=#0000FF VLink=#0000FF ALink=#0000FF><Font ID=locPageFont Face="Arial">
  735. <Table Border=0 CellSpacing=0 CellPadding=4 Width=100% BgColor=#008080>
  736. <TR>
  737. <TD><Font Color=#FFFFFF><LocID ID=locMSCertSrv><Font Face="Arial" Size=-1><B><I>Microsoft</I></B> Certificate Services</Font></LocID></Font></TD>
  738. <TD ID=locHomeAlign Align=Right><A Href="/certsrv"><Font Color=#FFFFFF><LocID ID=locHomeLink><Font Face="Arial" Size=-1><B>Home</B></Font></LocID></Font></A></TD>
  739. </TR>
  740. </Table>
  741. <P ID=locPageTitle> <B> Smart Card Certificate Enrollment Station </B>
  742. <!-- Green HR --><Table Border=0 CellSpacing=0 CellPadding=0 Width=100%><TR><TD BgColor=#008080><Img Src="certspc.gif" Alt="" Height=2 Width=0></TD></TR></Table>
  743. <Form Name=UIForm>
  744. <Table Border=0 Width=100%>
  745. <!-- subheading -->
  746. <TR><TD Colspan=2>
  747. <Table Border=0 CellPadding=0 CellSpacing=0 Width=100%>
  748. <TR><TD ID=locOptHead><Font Size=-1><B>Enrollment Options:</B></Font></TD></TR>
  749. <TR><TD Height=2 BgColor=#008080></TD></TR>
  750. </Table>
  751. </TD></TR>
  752. <TR>
  753. <TD ID=locTemplateLabel Align=Right><Font Size=-1>Certificate Template:</Font></TD>
  754. <TD><Select Name=lbCertTemplate OnChange="HandleTemplateChange" Language="VBScript"></Select></TD>
  755. </TR>
  756. <TR>
  757. <TD ID=locCALabel Align=Right><Font Size=-1>Certification Authority:</Font></TD>
  758. <TD><Select Name=lbCA></Select></TD>
  759. </TR>
  760. <TR>
  761. <TD ID=locCSPLabel Align=Right><Font Size=-1>Cryptographic <BR>Service Provider:</Font></TD>
  762. <TD><Select Name=lbCSP></Select></TD>
  763. </TR>
  764. <TR>
  765. <TD ID=locSigningCertLabel Align=Right><Font Size=-1>Administrator <BR>Signing Certificate:</Font></TD>
  766. <TD><Input ID=locTbSigningCert Type=Text Size=45 Name=tbSigningCert ReadOnly Disabled Value="(No certificate selected)" Title="Please click on the Select Certificate button to select a signing certificate">
  767. <Input ID=locBtnSelectSigningCert Type=Button Name=btnSelectSigningCert Value="Select Certificate..." OnClick="SelectSigningCert" Language="VBScript"></TD>
  768. </TR>
  769. <!-- subheading -->
  770. <TR><TD ColSpan=2>
  771. <Table Border=0 CellPadding=0 CellSpacing=0 Width=100%>
  772. <TR><TD ID=locUserHead><Font Size=-1><BR><B>User To Enroll:</B></Font></TD></TR>
  773. <TR><TD Height=2 BgColor=#008080></TD></TR>
  774. </Table>
  775. </TD></TR>
  776. <TR>
  777. <TD ID=locUsrAlign Align=Right></TD>
  778. <TD><Input ID=locTbUserName Type=Text Size=45 Name=tbUserName ReadOnly Disabled Value="(No user selected)" Title="Please click on the Select User button to select a user">
  779. <Input ID=locBtnSelectUserName Type=Button Name=btnSelectUserName Value="Select User..." OnClick="SelectUserName" Language="VBScript"></TD>
  780. </TR>
  781. </Table>
  782. <!-- status area -->
  783. <Table Border=0 CellPadding=0 CellSpacing=0 Width=100%>
  784. <TR><TD ID=locStatusHead><Font Size=-1><BR><B>Status:</B></Font></TD></TR>
  785. <TR><TD Height=2 BgColor=#008080></TD></TR>
  786. </Table>
  787. <P><Span ID=spnStatus></Span></P>
  788. <!-- Green HR --><Table Border=0 CellSpacing=0 CellPadding=0 Width=100%><TR><TD BgColor=#008080><Img Src="certspc.gif" Alt="" Height=2 Width=0></TD></TR></Table>
  789. <!-- White HR --><Table Border=0 CellSpacing=0 CellPadding=0 Width=100%><TR><TD BgColor=#FFFFFF><Img Src="certspc.gif" Alt="" Height=5 Width=0></TD></TR></Table>
  790. <Table Width=100% Border=0 CellPadding=0 CellSpacing=0><TR><TD ID=locButtonAlign Align=Right>
  791. <Span ID=spnRetry Style="display:none">
  792. <LocID><Input ID=locBtnRetry Type=Button Name=btnRetry Value="Retry" OnClick="Enroll" Language="VBScript">
  793. &nbsp;</LocID>
  794. </Span>
  795. <Span ID=spnViewCert Style="display:none">
  796. <LocID><Input ID=locBtnViewCert Type=Button Name=btnViewCert Value="View Certificate" OnClick="ViewCert" Language="VBScript">
  797. &nbsp;</LocID>
  798. </Span>
  799. <Span ID=spnNewUser Style="display:none">
  800. <LocID><Input ID=locBtnNewUser Type=Button Name=btnNewUser Value="New User" OnClick="NewUser" Language="VBScript">
  801. &nbsp;</LocID>
  802. </Span>
  803. <Span ID=spnEnroll Style="display:none">
  804. <LocID><Input ID=locBtnEnroll Type=Button Name=btnEnroll Value="Enroll" OnClick="Enroll" Language="VBScript">
  805. &nbsp;</LocID>
  806. </Span>
  807. <LocID ID=locSpc1>&nbsp;&nbsp;&nbsp;&nbsp;<LocID>
  808. </TD></TR></Table>
  809. <Span ID=spnSCrdEnrl Style="display:none"><!-- The control will be placed here --></Span>
  810. </Form>
  811. </Font>
  812. </Body>
  813. </HTML>