55 "encoding/binary"
66 "encoding/hex"
77 "fmt"
8+ "io"
89 "path"
910 "strings"
1011 "time"
@@ -26,6 +27,7 @@ type PackageInfo struct {
2627 Summary string
2728 PGP string
2829 SigMD5 string
30+ RSAHeader string
2931 DigestAlgorithm DigestAlgorithm
3032 InstallTime int
3133 BaseNames []string
@@ -236,118 +238,137 @@ func getNEVRA(indexEntries []indexEntry) (*PackageInfo, error) {
236238 // It is just string that we need to encode to hex
237239 digest := ie .Data
238240 pkgInfo .SigMD5 = hex .EncodeToString (digest )
241+ case RPMTAG_RSAHEADER :
242+ if ie .Info .Type != RPM_BIN_TYPE {
243+ return nil , xerrors .New ("invalid rsa signature" )
244+ }
245+ val , err := parsePGP (ie )
246+ if err != nil {
247+ return nil , xerrors .Errorf ("failed to parse rsa signature: %w" , err )
248+ }
249+ pkgInfo .RSAHeader = val
239250 case RPMTAG_PGP :
240- type pgpSig struct {
241- _ [3 ]byte
242- Date int32
243- KeyID [8 ]byte
244- PubKeyAlgo uint8
245- HashAlgo uint8
251+ if ie .Info .Type != RPM_BIN_TYPE {
252+ return nil , xerrors .New ("invalid pgp signature" )
246253 }
247-
248- type textSig struct {
249- _ [2 ]byte
250- PubKeyAlgo uint8
251- HashAlgo uint8
252- _ [4 ]byte
253- Date int32
254- _ [4 ]byte
255- KeyID [8 ]byte
254+ val , err := parsePGP (ie )
255+ if err != nil {
256+ return nil , xerrors .Errorf ("failed to parse pgp signature: %w" , err )
256257 }
258+ pkgInfo .PGP = val
259+ }
260+ }
257261
258- type pgp4Sig struct {
259- _ [2 ]byte
260- PubKeyAlgo uint8
261- HashAlgo uint8
262- _ [17 ]byte
263- KeyID [8 ]byte
264- _ [2 ]byte
265- Date int32
266- }
262+ return pkgInfo , nil
263+ }
267264
268- pubKeyLookup := map [ uint8 ] string {
269- 0x01 : "RSA" ,
270- }
271- hashLookup := map [ uint8 ] string {
272- 0x02 : "SHA1" ,
273- 0x08 : "SHA256" ,
274- }
265+ type pgpSig struct {
266+ _ [ 3 ] byte
267+ Date int32
268+ KeyID [ 8 ] byte
269+ PubKeyAlgo uint8
270+ HashAlgo uint8
271+ }
275272
276- if ie .Info .Type != RPM_BIN_TYPE {
277- return nil , xerrors .New ("invalid PGP signature" )
278- }
273+ type textSig struct {
274+ _ [2 ]byte
275+ PubKeyAlgo uint8
276+ HashAlgo uint8
277+ _ [4 ]byte
278+ Date int32
279+ _ [4 ]byte
280+ KeyID [8 ]byte
281+ }
279282
280- var tag , signatureType , version uint8
281- r := bytes .NewReader (ie .Data )
282- err := binary .Read (r , binary .BigEndian , & tag )
283- if err != nil {
284- return nil , err
285- }
286- err = binary .Read (r , binary .BigEndian , & signatureType )
287- if err != nil {
288- return nil , err
289- }
290- err = binary .Read (r , binary .BigEndian , & version )
283+ type pgp4Sig struct {
284+ _ [2 ]byte
285+ PubKeyAlgo uint8
286+ HashAlgo uint8
287+ _ [17 ]byte
288+ KeyID [8 ]byte
289+ _ [2 ]byte
290+ Date int32
291+ }
292+
293+ var pubKeyLookup = map [uint8 ]string {
294+ 0x01 : "RSA" ,
295+ }
296+ var hashLookup = map [uint8 ]string {
297+ 0x02 : "SHA1" ,
298+ 0x08 : "SHA256" ,
299+ }
300+
301+ func parsePGP (ie indexEntry ) (string , error ) {
302+ var tag , signatureType , version uint8
303+
304+ r := bytes .NewReader (ie .Data )
305+ err := binary .Read (r , binary .BigEndian , & tag )
306+ if err != nil {
307+ return "" , err
308+ }
309+ err = binary .Read (r , binary .BigEndian , & signatureType )
310+ if err != nil {
311+ return "" , err
312+ }
313+ err = binary .Read (r , binary .BigEndian , & version )
314+ if err != nil {
315+ return "" , err
316+ }
317+
318+ switch signatureType {
319+ case 0x01 :
320+ switch version {
321+ case 0x1c :
322+ sig := textSig {}
323+ err = binary .Read (r , binary .BigEndian , & sig )
291324 if err != nil {
292- return nil , err
293- }
294-
295- var pubKeyAlgo , hashAlgo , pkgDate string
296- var keyId [8 ]byte
297-
298- switch signatureType {
299- case 0x01 :
300- switch version {
301- case 0x1c :
302- sig := textSig {}
303- err = binary .Read (r , binary .BigEndian , & sig )
304- if err != nil {
305- return nil , xerrors .Errorf ("invalid PGP signature on decode: %w" , err )
306- }
307- pubKeyAlgo = pubKeyLookup [sig .PubKeyAlgo ]
308- hashAlgo = hashLookup [sig .HashAlgo ]
309- pkgDate = time .Unix (int64 (sig .Date ), 0 ).UTC ().Format ("Mon Jan _2 15:04:05 2006" )
310- keyId = sig .KeyID
311- default :
312- sig := pgpSig {}
313- err = binary .Read (r , binary .BigEndian , & sig )
314- if err != nil {
315- return nil , xerrors .Errorf ("invalid PGP signature on decode: %w" , err )
316- }
317- pubKeyAlgo = pubKeyLookup [sig .PubKeyAlgo ]
318- hashAlgo = hashLookup [sig .HashAlgo ]
319- pkgDate = time .Unix (int64 (sig .Date ), 0 ).UTC ().Format ("Mon Jan _2 15:04:05 2006" )
320- keyId = sig .KeyID
321- }
322- case 0x02 :
323- switch version {
324- case 0x33 :
325- sig := pgp4Sig {}
326- err = binary .Read (r , binary .BigEndian , & sig )
327- if err != nil {
328- return nil , xerrors .Errorf ("invalid PGP signature on decode: %w" , err )
329- }
330- pubKeyAlgo = pubKeyLookup [sig .PubKeyAlgo ]
331- hashAlgo = hashLookup [sig .HashAlgo ]
332- pkgDate = time .Unix (int64 (sig .Date ), 0 ).UTC ().Format ("Mon Jan _2 15:04:05 2006" )
333- keyId = sig .KeyID
334- default :
335- sig := pgpSig {}
336- err = binary .Read (r , binary .BigEndian , & sig )
337- if err != nil {
338- return nil , xerrors .Errorf ("invalid PGP signature on decode: %w" , err )
339- }
340- pubKeyAlgo = pubKeyLookup [sig .PubKeyAlgo ]
341- hashAlgo = hashLookup [sig .HashAlgo ]
342- pkgDate = time .Unix (int64 (sig .Date ), 0 ).UTC ().Format ("Mon Jan _2 15:04:05 2006" )
343- keyId = sig .KeyID
344- }
325+ return "" , fmt .Errorf ("invalid PGP signature on decode: %w" , err )
345326 }
346- pkgInfo .PGP = fmt .Sprintf ("%s/%s, %s, Key ID %x" , pubKeyAlgo , hashAlgo , pkgDate , keyId )
327+
328+ pubKeyAlgo := pubKeyLookup [sig .PubKeyAlgo ]
329+ hashAlgo := hashLookup [sig .HashAlgo ]
330+ pkgDate := time .Unix (int64 (sig .Date ), 0 ).UTC ().Format ("Mon Jan _2 15:04:05 2006" )
331+ keyId := sig .KeyID
332+
333+ return fmt .Sprintf ("%s/%s, %s, Key ID %x" , pubKeyAlgo , hashAlgo , pkgDate , keyId ), nil
334+ default :
335+ return decodePGPSig (version , r )
347336 }
337+ case 0x02 :
338+ return decodePGPSig (version , r )
348339 }
349340
350- return pkgInfo , nil
341+ return "" , nil
342+ }
343+
344+ func decodePGPSig (version uint8 , r io.Reader ) (string , error ) {
345+ var pubKeyAlgo , hashAlgo , pkgDate string
346+ var keyId [8 ]byte
347+
348+ switch {
349+ case version > 0x15 :
350+ sig := pgp4Sig {}
351+ err := binary .Read (r , binary .BigEndian , & sig )
352+ if err != nil {
353+ return "" , fmt .Errorf ("invalid PGP v4 signature on decode: %w" , err )
354+ }
355+ pubKeyAlgo = pubKeyLookup [sig .PubKeyAlgo ]
356+ hashAlgo = hashLookup [sig .HashAlgo ]
357+ pkgDate = time .Unix (int64 (sig .Date ), 0 ).UTC ().Format ("Mon Jan _2 15:04:05 2006" )
358+ keyId = sig .KeyID
359+
360+ default :
361+ sig := pgpSig {}
362+ err := binary .Read (r , binary .BigEndian , & sig )
363+ if err != nil {
364+ return "" , fmt .Errorf ("invalid PGP signature on decode: %w" , err )
365+ }
366+ pubKeyAlgo = pubKeyLookup [sig .PubKeyAlgo ]
367+ hashAlgo = hashLookup [sig .HashAlgo ]
368+ pkgDate = time .Unix (int64 (sig .Date ), 0 ).UTC ().Format ("Mon Jan _2 15:04:05 2006" )
369+ keyId = sig .KeyID
370+ }
371+ return fmt .Sprintf ("%s/%s, %s, Key ID %x" , pubKeyAlgo , hashAlgo , pkgDate , keyId ), nil
351372}
352373
353374const (
@@ -389,7 +410,7 @@ func parseStringArray(data []byte) []string {
389410}
390411
391412func (p * PackageInfo ) InstalledFileNames () ([]string , error ) {
392- if len (p .DirNames ) == 0 || len (p .DirIndexes ) == 0 || len (p .BaseNames ) == 0 {
413+ if p == nil || len (p .DirNames ) == 0 || len (p .DirIndexes ) == 0 || len (p .BaseNames ) == 0 {
393414 return nil , nil
394415 }
395416
0 commit comments