Skip to content

Commit 6243c4b

Browse files
authored
Fixed changes in review.
1 parent 94740e0 commit 6243c4b

File tree

2 files changed

+226
-16
lines changed

2 files changed

+226
-16
lines changed

keycard_link.go

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
package keycard_link
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"fmt"
7+
"os"
8+
"io"
9+
"runtime"
10+
"os/exec"
11+
"strings"
12+
"net/http"
13+
"path/filepath"
14+
)
15+
16+
// GLOBAL PLATFORM PRO VERSION V20.01.23
17+
18+
const gpJarURL = "https://github.com/martinpaljak/GlobalPlatformPro/releases/download/v20.01.23/gp.jar"
19+
20+
func InstallKeycard() error {
21+
cmd := exec.Command("./keycard-linux-amd64", "install")
22+
output, err := cmd.CombinedOutput()
23+
if err != nil {
24+
return fmt.Errorf("error executing keycard-linux-amd64 install: %v, output: %s", err, output)
25+
}
26+
fmt.Printf("Output: %s\n", output)
27+
return nil
28+
}
29+
30+
// GetKeycardPublicKey retrieves the public key from the keycard.
31+
func GetKeycardPublicKey() (string, error) {
32+
// Command to execute
33+
fmt.Print("Scan your card now! :) \n")
34+
cmd := exec.Command("./keycard-linux-amd64", "info")
35+
36+
// Capture the output of the command
37+
var out bytes.Buffer
38+
cmd.Stdout = &out
39+
40+
// Run the command
41+
err := cmd.Run()
42+
if err != nil {
43+
return "", err
44+
}
45+
46+
// Process the output to find the public key
47+
scanner := bufio.NewScanner(&out)
48+
for scanner.Scan() {
49+
line := scanner.Text()
50+
if strings.Contains(line, "PublicKey:") {
51+
// Assuming the public key is the last element in the line, separated by spaces
52+
parts := strings.Fields(line)
53+
if len(parts) > 1 {
54+
return parts[len(parts)-1], nil
55+
}
56+
}
57+
}
58+
59+
if err := scanner.Err(); err != nil {
60+
return "", err
61+
}
62+
63+
return "", fmt.Errorf("public key not found in the output")
64+
}
65+
66+
// ReadPassphrase prompts the user to enter a passphrase.
67+
func ReadPassphrase() (string, error) {
68+
fmt.Print("Enter a unique passphrase for this particular file: ")
69+
reader := bufio.NewReader(os.Stdin)
70+
passphrase, err := reader.ReadString('\n')
71+
if err != nil {
72+
return "", err
73+
}
74+
return strings.TrimSpace(passphrase), nil
75+
}
76+
77+
78+
79+
80+
func JavaDependency() {
81+
// Check if Java is installed
82+
if err := exec.Command("java", "-version").Run(); err == nil {
83+
fmt.Println("Java is already installed! :)")
84+
return
85+
}
86+
87+
// Determine the operating system
88+
switch os := runtime.GOOS; os {
89+
case "linux":
90+
// Identify Linux distribution
91+
out, err := exec.Command("sh", "-c", "cat /etc/os-release").Output()
92+
if err != nil {
93+
fmt.Printf("Failed to execute command: %s\n", err)
94+
return
95+
}
96+
97+
// Parse /etc/os-release
98+
osRelease := string(out)
99+
if strings.Contains(osRelease, "ID=arch") || strings.Contains(osRelease, "ID_LIKE=arch") {
100+
// Install Java for Arch-based systems
101+
fmt.Println("Installing Java using pacman...")
102+
exec.Command("sudo", "pacman", "-Sy", "--noconfirm", "jdk-openjdk").Run()
103+
104+
// Install pcscd and pcsc-tools using pacman
105+
fmt.Println("Installing pcscd and pcsc-tools using pacman...")
106+
exec.Command("sudo", "pacman", "-Syu", "--noconfirm", "ccid", "libnfc", "acsccid", "pcsclite", "pcsc-tools").Run()
107+
exec.Command("sudo", "systemctl", "enable", "--now", "pcscd").Run()
108+
} else if strings.Contains(osRelease, "ID=debian") || strings.Contains(osRelease, "ID=ubuntu") {
109+
// Install Java for Debian-based systems
110+
fmt.Println("Installing Java using apt...")
111+
exec.Command("sudo", "apt", "update").Run()
112+
exec.Command("sudo", "apt", "install", "-y", "default-jdk").Run()
113+
114+
// Install pcscd and pcsc-tools using apt
115+
fmt.Println("Installing pcscd and pcsc-tools using apt...")
116+
exec.Command("sudo", "apt-get", "update").Run()
117+
exec.Command("sudo", "apt-get", "install", "-y", "pcsc-lite", "pcsc-tools").Run()
118+
exec.Command("sudo", "systemctl", "enable", "pcscd").Run()
119+
exec.Command("sudo", "systemctl", "start", "pcscd").Run()
120+
} else {
121+
fmt.Println("Unsupported Linux distribution")
122+
}
123+
default:
124+
fmt.Printf("%s is not supported by this script\n", os)
125+
}
126+
}
127+
128+
func GlobalPlatformDependency() {
129+
// Define paths to search
130+
paths := []string{"../", os.Getenv("HOME") + "/Downloads/"}
131+
132+
found := false
133+
for _, path := range paths {
134+
found = searchFile(path, "gp.jar")
135+
if found {
136+
fmt.Println("gp.jar found in:", path)
137+
fmt.Println("Nice, GlobalPlatformDependency is good :) ")
138+
break
139+
}
140+
}
141+
142+
// If not found, download the file
143+
if !found {
144+
fmt.Println("gp.jar not found :/ Downloading...")
145+
err := downloadFile("gp.jar", gpJarURL)
146+
if err != nil {
147+
fmt.Println("Error downloading gp.jar:", err)
148+
return
149+
}
150+
fmt.Println("Downloaded gp.jar successfully.")
151+
}
152+
}
153+
154+
func searchFile(dir, filename string) bool {
155+
found := false
156+
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
157+
if err != nil {
158+
return err
159+
}
160+
if info.Name() == filename {
161+
found = true
162+
}
163+
return nil
164+
})
165+
return found
166+
}
167+
168+
169+
170+
171+
172+
func downloadFile(filepath string, url string) error {
173+
// Get the data
174+
resp, err := http.Get(url)
175+
if err != nil {
176+
return err
177+
}
178+
defer resp.Body.Close()
179+
180+
// Create the file
181+
out, err := os.Create(filepath)
182+
if err != nil {
183+
return err
184+
}
185+
defer out.Close()
186+
187+
buf := make([]byte, 1024) // buffer size
188+
for {
189+
n, err := resp.Body.Read(buf)
190+
if err != nil && err != io.EOF {
191+
return err
192+
}
193+
if n == 0 {
194+
break
195+
}
196+
197+
if _, writeErr := out.Write(buf[:n]); writeErr != nil {
198+
return writeErr
199+
}
200+
}
201+
202+
return nil
203+
}
204+
205+

