error.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // BSD 3-Clause License
  2. //
  3. // Copyright (c) 2019, Guillaume Ballet
  4. // All rights reserved.
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are met:
  8. //
  9. // * Redistributions of source code must retain the above copyright notice, this
  10. // list of conditions and the following disclaimer.
  11. //
  12. // * Redistributions in binary form must reproduce the above copyright notice,
  13. // this list of conditions and the following disclaimer in the documentation
  14. // and/or other materials provided with the distribution.
  15. //
  16. // * Neither the name of the copyright holder nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. package pcsc
  31. import "fmt"
  32. type ErrorCode uint32
  33. const (
  34. SCardSuccess ErrorCode = 0x00000000 /* No error was encountered. */
  35. ErrSCardInternal = 0x80100001 /* An internal consistency check failed. */
  36. ErrSCardCancelled = 0x80100002 /* The action was cancelled by an SCardCancel request. */
  37. ErrSCardInvalidHandle = 0x80100003 /* The supplied handle was invalid. */
  38. ErrSCardInvalidParameter = 0x80100004 /* One or more of the supplied parameters could not be properly interpreted. */
  39. ErrSCardInvalidTarget = 0x80100005 /* Registry startup information is missing or invalid. */
  40. ErrSCardNoMemory = 0x80100006 /* Not enough memory available to complete this command. */
  41. ErrSCardWaitedTooLong = 0x80100007 /* An internal consistency timer has expired. */
  42. ErrSCardInsufficientBuffer = 0x80100008 /* The data buffer to receive returned data is too small for the returned data. */
  43. ErrScardUnknownReader = 0x80100009 /* The specified reader name is not recognized. */
  44. ErrSCardTimeout = 0x8010000A /* The user-specified timeout value has expired. */
  45. ErrSCardSharingViolation = 0x8010000B /* The smart card cannot be accessed because of other connections outstanding. */
  46. ErrSCardNoSmartCard = 0x8010000C /* The operation requires a Smart Card, but no Smart Card is currently in the device. */
  47. ErrSCardUnknownCard = 0x8010000D /* The specified smart card name is not recognized. */
  48. ErrSCardCannotDispose = 0x8010000E /* The system could not dispose of the media in the requested manner. */
  49. ErrSCardProtoMismatch = 0x8010000F /* The requested protocols are incompatible with the protocol currently in use with the smart card. */
  50. ErrSCardNotReady = 0x80100010 /* The reader or smart card is not ready to accept commands. */
  51. ErrSCardInvalidValue = 0x80100011 /* One or more of the supplied parameters values could not be properly interpreted. */
  52. ErrSCardSystemCancelled = 0x80100012 /* The action was cancelled by the system, presumably to log off or shut down. */
  53. ErrSCardCommError = 0x80100013 /* An internal communications error has been detected. */
  54. ErrScardUnknownError = 0x80100014 /* An internal error has been detected, but the source is unknown. */
  55. ErrSCardInvalidATR = 0x80100015 /* An ATR obtained from the registry is not a valid ATR string. */
  56. ErrSCardNotTransacted = 0x80100016 /* An attempt was made to end a non-existent transaction. */
  57. ErrSCardReaderUnavailable = 0x80100017 /* The specified reader is not currently available for use. */
  58. ErrSCardShutdown = 0x80100018 /* The operation has been aborted to allow the server application to exit. */
  59. ErrSCardPCITooSmall = 0x80100019 /* The PCI Receive buffer was too small. */
  60. ErrSCardReaderUnsupported = 0x8010001A /* The reader driver does not meet minimal requirements for support. */
  61. ErrSCardDuplicateReader = 0x8010001B /* The reader driver did not produce a unique reader name. */
  62. ErrSCardCardUnsupported = 0x8010001C /* The smart card does not meet minimal requirements for support. */
  63. ErrScardNoService = 0x8010001D /* The Smart card resource manager is not running. */
  64. ErrSCardServiceStopped = 0x8010001E /* The Smart card resource manager has shut down. */
  65. ErrSCardUnexpected = 0x8010001F /* An unexpected card error has occurred. */
  66. ErrSCardUnsupportedFeature = 0x8010001F /* This smart card does not support the requested feature. */
  67. ErrSCardICCInstallation = 0x80100020 /* No primary provider can be found for the smart card. */
  68. ErrSCardICCCreateOrder = 0x80100021 /* The requested order of object creation is not supported. */
  69. ErrSCardDirNotFound = 0x80100023 /* The identified directory does not exist in the smart card. */
  70. ErrSCardFileNotFound = 0x80100024 /* The identified file does not exist in the smart card. */
  71. ErrSCardNoDir = 0x80100025 /* The supplied path does not represent a smart card directory. */
  72. ErrSCardNoFile = 0x80100026 /* The supplied path does not represent a smart card file. */
  73. ErrScardNoAccess = 0x80100027 /* Access is denied to this file. */
  74. ErrSCardWriteTooMany = 0x80100028 /* The smart card does not have enough memory to store the information. */
  75. ErrSCardBadSeek = 0x80100029 /* There was an error trying to set the smart card file object pointer. */
  76. ErrSCardInvalidCHV = 0x8010002A /* The supplied PIN is incorrect. */
  77. ErrSCardUnknownResMNG = 0x8010002B /* An unrecognized error code was returned from a layered component. */
  78. ErrSCardNoSuchCertificate = 0x8010002C /* The requested certificate does not exist. */
  79. ErrSCardCertificateUnavailable = 0x8010002D /* The requested certificate could not be obtained. */
  80. ErrSCardNoReadersAvailable = 0x8010002E /* Cannot find a smart card reader. */
  81. ErrSCardCommDataLost = 0x8010002F /* A communications error with the smart card has been detected. Retry the operation. */
  82. ErrScardNoKeyContainer = 0x80100030 /* The requested key container does not exist on the smart card. */
  83. ErrSCardServerTooBusy = 0x80100031 /* The Smart Card Resource Manager is too busy to complete this operation. */
  84. ErrSCardUnsupportedCard = 0x80100065 /* The reader cannot communicate with the card, due to ATR string configuration conflicts. */
  85. ErrSCardUnresponsiveCard = 0x80100066 /* The smart card is not responding to a reset. */
  86. ErrSCardUnpoweredCard = 0x80100067 /* Power has been removed from the smart card, so that further communication is not possible. */
  87. ErrSCardResetCard = 0x80100068 /* The smart card has been reset, so any shared state information is invalid. */
  88. ErrSCardRemovedCard = 0x80100069 /* The smart card has been removed, so further communication is not possible. */
  89. ErrSCardSecurityViolation = 0x8010006A /* Access was denied because of a security violation. */
  90. ErrSCardWrongCHV = 0x8010006B /* The card cannot be accessed because the wrong PIN was presented. */
  91. ErrSCardCHVBlocked = 0x8010006C /* The card cannot be accessed because the maximum number of PIN entry attempts has been reached. */
  92. ErrSCardEOF = 0x8010006D /* The end of the smart card file has been reached. */
  93. ErrSCardCancelledByUser = 0x8010006E /* The user pressed "Cancel" on a Smart Card Selection Dialog. */
  94. ErrSCardCardNotAuthenticated = 0x8010006F /* No PIN was presented to the smart card. */
  95. )
  96. // Code returns the error code, with an uint32 type to be used in PutUInt32
  97. func (code ErrorCode) Code() uint32 {
  98. return uint32(code)
  99. }
  100. func (code ErrorCode) Error() error {
  101. switch code {
  102. case SCardSuccess:
  103. return fmt.Errorf("Command successful")
  104. case ErrSCardInternal:
  105. return fmt.Errorf("Internal error")
  106. case ErrSCardCancelled:
  107. return fmt.Errorf("Command cancelled")
  108. case ErrSCardInvalidHandle:
  109. return fmt.Errorf("Invalid handle")
  110. case ErrSCardInvalidParameter:
  111. return fmt.Errorf("Invalid parameter given")
  112. case ErrSCardInvalidTarget:
  113. return fmt.Errorf("Invalid target given")
  114. case ErrSCardNoMemory:
  115. return fmt.Errorf("Not enough memory")
  116. case ErrSCardWaitedTooLong:
  117. return fmt.Errorf("Waited too long")
  118. case ErrSCardInsufficientBuffer:
  119. return fmt.Errorf("Insufficient buffer")
  120. case ErrScardUnknownReader:
  121. return fmt.Errorf("Unknown reader specified")
  122. case ErrSCardTimeout:
  123. return fmt.Errorf("Command timeout")
  124. case ErrSCardSharingViolation:
  125. return fmt.Errorf("Sharing violation")
  126. case ErrSCardNoSmartCard:
  127. return fmt.Errorf("No smart card inserted")
  128. case ErrSCardUnknownCard:
  129. return fmt.Errorf("Unknown card")
  130. case ErrSCardCannotDispose:
  131. return fmt.Errorf("Cannot dispose handle")
  132. case ErrSCardProtoMismatch:
  133. return fmt.Errorf("Card protocol mismatch")
  134. case ErrSCardNotReady:
  135. return fmt.Errorf("Subsystem not ready")
  136. case ErrSCardInvalidValue:
  137. return fmt.Errorf("Invalid value given")
  138. case ErrSCardSystemCancelled:
  139. return fmt.Errorf("System cancelled")
  140. case ErrSCardCommError:
  141. return fmt.Errorf("RPC transport error")
  142. case ErrScardUnknownError:
  143. return fmt.Errorf("Unknown error")
  144. case ErrSCardInvalidATR:
  145. return fmt.Errorf("Invalid ATR")
  146. case ErrSCardNotTransacted:
  147. return fmt.Errorf("Transaction failed")
  148. case ErrSCardReaderUnavailable:
  149. return fmt.Errorf("Reader is unavailable")
  150. /* case SCARD_P_SHUTDOWN: */
  151. case ErrSCardPCITooSmall:
  152. return fmt.Errorf("PCI struct too small")
  153. case ErrSCardReaderUnsupported:
  154. return fmt.Errorf("Reader is unsupported")
  155. case ErrSCardDuplicateReader:
  156. return fmt.Errorf("Reader already exists")
  157. case ErrSCardCardUnsupported:
  158. return fmt.Errorf("Card is unsupported")
  159. case ErrScardNoService:
  160. return fmt.Errorf("Service not available")
  161. case ErrSCardServiceStopped:
  162. return fmt.Errorf("Service was stopped")
  163. /* case SCARD_E_UNEXPECTED: */
  164. /* case SCARD_E_ICC_CREATEORDER: */
  165. /* case SCARD_E_UNSUPPORTED_FEATURE: */
  166. /* case SCARD_E_DIR_NOT_FOUND: */
  167. /* case SCARD_E_NO_DIR: */
  168. /* case SCARD_E_NO_FILE: */
  169. /* case SCARD_E_NO_ACCESS: */
  170. /* case SCARD_E_WRITE_TOO_MANY: */
  171. /* case SCARD_E_BAD_SEEK: */
  172. /* case SCARD_E_INVALID_CHV: */
  173. /* case SCARD_E_UNKNOWN_RES_MNG: */
  174. /* case SCARD_E_NO_SUCH_CERTIFICATE: */
  175. /* case SCARD_E_CERTIFICATE_UNAVAILABLE: */
  176. case ErrSCardNoReadersAvailable:
  177. return fmt.Errorf("Cannot find a smart card reader")
  178. /* case SCARD_E_COMM_DATA_LOST: */
  179. /* case SCARD_E_NO_KEY_CONTAINER: */
  180. /* case SCARD_E_SERVER_TOO_BUSY: */
  181. case ErrSCardUnsupportedCard:
  182. return fmt.Errorf("Card is not supported")
  183. case ErrSCardUnresponsiveCard:
  184. return fmt.Errorf("Card is unresponsive")
  185. case ErrSCardUnpoweredCard:
  186. return fmt.Errorf("Card is unpowered")
  187. case ErrSCardResetCard:
  188. return fmt.Errorf("Card was reset")
  189. case ErrSCardRemovedCard:
  190. return fmt.Errorf("Card was removed")
  191. /* case SCARD_W_SECURITY_VIOLATION: */
  192. /* case SCARD_W_WRONG_CHV: */
  193. /* case SCARD_W_CHV_BLOCKED: */
  194. /* case SCARD_W_EOF: */
  195. /* case SCARD_W_CANCELLED_BY_USER: */
  196. /* case SCARD_W_CARD_NOT_AUTHENTICATED: */
  197. case ErrSCardUnsupportedFeature:
  198. return fmt.Errorf("Feature not supported")
  199. default:
  200. return fmt.Errorf("unknown error: %08x", code)
  201. }
  202. }