Skip to content

Commit

Permalink
Merge pull request #16 from i2p/generateKeys
Browse files Browse the repository at this point in the history
fix public key out put in generateKeys
  • Loading branch information
eyedeekay authored Dec 13, 2020
2 parents 8623304 + b410da5 commit ebf7d0b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 15 deletions.
13 changes: 12 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
*.o
libsam3-tests
*.a
*.a
examples/libsam3
examples/sam3/dgrams
examples/sam3/dgramc
examples/sam3/keys
examples/sam3/keysp
examples/sam3namelookup
examples/sam3/streamss
examples/sam3/streamcs
examples/sam3/log
examples/sam3/err

7 changes: 6 additions & 1 deletion examples/sam3/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ ssclient:
ssserver:
${CC} ${CFLAGS} streamss.c -o streamss ../libsam3/libsam3.o

keysp:
${CXX} ${CFLAGS} keys.cc -o keysp ../libsam3/libsam3.o

keys:
${CC} ${CFLAGS} keys.c -o keys ../libsam3/libsam3.o

clean:
rm -f samtest lookup dgramc dgrams streamc streams streams.key test-lookup
rm -f samtest lookup dgramc dgrams streamc streams streams.key test-lookup keys keysp

debug:
sed -i 's|// libsam3_debug = 1;|libsam3_debug = 1;|g' *.c
Expand Down
20 changes: 20 additions & 0 deletions examples/sam3/keys.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//#include <string>
//#include <iostream>
#include "../libsam3/libsam3.h"
#include <stdio.h>

int main() {
// The session is only usef for transporting the data
Sam3Session ss;

if (0 > sam3GenerateKeys(&ss, SAM3_HOST_DEFAULT, SAM3_PORT_DEFAULT, 4)) {
printf("got error");
return -1;
}
printf("\tpubkey: %s \n \tprivkey: %s", ss.pubkey, ss.privkey);
/*auto pub = std::string(ss.pubkey);
auto priv = std::string(ss.privkey);
std::cout << "pub " << pub << std::endl << "priv " << priv << std::endl;*/
return 0;
}
19 changes: 19 additions & 0 deletions examples/sam3/keys.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <string>
#include <iostream>
#include <stdio.h>
#include "../libsam3/libsam3.h"

int main() {
// The session is only usef for transporting the data
Sam3Session ss;

if (0 > sam3GenerateKeys(&ss, SAM3_HOST_DEFAULT, SAM3_PORT_DEFAULT, Sam3SigType::EdDSA_SHA512_Ed25519)) {
printf("got error");
return -1;
}
auto pub = std::string(ss.pubkey);
auto priv = std::string(ss.privkey);

std::cout << "pub " << pub << std::endl << "priv " << priv << std::endl;
return 0;
}
29 changes: 16 additions & 13 deletions src/libsam3/libsam3.c
Original file line number Diff line number Diff line change
Expand Up @@ -716,20 +716,23 @@ int sam3GenerateKeys(Sam3Session *ses, const char *hostname, int port,
return -1;
}
//
if (sam3tcpPrintf(fd, "DEST GENERATE %s\n", sigtypes[sigType]) >= 0) {
if ((rep = sam3ReadReply(fd)) != NULL &&
sam3IsGoodReply(rep, "DEST", "REPLY", NULL, NULL)) {
const char *pub = sam3FindField(rep, "PUB"),
*priv = sam3FindField(rep, "PRIV");
//
if (pub != NULL && sam3CheckValidKeyLength(pub) && priv != NULL &&
strlen(priv) >= SAM3_PRIVKEY_MIN_SIZE) {
strcpy(ses->pubkey, pub);
strcpy(ses->privkey, priv);
res = 0;
}
}
if (sam3tcpPrintf(fd, "DEST GENERATE\n") < 0) {
strcpyerr(ses, "DEST_ERROR");
}

rep = sam3ReadReply(fd);
// sam3DumpFieldList(rep);
if (!sam3IsGoodReply(rep, "DEST", "REPLY", "PUB", NULL)) {
strcpyerr(ses, "PUBKEY_ERROR");
}
if (!sam3IsGoodReply(rep, "DEST", "REPLY", "PRIV", NULL)) {
strcpyerr(ses, "PRIVKEY_ERROR");
}
const char *pub = sam3FindField(rep, "PUB");
strcpy(ses->pubkey, pub);
const char *priv = sam3FindField(rep, "PRIV");
strcpy(ses->privkey, priv);
res = 0;
//
sam3FreeFieldList(rep);
sam3tcpDisconnect(fd);
Expand Down

0 comments on commit ebf7d0b

Please sign in to comment.