|
| 1 | +package pinentry |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +// Pinentry gets the PIN from the user to access the smart card or hardware key |
| 13 | +type Pinentry struct { |
| 14 | + path string |
| 15 | +} |
| 16 | + |
| 17 | +// NewPinentry initializes the pinentry program used to get the PIN |
| 18 | +func NewPinentry() (*Pinentry, error) { |
| 19 | + fromEnv := os.Getenv("SMIMESIGM_PINENTRY") |
| 20 | + if len(fromEnv) > 0 { |
| 21 | + pinentryFromEnv, err := exec.LookPath(fromEnv) |
| 22 | + if err == nil && len(pinentryFromEnv) > 0 { |
| 23 | + return &Pinentry{path: pinentryFromEnv}, nil |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + executables := pinentryPaths() |
| 28 | + for _, programName := range executables { |
| 29 | + pinentry, err := exec.LookPath(programName) |
| 30 | + if err == nil && len(pinentry) > 0 { |
| 31 | + return &Pinentry{path: pinentry}, nil |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + return nil, fmt.Errorf("failed to find suitable program to enter pin") |
| 36 | +} |
| 37 | + |
| 38 | +// Get executes the pinentry program and returns the PIN entered by the user |
| 39 | +// see https://www.gnupg.org/documentation/manuals/assuan/Introduction.html for more details |
| 40 | +func (pin *Pinentry) Get(prompt string) (string, error) { |
| 41 | + cmd := exec.Command(pin.path) |
| 42 | + stdin, err := cmd.StdinPipe() |
| 43 | + if err != nil { |
| 44 | + return "", err |
| 45 | + } |
| 46 | + |
| 47 | + stdout, err := cmd.StdoutPipe() |
| 48 | + if err != nil { |
| 49 | + return "", err |
| 50 | + } |
| 51 | + |
| 52 | + err = cmd.Start() |
| 53 | + if err != nil { |
| 54 | + return "", err |
| 55 | + } |
| 56 | + |
| 57 | + bufferReader := bufio.NewReader(stdout) |
| 58 | + lineBytes, _, err := bufferReader.ReadLine() |
| 59 | + if err != nil { |
| 60 | + return "", err |
| 61 | + } |
| 62 | + |
| 63 | + line := string(lineBytes) |
| 64 | + if !strings.HasPrefix(line, "OK") { |
| 65 | + return "", fmt.Errorf("failed to initialize pinentry, got response: %v", line) |
| 66 | + } |
| 67 | + |
| 68 | + terminal := os.Getenv("TERM") |
| 69 | + if len(terminal) > 0 { |
| 70 | + if ok := setOption(stdin, bufferReader, fmt.Sprintf("OPTION ttytype=%s\n", terminal)); !ok { |
| 71 | + return "", fmt.Errorf("failed to set ttytype") |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if ok := setOption(stdin, bufferReader, fmt.Sprintf("OPTION ttyname=%v\n", tty())); !ok { |
| 76 | + return "", fmt.Errorf("failed to set ttyname") |
| 77 | + } |
| 78 | + |
| 79 | + if ok := setOption(stdin, bufferReader, "SETPROMPT PIN:\n"); !ok { |
| 80 | + return "", fmt.Errorf("failed to set prompt") |
| 81 | + } |
| 82 | + if ok := setOption(stdin, bufferReader, "SETTITLE smimesign\n"); !ok { |
| 83 | + return "", fmt.Errorf("failed to set title") |
| 84 | + } |
| 85 | + if ok := setOption(stdin, bufferReader, fmt.Sprintf("SETDESC %s\n", prompt)); !ok { |
| 86 | + return "", fmt.Errorf("failed to set description") |
| 87 | + } |
| 88 | + |
| 89 | + _, err = fmt.Fprint(stdin, "GETPIN\n") |
| 90 | + if err != nil { |
| 91 | + return "", err |
| 92 | + } |
| 93 | + |
| 94 | + lineBytes, _, err = bufferReader.ReadLine() |
| 95 | + if err != nil { |
| 96 | + return "", err |
| 97 | + } |
| 98 | + |
| 99 | + line = string(lineBytes) |
| 100 | + |
| 101 | + _, err = fmt.Fprint(stdin, "BYE\n") |
| 102 | + if err != nil { |
| 103 | + return "", err |
| 104 | + } |
| 105 | + |
| 106 | + if err = cmd.Wait(); err != nil { |
| 107 | + return "", err |
| 108 | + } |
| 109 | + |
| 110 | + if !strings.HasPrefix(line, "D ") { |
| 111 | + return "", fmt.Errorf(line) |
| 112 | + } |
| 113 | + |
| 114 | + return strings.TrimPrefix(line, "D "), nil |
| 115 | +} |
| 116 | + |
| 117 | +func setOption(writer io.Writer, bufferedReader *bufio.Reader, option string) bool { |
| 118 | + _, err := fmt.Fprintf(writer, option) |
| 119 | + lineBytes, _, err := bufferedReader.ReadLine() |
| 120 | + if err != nil { |
| 121 | + return false |
| 122 | + } |
| 123 | + |
| 124 | + line := string(lineBytes) |
| 125 | + if !strings.HasPrefix(line, "OK") { |
| 126 | + return false |
| 127 | + } |
| 128 | + return true |
| 129 | +} |
0 commit comments