main.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,27 +69,31 @@ func main() {
6969
case "3":
7070
qr()
7171

72-
case "4":
73-
err := art_link.PrintFileSlowly("apexflexflexsecure.txt")
74-
if err != nil {
75-
fmt.Println("Error displaying ASCII art:", err)
76-
}
7772

78-
fmt.Println("Exiting...")
79-
os.Exit(0)
80-
case "5":
73+
case "4":
8174
fmt.Println("Installing Dependencies...")
8275
keycard_link.JavaDependency()
8376
keycard_link.GlobalPlatformDependency()
8477

85-
case "6":
78+
case "5":
8679
fmt.Println("Installing Keycard...")
87-
err := keycard_link.installKeycard()
80+
err := keycard_link.InstallKeycard()
8881
if err != nil {
8982
fmt.Println("Error installing keycard:", err)
9083
}
9184

85+
case "6":
86+
fmt.Println("Running Connection test to the IPFS Network.")
87+
cid := "bafkreie7ohywtosou76tasm7j63yigtzxe7d5zqus4zu3j6oltvgtibeom" // Welcome to IPFS CID
88+
runIPFSTestWithViu(cid)
89+
90+
9291
case "7":
92+
err := art_link.PrintFileSlowly("apexflexflexsecure.txt")
93+
if err != nil {
94+
fmt.Println("Error displaying ASCII art:", err)
95+
}
96+
9397
fmt.Println("Exiting...")
9498
os.Exit(0)
9599

@@ -130,13 +134,14 @@ func menu() (string, error) {
130134
fmt.Println("---------------------------------------------")
131135
fmt.Println("IPFS-Secure | NFC Interface for IPFS")
132136
fmt.Println("=============================================")
133-
fmt.Println("What would you like to do?")
134-
fmt.Println("1. Encrypt / upload sensitive data to IPFS.")
135-
fmt.Println("2. Decrypt / pull file with CID.")
136-
fmt.Println("3. Print CID Log to QR code.")
137-
fmt.Println("4. Install Dependencies (Java and PCSCD)")
137+
fmt.Println(" What would you like to do?")
138+
fmt.Println("1. Encrypt / upload sensitive data to IPFS")
139+
fmt.Println("2. Decrypt / pull file with CID")
140+
fmt.Println("3. Print CID Log to QR code")
141+
fmt.Println("4. Install Dependencies (Java, GPP)")
138142
fmt.Println("5. Install Keycard onto Implant")
139-
fmt.Println("6. Exit.")
143+
fmt.Println("6. Run Connection Test to IPFS")
144+
fmt.Println("7. Exit the Program")
140145
fmt.Println("=============================================")
141146
return generalAskUser("Enter your choice: ")
142147
}

0 commit comments

Comments
 (0)