Skip to content

Commit cc14548

Browse files
Merge pull request #29 from spiegel-im-spiegel/add-hkp-mode
Add hkp mode
2 parents 68e7dea + eb9120c commit cc14548

File tree

18 files changed

+691
-74
lines changed

18 files changed

+691
-74
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,3 @@ _cgo_gotypes.go
2525
_cgo_export.*
2626

2727
_testmain.go
28-
29-
go.sum

README.md

Lines changed: 120 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,30 @@ See [latest release](https://github.com/spiegel-im-spiegel/gpgpdump/releases/lat
6969
```
7070
$ gpgpdump -h
7171
Usage:
72-
gpgpdump [flags] [OpenPGP file]
72+
gpgpdump [flags]
73+
gpgpdump [command]
74+
75+
Available Commands:
76+
help Help about any command
77+
hkp Dumps from OpenPGP key server
78+
version Print the version number
7379
7480
Flags:
75-
-a, --armor accepts ASCII input only
76-
--debug for debug
77-
-h, --help help for gpgpdump
78-
--indent int indent size for output string
79-
-i, --int dumps multi-precision integers
80-
-j, --json output with JSON format
81-
-l, --literal dumps literal packets (tag 11)
82-
-m, --marker dumps marker packets (tag 10)
83-
-p, --private dumps private packets (tag 60-63)
84-
-t, --toml output with TOML format
85-
-u, --utc output with UTC time
86-
-v, --version output version of gpgpdump
81+
-a, --armor accepts ASCII input only
82+
--debug for debug
83+
-f, --file string path of OpenPGP file
84+
-h, --help help for gpgpdump
85+
--indent int indent size for output string
86+
-i, --int dumps multi-precision integers
87+
-j, --json output with JSON format
88+
-l, --literal dumps literal packets (tag 11)
89+
-m, --marker dumps marker packets (tag 10)
90+
-p, --private dumps private packets (tag 60-63)
91+
-t, --toml output with TOML format
92+
-u, --utc output with UTC time
93+
-v, --version output version of gpgpdump
94+
95+
Use "gpgpdump [command] --help" for more information about a command.
8796
8897
$ cat sig
8998
-----BEGIN PGP SIGNATURE-----
@@ -221,6 +230,104 @@ $ cat sig | gpgpdump -j -u --indent 2
221230
}
222231
```
223232

233+
### HKP Access Mode
234+
235+
```
236+
$ gpgpdump hkp -h
237+
Dumps from OpenPGP key server
238+
239+
Usage:
240+
gpgpdump hkp [flags] <user id>
241+
242+
Flags:
243+
-h, --help help for hkp
244+
--keyserver string OpenPGP key server (default "keys.gnupg.net")
245+
--port int port number of OpenPGP key server (default 11371)
246+
--proxy string URL of proxy server
247+
--raw output raw text from OpenPGP key server
248+
--secure enable HKP over HTTPS
249+
250+
Global Flags:
251+
-a, --armor accepts ASCII input only
252+
--debug for debug
253+
--indent int indent size for output string
254+
-i, --int dumps multi-precision integers
255+
-j, --json output with JSON format
256+
-l, --literal dumps literal packets (tag 11)
257+
-m, --marker dumps marker packets (tag 10)
258+
-p, --private dumps private packets (tag 60-63)
259+
-t, --toml output with TOML format
260+
-u, --utc output with UTC time
261+
262+
$ gpgpdump hkp -u --indent 2 0x44ce6900e2b307a4
263+
Public-Key Packet (tag 6) (269 bytes)
264+
Version: 4 (current)
265+
Public key creation time: 2009-11-08T15:20:55Z
266+
4a f6 e1 d7
267+
Public-key Algorithm: RSA (Encrypt or Sign) (pub 1)
268+
RSA public modulus n (2048 bits)
269+
RSA public encryption exponent e (17 bits)
270+
User ID Packet (tag 13) (25 bytes)
271+
User ID: Alice <[email protected]>
272+
Signature Packet (tag 2) (312 bytes)
273+
Version: 4 (current)
274+
Signiture Type: Positive certification of a User ID and Public-Key packet (0x13)
275+
Public-key Algorithm: RSA (Encrypt or Sign) (pub 1)
276+
Hash Algorithm: SHA-1 (hash 2)
277+
Hashed Subpacket (34 bytes)
278+
Signature Creation Time (sub 2): 2009-11-08T15:20:55Z
279+
Key Flags (sub 27) (1 bytes)
280+
Flag: This key may be used to certify other keys.
281+
Flag: This key may be used to sign data.
282+
Preferred Symmetric Algorithms (sub 11) (5 bytes)
283+
Symmetric Algorithm: AES with 256-bit key (sym 9)
284+
Symmetric Algorithm: AES with 192-bit key (sym 8)
285+
Symmetric Algorithm: AES with 128-bit key (sym 7)
286+
Symmetric Algorithm: CAST5 (128 bit key, as per) (sym 3)
287+
Symmetric Algorithm: TripleDES (168 bit key derived from 192) (sym 2)
288+
Preferred Hash Algorithms (sub 21) (5 bytes)
289+
Hash Algorithm: SHA2-256 (hash 8)
290+
Hash Algorithm: SHA-1 (hash 2)
291+
Hash Algorithm: SHA2-384 (hash 9)
292+
Hash Algorithm: SHA2-512 (hash 10)
293+
Hash Algorithm: SHA2-224 (hash 11)
294+
Preferred Compression Algorithms (sub 22) (3 bytes)
295+
Compression Algorithm: ZLIB <RFC1950> (comp 2)
296+
Compression Algorithm: BZip2 (comp 3)
297+
Compression Algorithm: ZIP <RFC1951> (comp 1)
298+
Features (sub 30) (1 bytes)
299+
Flag: Modification Detection (packets 18 and 19)
300+
Key Server Preferences (sub 23) (1 bytes)
301+
Flag: No-modify
302+
Unhashed Subpacket (10 bytes)
303+
Issuer (sub 16): 0x44ce6900e2b307a4
304+
Hash left 2 bytes
305+
93 62
306+
RSA signature value m^d mod n (2045 bits)
307+
Public-Subkey Packet (tag 14) (269 bytes)
308+
Version: 4 (current)
309+
Public key creation time: 2009-11-08T15:20:55Z
310+
4a f6 e1 d7
311+
Public-key Algorithm: RSA (Encrypt or Sign) (pub 1)
312+
RSA public modulus n (2048 bits)
313+
RSA public encryption exponent e (17 bits)
314+
Signature Packet (tag 2) (287 bytes)
315+
Version: 4 (current)
316+
Signiture Type: Subkey Binding Signature (0x18)
317+
Public-key Algorithm: RSA (Encrypt or Sign) (pub 1)
318+
Hash Algorithm: SHA-1 (hash 2)
319+
Hashed Subpacket (9 bytes)
320+
Signature Creation Time (sub 2): 2009-11-08T15:20:55Z
321+
Key Flags (sub 27) (1 bytes)
322+
Flag: This key may be used to encrypt communications.
323+
Flag: This key may be used to encrypt storage.
324+
Unhashed Subpacket (10 bytes)
325+
Issuer (sub 16): 0x44ce6900e2b307a4
326+
Hash left 2 bytes
327+
66 f3
328+
RSA signature value m^d mod n (2048 bits)
329+
```
330+
224331
[gpgpdump]: https://github.com/spiegel-im-spiegel/gpgpdump "spiegel-im-spiegel/gpgpdump: gpgpdump - OpenPGP packet visualizer"
225332
[RFC 4880]: https://tools.ietf.org/html/rfc4880
226333
[RFC 4880bis]: https://datatracker.ietf.org/doc/draft-ietf-openpgp-rfc4880bis/

cli/gpgpdump/facade/armor_test.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,27 @@ func TestArmorErr(t *testing.T) {
3535
outBuf := new(bytes.Buffer)
3636
outErrBuf := new(bytes.Buffer)
3737
ui := rwi.New(rwi.WithReader(inData), rwi.WithWriter(outBuf), rwi.WithErrorWriter(outErrBuf))
38-
args := []string{"-a", "--debug"}
38+
args := []string{"-a"}
3939

4040
exit := Execute(ui, args)
4141
if exit != exitcode.Abnormal {
4242
t.Errorf("Execute(armor) = \"%v\", want \"%v\".", exit, exitcode.Abnormal)
43-
} else {
44-
fmt.Printf("Info: %+v", outErrBuf.String())
4543
}
44+
fmt.Printf("Info: %+v", outErrBuf.String())
45+
}
46+
47+
func TestArmorErrDebug(t *testing.T) {
48+
inData := bytes.NewReader(bindata1)
49+
outBuf := new(bytes.Buffer)
50+
outErrBuf := new(bytes.Buffer)
51+
ui := rwi.New(rwi.WithReader(inData), rwi.WithWriter(outBuf), rwi.WithErrorWriter(outErrBuf))
52+
args := []string{"-a", "--debug"}
53+
54+
exit := Execute(ui, args)
55+
if exit != exitcode.Normal {
56+
t.Errorf("Execute(armor) = \"%v\", want \"%v\".", exit, exitcode.Normal)
57+
}
58+
fmt.Printf("Info: %+v", outErrBuf.String())
4659
}
4760

4861
/* Copyright 2017-2019 Spiegel

cli/gpgpdump/facade/facade.go

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/spiegel-im-spiegel/gocli/exitcode"
1212
"github.com/spiegel-im-spiegel/gocli/rwi"
1313
"github.com/spiegel-im-spiegel/gpgpdump"
14+
"github.com/spiegel-im-spiegel/gpgpdump/errs"
15+
"github.com/spiegel-im-spiegel/gpgpdump/info"
1416
"github.com/spiegel-im-spiegel/gpgpdump/options"
1517
)
1618

@@ -30,13 +32,15 @@ var (
3032
versionFlag bool //version flag
3133
jsonFlag bool //output with JSON format
3234
tomlFlag bool //output with TOML format
35+
debugFlag bool //debug flag
3336
indentSize int
37+
filePath string
3438
)
3539

3640
//newRootCmd returns cobra.Command instance for root command
3741
func newRootCmd(ui *rwi.RWI, args []string) *cobra.Command {
3842
rootCmd := &cobra.Command{
39-
Use: Name + " [flags] [OpenPGP file]",
43+
Use: Name,
4044
RunE: func(cmd *cobra.Command, args []string) error {
4145
//parse options
4246
if versionFlag {
@@ -46,65 +50,72 @@ func newRootCmd(ui *rwi.RWI, args []string) *cobra.Command {
4650

4751
//open PGP file
4852
reader := ui.Reader()
49-
if len(args) > 0 {
50-
file, err := os.Open(args[0]) //args[0] is maybe file path
53+
if len(filePath) > 0 {
54+
file, err := os.Open(filePath)
5155
if err != nil {
52-
return err
56+
return debugPrint(ui, err)
5357
}
5458
defer file.Close()
5559
reader = file
5660
}
5761

5862
//parse OpenPGP packets
59-
info, err := gpgpdump.Parse(reader, opts)
63+
r, err := marshalPacketInfo(gpgpdump.Parse(reader, opts))
6064
if err != nil {
61-
if opts.Debug() {
62-
_ = ui.OutputErrln(fmt.Sprintf("%+v", err))
63-
}
64-
return err
65-
}
66-
67-
//marshal packet info
68-
var result io.Reader
69-
if jsonFlag {
70-
result, err = info.JSON(indentSize)
71-
} else if tomlFlag {
72-
result, err = info.TOML(indentSize)
73-
} else if indentSize > 0 {
74-
result = info.ToString(strings.Repeat(" ", indentSize))
75-
err = nil
76-
} else {
77-
result = info.ToString("\t")
78-
err = nil
65+
return debugPrint(ui, err)
7966
}
80-
if err != nil {
81-
if opts.Debug() {
82-
_ = ui.OutputErrln(fmt.Sprintf("%+v", err))
83-
}
84-
return err
85-
}
86-
return ui.WriteFrom(result)
67+
return debugPrint(ui, ui.WriteFrom(r))
8768
},
8869
}
8970
rootCmd.Flags().BoolVarP(&versionFlag, "version", "v", false, "output version of "+Name)
90-
rootCmd.Flags().BoolVarP(&jsonFlag, "json", "j", false, "output with JSON format")
91-
rootCmd.Flags().BoolVarP(&tomlFlag, "toml", "t", false, "output with TOML format")
92-
rootCmd.Flags().IntVarP(&indentSize, "indent", "", 0, "indent size for output string")
93-
rootCmd.Flags().BoolP(options.ARMOR.String(), "a", false, "accepts ASCII input only")
94-
rootCmd.Flags().BoolP(options.DEBUG.String(), "", false, "for debug") //not use
95-
//rootCmd.Flags().BoolP(options.GDUMP.String(), "g", false, "selects alternate (GnuPG type) dump format") //not use
96-
rootCmd.Flags().BoolP(options.INTEGER.String(), "i", false, "dumps multi-precision integers")
97-
rootCmd.Flags().BoolP(options.LITERAL.String(), "l", false, "dumps literal packets (tag 11)")
98-
rootCmd.Flags().BoolP(options.MARKER.String(), "m", false, "dumps marker packets (tag 10)")
99-
rootCmd.Flags().BoolP(options.PRIVATE.String(), "p", false, "dumps private packets (tag 60-63)")
100-
rootCmd.Flags().BoolP(options.UTC.String(), "u", false, "output with UTC time")
71+
rootCmd.Flags().StringVarP(&filePath, "file", "f", "", "path of OpenPGP file")
72+
rootCmd.PersistentFlags().BoolVarP(&jsonFlag, "json", "j", false, "output with JSON format")
73+
rootCmd.PersistentFlags().BoolVarP(&tomlFlag, "toml", "t", false, "output with TOML format")
74+
rootCmd.PersistentFlags().IntVarP(&indentSize, "indent", "", 0, "indent size for output string")
75+
rootCmd.PersistentFlags().BoolP(options.ARMOR.String(), "a", false, "accepts ASCII input only")
76+
rootCmd.PersistentFlags().BoolP(options.DEBUG.String(), "", false, "for debug") //not use
77+
//rootCmd.PersistentFlags().BoolP(options.GDUMP.String(), "g", false, "selects alternate (GnuPG type) dump format") //not use
78+
rootCmd.PersistentFlags().BoolP(options.INTEGER.String(), "i", false, "dumps multi-precision integers")
79+
rootCmd.PersistentFlags().BoolP(options.LITERAL.String(), "l", false, "dumps literal packets (tag 11)")
80+
rootCmd.PersistentFlags().BoolP(options.MARKER.String(), "m", false, "dumps marker packets (tag 10)")
81+
rootCmd.PersistentFlags().BoolP(options.PRIVATE.String(), "p", false, "dumps private packets (tag 60-63)")
82+
rootCmd.PersistentFlags().BoolP(options.UTC.String(), "u", false, "output with UTC time")
10183

10284
rootCmd.SetArgs(args)
10385
rootCmd.SetOutput(ui.ErrorWriter())
86+
rootCmd.AddCommand(newVersionCmd(ui))
87+
rootCmd.AddCommand(newHkpCmd(ui))
10488

10589
return rootCmd
10690
}
10791

92+
func debugPrint(ui *rwi.RWI, err error) error {
93+
if debugFlag && err != nil {
94+
fmt.Fprintf(ui.ErrorWriter(), "error: %+v\n", err)
95+
return nil
96+
}
97+
return errs.Cause(err)
98+
}
99+
100+
func marshalPacketInfo(i *info.Info, e error) (r io.Reader, err error) {
101+
if e != nil {
102+
err = e
103+
return
104+
}
105+
if jsonFlag {
106+
r, err = i.JSON(indentSize)
107+
} else if tomlFlag {
108+
r, err = i.TOML(indentSize)
109+
} else if indentSize > 0 {
110+
r = i.ToString(strings.Repeat(" ", indentSize))
111+
err = nil
112+
} else {
113+
r = i.ToString("\t")
114+
err = nil
115+
}
116+
return
117+
}
118+
108119
func getBool(cmd *cobra.Command, code options.OptCode) (options.OptCode, bool) {
109120
name := code.String()
110121
f, err := cmd.Flags().GetBool(name)
@@ -115,7 +126,7 @@ func getBool(cmd *cobra.Command, code options.OptCode) (options.OptCode, bool) {
115126
}
116127

117128
func parseOpt(cmd *cobra.Command) options.Options {
118-
return options.New(
129+
opts := options.New(
119130
options.Set(getBool(cmd, options.ARMOR)),
120131
options.Set(getBool(cmd, options.DEBUG)), //for debug
121132
//options.Set(getBool(cmd, options.GDUMP)), //not use
@@ -125,6 +136,8 @@ func parseOpt(cmd *cobra.Command) options.Options {
125136
options.Set(getBool(cmd, options.PRIVATE)),
126137
options.Set(getBool(cmd, options.UTC)),
127138
)
139+
debugFlag = opts.Debug()
140+
return opts
128141
}
129142

130143
//Execute is called from main function

cli/gpgpdump/facade/facade_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func TestLoadByFile(t *testing.T) {
9595
outBuf := new(bytes.Buffer)
9696
outErrBuf := new(bytes.Buffer)
9797
ui := rwi.New(rwi.WithWriter(outBuf), rwi.WithErrorWriter(outErrBuf))
98-
args := []string{"testdata/bindata1"}
98+
args := []string{"-f", "testdata/bindata1"}
9999

100100
exit := Execute(ui, args)
101101
if exit != exitcode.Normal {

0 commit comments

Comments
 (0)