pgp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. #------------------------------------------------------------------------------
  2. # $File: pgp,v 1.25 2021/04/26 15:56:00 christos Exp $
  3. # pgp: file(1) magic for Pretty Good Privacy
  4. # Handling of binary PGP keys is in pgp-binary-keys.
  5. # see https://lists.gnupg.org/pipermail/gnupg-devel/1999-September/016052.html
  6. #
  7. 0 beshort 0xa600 PGP encrypted data
  8. #!:mime application/pgp-encrypted
  9. #0 string -----BEGIN\040PGP text/PGP armored data
  10. !:mime text/PGP # encoding: armored data
  11. #>15 string PUBLIC\040KEY\040BLOCK- public key block
  12. #>15 string MESSAGE- message
  13. #>15 string SIGNED\040MESSAGE- signed message
  14. #>15 string PGP\040SIGNATURE- signature
  15. # Update: Joerg Jenderek
  16. # URL: http://en.wikipedia.org/wiki/Pretty_Good_Privacy
  17. # Reference: https://reposcope.com/mimetype/application/pgp-keys
  18. 2 string ---BEGIN\040PGP\040PRIVATE\040KEY\040BLOCK- PGP private key block
  19. #!:mime text/PGP
  20. !:mime application/pgp-keys
  21. !:ext asc
  22. 2 string ---BEGIN\040PGP\040PUBLIC\040KEY\040BLOCK- PGP public key block
  23. !:mime application/pgp-keys
  24. !:ext asc
  25. >10 search/100 \n\n
  26. >>&0 use pgp
  27. 0 string -----BEGIN\040PGP\040MESSAGE- PGP message
  28. # https://reposcope.com/mimetype/application/pgp-encrypted
  29. #!:mime application/pgp
  30. !:mime application/pgp-encrypted
  31. !:ext asc
  32. #!:ext asc/pgp/gpg
  33. >10 search/100 \n\n
  34. >>&0 use pgp
  35. # Reference: https://www.gnupg.org/gph/en/manual/x135.html
  36. 0 string -----BEGIN\040PGP\040SIGNED\040MESSAGE- PGP signed message
  37. #!:mime text/plain
  38. !:mime text/PGP
  39. #!:mime application/pgp
  40. !:ext asc
  41. 0 string -----BEGIN\040PGP\040SIGNATURE- PGP signature
  42. # https://reposcope.com/mimetype/application/pgp-signature
  43. !:mime application/pgp-signature
  44. !:ext asc
  45. >10 search/100 \n\n
  46. >>&0 use pgp
  47. # Decode the type of the packet based on it's base64 encoding.
  48. # Idea from Mark Martinec
  49. # The specification is in RFC 4880, section 4.2 and 4.3:
  50. # https://tools.ietf.org/html/rfc4880#section-4.2
  51. 0 name pgp
  52. >0 byte 0x67 Reserved (old)
  53. >0 byte 0x68 Public-Key Encrypted Session Key (old)
  54. >0 byte 0x69 Signature (old)
  55. >0 byte 0x6a Symmetric-Key Encrypted Session Key (old)
  56. >0 byte 0x6b One-Pass Signature (old)
  57. >0 byte 0x6c Secret-Key (old)
  58. >0 byte 0x6d Public-Key (old)
  59. >0 byte 0x6e Secret-Subkey (old)
  60. >0 byte 0x6f Compressed Data (old)
  61. >0 byte 0x70 Symmetrically Encrypted Data (old)
  62. >0 byte 0x71 Marker (old)
  63. >0 byte 0x72 Literal Data (old)
  64. >0 byte 0x73 Trust (old)
  65. >0 byte 0x74 User ID (old)
  66. >0 byte 0x75 Public-Subkey (old)
  67. >0 byte 0x76 Unused (old)
  68. >0 byte 0x77
  69. >>1 byte&0xc0 0x00 Reserved
  70. >>1 byte&0xc0 0x40 Public-Key Encrypted Session Key
  71. >>1 byte&0xc0 0x80 Signature
  72. >>1 byte&0xc0 0xc0 Symmetric-Key Encrypted Session Key
  73. >0 byte 0x78
  74. >>1 byte&0xc0 0x00 One-Pass Signature
  75. >>1 byte&0xc0 0x40 Secret-Key
  76. >>1 byte&0xc0 0x80 Public-Key
  77. >>1 byte&0xc0 0xc0 Secret-Subkey
  78. >0 byte 0x79
  79. >>1 byte&0xc0 0x00 Compressed Data
  80. >>1 byte&0xc0 0x40 Symmetrically Encrypted Data
  81. >>1 byte&0xc0 0x80 Marker
  82. >>1 byte&0xc0 0xc0 Literal Data
  83. >0 byte 0x7a
  84. >>1 byte&0xc0 0x00 Trust
  85. >>1 byte&0xc0 0x40 User ID
  86. >>1 byte&0xc0 0x80 Public-Subkey
  87. >>1 byte&0xc0 0xc0 Unused [z%x]
  88. >0 byte 0x30
  89. >>1 byte&0xc0 0x00 Unused [0%x]
  90. >>1 byte&0xc0 0x40 User Attribute
  91. >>1 byte&0xc0 0x80 Sym. Encrypted and Integrity Protected Data
  92. >>1 byte&0xc0 0xc0 Modification Detection Code
  93. # magic signatures to detect PGP crypto material (from stef)
  94. # detects and extracts metadata from:
  95. # - symmetric encrypted packet header
  96. # - RSA (e=65537) secret (sub-)keys
  97. # 1024b RSA encrypted data
  98. 0 string \x84\x8c\x03 PGP RSA encrypted session key -
  99. >3 belong x keyid: %08X
  100. >7 belong x %08X
  101. >11 byte 0x01 RSA (Encrypt or Sign) 1024b
  102. >11 byte 0x02 RSA Encrypt-Only 1024b
  103. >12 string \x04\x00
  104. >12 string \x03\xff
  105. >12 string \x03\xfe
  106. >12 string \x03\xfd
  107. >12 string \x03\xfc
  108. >12 string \x03\xfb
  109. >12 string \x03\xfa
  110. >12 string \x03\xf9
  111. >142 byte 0xd2 .
  112. # 2048b RSA encrypted data
  113. 0 string \x85\x01\x0c\x03 PGP RSA encrypted session key -
  114. >4 belong x keyid: %08X
  115. >8 belong x %08X
  116. >12 byte 0x01 RSA (Encrypt or Sign) 2048b
  117. >12 byte 0x02 RSA Encrypt-Only 2048b
  118. >13 string \x08\x00
  119. >13 string \x07\xff
  120. >13 string \x07\xfe
  121. >13 string \x07\xfd
  122. >13 string \x07\xfc
  123. >13 string \x07\xfb
  124. >13 string \x07\xfa
  125. >13 string \x07\xf9
  126. >271 byte 0xd2 .
  127. # 3072b RSA encrypted data
  128. 0 string \x85\x01\x8c\x03 PGP RSA encrypted session key -
  129. >4 belong x keyid: %08X
  130. >8 belong x %08X
  131. >12 byte 0x01 RSA (Encrypt or Sign) 3072b
  132. >12 byte 0x02 RSA Encrypt-Only 3072b
  133. >13 string \x0c\x00
  134. >13 string \x0b\xff
  135. >13 string \x0b\xfe
  136. >13 string \x0b\xfd
  137. >13 string \x0b\xfc
  138. >13 string \x0b\xfb
  139. >13 string \x0b\xfa
  140. >13 string \x0b\xf9
  141. >399 byte 0xd2 .
  142. # 4096b RSA encrypted data
  143. 0 string \x85\x02\x0c\x03 PGP RSA encrypted session key -
  144. >4 belong x keyid: %08X
  145. >8 belong x %08X
  146. >12 byte 0x01 RSA (Encrypt or Sign) 4096b
  147. >12 byte 0x02 RSA Encrypt-Only 4096b
  148. >13 string \x10\x00
  149. >13 string \x0f\xff
  150. >13 string \x0f\xfe
  151. >13 string \x0f\xfd
  152. >13 string \x0f\xfc
  153. >13 string \x0f\xfb
  154. >13 string \x0f\xfa
  155. >13 string \x0f\xf9
  156. >527 byte 0xd2 .
  157. # 8192b RSA encrypted data
  158. 0 string \x85\x04\x0c\x03 PGP RSA encrypted session key -
  159. >4 belong x keyid: %08X
  160. >8 belong x %08X
  161. >12 byte 0x01 RSA (Encrypt or Sign) 8192b
  162. >12 byte 0x02 RSA Encrypt-Only 8192b
  163. >13 string \x20\x00
  164. >13 string \x1f\xff
  165. >13 string \x1f\xfe
  166. >13 string \x1f\xfd
  167. >13 string \x1f\xfc
  168. >13 string \x1f\xfb
  169. >13 string \x1f\xfa
  170. >13 string \x1f\xf9
  171. >1039 byte 0xd2 .
  172. # 1024b Elgamal encrypted data
  173. 0 string \x85\x01\x0e\x03 PGP Elgamal encrypted session key -
  174. >4 belong x keyid: %08X
  175. >8 belong x %08X
  176. >12 byte 0x10 Elgamal Encrypt-Only 1024b.
  177. >13 string \x04\x00
  178. >13 string \x03\xff
  179. >13 string \x03\xfe
  180. >13 string \x03\xfd
  181. >13 string \x03\xfc
  182. >13 string \x03\xfb
  183. >13 string \x03\xfa
  184. >13 string \x03\xf9
  185. # 2048b Elgamal encrypted data
  186. 0 string \x85\x02\x0e\x03 PGP Elgamal encrypted session key -
  187. >4 belong x keyid: %08X
  188. >8 belong x %08X
  189. >12 byte 0x10 Elgamal Encrypt-Only 2048b.
  190. >13 string \x08\x00
  191. >13 string \x07\xff
  192. >13 string \x07\xfe
  193. >13 string \x07\xfd
  194. >13 string \x07\xfc
  195. >13 string \x07\xfb
  196. >13 string \x07\xfa
  197. >13 string \x07\xf9
  198. # 3072b Elgamal encrypted data
  199. 0 string \x85\x03\x0e\x03 PGP Elgamal encrypted session key -
  200. >4 belong x keyid: %08X
  201. >8 belong x %08X
  202. >12 byte 0x10 Elgamal Encrypt-Only 3072b.
  203. >13 string \x0c\x00
  204. >13 string \x0b\xff
  205. >13 string \x0b\xfe
  206. >13 string \x0b\xfd
  207. >13 string \x0b\xfc
  208. >13 string \x0b\xfb
  209. >13 string \x0b\xfa
  210. >13 string \x0b\xf9
  211. # crypto algo mapper
  212. 0 name crypto
  213. >0 byte 0x00 Plaintext or unencrypted data
  214. >0 byte 0x01 IDEA
  215. >0 byte 0x02 TripleDES
  216. >0 byte 0x03 CAST5 (128 bit key)
  217. >0 byte 0x04 Blowfish (128 bit key, 16 rounds)
  218. >0 byte 0x07 AES with 128-bit key
  219. >0 byte 0x08 AES with 192-bit key
  220. >0 byte 0x09 AES with 256-bit key
  221. >0 byte 0x0a Twofish with 256-bit key
  222. # hash algo mapper
  223. 0 name hash
  224. >0 byte 0x01 MD5
  225. >0 byte 0x02 SHA-1
  226. >0 byte 0x03 RIPE-MD/160
  227. >0 byte 0x08 SHA256
  228. >0 byte 0x09 SHA384
  229. >0 byte 0x0a SHA512
  230. >0 byte 0x0b SHA224
  231. # display public key algorithms as human readable text
  232. 0 name key_algo
  233. >0 byte 0x01 RSA (Encrypt or Sign)
  234. # keep old look of version 5.28 without parentheses
  235. >0 byte 0x02 RSA Encrypt-Only
  236. >0 byte 0x03 RSA (Sign-Only)
  237. >0 byte 16 ElGamal (Encrypt-Only)
  238. >0 byte 17 DSA
  239. >0 byte 18 Elliptic Curve
  240. >0 byte 19 ECDSA
  241. >0 byte 20 ElGamal (Encrypt or Sign)
  242. >0 byte 21 Diffie-Hellman
  243. >0 default x
  244. >>0 ubyte <22 unknown (pub %d)
  245. # this should never happen
  246. >>0 ubyte >21 invalid (%d)
  247. # pgp symmetric encrypted data
  248. 0 byte 0x8c PGP symmetric key encrypted data -
  249. >1 byte 0x0d
  250. >1 byte 0x0c
  251. >2 byte 0x04
  252. >3 use crypto
  253. >4 byte 0x01 salted -
  254. >>5 use hash
  255. >>14 byte 0xd2 .
  256. >>14 byte 0xc9 .
  257. >4 byte 0x03 salted & iterated -
  258. >>5 use hash
  259. >>15 byte 0xd2 .
  260. >>15 byte 0xc9 .
  261. # encrypted keymaterial needs s2k & can be checksummed/hashed
  262. 0 name chkcrypto
  263. >0 use crypto
  264. >1 byte 0x00 Simple S2K
  265. >1 byte 0x01 Salted S2K
  266. >1 byte 0x03 Salted&Iterated S2K
  267. >2 use hash
  268. # all PGP keys start with this prolog
  269. # containing version, creation date, and purpose
  270. 0 name keyprolog
  271. >0 byte 0x04
  272. >1 beldate x created on %s -
  273. >5 byte 0x01 RSA (Encrypt or Sign)
  274. >5 byte 0x02 RSA Encrypt-Only
  275. # end of secret keys known signature
  276. # contains e=65537 and the prolog to
  277. # the encrypted parameters
  278. 0 name keyend
  279. >0 string \x00\x11\x01\x00\x01 e=65537
  280. >5 use crypto
  281. >5 byte 0xff checksummed
  282. >>6 use chkcrypto
  283. >5 byte 0xfe hashed
  284. >>6 use chkcrypto
  285. # PGP secret keys contain also the public parts
  286. # these vary by bitsize of the key
  287. 0 name x1024
  288. >0 use keyprolog
  289. >6 string \x03\xfe
  290. >6 string \x03\xff
  291. >6 string \x04\x00
  292. >136 use keyend
  293. 0 name x2048
  294. >0 use keyprolog
  295. >6 string \x80\x00
  296. >6 string \x07\xfe
  297. >6 string \x07\xff
  298. >264 use keyend
  299. 0 name x3072
  300. >0 use keyprolog
  301. >6 string \x0b\xfe
  302. >6 string \x0b\xff
  303. >6 string \x0c\x00
  304. >392 use keyend
  305. 0 name x4096
  306. >0 use keyprolog
  307. >6 string \x10\x00
  308. >6 string \x0f\xfe
  309. >6 string \x0f\xff
  310. >520 use keyend
  311. # \x00|\x1f[\xfe\xff]).{1024})'
  312. 0 name x8192
  313. >0 use keyprolog
  314. >6 string \x20\x00
  315. >6 string \x1f\xfe
  316. >6 string \x1f\xff
  317. >1032 use keyend
  318. # depending on the size of the pkt
  319. # we branch into the proper key size
  320. # signatures defined as x{keysize}
  321. 0 name pgpkey
  322. >0 string \x01\xd8 1024b
  323. >>2 use x1024
  324. >0 string \x01\xeb 1024b
  325. >>2 use x1024
  326. >0 string \x01\xfb 1024b
  327. >>2 use x1024
  328. >0 string \x01\xfd 1024b
  329. >>2 use x1024
  330. >0 string \x01\xf3 1024b
  331. >>2 use x1024
  332. >0 string \x01\xee 1024b
  333. >>2 use x1024
  334. >0 string \x01\xfe 1024b
  335. >>2 use x1024
  336. >0 string \x01\xf4 1024b
  337. >>2 use x1024
  338. >0 string \x02\x0d 1024b
  339. >>2 use x1024
  340. >0 string \x02\x03 1024b
  341. >>2 use x1024
  342. >0 string \x02\x05 1024b
  343. >>2 use x1024
  344. >0 string \x02\x15 1024b
  345. >>2 use x1024
  346. >0 string \x02\x00 1024b
  347. >>2 use x1024
  348. >0 string \x02\x10 1024b
  349. >>2 use x1024
  350. >0 string \x02\x04 1024b
  351. >>2 use x1024
  352. >0 string \x02\x06 1024b
  353. >>2 use x1024
  354. >0 string \x02\x16 1024b
  355. >>2 use x1024
  356. >0 string \x03\x98 2048b
  357. >>2 use x2048
  358. >0 string \x03\xab 2048b
  359. >>2 use x2048
  360. >0 string \x03\xbb 2048b
  361. >>2 use x2048
  362. >0 string \x03\xbd 2048b
  363. >>2 use x2048
  364. >0 string \x03\xcd 2048b
  365. >>2 use x2048
  366. >0 string \x03\xb3 2048b
  367. >>2 use x2048
  368. >0 string \x03\xc3 2048b
  369. >>2 use x2048
  370. >0 string \x03\xc5 2048b
  371. >>2 use x2048
  372. >0 string \x03\xd5 2048b
  373. >>2 use x2048
  374. >0 string \x03\xae 2048b
  375. >>2 use x2048
  376. >0 string \x03\xbe 2048b
  377. >>2 use x2048
  378. >0 string \x03\xc0 2048b
  379. >>2 use x2048
  380. >0 string \x03\xd0 2048b
  381. >>2 use x2048
  382. >0 string \x03\xb4 2048b
  383. >>2 use x2048
  384. >0 string \x03\xc4 2048b
  385. >>2 use x2048
  386. >0 string \x03\xc6 2048b
  387. >>2 use x2048
  388. >0 string \x03\xd6 2048b
  389. >>2 use x2048
  390. >0 string \x05X 3072b
  391. >>2 use x3072
  392. >0 string \x05k 3072b
  393. >>2 use x3072
  394. >0 string \x05{ 3072b
  395. >>2 use x3072
  396. >0 string \x05} 3072b
  397. >>2 use x3072
  398. >0 string \x05\x8d 3072b
  399. >>2 use x3072
  400. >0 string \x05s 3072b
  401. >>2 use x3072
  402. >0 string \x05\x83 3072b
  403. >>2 use x3072
  404. >0 string \x05\x85 3072b
  405. >>2 use x3072
  406. >0 string \x05\x95 3072b
  407. >>2 use x3072
  408. >0 string \x05n 3072b
  409. >>2 use x3072
  410. >0 string \x05\x7e 3072b
  411. >>2 use x3072
  412. >0 string \x05\x80 3072b
  413. >>2 use x3072
  414. >0 string \x05\x90 3072b
  415. >>2 use x3072
  416. >0 string \x05t 3072b
  417. >>2 use x3072
  418. >0 string \x05\x84 3072b
  419. >>2 use x3072
  420. >0 string \x05\x86 3072b
  421. >>2 use x3072
  422. >0 string \x05\x96 3072b
  423. >>2 use x3072
  424. >0 string \x07[ 4096b
  425. >>2 use x4096
  426. >0 string \x07\x18 4096b
  427. >>2 use x4096
  428. >0 string \x07+ 4096b
  429. >>2 use x4096
  430. >0 string \x07; 4096b
  431. >>2 use x4096
  432. >0 string \x07= 4096b
  433. >>2 use x4096
  434. >0 string \x07M 4096b
  435. >>2 use x4096
  436. >0 string \x073 4096b
  437. >>2 use x4096
  438. >0 string \x07C 4096b
  439. >>2 use x4096
  440. >0 string \x07E 4096b
  441. >>2 use x4096
  442. >0 string \x07U 4096b
  443. >>2 use x4096
  444. >0 string \x07. 4096b
  445. >>2 use x4096
  446. >0 string \x07> 4096b
  447. >>2 use x4096
  448. >0 string \x07@ 4096b
  449. >>2 use x4096
  450. >0 string \x07P 4096b
  451. >>2 use x4096
  452. >0 string \x074 4096b
  453. >>2 use x4096
  454. >0 string \x07D 4096b
  455. >>2 use x4096
  456. >0 string \x07F 4096b
  457. >>2 use x4096
  458. >0 string \x07V 4096b
  459. >>2 use x4096
  460. >0 string \x0e[ 8192b
  461. >>2 use x8192
  462. >0 string \x0e\x18 8192b
  463. >>2 use x8192
  464. >0 string \x0e+ 8192b
  465. >>2 use x8192
  466. >0 string \x0e; 8192b
  467. >>2 use x8192
  468. >0 string \x0e= 8192b
  469. >>2 use x8192
  470. >0 string \x0eM 8192b
  471. >>2 use x8192
  472. >0 string \x0e3 8192b
  473. >>2 use x8192
  474. >0 string \x0eC 8192b
  475. >>2 use x8192
  476. >0 string \x0eE 8192b
  477. >>2 use x8192
  478. >0 string \x0eU 8192b
  479. >>2 use x8192
  480. >0 string \x0e. 8192b
  481. >>2 use x8192
  482. >0 string \x0e> 8192b
  483. >>2 use x8192
  484. >0 string \x0e@ 8192b
  485. >>2 use x8192
  486. >0 string \x0eP 8192b
  487. >>2 use x8192
  488. >0 string \x0e4 8192b
  489. >>2 use x8192
  490. >0 string \x0eD 8192b
  491. >>2 use x8192
  492. >0 string \x0eF 8192b
  493. >>2 use x8192
  494. >0 string \x0eV 8192b
  495. >>2 use x8192
  496. # PGP RSA (e=65537) secret (sub-)key header
  497. 0 byte 0x97 PGP Secret Sub-key -
  498. >1 use pgpkey
  499. 0 byte 0x9d
  500. # Update: Joerg Jenderek
  501. # secret subkey packet (tag 7) with same structure as secret key packet (tag 5)
  502. # skip Fetus.Sys16 CALIBUS.MAIN OrbFix.Sys16.Ex by looking for positive len
  503. >1 ubeshort >0
  504. #>1 ubeshort x \b, body length %#x
  505. # next packet type often 88h,89h~(tag 2)~Signature Packet
  506. #>>(1.S+3) ubyte x \b, next packet type %#x
  507. # skip Dragon.SHR DEMO.INIT by looking for positive version
  508. >>3 ubyte >0
  509. # skip BUISSON.13 GUITAR1 by looking for low version number
  510. >>>3 ubyte <5 PGP Secret Sub-key
  511. # sub-key are normally part of secret key. So it does not occur as standalone file
  512. #!:ext bin
  513. # version 2,3~old 4~new . Comment following line for version 5.28 look
  514. >>>>3 ubyte x (v%d)
  515. >>>>3 ubyte x -
  516. # old versions 2 or 3 but no real example found
  517. >>>>3 ubyte <4
  518. # 2 byte for key bits in version 5.28 look
  519. >>>>>11 ubeshort x %db
  520. >>>>>4 beldate x created on %s -
  521. # old versions use 2 additional bytes after time stamp
  522. #>>>>>8 ubeshort x %#x
  523. # display key algorithm 1~RSA Encrypt|Sign - 21~Diffie-Hellman
  524. >>>>>10 use key_algo
  525. >>>>>(11.S/8) ubequad x
  526. # look after first key
  527. >>>>>>&5 use keyend
  528. # new version
  529. >>>>3 ubyte >3
  530. >>>>>9 ubeshort x %db
  531. >>>>>4 beldate x created on %s -
  532. # display key algorithm
  533. >>>>>8 use key_algo
  534. >>>>>(9.S/8) ubequad x
  535. # look after first key for something like s2k
  536. >>>>>>&3 use keyend