rfc4108.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. #
  2. # This file is part of pyasn1-modules software.
  3. #
  4. # Created by Russ Housley with assistance from asn1ate v.0.6.0.
  5. # Modified by Russ Housley to add items from the verified errata.
  6. # Modified by Russ Housley to add maps for use with opentypes.
  7. #
  8. # Copyright (c) 2019, Vigil Security, LLC
  9. # License: http://snmplabs.com/pyasn1/license.html
  10. #
  11. # CMS Firmware Wrapper
  12. #
  13. # ASN.1 source from:
  14. # https://www.rfc-editor.org/rfc/rfc4108.txt
  15. # https://www.rfc-editor.org/errata_search.php?rfc=4108
  16. #
  17. from pyasn1.type import univ, char, namedtype, namedval, tag, constraint, useful
  18. from pyasn1_modules import rfc5280
  19. from pyasn1_modules import rfc5652
  20. MAX = float('inf')
  21. class HardwareSerialEntry(univ.Choice):
  22. pass
  23. HardwareSerialEntry.componentType = namedtype.NamedTypes(
  24. namedtype.NamedType('all', univ.Null()),
  25. namedtype.NamedType('single', univ.OctetString()),
  26. namedtype.NamedType('block', univ.Sequence(componentType=namedtype.NamedTypes(
  27. namedtype.NamedType('low', univ.OctetString()),
  28. namedtype.NamedType('high', univ.OctetString())
  29. ))
  30. )
  31. )
  32. class HardwareModules(univ.Sequence):
  33. pass
  34. HardwareModules.componentType = namedtype.NamedTypes(
  35. namedtype.NamedType('hwType', univ.ObjectIdentifier()),
  36. namedtype.NamedType('hwSerialEntries', univ.SequenceOf(componentType=HardwareSerialEntry()))
  37. )
  38. class CommunityIdentifier(univ.Choice):
  39. pass
  40. CommunityIdentifier.componentType = namedtype.NamedTypes(
  41. namedtype.NamedType('communityOID', univ.ObjectIdentifier()),
  42. namedtype.NamedType('hwModuleList', HardwareModules())
  43. )
  44. class PreferredPackageIdentifier(univ.Sequence):
  45. pass
  46. PreferredPackageIdentifier.componentType = namedtype.NamedTypes(
  47. namedtype.NamedType('fwPkgID', univ.ObjectIdentifier()),
  48. namedtype.NamedType('verNum', univ.Integer().subtype(subtypeSpec=constraint.ValueRangeConstraint(0, MAX)))
  49. )
  50. class PreferredOrLegacyPackageIdentifier(univ.Choice):
  51. pass
  52. PreferredOrLegacyPackageIdentifier.componentType = namedtype.NamedTypes(
  53. namedtype.NamedType('preferred', PreferredPackageIdentifier()),
  54. namedtype.NamedType('legacy', univ.OctetString())
  55. )
  56. class CurrentFWConfig(univ.Sequence):
  57. pass
  58. CurrentFWConfig.componentType = namedtype.NamedTypes(
  59. namedtype.OptionalNamedType('fwPkgType', univ.Integer()),
  60. namedtype.NamedType('fwPkgName', PreferredOrLegacyPackageIdentifier())
  61. )
  62. class PreferredOrLegacyStalePackageIdentifier(univ.Choice):
  63. pass
  64. PreferredOrLegacyStalePackageIdentifier.componentType = namedtype.NamedTypes(
  65. namedtype.NamedType('preferredStaleVerNum', univ.Integer().subtype(subtypeSpec=constraint.ValueRangeConstraint(0, MAX))),
  66. namedtype.NamedType('legacyStaleVersion', univ.OctetString())
  67. )
  68. class FirmwarePackageLoadErrorCode(univ.Enumerated):
  69. pass
  70. FirmwarePackageLoadErrorCode.namedValues = namedval.NamedValues(
  71. ('decodeFailure', 1),
  72. ('badContentInfo', 2),
  73. ('badSignedData', 3),
  74. ('badEncapContent', 4),
  75. ('badCertificate', 5),
  76. ('badSignerInfo', 6),
  77. ('badSignedAttrs', 7),
  78. ('badUnsignedAttrs', 8),
  79. ('missingContent', 9),
  80. ('noTrustAnchor', 10),
  81. ('notAuthorized', 11),
  82. ('badDigestAlgorithm', 12),
  83. ('badSignatureAlgorithm', 13),
  84. ('unsupportedKeySize', 14),
  85. ('signatureFailure', 15),
  86. ('contentTypeMismatch', 16),
  87. ('badEncryptedData', 17),
  88. ('unprotectedAttrsPresent', 18),
  89. ('badEncryptContent', 19),
  90. ('badEncryptAlgorithm', 20),
  91. ('missingCiphertext', 21),
  92. ('noDecryptKey', 22),
  93. ('decryptFailure', 23),
  94. ('badCompressAlgorithm', 24),
  95. ('missingCompressedContent', 25),
  96. ('decompressFailure', 26),
  97. ('wrongHardware', 27),
  98. ('stalePackage', 28),
  99. ('notInCommunity', 29),
  100. ('unsupportedPackageType', 30),
  101. ('missingDependency', 31),
  102. ('wrongDependencyVersion', 32),
  103. ('insufficientMemory', 33),
  104. ('badFirmware', 34),
  105. ('unsupportedParameters', 35),
  106. ('breaksDependency', 36),
  107. ('otherError', 99)
  108. )
  109. class VendorLoadErrorCode(univ.Integer):
  110. pass
  111. # Wrapped Firmware Key Unsigned Attribute and Object Identifier
  112. id_aa_wrappedFirmwareKey = univ.ObjectIdentifier('1.2.840.113549.1.9.16.2.39')
  113. class WrappedFirmwareKey(rfc5652.EnvelopedData):
  114. pass
  115. # Firmware Package Information Signed Attribute and Object Identifier
  116. id_aa_firmwarePackageInfo = univ.ObjectIdentifier('1.2.840.113549.1.9.16.2.42')
  117. class FirmwarePackageInfo(univ.Sequence):
  118. pass
  119. FirmwarePackageInfo.componentType = namedtype.NamedTypes(
  120. namedtype.OptionalNamedType('fwPkgType', univ.Integer()),
  121. namedtype.OptionalNamedType('dependencies', univ.SequenceOf(componentType=PreferredOrLegacyPackageIdentifier()))
  122. )
  123. FirmwarePackageInfo.sizeSpec = univ.Sequence.sizeSpec + constraint.ValueSizeConstraint(1, 2)
  124. # Community Identifiers Signed Attribute and Object Identifier
  125. id_aa_communityIdentifiers = univ.ObjectIdentifier('1.2.840.113549.1.9.16.2.40')
  126. class CommunityIdentifiers(univ.SequenceOf):
  127. pass
  128. CommunityIdentifiers.componentType = CommunityIdentifier()
  129. # Implemented Compression Algorithms Signed Attribute and Object Identifier
  130. id_aa_implCompressAlgs = univ.ObjectIdentifier('1.2.840.113549.1.9.16.2.43')
  131. class ImplementedCompressAlgorithms(univ.SequenceOf):
  132. pass
  133. ImplementedCompressAlgorithms.componentType = univ.ObjectIdentifier()
  134. # Implemented Cryptographic Algorithms Signed Attribute and Object Identifier
  135. id_aa_implCryptoAlgs = univ.ObjectIdentifier('1.2.840.113549.1.9.16.2.38')
  136. class ImplementedCryptoAlgorithms(univ.SequenceOf):
  137. pass
  138. ImplementedCryptoAlgorithms.componentType = univ.ObjectIdentifier()
  139. # Decrypt Key Identifier Signed Attribute and Object Identifier
  140. id_aa_decryptKeyID = univ.ObjectIdentifier('1.2.840.113549.1.9.16.2.37')
  141. class DecryptKeyIdentifier(univ.OctetString):
  142. pass
  143. # Target Hardware Identifier Signed Attribute and Object Identifier
  144. id_aa_targetHardwareIDs = univ.ObjectIdentifier('1.2.840.113549.1.9.16.2.36')
  145. class TargetHardwareIdentifiers(univ.SequenceOf):
  146. pass
  147. TargetHardwareIdentifiers.componentType = univ.ObjectIdentifier()
  148. # Firmware Package Identifier Signed Attribute and Object Identifier
  149. id_aa_firmwarePackageID = univ.ObjectIdentifier('1.2.840.113549.1.9.16.2.35')
  150. class FirmwarePackageIdentifier(univ.Sequence):
  151. pass
  152. FirmwarePackageIdentifier.componentType = namedtype.NamedTypes(
  153. namedtype.NamedType('name', PreferredOrLegacyPackageIdentifier()),
  154. namedtype.OptionalNamedType('stale', PreferredOrLegacyStalePackageIdentifier())
  155. )
  156. # Firmware Package Message Digest Signed Attribute and Object Identifier
  157. id_aa_fwPkgMessageDigest = univ.ObjectIdentifier('1.2.840.113549.1.9.16.2.41')
  158. class FirmwarePackageMessageDigest(univ.Sequence):
  159. pass
  160. FirmwarePackageMessageDigest.componentType = namedtype.NamedTypes(
  161. namedtype.NamedType('algorithm', rfc5280.AlgorithmIdentifier()),
  162. namedtype.NamedType('msgDigest', univ.OctetString())
  163. )
  164. # Firmware Package Load Error Report Content Type and Object Identifier
  165. class FWErrorVersion(univ.Integer):
  166. pass
  167. FWErrorVersion.namedValues = namedval.NamedValues(
  168. ('v1', 1)
  169. )
  170. id_ct_firmwareLoadError = univ.ObjectIdentifier('1.2.840.113549.1.9.16.1.18')
  171. class FirmwarePackageLoadError(univ.Sequence):
  172. pass
  173. FirmwarePackageLoadError.componentType = namedtype.NamedTypes(
  174. namedtype.DefaultedNamedType('version', FWErrorVersion().subtype(value='v1')),
  175. namedtype.NamedType('hwType', univ.ObjectIdentifier()),
  176. namedtype.NamedType('hwSerialNum', univ.OctetString()),
  177. namedtype.NamedType('errorCode', FirmwarePackageLoadErrorCode()),
  178. namedtype.OptionalNamedType('vendorErrorCode', VendorLoadErrorCode()),
  179. namedtype.OptionalNamedType('fwPkgName', PreferredOrLegacyPackageIdentifier()),
  180. namedtype.OptionalNamedType('config', univ.SequenceOf(componentType=CurrentFWConfig()).subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1)))
  181. )
  182. # Firmware Package Load Receipt Content Type and Object Identifier
  183. class FWReceiptVersion(univ.Integer):
  184. pass
  185. FWReceiptVersion.namedValues = namedval.NamedValues(
  186. ('v1', 1)
  187. )
  188. id_ct_firmwareLoadReceipt = univ.ObjectIdentifier('1.2.840.113549.1.9.16.1.17')
  189. class FirmwarePackageLoadReceipt(univ.Sequence):
  190. pass
  191. FirmwarePackageLoadReceipt.componentType = namedtype.NamedTypes(
  192. namedtype.DefaultedNamedType('version', FWReceiptVersion().subtype(value='v1')),
  193. namedtype.NamedType('hwType', univ.ObjectIdentifier()),
  194. namedtype.NamedType('hwSerialNum', univ.OctetString()),
  195. namedtype.NamedType('fwPkgName', PreferredOrLegacyPackageIdentifier()),
  196. namedtype.OptionalNamedType('trustAnchorKeyID', univ.OctetString()),
  197. namedtype.OptionalNamedType('decryptKeyID', univ.OctetString().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1)))
  198. )
  199. # Firmware Package Content Type and Object Identifier
  200. id_ct_firmwarePackage = univ.ObjectIdentifier('1.2.840.113549.1.9.16.1.16')
  201. class FirmwarePkgData(univ.OctetString):
  202. pass
  203. # Other Name syntax for Hardware Module Name
  204. id_on_hardwareModuleName = univ.ObjectIdentifier('1.3.6.1.5.5.7.8.4')
  205. class HardwareModuleName(univ.Sequence):
  206. pass
  207. HardwareModuleName.componentType = namedtype.NamedTypes(
  208. namedtype.NamedType('hwType', univ.ObjectIdentifier()),
  209. namedtype.NamedType('hwSerialNum', univ.OctetString())
  210. )
  211. # Map of Attribute Type OIDs to Attributes is added to the
  212. # ones that are in rfc5652.py
  213. _cmsAttributesMapUpdate = {
  214. id_aa_wrappedFirmwareKey: WrappedFirmwareKey(),
  215. id_aa_firmwarePackageInfo: FirmwarePackageInfo(),
  216. id_aa_communityIdentifiers: CommunityIdentifiers(),
  217. id_aa_implCompressAlgs: ImplementedCompressAlgorithms(),
  218. id_aa_implCryptoAlgs: ImplementedCryptoAlgorithms(),
  219. id_aa_decryptKeyID: DecryptKeyIdentifier(),
  220. id_aa_targetHardwareIDs: TargetHardwareIdentifiers(),
  221. id_aa_firmwarePackageID: FirmwarePackageIdentifier(),
  222. id_aa_fwPkgMessageDigest: FirmwarePackageMessageDigest(),
  223. }
  224. rfc5652.cmsAttributesMap.update(_cmsAttributesMapUpdate)
  225. # Map of Content Type OIDs to Content Types is added to the
  226. # ones that are in rfc5652.py
  227. _cmsContentTypesMapUpdate = {
  228. id_ct_firmwareLoadError: FirmwarePackageLoadError(),
  229. id_ct_firmwareLoadReceipt: FirmwarePackageLoadReceipt(),
  230. id_ct_firmwarePackage: FirmwarePkgData(),
  231. }
  232. rfc5652.cmsContentTypesMap.update(_cmsContentTypesMapUpdate)
  233. # Map of Other Name OIDs to Other Name is added to the
  234. # ones that are in rfc5280.py
  235. _anotherNameMapUpdate = {
  236. id_on_hardwareModuleName: HardwareModuleName(),
  237. }
  238. rfc5280.anotherNameMap.update(_anotherNameMapUpdate)