Compare commits
244 Commits
structify
...
old-router
Author | SHA1 | Date | |
---|---|---|---|
7709efff2b | |||
015c4b23e2 | |||
e29c3c7abb | |||
6f6291a9f6 | |||
767864d457 | |||
0a98236d85 | |||
c1fa63f6ec | |||
a75c275b4c | |||
d40b3e0cd3 | |||
2ee2d77d7c | |||
dc9b969bcf | |||
575e877115 | |||
271cf56ded | |||
a29fa0bc03 | |||
63c48dd3b7 | |||
8bec47efd2 | |||
69a50e2035 | |||
8319444890 | |||
b378661e0e | |||
f4086e5f68 | |||
877fc707c4 | |||
98d05e27c8 | |||
4020db8a19 | |||
67a02f5d69 | |||
ca1280231c | |||
02b309df43 | |||
a5b3c3f194 | |||
0aa7a5554b | |||
266a1b71d6 | |||
d32f2e78ab | |||
9e806bc32e | |||
c52112a36f | |||
db0fd9f7e9 | |||
3ab258cde6 | |||
20b9bbd8e4 | |||
1fa520613c | |||
fb99b98a7e | |||
d6b8cd9d4d | |||
92e4656774 | |||
5f2bfb8d9d | |||
9494c226a6 | |||
344edc6d41 | |||
9eea99b489 | |||
c984f94b90 | |||
a17f0208dd | |||
487815f8f1 | |||
24e0baa879 | |||
a5d2f0de8c | |||
423f616d53 | |||
c65048f3c4 | |||
39b683dac8 | |||
46883f6457 | |||
4be55062fc | |||
12a3fd4623 | |||
80e539930e | |||
e58d326d89 | |||
e6f84c16f6 | |||
971a18de8d | |||
833836ae67 | |||
3f191db37a | |||
6865bae386 | |||
f0d9f89ed9 | |||
c149eef1de | |||
748dc8a66f | |||
677aac500e | |||
4e06d3d5ed | |||
5eaa9cf588 | |||
4dbf537e94 | |||
882c018c0c | |||
5432502852 | |||
0ea5743365 | |||
a9dc482bda | |||
c31d20fec0 | |||
647546a374 | |||
e468520906 | |||
ade80e577c | |||
f45d301868 | |||
0256908395 | |||
3c5aa206d1 | |||
a4517cafd7 | |||
f4f39ca53c | |||
220159476a | |||
792cd49208 | |||
68051630c0 | |||
a3340eb40a | |||
465a7787a9 | |||
af3bc44dba | |||
f850f482cf | |||
3f23376d22 | |||
aa98589f1c | |||
c31e990995 | |||
8e97eb5f77 | |||
be35267079 | |||
013d35b447 | |||
e8c9dc17a3 | |||
a1d574446b | |||
8680acc595 | |||
84e3c5accc | |||
e772fb5ceb | |||
a533cd7ce4 | |||
df37d49444 | |||
4b2600a065 | |||
1c4f937002 | |||
5c2b408f65 | |||
beb533a09b | |||
f022522ad5 | |||
2191c40ac6 | |||
3bca467f28 | |||
a2fd65ee32 | |||
b894e8fb17 | |||
93a71c7398 | |||
b6544ad194 | |||
a72b61a886 | |||
dda4f90b6f | |||
1d1d8126c2 | |||
73db39ae50 | |||
53e902f491 | |||
2f2cd2249c | |||
4496c11394 | |||
69449a20b5 | |||
a7689e801a | |||
278bdee277 | |||
684e89c957 | |||
50fa9fc374 | |||
491b25022e | |||
677a6b354b | |||
9469fd83aa | |||
8173ae49e6 | |||
2f109d5b4d | |||
d7378d7b08 | |||
3a51a1229e | |||
2b18b2941d | |||
1eb29ec4ab | |||
d900d7faf8 | |||
03c9d60ab9 | |||
de2caf499e | |||
284dd7287e | |||
9f4154ff45 | |||
08a0d92742 | |||
524526d946 | |||
09c7d32797 | |||
16961abc96 | |||
8fa355f067 | |||
0c7a3f0f22 | |||
3d535f67a1 | |||
bba9350506 | |||
cbc0de4e7e | |||
310ef07d3c | |||
14fc6fc3a8 | |||
a3ce9d36c6 | |||
58a43cdfaf | |||
15a5ca5daf | |||
08a41686b6 | |||
8318fd8f57 | |||
9c0552e236 | |||
20b018a708 | |||
6c62faa49b | |||
a7e31b7833 | |||
c09161c824 | |||
aca62174e6 | |||
bd27f00959 | |||
05c4d3d973 | |||
40d0ea5ff5 | |||
58e8f78c56 | |||
ac705dee76 | |||
ca0443ae6a | |||
94d0d8efb7 | |||
8176e126cb | |||
992c5537db | |||
5ff9f5578e | |||
889ec939b3 | |||
6e3a5923cd | |||
ce25a2f0fb | |||
ec22860bb5 | |||
cefdcd8c14 | |||
d519f7678d | |||
8dfc5ee5b8 | |||
d8317351cf | |||
ef127d81c1 | |||
8acfba160f | |||
d93e78d772 | |||
bdc9ca4c2b | |||
83f3d11621 | |||
784f72403b | |||
664a17e42a | |||
37718c36f8 | |||
b0fcf4bc11 | |||
65febb5dcf | |||
116e22f8da | |||
0419665d7b | |||
dab108c270 | |||
0fcded4c51 | |||
7d16b0b257 | |||
a689f26d73 | |||
8d631239b7 | |||
fdc34b382e | |||
a4ed06e530 | |||
c6e359f152 | |||
70415b34eb | |||
dbd4c7d346 | |||
6b5ea57cbd | |||
43c3498389 | |||
f90d1d5f42 | |||
c8c4196c6f | |||
68a6ed02be | |||
53dd3230df | |||
c3147c3570 | |||
76fba3c8ba | |||
0cae80c698 | |||
7deba5f725 | |||
35983423ed | |||
136133e643 | |||
8d39f1512a | |||
c9b5fa1406 | |||
cdc9998f10 | |||
4c06fcaff0 | |||
403ef30119 | |||
feef6e6bbf | |||
ad7a828d43 | |||
416cf6546a | |||
9b81a5c6d8 | |||
940347351e | |||
726bff974c | |||
74591a96cb | |||
eb117aa1bd | |||
88d8d09527 | |||
83e999992f | |||
a437bab373 | |||
2d310fe91a | |||
fefbfc4be6 | |||
0ec4f55fa9 | |||
8bd30d1e23 | |||
b7768d4d99 | |||
171f09bba6 | |||
13b996d0da | |||
82f0e86931 | |||
f113977419 | |||
86dc323348 | |||
a80c9973f9 | |||
7adf3d7577 | |||
42be0d6b5c | |||
d837630ff6 | |||
590d576b74 | |||
fa86f8e3b3 |
26
.circleci/config.yml
Normal file
26
.circleci/config.yml
Normal file
@ -0,0 +1,26 @@
|
||||
# Use the latest 2.1 version of CircleCI pipeline process engine. See: https://circleci.com/docs/2.0/configuration-reference
|
||||
version: 2.1
|
||||
jobs:
|
||||
build:
|
||||
working_directory: ~/repo
|
||||
docker:
|
||||
- image: circleci/golang:1.15.8
|
||||
steps:
|
||||
- checkout
|
||||
- restore_cache:
|
||||
keys:
|
||||
- go-mod-v4-{{ checksum "go.sum" }}
|
||||
- run:
|
||||
name: Install Dependencies
|
||||
command: go mod download
|
||||
- save_cache:
|
||||
key: go-mod-v4-{{ checksum "go.sum" }}
|
||||
paths:
|
||||
- "/go/pkg/mod"
|
||||
- run:
|
||||
name: Run tests
|
||||
command: |
|
||||
mkdir -p /tmp/test-reports
|
||||
gotestsum --junitfile /tmp/test-reports/unit-tests.xml
|
||||
- store_test_results:
|
||||
path: /tmp/test-reports
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -6,3 +6,6 @@
|
||||
*exportable-fuzz.zip
|
||||
go-i2p
|
||||
*.exe
|
||||
.idea/
|
||||
router.info
|
||||
log
|
||||
|
@ -9,7 +9,6 @@ Install required dependencies
|
||||
This example assumes Ubuntu 16.04
|
||||
|
||||
```sh
|
||||
sudo apt-get install pkg-config libsodium-dev
|
||||
go get github.com/hkparker/go-i2p
|
||||
go get github.com/Sirupsen/logrus
|
||||
go get github.com/stretchr/testify/assert
|
||||
|
59
Makefile
59
Makefile
@ -1,5 +1,8 @@
|
||||
RELEASE_TAG=0.0.1
|
||||
RELEASE_VERSION=${RELEASE_TAG}
|
||||
RELEASE_DESCRIPTION=`cat PASTA.md`
|
||||
REPO := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
|
||||
|
||||
CGO_ENABLED=0
|
||||
|
||||
ifdef GOROOT
|
||||
GO = $(GOROOT)/bin/go
|
||||
@ -13,13 +16,61 @@ else
|
||||
EXE := $(REPO)/go-i2p
|
||||
endif
|
||||
|
||||
#check for gofumpt
|
||||
check_gofumpt:
|
||||
@which gofumpt > /dev/null 2>&1 || (echo "gofumpt is required but not installed. Please install it from https://github.com/mvdan/gofumpt."; exit 1)
|
||||
|
||||
build: clean $(EXE)
|
||||
|
||||
$(EXE):
|
||||
$(GO) build -v -o $(EXE)
|
||||
$(GO) build --tags netgo,osusergo -v -o $(EXE)
|
||||
|
||||
test:
|
||||
$(GO) test ./...
|
||||
test: check_gofumpt fmt
|
||||
$(GO) test -v -failfast ./lib/common/...
|
||||
|
||||
clean:
|
||||
$(GO) clean -v
|
||||
|
||||
fmt:
|
||||
find . -name '*.go' -exec gofumpt -w {} \;
|
||||
|
||||
info:
|
||||
echo "GOROOT: ${GOROOT}"
|
||||
echo "GO: ${GO}"
|
||||
echo "REPO: ${REPO}"
|
||||
|
||||
release:
|
||||
github-release release -u go-i2p -r go-i2p -n "${RELEASE_VERSION}" -t "${RELEASE_TAG}" -d "${RELEASE_DESCRIPTION}" -p
|
||||
|
||||
callvis:
|
||||
go-callvis -format svg -focus upgrade -group pkg,type -limit github.com/go-i2p/go-i2p github.com/go-i2p/go-i2p
|
||||
|
||||
godoc:
|
||||
find lib -type d -exec bash -c "ls {}/*.go && godocdown -o ./{}/doc.md ./{}" \;
|
||||
|
||||
# Include test definitions
|
||||
-include doc/tests/*.mk
|
||||
|
||||
# Define the all-tests target that runs every test suite
|
||||
test-all: test-string-all \
|
||||
test-mapping-all \
|
||||
test-crypto-aes-all \
|
||||
test-crypto-dsa-all \
|
||||
test-crypto-ed25519-all \
|
||||
test-crypto-elg-all \
|
||||
test-crypto-hmac-all \
|
||||
test-i2np-header-all \
|
||||
test-i2np-build-request-all \
|
||||
test-key-cert-all \
|
||||
test-keys-cert-all \
|
||||
test-lease-set-all \
|
||||
test-noise-transport-all \
|
||||
test-router-address-all \
|
||||
test-router-info-all \
|
||||
test-su3-all \
|
||||
test-tunnel-all
|
||||
|
||||
#-include $(shell find doc/tests -type f -name '*.mk') #search for .mk files recursively
|
||||
|
||||
#test-base64-encode-decode-not-mangled:
|
||||
#go test -v ./lib/common/base64 -run TestEncodeDecodeNotMangled
|
83
README.md
83
README.md
@ -4,10 +4,20 @@ A pure Go implementation of the I2P router.
|
||||
|
||||
## Status
|
||||
|
||||
go-i2p is in early development.
|
||||
go-i2p is in early development. The master branch is being refactored and API's are
|
||||
definitely going to change. If you choose to use any part of this code right now,
|
||||
please keep up with these changes, as they will not be backward compatible and require
|
||||
**Fundamentally** changing any code that treats this as a dependency.
|
||||
|
||||
### Implemented Features
|
||||
|
||||
- Clients
|
||||
- [ ] Datagrams
|
||||
- [ ] I2CP
|
||||
- [ ] Message routing
|
||||
- [ ] SAM
|
||||
- [ ] Streaming
|
||||
- [ ] Tunnel Manager
|
||||
- Cryptographic primitives
|
||||
- Signing
|
||||
- [ ] ECDSA_SHA256_P256
|
||||
@ -23,32 +33,85 @@ go-i2p is in early development.
|
||||
- [ ] RSA_SHA384_3072
|
||||
- [ ] RSA_SHA512_4096
|
||||
- [ ] Ed25519
|
||||
- [ ] Red25519
|
||||
- [ ] ElGamal
|
||||
- [ ] AES256
|
||||
- [ ] X25519
|
||||
- [ ] ChaCha20/Poly1305
|
||||
- [ ] Elligator2
|
||||
- [ ] HKDF
|
||||
- [ ] HMAC
|
||||
- [X] Noise subsystem
|
||||
- End-to-End Crypto
|
||||
- [ ] Garlic messages
|
||||
- [ ] ElGamal/AES+SessionTag
|
||||
- [ ] Ratchet/X25519
|
||||
- I2NP
|
||||
- [ ] Message parsing
|
||||
- [ ] Message handling
|
||||
- NetDB
|
||||
- [ ] Local storage
|
||||
- [ ] Persistence to disk
|
||||
- [ ] Reseeding
|
||||
- [/] Local storage
|
||||
- [/] Persistence to disk
|
||||
- [X] Reseeding
|
||||
- [ ] Lookups
|
||||
- [ ] Expiry
|
||||
- [ ] Exploration
|
||||
- [ ] Publishing
|
||||
- [ ] Floodfill
|
||||
- [ ] LS2 and Encrypted Leasesets
|
||||
- Transports
|
||||
- [ ] Transport manager
|
||||
- NTCP
|
||||
- [ ] Handshake
|
||||
- [ ] Session tracking
|
||||
- [ ] Automatic session creation
|
||||
- [X] Transport manager
|
||||
- NTCP2
|
||||
- [ ] Handshake
|
||||
- [ ] Session tracking
|
||||
- [ ] Automatic session creation
|
||||
- [ ] SSU
|
||||
- SSU2
|
||||
- [ ] Handshake
|
||||
- [ ] Session tracking
|
||||
- [ ] Automatic session creation
|
||||
- [ ] Peer Tests
|
||||
- [ ] Introducers
|
||||
- Tunnels
|
||||
- [ ] Building
|
||||
- [ ] Build Message Crypto (ElGamal)
|
||||
- [ ] Build Message Crypto (ECIES)
|
||||
- [ ] Participating
|
||||
- [ ] Tunnel Message Crypto
|
||||
- [ ] Tunnel Message Fragmentation/Reassembly
|
||||
- Common Data Structures
|
||||
- [X] Keys and Cert
|
||||
- [X] Key Certificates
|
||||
- [X] Certificate
|
||||
- [X] Lease
|
||||
- [X] Lease Set
|
||||
- [X] Router Info
|
||||
- [X] Router Identity
|
||||
- [X] Router Address
|
||||
- [X] Session Key
|
||||
- [X] Signature Types
|
||||
- [X] Destination
|
||||
- [X] Data Types
|
||||
- [X] Session Tag
|
||||
|
||||
## Verbosity ##
|
||||
Logging can be enabled and configured using the DEBUG_I2P environment variable. By default, logging is disabled.
|
||||
|
||||
There are three available log levels:
|
||||
|
||||
- Debug
|
||||
```shell
|
||||
export DEBUG_I2P=debug
|
||||
```
|
||||
- Warn
|
||||
```shell
|
||||
export DEBUG_I2P=warn
|
||||
```
|
||||
- Error
|
||||
```shell
|
||||
export DEBUG_I2P=error
|
||||
```
|
||||
|
||||
If DEBUG_I2P is set to an unrecognized variable, it will fall back to "debug".
|
||||
|
||||
## Contributing
|
||||
|
||||
|
41
ROADMAP.md
Normal file
41
ROADMAP.md
Normal file
@ -0,0 +1,41 @@
|
||||
# go-i2p Implementation Roadmap
|
||||
|
||||
## Transport Layer (NTCP2)
|
||||
- Build on existing lib/transport/noise implementation
|
||||
- Core NTCP2 components:
|
||||
* Session handshake using noise protocol
|
||||
* Connection management
|
||||
* I2NP message transport
|
||||
|
||||
## Reseed System
|
||||
- SU3 file format implementation:
|
||||
* Format parsing and validation(Much of this work is done in reseed-tools, may need to be moved here)
|
||||
* Signature verification system(Much of this work is done in reseed-tools, may need to be moved here)
|
||||
- Local reseed functionality:
|
||||
* File-based reseed operations
|
||||
- Self-signed/Package-pinned X.509 certificate handling for reseed validation
|
||||
|
||||
## NetDb and Database Store
|
||||
- Database Store message handling:
|
||||
* Message structure implementation
|
||||
* Message handling implementation
|
||||
- NetDb core implementation:
|
||||
* RouterInfo management
|
||||
* LeaseSet management
|
||||
* Lookup system
|
||||
* Storage interface
|
||||
* Peer selection logic?(Maybe do something very basic for now like i2pd used to do, and then improve it later, the important part will be interface design at first)
|
||||
|
||||
## Tunnel Implementation
|
||||
- Tunnel cryptography:
|
||||
* Key generation and management
|
||||
* Layered encryption scheme
|
||||
- Message processing:
|
||||
* Build request/response handling
|
||||
* Gateway implementation
|
||||
* Message forwarding logic
|
||||
|
||||
Notes:
|
||||
- Excluding legacy protocols (SSU1, NTCP1, elgamal, DSA)
|
||||
- Leveraging existing noise protocol implementation
|
||||
- SSU2 is not on this roadmap but is fair game for implementation as soon as NTCP2 is done. We're focused on NTCP2 to get this thing sending I2NP messages.
|
17
doc/tests/aes.mk
Normal file
17
doc/tests/aes.mk
Normal file
@ -0,0 +1,17 @@
|
||||
test-crypto-aes-all: test-crypto-aes-core test-crypto-aes-validation test-crypto-aes-padding
|
||||
|
||||
test-crypto-aes-core:
|
||||
$(GO) test -v ./lib/crypto -run TestAESEncryptDecrypt
|
||||
|
||||
test-crypto-aes-validation:
|
||||
$(GO) test -v ./lib/crypto -run TestAESEncryptInvalidKey
|
||||
$(GO) test -v ./lib/crypto -run TestAESDecryptInvalidInput
|
||||
|
||||
test-crypto-aes-padding:
|
||||
$(GO) test -v ./lib/crypto -run TestPKCS7PadUnpad
|
||||
$(GO) test -v ./lib/crypto -run TestPKCS7UnpadInvalidInput
|
||||
|
||||
.PHONY: test-crypto-aes-all \
|
||||
test-crypto-aes-core \
|
||||
test-crypto-aes-validation \
|
||||
test-crypto-aes-padding
|
4
doc/tests/base32.mk
Normal file
4
doc/tests/base32.mk
Normal file
@ -0,0 +1,4 @@
|
||||
test-base32-encode-decode-not-mangled:
|
||||
$(GO) test -v ./lib/common/base32 -run TestEncodeDecodeNotMangled
|
||||
|
||||
.PHONY: test-base32-encode-decode-not-mangled
|
4
doc/tests/base64.mk
Normal file
4
doc/tests/base64.mk
Normal file
@ -0,0 +1,4 @@
|
||||
test-base64-encode-decode-not-mangled:
|
||||
$(GO) test -v ./lib/common/base64 -run TestEncodeDecodeNotMangled
|
||||
|
||||
.PHONY: test-base64-encode-decode-not-mangled
|
24
doc/tests/build_request_record.mk
Normal file
24
doc/tests/build_request_record.mk
Normal file
@ -0,0 +1,24 @@
|
||||
test-build-request-all: test-build-request-receive test-build-request-ident test-build-request-components
|
||||
|
||||
test-build-request-receive:
|
||||
$(GO) test -v ./lib/i2np -run TestReadBuildRequestRecordReceiveTunnel
|
||||
|
||||
test-build-request-ident:
|
||||
$(GO) test -v ./lib/i2np -run TestReadBuildRequestRecordOurIdent
|
||||
|
||||
test-build-request-components:
|
||||
$(GO) test -v ./lib/i2np -run TestReadBuildRequestRecordNextTunnel
|
||||
$(GO) test -v ./lib/i2np -run TestReadBuildRequestRecordNextIdent
|
||||
$(GO) test -v ./lib/i2np -run TestReadBuildRequestRecordLayerKey
|
||||
$(GO) test -v ./lib/i2np -run TestReadBuildRequestRecordIVKey
|
||||
$(GO) test -v ./lib/i2np -run TestReadBuildRequestRecordReplyKey
|
||||
$(GO) test -v ./lib/i2np -run TestReadBuildRequestRecordReplyIV
|
||||
$(GO) test -v ./lib/i2np -run TestReadBuildRequestRecordFlag
|
||||
$(GO) test -v ./lib/i2np -run TestReadBuildRequestRecordRequestTime
|
||||
$(GO) test -v ./lib/i2np -run TestReadBuildRequestRecordSendMessageID
|
||||
$(GO) test -v ./lib/i2np -run TestReadBuildRequestRecordPadding
|
||||
|
||||
.PHONY: test-build-request-all \
|
||||
test-build-request-receive \
|
||||
test-build-request-ident \
|
||||
test-build-request-components
|
61
doc/tests/certificate.mk
Normal file
61
doc/tests/certificate.mk
Normal file
@ -0,0 +1,61 @@
|
||||
|
||||
test-cert-all: test-cert-type test-cert-length test-cert-data test-cert-read test-cert-length-correct test-cert-length-too-short test-cert-length-data-short test-cert-data-correct test-cert-data-too-long test-cert-data-too-short test-cert-read-correct test-cert-read-short test-cert-read-remainder test-cert-read-invalid
|
||||
|
||||
test-cert-type:
|
||||
$(GO) test -v ./lib/common/certificate -run TestCertificateTypeIsFirstByte
|
||||
|
||||
test-cert-length:
|
||||
$(GO) test -v ./lib/common/certificate -run TestCertificateLength
|
||||
|
||||
test-cert-data:
|
||||
$(GO) test -v ./lib/common/certificate -run TestCertificateData
|
||||
|
||||
test-cert-read:
|
||||
$(GO) test -v ./lib/common/certificate -run TestReadCertificate
|
||||
|
||||
test-cert-length-correct:
|
||||
$(GO) test -v ./lib/common/certificate -run TestCertificateLengthCorrect
|
||||
|
||||
test-cert-length-too-short:
|
||||
$(GO) test -v ./lib/common/certificate -run TestCertificateLengthErrWhenTooShort
|
||||
|
||||
test-cert-length-data-short:
|
||||
$(GO) test -v ./lib/common/certificate -run TestCertificateLengthErrWhenDataTooShort
|
||||
|
||||
test-cert-data-correct:
|
||||
$(GO) test -v ./lib/common/certificate -run TestCertificateDataWhenCorrectSize
|
||||
|
||||
test-cert-data-too-long:
|
||||
$(GO) test -v ./lib/common/certificate -run TestCertificateDataWhenTooLong
|
||||
|
||||
test-cert-data-too-short:
|
||||
$(GO) test -v ./lib/common/certificate -run TestCertificateDataWhenTooShort
|
||||
|
||||
test-cert-read-correct:
|
||||
$(GO) test -v ./lib/common/certificate -run TestReadCertificateWithCorrectData
|
||||
|
||||
test-cert-read-short:
|
||||
$(GO) test -v ./lib/common/certificate -run TestReadCertificateWithDataTooShort
|
||||
|
||||
test-cert-read-remainder:
|
||||
$(GO) test -v ./lib/common/certificate -run TestReadCertificateWithRemainder
|
||||
|
||||
test-cert-read-invalid:
|
||||
$(GO) test -v ./lib/common/certificate -run TestReadCertificateWithInvalidLength
|
||||
|
||||
# Declare all targets as PHONY
|
||||
.PHONY: test-cert-all \
|
||||
test-cert-type \
|
||||
test-cert-length \
|
||||
test-cert-data \
|
||||
test-cert-read \
|
||||
test-cert-length-correct \
|
||||
test-cert-length-too-short \
|
||||
test-cert-length-data-short \
|
||||
test-cert-data-correct \
|
||||
test-cert-data-too-long \
|
||||
test-cert-data-too-short \
|
||||
test-cert-read-correct \
|
||||
test-cert-read-short \
|
||||
test-cert-read-remainder \
|
||||
test-cert-read-invalid
|
2
doc/tests/date.mk
Normal file
2
doc/tests/date.mk
Normal file
@ -0,0 +1,2 @@
|
||||
test-date-time-from-milliseconds:
|
||||
$(GO) test -v ./lib/common/data -run TestTimeFromMilliseconds
|
20
doc/tests/dsa.mk
Normal file
20
doc/tests/dsa.mk
Normal file
@ -0,0 +1,20 @@
|
||||
test-crypto-dsa-all: test-crypto-dsa test-crypto-dsa-benchmarks
|
||||
|
||||
test-crypto-dsa:
|
||||
$(GO) test -v ./lib/crypto -run TestDSA
|
||||
|
||||
test-crypto-dsa-benchmarks:
|
||||
$(GO) test -v ./lib/crypto -bench=DSA -run=^$
|
||||
|
||||
# Individual benchmarks
|
||||
test-crypto-dsa-bench-generate:
|
||||
$(GO) test -v ./lib/crypto -bench=DSAGenerate -run=^$
|
||||
|
||||
test-crypto-dsa-bench-sign-verify:
|
||||
$(GO) test -v ./lib/crypto -bench=DSASignVerify -run=^$
|
||||
|
||||
.PHONY: test-crypto-dsa-all \
|
||||
test-crypto-dsa \
|
||||
test-crypto-dsa-benchmarks \
|
||||
test-crypto-dsa-bench-generate \
|
||||
test-crypto-dsa-bench-sign-verify
|
7
doc/tests/ed25519.mk
Normal file
7
doc/tests/ed25519.mk
Normal file
@ -0,0 +1,7 @@
|
||||
test-crypto-ed25519-all: test-crypto-ed25519
|
||||
|
||||
test-crypto-ed25519:
|
||||
$(GO) test -v ./lib/crypto -run TestEd25519
|
||||
|
||||
.PHONY: test-crypto-ed25519-all \
|
||||
test-crypto-ed25519
|
24
doc/tests/elg.mk
Normal file
24
doc/tests/elg.mk
Normal file
@ -0,0 +1,24 @@
|
||||
test-crypto-elg-all: test-crypto-elg test-crypto-elg-benchmarks
|
||||
|
||||
test-crypto-elg:
|
||||
$(GO) test -v ./lib/crypto -run TestElg
|
||||
|
||||
test-crypto-elg-benchmarks:
|
||||
$(GO) test -v ./lib/crypto -bench=Elg -run=^$
|
||||
|
||||
# Individual benchmarks
|
||||
test-crypto-elg-bench-generate:
|
||||
$(GO) test -v ./lib/crypto -bench=ElgGenerate -run=^$
|
||||
|
||||
test-crypto-elg-bench-encrypt:
|
||||
$(GO) test -v ./lib/crypto -bench=ElgEncrypt -run=^$
|
||||
|
||||
test-crypto-elg-bench-decrypt:
|
||||
$(GO) test -v ./lib/crypto -bench=ElgDecrypt -run=^$
|
||||
|
||||
.PHONY: test-crypto-elg-all \
|
||||
test-crypto-elg \
|
||||
test-crypto-elg-benchmarks \
|
||||
test-crypto-elg-bench-generate \
|
||||
test-crypto-elg-bench-encrypt \
|
||||
test-crypto-elg-bench-decrypt
|
30
doc/tests/header.mk
Normal file
30
doc/tests/header.mk
Normal file
@ -0,0 +1,30 @@
|
||||
|
||||
test-i2np-header-all: test-i2np-type test-i2np-message test-i2np-expiration test-i2np-ntcp-components test-i2np-data test-i2np-regression
|
||||
|
||||
test-i2np-type:
|
||||
$(GO) test -v ./lib/i2np -run TestReadI2NPTypeWith
|
||||
|
||||
test-i2np-message:
|
||||
$(GO) test -v ./lib/i2np -run TestReadI2NPNTCPMessageID
|
||||
|
||||
test-i2np-expiration:
|
||||
$(GO) test -v ./lib/i2np -run TestReadI2NPNTCPMessageExpiration
|
||||
$(GO) test -v ./lib/i2np -run TestReadI2NPSSUMessageExpiration
|
||||
|
||||
test-i2np-ntcp-components:
|
||||
$(GO) test -v ./lib/i2np -run TestReadI2NPNTCPMessageSize
|
||||
$(GO) test -v ./lib/i2np -run TestReadI2NPNTCPMessageChecksum
|
||||
|
||||
test-i2np-data:
|
||||
$(GO) test -v ./lib/i2np -run TestReadI2NPNTCPData
|
||||
|
||||
test-i2np-regression:
|
||||
$(GO) test -v ./lib/i2np -run TestCrasherRegression123781
|
||||
|
||||
.PHONY: test-i2np-header-all \
|
||||
test-i2np-type \
|
||||
test-i2np-message \
|
||||
test-i2np-expiration \
|
||||
test-i2np-ntcp-components \
|
||||
test-i2np-data \
|
||||
test-i2np-regression
|
7
doc/tests/hmac.mk
Normal file
7
doc/tests/hmac.mk
Normal file
@ -0,0 +1,7 @@
|
||||
test-crypto-hmac-all: test-crypto-hmac
|
||||
|
||||
test-crypto-hmac:
|
||||
$(GO) test -v ./lib/crypto -run Test_I2PHMAC
|
||||
|
||||
.PHONY: test-crypto-hmac-all \
|
||||
test-crypto-hmac
|
15
doc/tests/integer.mk
Normal file
15
doc/tests/integer.mk
Normal file
@ -0,0 +1,15 @@
|
||||
test-integer-all: test-integer-big-endian test-integer-one-byte test-integer-zero
|
||||
|
||||
test-integer-big-endian:
|
||||
$(GO) test -v ./lib/common/integer -run TestIntegerBigEndian
|
||||
|
||||
test-integer-one-byte:
|
||||
$(GO) test -v ./lib/common/integer -run TestWorksWithOneByte
|
||||
|
||||
test-integer-zero:
|
||||
$(GO) test -v ./lib/common/integer -run TestIsZeroWithNoData
|
||||
|
||||
.PHONY: test-integer-all \
|
||||
test-integer-big-endian \
|
||||
test-integer-one-byte \
|
||||
test-integer-zero
|
23
doc/tests/key_certificate.mk
Normal file
23
doc/tests/key_certificate.mk
Normal file
@ -0,0 +1,23 @@
|
||||
test-key-cert-all: test-key-cert-signing test-key-cert-public test-key-cert-construct
|
||||
|
||||
test-key-cert-signing:
|
||||
$(GO) test -v ./lib/common/key_certificate -run TestSingingPublicKeyTypeReturnsCorrectInteger
|
||||
$(GO) test -v ./lib/common/key_certificate -run TestSingingPublicKeyTypeReportsWhenDataTooSmall
|
||||
$(GO) test -v ./lib/common/key_certificate -run TestConstructSigningPublicKeyReportsWhenDataTooSmall
|
||||
$(GO) test -v ./lib/common/key_certificate -run TestConstructSigningPublicKeyWithDSASHA1
|
||||
$(GO) test -v ./lib/common/key_certificate -run TestConstructSigningPublicKeyWithP256
|
||||
$(GO) test -v ./lib/common/key_certificate -run TestConstructSigningPublicKeyWithP384
|
||||
$(GO) test -v ./lib/common/key_certificate -run TestConstructSigningPublicKeyWithP521
|
||||
|
||||
test-key-cert-public:
|
||||
$(GO) test -v ./lib/common/key_certificate -run TestPublicKeyTypeReturnsCorrectInteger
|
||||
$(GO) test -v ./lib/common/key_certificate -run TestPublicKeyTypeReportsWhenDataTooSmall
|
||||
|
||||
test-key-cert-construct:
|
||||
$(GO) test -v ./lib/common/key_certificate -run TestConstructPublicKeyReportsWhenDataTooSmall
|
||||
$(GO) test -v ./lib/common/key_certificate -run TestConstructPublicKeyReturnsCorrectDataWithElg
|
||||
|
||||
.PHONY: test-key-cert-all \
|
||||
test-key-cert-signing \
|
||||
test-key-cert-public \
|
||||
test-key-cert-construct
|
30
doc/tests/keys_and_cert.mk
Normal file
30
doc/tests/keys_and_cert.mk
Normal file
@ -0,0 +1,30 @@
|
||||
test-keys-cert-all: test-keys-cert-certificate test-keys-cert-public test-keys-cert-signing test-keys-cert-creation
|
||||
|
||||
test-keys-cert-certificate:
|
||||
$(GO) test -v ./lib/common/keys_and_cert -run TestCertificateWithValidData
|
||||
|
||||
test-keys-cert-public:
|
||||
$(GO) test -v ./lib/common/keys_and_cert -run TestPublicKeyWithBadData
|
||||
$(GO) test -v ./lib/common/keys_and_cert -run TestPublicKeyWithBadCertificate
|
||||
$(GO) test -v ./lib/common/keys_and_cert -run TestPublicKeyWithNullCertificate
|
||||
$(GO) test -v ./lib/common/keys_and_cert -run TestPublicKeyWithKeyCertificate
|
||||
|
||||
test-keys-cert-signing:
|
||||
$(GO) test -v ./lib/common/keys_and_cert -run TestSigningPublicKeyWithBadData
|
||||
$(GO) test -v ./lib/common/keys_and_cert -run TestSigningPublicKeyWithBadCertificate
|
||||
$(GO) test -v ./lib/common/keys_and_cert -run TestSigningPublicKeyWithNullCertificate
|
||||
$(GO) test -v ./lib/common/keys_and_cert -run TestSigningPublicKeyWithKeyCertificate
|
||||
|
||||
test-keys-cert-creation:
|
||||
$(GO) test -v ./lib/common/keys_and_cert -run TestNewKeysAndCertWithMissingData
|
||||
$(GO) test -v ./lib/common/keys_and_cert -run TestNewKeysAndCertWithMissingCertData
|
||||
$(GO) test -v ./lib/common/keys_and_cert -run TestNewKeysAndCertWithValidDataWithCertificate
|
||||
$(GO) test -v ./lib/common/keys_and_cert -run TestNewKeysAndCertWithValidDataWithoutCertificate
|
||||
$(GO) test -v ./lib/common/keys_and_cert -run TestNewKeysAndCertWithValidDataWithCertificateAndRemainder
|
||||
$(GO) test -v ./lib/common/keys_and_cert -run TestNewKeysAndCertWithValidDataWithoutCertificateAndRemainder
|
||||
|
||||
.PHONY: test-keys-cert-all \
|
||||
test-keys-cert-certificate \
|
||||
test-keys-cert-public \
|
||||
test-keys-cert-signing \
|
||||
test-keys-cert-creation
|
22
doc/tests/lease_set.mk
Normal file
22
doc/tests/lease_set.mk
Normal file
@ -0,0 +1,22 @@
|
||||
test-lease-set-all: test-lease-set-basic test-lease-set-leases test-lease-set-expiration
|
||||
|
||||
test-lease-set-basic:
|
||||
$(GO) test -v ./lib/common/lease_set -run TestDestinationIsCorrect
|
||||
$(GO) test -v ./lib/common/lease_set -run TestPublicKeyIsCorrect
|
||||
$(GO) test -v ./lib/common/lease_set -run TestSigningKeyIsCorrect
|
||||
$(GO) test -v ./lib/common/lease_set -run TestSignatureIsCorrect
|
||||
|
||||
test-lease-set-leases:
|
||||
$(GO) test -v ./lib/common/lease_set -run TestLeaseCountCorrect
|
||||
$(GO) test -v ./lib/common/lease_set -run TestLeaseCountCorrectWithMultiple
|
||||
$(GO) test -v ./lib/common/lease_set -run TestLeaseCountErrorWithTooMany
|
||||
$(GO) test -v ./lib/common/lease_set -run TestLeasesHaveCorrectData
|
||||
|
||||
test-lease-set-expiration:
|
||||
$(GO) test -v ./lib/common/lease_set -run TestNewestExpirationIsCorrect
|
||||
$(GO) test -v ./lib/common/lease_set -run TestOldestExpirationIsCorrect
|
||||
|
||||
.PHONY: test-lease-set-all \
|
||||
test-lease-set-basic \
|
||||
test-lease-set-leases \
|
||||
test-lease-set-expiration
|
28
doc/tests/mapping.mk
Normal file
28
doc/tests/mapping.mk
Normal file
@ -0,0 +1,28 @@
|
||||
test-mapping-all: test-mapping-values test-mapping-duplicates test-mapping-conversion test-mapping-utils
|
||||
|
||||
test-mapping-values:
|
||||
$(GO) test -v ./lib/common/data -run TestValuesExclusesPairWithBadData
|
||||
$(GO) test -v ./lib/common/data -run TestValuesWarnsMissingData
|
||||
$(GO) test -v ./lib/common/data -run TestValuesWarnsExtraData
|
||||
$(GO) test -v ./lib/common/data -run TestValuesEnforcesEqualDelimitor
|
||||
$(GO) test -v ./lib/common/data -run TestValuesEnforcedSemicolonDelimitor
|
||||
$(GO) test -v ./lib/common/data -run TestValuesReturnsValues
|
||||
|
||||
test-mapping-duplicates:
|
||||
$(GO) test -v ./lib/common/data -run TestHasDuplicateKeysTrueWhenDuplicates
|
||||
$(GO) test -v ./lib/common/data -run TestHasDuplicateKeysFalseWithoutDuplicates
|
||||
$(GO) test -v ./lib/common/data -run TestReadMappingHasDuplicateKeys
|
||||
|
||||
test-mapping-conversion:
|
||||
$(GO) test -v ./lib/common/data -run TestGoMapToMappingProducesCorrectMapping
|
||||
$(GO) test -v ./lib/common/data -run TestFullGoMapToMappingProducesCorrectMapping
|
||||
|
||||
test-mapping-utils:
|
||||
$(GO) test -v ./lib/common/data -run TestStopValueRead
|
||||
$(GO) test -v ./lib/common/data -run TestBeginsWith
|
||||
|
||||
.PHONY: test-mapping-all \
|
||||
test-mapping-values \
|
||||
test-mapping-duplicates \
|
||||
test-mapping-conversion \
|
||||
test-mapping-utils
|
2
doc/tests/mapping_values.mk
Normal file
2
doc/tests/mapping_values.mk
Normal file
@ -0,0 +1,2 @@
|
||||
test-mapping-values-order:
|
||||
$(GO) test -v ./lib/common/data -run TestMappingOrderSortsValuesThenKeys
|
19
doc/tests/noise.mk
Normal file
19
doc/tests/noise.mk
Normal file
@ -0,0 +1,19 @@
|
||||
test-noise-transport-all: test-noise-packet-encryption test-noise-transport-connection test-noise-packet-obfuscation test-noise-packet-obfuscation-func
|
||||
|
||||
test-noise-packet-encryption:
|
||||
$(GO) test -v ./lib/transport/noise -run TestEncryptDecryptPacketOffline
|
||||
|
||||
test-noise-transport-connection:
|
||||
$(GO) test -v ./lib/transport/noise -run TestTransport
|
||||
|
||||
test-noise-packet-obfuscation:
|
||||
$(GO) test -v ./lib/transport/noise -run TestEncryptDecryptPacketObfsOffline
|
||||
|
||||
test-noise-packet-obfuscation-func:
|
||||
$(GO) test -v ./lib/transport/noise -run TestEncryptDecryptPacketObfsOfflineWithFunc
|
||||
|
||||
.PHONY: test-noise-transport-all \
|
||||
test-noise-packet-encryption \
|
||||
test-noise-transport-connection \
|
||||
test-noise-packet-obfuscation \
|
||||
test-noise-packet-obfuscation-func
|
19
doc/tests/router_address.mk
Normal file
19
doc/tests/router_address.mk
Normal file
@ -0,0 +1,19 @@
|
||||
test-router-address-all: test-router-address-validation test-router-address-functionality test-router-address-fuzz
|
||||
|
||||
test-router-address-validation:
|
||||
$(GO) test -v ./lib/common/router_address -run TestCheckValidReportsEmptySlice
|
||||
$(GO) test -v ./lib/common/router_address -run TestCheckRouterAddressValidReportsDataMissing
|
||||
$(GO) test -v ./lib/common/router_address -run TestCheckRouterAddressValidNoErrWithValidData
|
||||
|
||||
test-router-address-functionality:
|
||||
$(GO) test -v ./lib/common/router_address -run TestRouterAddressCostReturnsFirstByte
|
||||
$(GO) test -v ./lib/common/router_address -run TestRouterAddressExpirationReturnsCorrectData
|
||||
$(GO) test -v ./lib/common/router_address -run TestReadRouterAddressReturnsCorrectRemainderWithoutError
|
||||
|
||||
test-router-address-fuzz:
|
||||
$(GO) test -v ./lib/common/router_address -run TestCorrectsFuzzCrasher1
|
||||
|
||||
.PHONY: test-router-address-all \
|
||||
test-router-address-validation \
|
||||
test-router-address-functionality \
|
||||
test-router-address-fuzz
|
26
doc/tests/router_info.mk
Normal file
26
doc/tests/router_info.mk
Normal file
@ -0,0 +1,26 @@
|
||||
test-router-info-all: test-router-info-published test-router-info-addresses test-router-info-identity test-router-info-misc
|
||||
|
||||
test-router-info-published:
|
||||
$(GO) test -v ./lib/common/router_info -run TestPublishedReturnsCorrectDate
|
||||
$(GO) test -v ./lib/common/router_info -run TestPublishedReturnsCorrectErrorWithPartialDate
|
||||
$(GO) test -v ./lib/common/router_info -run TestPublishedReturnsCorrectErrorWithInvalidData
|
||||
|
||||
test-router-info-addresses:
|
||||
$(GO) test -v ./lib/common/router_info -run TestRouterAddressCountReturnsCorrectCount
|
||||
$(GO) test -v ./lib/common/router_info -run TestRouterAddressCountReturnsCorrectErrorWithInvalidData
|
||||
$(GO) test -v ./lib/common/router_info -run TestRouterAddressesReturnsAddresses
|
||||
$(GO) test -v ./lib/common/router_info -run TestRouterAddressesReturnsAddressesWithMultiple
|
||||
|
||||
test-router-info-identity:
|
||||
$(GO) test -v ./lib/common/router_info -run TestRouterIdentityIsCorrect
|
||||
|
||||
test-router-info-misc:
|
||||
$(GO) test -v ./lib/common/router_info -run TestPeerSizeIsZero
|
||||
$(GO) test -v ./lib/common/router_info -run TestOptionsAreCorrect
|
||||
$(GO) test -v ./lib/common/router_info -run TestSignatureIsCorrectSize
|
||||
|
||||
.PHONY: test-router-info-all \
|
||||
test-router-info-published \
|
||||
test-router-info-addresses \
|
||||
test-router-info-identity \
|
||||
test-router-info-misc
|
27
doc/tests/string.mk
Normal file
27
doc/tests/string.mk
Normal file
@ -0,0 +1,27 @@
|
||||
test-string-all: test-string-length test-string-data test-string-conversion test-string-read
|
||||
|
||||
test-string-length:
|
||||
$(GO) test -v ./lib/common/data -run TestStringReportsCorrectLength
|
||||
$(GO) test -v ./lib/common/data -run TestI2PStringReportsLengthZeroError
|
||||
$(GO) test -v ./lib/common/data -run TestI2PStringReportsExtraDataError
|
||||
$(GO) test -v ./lib/common/data -run TestI2PStringDataReportsLengthZeroError
|
||||
|
||||
test-string-data:
|
||||
$(GO) test -v ./lib/common/data -run TestI2PStringDataReportsExtraDataError
|
||||
$(GO) test -v ./lib/common/data -run TestI2PStringDataEmptyWhenZeroLength
|
||||
$(GO) test -v ./lib/common/data -run TestI2PStringDataErrorWhenNonZeroLengthOnly
|
||||
|
||||
test-string-conversion:
|
||||
$(GO) test -v ./lib/common/data -run TestToI2PI2PStringFormatsCorrectly
|
||||
$(GO) test -v ./lib/common/data -run TestToI2PStringReportsOverflows
|
||||
|
||||
test-string-read:
|
||||
$(GO) test -v ./lib/common/data -run TestReadStringReadsLength
|
||||
$(GO) test -v ./lib/common/data -run TestReadI2PStringErrWhenEmptySlice
|
||||
$(GO) test -v ./lib/common/data -run TestReadI2PStringErrWhenDataTooShort
|
||||
|
||||
.PHONY: test-string-all \
|
||||
test-string-length \
|
||||
test-string-data \
|
||||
test-string-conversion \
|
||||
test-string-read
|
11
doc/tests/su3.mk
Normal file
11
doc/tests/su3.mk
Normal file
@ -0,0 +1,11 @@
|
||||
test-su3-all: test-su3-read test-su3-signature
|
||||
|
||||
test-su3-read:
|
||||
$(GO) test -v ./lib/su3 -run TestRead
|
||||
|
||||
test-su3-signature:
|
||||
$(GO) test -v ./lib/su3 -run TestReadSignatureFirst
|
||||
|
||||
.PHONY: test-su3-all \
|
||||
test-su3-read \
|
||||
test-su3-signature
|
22
doc/tests/tunnel.mk
Normal file
22
doc/tests/tunnel.mk
Normal file
@ -0,0 +1,22 @@
|
||||
test-tunnel-all: test-tunnel-delivery-instructions test-tunnel-message
|
||||
|
||||
# Tests from delivery_test.go
|
||||
test-tunnel-delivery-instructions:
|
||||
$(GO) test -v ./lib/tunnel -run TestReadDeliveryInstructions
|
||||
|
||||
# Tests from message_test.go
|
||||
test-tunnel-message: test-tunnel-message-padding test-tunnel-message-fragments
|
||||
|
||||
test-tunnel-message-padding:
|
||||
$(GO) test -v ./lib/tunnel -run TestDeliveryInstructionDataWithNoPadding
|
||||
$(GO) test -v ./lib/tunnel -run TestDeliveryInstructionDataWithSomePadding
|
||||
$(GO) test -v ./lib/tunnel -run TestDeliveryInstructionDataWithOnlyPadding
|
||||
|
||||
test-tunnel-message-fragments:
|
||||
$(GO) test -v ./lib/tunnel -run TestDeliveryInstructionsWithFragments
|
||||
|
||||
.PHONY: test-tunnel-all \
|
||||
test-tunnel-delivery-instructions \
|
||||
test-tunnel-message \
|
||||
test-tunnel-message-padding \
|
||||
test-tunnel-message-fragments
|
42
go.mod
42
go.mod
@ -1,9 +1,43 @@
|
||||
module github.com/go-i2p/go-i2p
|
||||
|
||||
go 1.16
|
||||
go 1.23.1
|
||||
|
||||
require (
|
||||
github.com/sirupsen/logrus v1.8.1
|
||||
github.com/stretchr/testify v1.7.0
|
||||
golang.org/x/crypto v0.0.0-20210415154028-4f45737414dc
|
||||
github.com/beevik/ntp v1.4.3
|
||||
github.com/emirpasic/gods v1.18.1
|
||||
github.com/eyedeekay/go-unzip v0.0.0-20240201194209-560d8225b50e
|
||||
github.com/flynn/noise v1.1.0
|
||||
github.com/sirupsen/logrus v1.9.3
|
||||
github.com/spf13/cobra v1.8.1
|
||||
github.com/spf13/viper v1.19.0
|
||||
github.com/stretchr/testify v1.9.0
|
||||
go.step.sm/crypto v0.53.0
|
||||
golang.org/x/crypto v0.27.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
require (
|
||||
filippo.io/edwards25519 v1.1.0 // indirect
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
|
||||
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||
github.com/magiconair/properties v1.8.7 // indirect
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
|
||||
github.com/sagikazarmark/locafero v0.4.0 // indirect
|
||||
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
|
||||
github.com/sourcegraph/conc v0.3.0 // indirect
|
||||
github.com/spf13/afero v1.11.0 // indirect
|
||||
github.com/spf13/cast v1.7.0 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/subosito/gotenv v1.6.0 // indirect
|
||||
go.uber.org/atomic v1.9.0 // indirect
|
||||
go.uber.org/multierr v1.9.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
|
||||
golang.org/x/net v0.29.0 // indirect
|
||||
golang.org/x/sys v0.25.0 // indirect
|
||||
golang.org/x/text v0.18.0 // indirect
|
||||
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||
)
|
||||
|
106
go.sum
106
go.sum
@ -1,24 +1,106 @@
|
||||
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||
github.com/beevik/ntp v1.4.3 h1:PlbTvE5NNy4QHmA4Mg57n7mcFTmr1W1j3gcK7L1lqho=
|
||||
github.com/beevik/ntp v1.4.3/go.mod h1:Unr8Zg+2dRn7d8bHFuehIMSvvUYssHMxW3Q5Nx4RW5Q=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
|
||||
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
|
||||
github.com/eyedeekay/go-unzip v0.0.0-20240201194209-560d8225b50e h1:NMjWYVkgcQHGOy0/VxU0TU6smrcoxzj9hwDesx2sB0w=
|
||||
github.com/eyedeekay/go-unzip v0.0.0-20240201194209-560d8225b50e/go.mod h1:fKfFM3BsOOyjtZmEty7FsGzGabXo8Eb/dHjyIhTtxsE=
|
||||
github.com/flynn/noise v1.1.0 h1:KjPQoQCEFdZDiP03phOvGi11+SVVhBG2wOWAorLsstg=
|
||||
github.com/flynn/noise v1.1.0/go.mod h1:xbMo+0i6+IGbYdJhF31t2eR1BIU0CYc12+BNAKwUTag=
|
||||
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
|
||||
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
|
||||
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
||||
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
|
||||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
|
||||
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
|
||||
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
|
||||
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
|
||||
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
|
||||
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
|
||||
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
|
||||
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
|
||||
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
|
||||
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
|
||||
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
|
||||
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
|
||||
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
|
||||
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
|
||||
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
|
||||
github.com/spf13/cast v1.7.0 h1:ntdiHjuueXFgm5nzDRdOS4yfT43P5Fnud6DH50rz/7w=
|
||||
github.com/spf13/cast v1.7.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
|
||||
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
|
||||
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
|
||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI=
|
||||
github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
golang.org/x/crypto v0.0.0-20210415154028-4f45737414dc h1:+q90ECDSAQirdykUN6sPEiBXBsp8Csjcca8Oy7bgLTA=
|
||||
golang.org/x/crypto v0.0.0-20210415154028-4f45737414dc/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
|
||||
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
|
||||
go.step.sm/crypto v0.53.0 h1:+1as1ogzuCzx15/468M4mEC5juogI5a0Fzbsyh1CuYY=
|
||||
go.step.sm/crypto v0.53.0/go.mod h1:AqLU78RqNUHepLzyOWZuNN/2++Lu7dZENdO9UzWOGSk=
|
||||
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
|
||||
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
|
||||
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
|
||||
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
|
||||
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
|
||||
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
|
||||
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
|
||||
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
|
||||
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
|
||||
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
@ -1,6 +1,6 @@
|
||||
package bootstrap
|
||||
|
||||
import "github.com/go-i2p/go-i2p/lib/common"
|
||||
import "github.com/go-i2p/go-i2p/lib/common/router_info"
|
||||
|
||||
// interface defining a way to bootstrap into the i2p network
|
||||
type Bootstrap interface {
|
||||
@ -9,5 +9,5 @@ type Bootstrap interface {
|
||||
// if n is 0 then try obtaining as many router infos as possible
|
||||
// returns nil and error if we cannot fetch ANY router infos
|
||||
// returns a channel that yields 1 slice of router infos containing n or fewer router infos, caller must close channel after use
|
||||
GetPeers(n int) (chan []common.RouterInfo, error)
|
||||
GetPeers(n int) (chan []router_info.RouterInfo, error)
|
||||
}
|
||||
|
@ -1,4 +1,2 @@
|
||||
//
|
||||
// provides generic interfaces for initial bootstrap into network and network reseeding
|
||||
//
|
||||
package bootstrap
|
||||
|
23
lib/bootstrap/doc.md
Normal file
23
lib/bootstrap/doc.md
Normal file
@ -0,0 +1,23 @@
|
||||
# bootstrap
|
||||
--
|
||||
import "github.com/go-i2p/go-i2p/lib/bootstrap"
|
||||
|
||||
provides generic interfaces for initial bootstrap into network and network
|
||||
### reseeding
|
||||
|
||||
## Usage
|
||||
|
||||
#### type Bootstrap
|
||||
|
||||
```go
|
||||
type Bootstrap interface {
|
||||
// get more peers for bootstrap
|
||||
// try obtaining at most n router infos
|
||||
// if n is 0 then try obtaining as many router infos as possible
|
||||
// returns nil and error if we cannot fetch ANY router infos
|
||||
// returns a channel that yields 1 slice of router infos containing n or fewer router infos, caller must close channel after use
|
||||
GetPeers(n int) (chan []router_info.RouterInfo, error)
|
||||
}
|
||||
```
|
||||
|
||||
interface defining a way to bootstrap into the i2p network
|
@ -1,18 +1,23 @@
|
||||
//
|
||||
// base32 encoding using I2P's alphabet
|
||||
//
|
||||
// Package base32 implmenets utilities for encoding and decoding text using I2P's alphabet
|
||||
package base32
|
||||
|
||||
import (
|
||||
b32 "encoding/base32"
|
||||
)
|
||||
|
||||
var I2PEncoding *b32.Encoding = b32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567")
|
||||
// I2PEncodeAlphabet is the base32 encoding used throughout I2P.
|
||||
// RFC 3548 using lowercase characters.
|
||||
const I2PEncodeAlphabet = "abcdefghijklmnopqrstuvwxyz234567"
|
||||
|
||||
//
|
||||
// Return a go string of the I2P base32
|
||||
// encoding of the provided byte slice
|
||||
//
|
||||
// I2PEncoding is the standard base32 encoding used through I2P.
|
||||
var I2PEncoding *b32.Encoding = b32.NewEncoding(I2PEncodeAlphabet)
|
||||
|
||||
// EncodeToString encodes []byte to a base32 string using I2PEncoding
|
||||
func EncodeToString(data []byte) string {
|
||||
return I2PEncoding.EncodeToString(data)
|
||||
}
|
||||
|
||||
// DecodeString decodes base64 string to []byte I2PEncoding
|
||||
func DecodeString(data string) ([]byte, error) {
|
||||
return I2PEncoding.DecodeString(data)
|
||||
}
|
||||
|
20
lib/common/base32/base32_test.go
Normal file
20
lib/common/base32/base32_test.go
Normal file
@ -0,0 +1,20 @@
|
||||
package base32
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestEncodeDecodeNotMangled(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
// Random pangram
|
||||
testInput := []byte("How vexingly quick daft zebras jump!")
|
||||
|
||||
encodedString := EncodeToString(testInput)
|
||||
decodedString, err := DecodeString(encodedString)
|
||||
assert.Nil(err)
|
||||
|
||||
assert.ElementsMatch(testInput, decodedString)
|
||||
}
|
33
lib/common/base32/doc.md
Normal file
33
lib/common/base32/doc.md
Normal file
@ -0,0 +1,33 @@
|
||||
# base32
|
||||
--
|
||||
import "github.com/go-i2p/go-i2p/lib/common/base32"
|
||||
|
||||
Package base32 implmenets utilities for encoding and decoding text using I2P's
|
||||
### alphabet
|
||||
|
||||
## Usage
|
||||
|
||||
```go
|
||||
const I2PEncodeAlphabet = "abcdefghijklmnopqrstuvwxyz234567"
|
||||
```
|
||||
I2PEncodeAlphabet is the base32 encoding used throughout I2P. RFC 3548 using
|
||||
lowercase characters.
|
||||
|
||||
```go
|
||||
var I2PEncoding *b32.Encoding = b32.NewEncoding(I2PEncodeAlphabet)
|
||||
```
|
||||
I2PEncoding is the standard base32 encoding used through I2P.
|
||||
|
||||
#### func DecodeString
|
||||
|
||||
```go
|
||||
func DecodeString(data string) ([]byte, error)
|
||||
```
|
||||
DecodeString decodes base64 string to []byte I2PEncoding
|
||||
|
||||
#### func EncodeToString
|
||||
|
||||
```go
|
||||
func EncodeToString(data []byte) string
|
||||
```
|
||||
EncodeToString encodes []byte to a base32 string using I2PEncoding
|
@ -1,30 +1,23 @@
|
||||
//
|
||||
// base64 encoding using I2P's alphabet
|
||||
//
|
||||
// Package base64 implmenets utilities for encoding and decoding text using I2P's alphabet
|
||||
package base64
|
||||
|
||||
import (
|
||||
b64 "encoding/base64"
|
||||
)
|
||||
|
||||
// i2p base64 alphabet
|
||||
const Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-~"
|
||||
// I2PEncodeAlphabet is the base64 encoding used throughout I2P.
|
||||
// RFC 4648 with "/"" replaced with "~", and "+" replaced with "-".
|
||||
const I2PEncodeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-~"
|
||||
|
||||
// i2p base64 encoding
|
||||
var I2PEncoding *b64.Encoding = b64.NewEncoding(Alphabet)
|
||||
// I2PEncoding is the standard base64 encoding used through I2P.
|
||||
var I2PEncoding *b64.Encoding = b64.NewEncoding(I2PEncodeAlphabet)
|
||||
|
||||
//
|
||||
// Return a go string of the I2P base64
|
||||
// encoding of the provided byte slice
|
||||
//
|
||||
// I2PEncoding is the standard base64 encoding used through I2P.
|
||||
func EncodeToString(data []byte) string {
|
||||
return I2PEncoding.EncodeToString(data)
|
||||
}
|
||||
|
||||
//
|
||||
// decode string using i2p base64 encoding
|
||||
// returns error if data is malfromed
|
||||
//
|
||||
func DecodeFromString(str string) (d []byte, err error) {
|
||||
// DecodeString decodes base64 string to []byte I2PEncoding
|
||||
func DecodeString(str string) ([]byte, error) {
|
||||
return I2PEncoding.DecodeString(str)
|
||||
}
|
||||
|
20
lib/common/base64/base64_test.go
Normal file
20
lib/common/base64/base64_test.go
Normal file
@ -0,0 +1,20 @@
|
||||
package base64
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestEncodeDecodeNotMangled(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
// Random pangram
|
||||
testInput := []byte("Glib jocks quiz nymph to vex dwarf.")
|
||||
|
||||
encodedString := EncodeToString(testInput)
|
||||
decodedString, err := DecodeString(encodedString)
|
||||
assert.Nil(err)
|
||||
|
||||
assert.ElementsMatch(testInput, decodedString)
|
||||
}
|
33
lib/common/base64/doc.md
Normal file
33
lib/common/base64/doc.md
Normal file
@ -0,0 +1,33 @@
|
||||
# base64
|
||||
--
|
||||
import "github.com/go-i2p/go-i2p/lib/common/base64"
|
||||
|
||||
Package base64 implmenets utilities for encoding and decoding text using I2P's
|
||||
### alphabet
|
||||
|
||||
## Usage
|
||||
|
||||
```go
|
||||
const I2PEncodeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-~"
|
||||
```
|
||||
I2PEncodeAlphabet is the base64 encoding used throughout I2P. RFC 4648 with "/""
|
||||
replaced with "~", and "+" replaced with "-".
|
||||
|
||||
```go
|
||||
var I2PEncoding *b64.Encoding = b64.NewEncoding(I2PEncodeAlphabet)
|
||||
```
|
||||
I2PEncoding is the standard base64 encoding used through I2P.
|
||||
|
||||
#### func DecodeString
|
||||
|
||||
```go
|
||||
func DecodeString(str string) ([]byte, error)
|
||||
```
|
||||
DecodeString decodes base64 string to []byte I2PEncoding
|
||||
|
||||
#### func EncodeToString
|
||||
|
||||
```go
|
||||
func EncodeToString(data []byte) string
|
||||
```
|
||||
I2PEncoding is the standard base64 encoding used through I2P.
|
@ -1,139 +0,0 @@
|
||||
package common
|
||||
|
||||
/*
|
||||
I2P Certificate
|
||||
https://geti2p.net/spec/common-structures#certificate
|
||||
Accurate for version 0.9.24
|
||||
|
||||
+----+----+----+----+----+-//
|
||||
|type| length | payload
|
||||
+----+----+----+----+----+-//
|
||||
|
||||
type :: Integer
|
||||
length -> 1 byte
|
||||
|
||||
case 0 -> NULL
|
||||
case 1 -> HASHCASH
|
||||
case 2 -> HIDDEN
|
||||
case 3 -> SIGNED
|
||||
case 4 -> MULTIPLE
|
||||
case 5 -> KEY
|
||||
|
||||
length :: Integer
|
||||
length -> 2 bytes
|
||||
|
||||
payload :: data
|
||||
length -> $length bytes
|
||||
*/
|
||||
|
||||
import (
|
||||
"errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Certificate Types
|
||||
const (
|
||||
CERT_NULL = iota
|
||||
CERT_HASHCASH
|
||||
CERT_HIDDEN
|
||||
CERT_SIGNED
|
||||
CERT_MULTIPLE
|
||||
CERT_KEY
|
||||
)
|
||||
|
||||
// Minimum size of a valid Certificate
|
||||
const (
|
||||
CERT_MIN_SIZE = 3
|
||||
)
|
||||
|
||||
type Certificate []byte
|
||||
|
||||
//
|
||||
// Return the Certificate Type specified in the first byte of the Certificate,
|
||||
// and an error if the certificate is shorter than the minimum certificate size.
|
||||
//
|
||||
func (certificate Certificate) Type() (cert_type int, err error) {
|
||||
cert_len := len(certificate)
|
||||
if cert_len < CERT_MIN_SIZE {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(Certificate) Type",
|
||||
"certificate_bytes_length": cert_len,
|
||||
"reason": "too short (len < CERT_MIN_SIZE)",
|
||||
}).Error("invalid certificate")
|
||||
err = errors.New("error parsing certificate length: certificate is too short")
|
||||
return
|
||||
}
|
||||
cert_type = Integer([]byte{certificate[0]})
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Look up the length of the Certificate, reporting errors if the certificate is
|
||||
// shorter than the minimum certificate size or if the reported length doesn't
|
||||
// match the provided data.
|
||||
//
|
||||
func (certificate Certificate) Length() (length int, err error) {
|
||||
cert_len := len(certificate)
|
||||
_, err = certificate.Type()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
length = Integer(certificate[1:CERT_MIN_SIZE])
|
||||
inferred_len := length + CERT_MIN_SIZE
|
||||
if inferred_len > cert_len {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(Certificate) Length",
|
||||
"certificate_bytes_length": cert_len,
|
||||
"certificate_length_field": length,
|
||||
"expected_bytes_length": inferred_len,
|
||||
"reason": "data shorter than specified",
|
||||
}).Warn("certificate format warning")
|
||||
err = errors.New("certificate parsing warning: certificate data is shorter than specified by length")
|
||||
} else if cert_len > inferred_len {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(Certificate) Length",
|
||||
"certificate_bytes_length": cert_len,
|
||||
"certificate_length_field": length,
|
||||
"expected_bytes_length": inferred_len,
|
||||
"reason": "data longer than expected",
|
||||
}).Warn("certificate format warning")
|
||||
err = errors.New("certificate parsing warning: certificate contains data beyond length")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Return the Certificate data and any errors encountered parsing the Certificate.
|
||||
//
|
||||
func (certificate Certificate) Data() (data []byte, err error) {
|
||||
length, err := certificate.Length()
|
||||
if err != nil {
|
||||
switch err.Error() {
|
||||
case "error parsing certificate length: certificate is too short":
|
||||
return
|
||||
case "certificate parsing warning: certificate data is shorter than specified by length":
|
||||
data = certificate[CERT_MIN_SIZE:]
|
||||
return
|
||||
case "certificate parsing warning: certificate contains data beyond length":
|
||||
data = certificate[CERT_MIN_SIZE : length+CERT_MIN_SIZE]
|
||||
return
|
||||
}
|
||||
}
|
||||
data = certificate[CERT_MIN_SIZE:]
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Read a Certificate from a slice of bytes, returning any extra data on the end of the slice
|
||||
// and any errors if a valid Certificate could not be read.
|
||||
//
|
||||
func ReadCertificate(data []byte) (certificate Certificate, remainder []byte, err error) {
|
||||
certificate = Certificate(data)
|
||||
length, err := certificate.Length()
|
||||
if err != nil && err.Error() == "certificate parsing warning: certificate contains data beyond length" {
|
||||
certificate = Certificate(data[:length+CERT_MIN_SIZE])
|
||||
remainder = data[length+CERT_MIN_SIZE:]
|
||||
err = nil
|
||||
}
|
||||
return
|
||||
}
|
295
lib/common/certificate/certificate.go
Normal file
295
lib/common/certificate/certificate.go
Normal file
@ -0,0 +1,295 @@
|
||||
// Package certificate implements the certificate common-structure of I2P.
|
||||
|
||||
package certificate
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
// log "github.com/sirupsen/logrus"
|
||||
"github.com/go-i2p/go-i2p/lib/util/logger"
|
||||
|
||||
. "github.com/go-i2p/go-i2p/lib/common/data"
|
||||
)
|
||||
|
||||
var log = logger.GetGoI2PLogger()
|
||||
|
||||
// Certificate Types
|
||||
const (
|
||||
CERT_NULL = iota
|
||||
CERT_HASHCASH
|
||||
CERT_HIDDEN
|
||||
CERT_SIGNED
|
||||
CERT_MULTIPLE
|
||||
CERT_KEY
|
||||
)
|
||||
|
||||
// CERT_MIN_SIZE is the minimum size of a valid Certificate in []byte
|
||||
// 1 byte for type
|
||||
// 2 bytes for payload length
|
||||
const CERT_MIN_SIZE = 3
|
||||
|
||||
/*
|
||||
[I2P Certificate]
|
||||
Accurate for version 0.9.49
|
||||
|
||||
Description
|
||||
A certifificate is a container for various receipts of proof of works used throughout the I2P network.
|
||||
|
||||
Contents
|
||||
1 byte Integer specifying certificate type, followed by a 2 byte Integer specifying the size of the certificate playload, then that many bytes.
|
||||
|
||||
+----+----+----+----+----+-//
|
||||
|type| length | payload
|
||||
+----+----+----+----+----+-//
|
||||
|
||||
type :: Integer
|
||||
length -> 1 byte
|
||||
|
||||
case 0 -> NULL
|
||||
case 1 -> HASHCASH
|
||||
case 2 -> HIDDEN
|
||||
case 3 -> SIGNED
|
||||
case 4 -> MULTIPLE
|
||||
case 5 -> KEY
|
||||
|
||||
length :: Integer
|
||||
length -> 2 bytes
|
||||
|
||||
payload :: data
|
||||
length -> $length bytes
|
||||
*/
|
||||
|
||||
// Certificate is the representation of an I2P Certificate.
|
||||
//
|
||||
// https://geti2p.net/spec/common-structures#certificate
|
||||
type Certificate struct {
|
||||
kind Integer
|
||||
len Integer
|
||||
payload []byte
|
||||
}
|
||||
|
||||
// RawBytes returns the entire certificate in []byte form, includes excess payload data.
|
||||
func (c *Certificate) RawBytes() []byte {
|
||||
bytes := c.kind.Bytes()
|
||||
bytes = append(bytes, c.len.Bytes()...)
|
||||
bytes = append(bytes, c.payload...)
|
||||
log.WithFields(logrus.Fields{
|
||||
"raw_bytes_length": len(bytes),
|
||||
}).Debug("Generated raw bytes for certificate")
|
||||
return bytes
|
||||
}
|
||||
|
||||
// ExcessBytes returns the excess bytes in a certificate found after the specified payload length.
|
||||
func (c *Certificate) ExcessBytes() []byte {
|
||||
if len(c.payload) >= c.len.Int() {
|
||||
excess := c.payload[c.len.Int():]
|
||||
log.WithFields(logrus.Fields{
|
||||
"excess_bytes_length": len(excess),
|
||||
}).Debug("Found excess bytes in certificate")
|
||||
return excess
|
||||
}
|
||||
log.Debug("No excess bytes found in certificate")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Data returns the payload of a Certificate, payload is trimmed to the specified length
|
||||
func (c *Certificate) Data() []byte {
|
||||
length := c.Length()
|
||||
if length == 0 {
|
||||
return []byte{}
|
||||
}
|
||||
|
||||
if length > len(c.payload) {
|
||||
log.WithFields(logrus.Fields{
|
||||
"specified_length": length,
|
||||
"actual_length": len(c.payload),
|
||||
}).Warn("Certificate payload shorter than specified length")
|
||||
return c.payload
|
||||
}
|
||||
|
||||
return c.payload[:length]
|
||||
}
|
||||
|
||||
// Bytes returns the entire certificate in []byte form
|
||||
func (c *Certificate) Bytes() []byte {
|
||||
if c.kind.Int() == CERT_NULL {
|
||||
return []byte{0, 0, 0}
|
||||
}
|
||||
|
||||
bytes := c.kind.Bytes()
|
||||
bytes = append(bytes, c.len.Bytes()...)
|
||||
bytes = append(bytes, c.Data()...)
|
||||
|
||||
log.WithFields(logrus.Fields{
|
||||
"bytes_length": len(bytes),
|
||||
"cert_type": c.kind.Int(),
|
||||
}).Debug("Generated bytes for certificate")
|
||||
|
||||
return bytes
|
||||
}
|
||||
|
||||
func (c *Certificate) length() (cert_len int) {
|
||||
cert_len = len(c.Bytes())
|
||||
return
|
||||
}
|
||||
|
||||
// Type returns the Certificate type specified in the first byte of the Certificate,
|
||||
func (c *Certificate) Type() (cert_type int) {
|
||||
cert_type = c.kind.Int()
|
||||
log.WithFields(logrus.Fields{
|
||||
"cert_type": cert_type,
|
||||
}).Debug("Retrieved certificate type")
|
||||
return
|
||||
}
|
||||
|
||||
// Length returns the payload length of a Certificate.
|
||||
func (c *Certificate) Length() (length int) {
|
||||
length = c.len.Int()
|
||||
log.WithFields(logrus.Fields{
|
||||
"length": length,
|
||||
}).Debug("Retrieved certificate length")
|
||||
return
|
||||
}
|
||||
|
||||
// readCertificate creates a new Certficiate from []byte
|
||||
// returns err if the certificate is too short or if the payload doesn't match specified length.
|
||||
func readCertificate(data []byte) (certificate Certificate, err error) {
|
||||
certificate = Certificate{}
|
||||
switch len(data) {
|
||||
case 0:
|
||||
certificate.kind = Integer([]byte{0})
|
||||
certificate.len = Integer([]byte{0})
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "(Certificate) NewCertificate",
|
||||
"certificate_bytes_length": len(data),
|
||||
"reason": "too short (len < CERT_MIN_SIZE)",
|
||||
}).Error("invalid certificate, empty")
|
||||
err = fmt.Errorf("error parsing certificate: certificate is empty")
|
||||
return
|
||||
case 1, 2:
|
||||
certificate.kind = Integer(data[0:1])
|
||||
certificate.len = Integer([]byte{0})
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "(Certificate) NewCertificate",
|
||||
"certificate_bytes_length": len(data),
|
||||
"reason": "too short (len < CERT_MIN_SIZE)",
|
||||
}).Error("invalid certificate, too short")
|
||||
err = fmt.Errorf("error parsing certificate: certificate is too short")
|
||||
return
|
||||
default:
|
||||
certificate.kind = Integer(data[0:1])
|
||||
certificate.len = Integer(data[1:3])
|
||||
|
||||
// Validate certificate type
|
||||
if certificate.kind.Int() < CERT_NULL || certificate.kind.Int() > CERT_KEY {
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "(Certificate) NewCertificate",
|
||||
"type": certificate.kind.Int(),
|
||||
}).Error("invalid certificate type")
|
||||
err = fmt.Errorf("error parsing certificate: invalid type: %d", certificate.kind.Int())
|
||||
return
|
||||
}
|
||||
|
||||
// Handle NULL certificates
|
||||
if certificate.kind.Int() == CERT_NULL && certificate.len.Int() != 0 {
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "(Certificate) NewCertificate",
|
||||
"length": certificate.len.Int(),
|
||||
}).Error("NULL certificate must have zero length")
|
||||
err = fmt.Errorf("error parsing certificate: NULL certificate must have zero length")
|
||||
return
|
||||
}
|
||||
|
||||
// Validate payload length
|
||||
expectedLength := certificate.len.Int()
|
||||
actualLength := len(data) - CERT_MIN_SIZE
|
||||
|
||||
if expectedLength > actualLength {
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "(Certificate) NewCertificate",
|
||||
"expected_length": expectedLength,
|
||||
"actual_length": actualLength,
|
||||
}).Error("certificate data shorter than specified length")
|
||||
err = fmt.Errorf("error parsing certificate: data shorter than specified length")
|
||||
return
|
||||
}
|
||||
|
||||
certificate.payload = data[CERT_MIN_SIZE:]
|
||||
}
|
||||
|
||||
log.WithFields(logrus.Fields{
|
||||
"type": certificate.kind.Int(),
|
||||
"length": certificate.len.Int(),
|
||||
}).Debug("Successfully parsed certificate")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// ReadCertificate creates a Certificate from []byte and returns any ExcessBytes at the end of the input.
|
||||
// returns err if the certificate could not be read.
|
||||
func ReadCertificate(data []byte) (certificate Certificate, remainder []byte, err error) {
|
||||
certificate, err = readCertificate(data)
|
||||
if err != nil && err.Error() == "certificate parsing warning: certificate data is longer than specified by length" {
|
||||
log.Warn("Certificate data longer than specified length")
|
||||
err = nil
|
||||
}
|
||||
remainder = certificate.ExcessBytes()
|
||||
log.WithFields(logrus.Fields{
|
||||
"remainder_length": len(remainder),
|
||||
}).Debug("Read certificate and extracted remainder")
|
||||
return
|
||||
}
|
||||
|
||||
// NewCertificate creates a new Certificate with default NULL type
|
||||
func NewCertificate() *Certificate {
|
||||
return &Certificate{
|
||||
kind: Integer([]byte{CERT_NULL}),
|
||||
len: Integer([]byte{0}),
|
||||
payload: make([]byte, 0),
|
||||
}
|
||||
}
|
||||
|
||||
// NewCertificateWithType creates a new Certificate with specified type and payload
|
||||
func NewCertificateWithType(certType uint8, payload []byte) (*Certificate, error) {
|
||||
// Validate certificate type
|
||||
switch certType {
|
||||
case CERT_NULL, CERT_HASHCASH, CERT_HIDDEN, CERT_SIGNED, CERT_MULTIPLE, CERT_KEY:
|
||||
// Valid type
|
||||
default:
|
||||
return nil, fmt.Errorf("invalid certificate type: %d", certType)
|
||||
}
|
||||
|
||||
// For NULL certificates, payload should be empty
|
||||
if certType == CERT_NULL && len(payload) > 0 {
|
||||
return nil, errors.New("NULL certificates must have empty payload")
|
||||
}
|
||||
length, _ := NewIntegerFromInt(len(payload), 2)
|
||||
|
||||
cert := &Certificate{
|
||||
kind: Integer([]byte{certType}),
|
||||
len: *length,
|
||||
payload: make([]byte, len(payload)),
|
||||
}
|
||||
|
||||
// Copy payload if present
|
||||
if len(payload) > 0 {
|
||||
copy(cert.payload, payload)
|
||||
}
|
||||
|
||||
return cert, nil
|
||||
}
|
||||
|
||||
func GetSignatureTypeFromCertificate(cert Certificate) (int, error) {
|
||||
if cert.Type() != CERT_KEY {
|
||||
return 0, fmt.Errorf("unexpected certificate type: %d", cert.Type)
|
||||
}
|
||||
if len(cert.payload) < 2 {
|
||||
return 0, fmt.Errorf("certificate payload too short to contain signature type")
|
||||
}
|
||||
sigType := int(binary.BigEndian.Uint16(cert.payload[0:2]))
|
||||
return sigType, nil
|
||||
}
|
@ -1,16 +1,17 @@
|
||||
package common
|
||||
package certificate
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCertificateTypeIsFirstByte(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
bytes := []byte{0x03, 0x00, 0x00}
|
||||
certificate := Certificate(bytes)
|
||||
cert_type, err := certificate.Type()
|
||||
certificate, err := readCertificate(bytes)
|
||||
cert_type := certificate.Type()
|
||||
|
||||
assert.Equal(cert_type, 3, "certificate.Type() should be the first bytes in a certificate")
|
||||
assert.Nil(err)
|
||||
@ -20,8 +21,8 @@ func TestCertificateLengthCorrect(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
bytes := []byte{0x03, 0x00, 0x02, 0xff, 0xff}
|
||||
certificate := Certificate(bytes)
|
||||
cert_len, err := certificate.Length()
|
||||
certificate, err := readCertificate(bytes)
|
||||
cert_len := certificate.Length()
|
||||
|
||||
assert.Equal(cert_len, 2, "certificate.Length() should return integer from second two bytes")
|
||||
assert.Nil(err)
|
||||
@ -31,12 +32,12 @@ func TestCertificateLengthErrWhenTooShort(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
bytes := []byte{0x03, 0x01}
|
||||
certificate := Certificate(bytes)
|
||||
cert_len, err := certificate.Length()
|
||||
certificate, _, err := ReadCertificate(bytes)
|
||||
cert_len := certificate.Length()
|
||||
|
||||
assert.Equal(cert_len, 0, "certificate.Length() did not return zero length for missing length data")
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("error parsing certificate length: certificate is too short", err.Error(), "correct error message should be returned")
|
||||
assert.Equal("error parsing certificate: certificate is too short", err.Error(), "correct error message should be returned")
|
||||
}
|
||||
}
|
||||
|
||||
@ -44,8 +45,8 @@ func TestCertificateLengthErrWhenDataTooShort(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
bytes := []byte{0x03, 0x00, 0x02, 0xff}
|
||||
certificate := Certificate(bytes)
|
||||
cert_len, err := certificate.Length()
|
||||
certificate, err := readCertificate(bytes)
|
||||
cert_len := certificate.Length()
|
||||
|
||||
assert.Equal(cert_len, 2, "certificate.Length() did not return indicated length when data was actually missing")
|
||||
if assert.NotNil(err) {
|
||||
@ -57,8 +58,8 @@ func TestCertificateDataWhenCorrectSize(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
bytes := []byte{0x03, 0x00, 0x01, 0xaa}
|
||||
certificate := Certificate(bytes)
|
||||
cert_data, err := certificate.Data()
|
||||
certificate, err := readCertificate(bytes)
|
||||
cert_data := certificate.Data()
|
||||
|
||||
assert.Nil(err, "certificate.Data() returned error with valid data")
|
||||
cert_len := len(cert_data)
|
||||
@ -70,13 +71,10 @@ func TestCertificateDataWhenTooLong(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
bytes := []byte{0x03, 0x00, 0x02, 0xff, 0xff, 0xaa, 0xaa}
|
||||
certificate := Certificate(bytes)
|
||||
cert_data, err := certificate.Data()
|
||||
certificate, _, _ := ReadCertificate(bytes)
|
||||
cert_data := certificate.Data()
|
||||
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("certificate parsing warning: certificate contains data beyond length", err.Error(), "correct error message should be returned")
|
||||
}
|
||||
cert_len := len(cert_data)
|
||||
cert_len := certificate.Length() // len(cert_data)
|
||||
assert.Equal(cert_len, 2, "certificate.Length() did not return indicated length when data was too long")
|
||||
if cert_data[0] != 0xff || cert_data[1] != 0xff {
|
||||
t.Fatal("certificate.Data() returned incorrect data when data was too long")
|
||||
@ -87,8 +85,8 @@ func TestCertificateDataWhenTooShort(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
bytes := []byte{0x03, 0x00, 0x02, 0xff}
|
||||
certificate := Certificate(bytes)
|
||||
cert_data, err := certificate.Data()
|
||||
certificate, err := readCertificate(bytes)
|
||||
cert_data := certificate.Data()
|
||||
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("certificate parsing warning: certificate data is shorter than specified by length", err.Error(), "correct error message should be returned")
|
||||
@ -104,7 +102,7 @@ func TestReadCertificateWithCorrectData(t *testing.T) {
|
||||
bytes := []byte{0x00, 0x00, 0x02, 0xff, 0xff}
|
||||
cert, remainder, err := ReadCertificate(bytes)
|
||||
|
||||
assert.Equal(len(cert), 5, "ReadCertificate() did not return correct amount of data for valid certificate")
|
||||
assert.Equal(cert.length(), 5, "ReadCertificate() did not return correct amount of data for valid certificate")
|
||||
assert.Equal(len(remainder), 0, "ReadCertificate() did not return a zero length remainder on a valid certificate")
|
||||
assert.Nil(err, "ReadCertificate() should not return an error with valid data")
|
||||
}
|
||||
@ -115,7 +113,7 @@ func TestReadCertificateWithDataTooShort(t *testing.T) {
|
||||
bytes := []byte{0x00, 0x00, 0x02, 0xff}
|
||||
cert, remainder, err := ReadCertificate(bytes)
|
||||
|
||||
assert.Equal(len(cert), 4, "ReadCertificate() did not return correct amount of data for certificate with missing data")
|
||||
assert.Equal(cert.length(), 4, "ReadCertificate() did not return correct amount of data for certificate with missing data")
|
||||
assert.Equal(len(remainder), 0, "ReadCertificate() did not return a zero length remainder on certificate with missing data")
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("certificate parsing warning: certificate data is shorter than specified by length", err.Error(), "correct error message should be returned")
|
||||
@ -128,9 +126,9 @@ func TestReadCertificateWithRemainder(t *testing.T) {
|
||||
bytes := []byte{0x00, 0x00, 0x02, 0xff, 0xff, 0x01}
|
||||
cert, remainder, err := ReadCertificate(bytes)
|
||||
|
||||
assert.Equal(len(cert), 5, "ReadCertificate() did not return correct amount of data for certificate with extra data")
|
||||
assert.Equal(cert.length(), 5, "ReadCertificate() did not return correct amount of data for certificate with extra data")
|
||||
assert.Equal(len(remainder), 1, "ReadCertificate() returned incorrect length remainder on certificate with extra data")
|
||||
assert.Equal(1, int(remainder[0]), "ReadCertificate() did not return correct remainder value")
|
||||
// assert.Equal(1, int(remainder[0]), "ReadCertificate() did not return correct remainder value")
|
||||
assert.Nil(err)
|
||||
}
|
||||
|
||||
@ -140,9 +138,9 @@ func TestReadCertificateWithInvalidLength(t *testing.T) {
|
||||
bytes := []byte{0x00, 0x00}
|
||||
cert, remainder, err := ReadCertificate(bytes)
|
||||
|
||||
assert.Equal(len(cert), 2, "ReadCertificate() should populate the certificate with the provided data even when invalid")
|
||||
assert.Equal(cert.length(), 2, "ReadCertificate() should populate the certificate with the provided data even when invalid")
|
||||
assert.Equal(len(remainder), 0, "ReadCertificate() returned non-zero length remainder on invalid certificate")
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("error parsing certificate length: certificate is too short", err.Error(), "correct error message should be returned")
|
||||
assert.Equal("error parsing certificate: certificate is too short", err.Error(), "correct error message should be returned")
|
||||
}
|
||||
}
|
98
lib/common/certificate/doc.md
Normal file
98
lib/common/certificate/doc.md
Normal file
@ -0,0 +1,98 @@
|
||||
# certificate
|
||||
--
|
||||
import "github.com/go-i2p/go-i2p/lib/common/certificate"
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
```go
|
||||
const (
|
||||
CERT_NULL = iota
|
||||
CERT_HASHCASH
|
||||
CERT_HIDDEN
|
||||
CERT_SIGNED
|
||||
CERT_MULTIPLE
|
||||
CERT_KEY
|
||||
)
|
||||
```
|
||||
Certificate Types
|
||||
|
||||
```go
|
||||
const CERT_MIN_SIZE = 3
|
||||
```
|
||||
CERT_MIN_SIZE is the minimum size of a valid Certificate in []byte 1 byte for
|
||||
type 2 bytes for payload length
|
||||
|
||||
#### type Certificate
|
||||
|
||||
```go
|
||||
type Certificate struct {
|
||||
}
|
||||
```
|
||||
|
||||
Certificate is the representation of an I2P Certificate.
|
||||
|
||||
https://geti2p.net/spec/common-structures#certificate
|
||||
|
||||
#### func NewCertificate
|
||||
|
||||
```go
|
||||
func NewCertificate(data []byte) (certificate Certificate, err error)
|
||||
```
|
||||
NewCertificate creates a new Certficiate from []byte returns err if the
|
||||
certificate is too short or if the payload doesn't match specified length.
|
||||
|
||||
#### func ReadCertificate
|
||||
|
||||
```go
|
||||
func ReadCertificate(data []byte) (certificate Certificate, remainder []byte, err error)
|
||||
```
|
||||
ReadCertificate creates a Certificate from []byte and returns any ExcessBytes at
|
||||
the end of the input. returns err if the certificate could not be read.
|
||||
|
||||
#### func (*Certificate) Bytes
|
||||
|
||||
```go
|
||||
func (c *Certificate) Bytes() []byte
|
||||
```
|
||||
Bytes returns the entire certificate in []byte form, trims payload to specified
|
||||
length.
|
||||
|
||||
#### func (*Certificate) Data
|
||||
|
||||
```go
|
||||
func (c *Certificate) Data() (data []byte)
|
||||
```
|
||||
Data returns the payload of a Certificate, payload is trimmed to the specified
|
||||
length.
|
||||
|
||||
#### func (*Certificate) ExcessBytes
|
||||
|
||||
```go
|
||||
func (c *Certificate) ExcessBytes() []byte
|
||||
```
|
||||
ExcessBytes returns the excess bytes in a certificate found after the specified
|
||||
payload length.
|
||||
|
||||
#### func (*Certificate) Length
|
||||
|
||||
```go
|
||||
func (c *Certificate) Length() (length int)
|
||||
```
|
||||
Length returns the payload length of a Certificate.
|
||||
|
||||
#### func (*Certificate) RawBytes
|
||||
|
||||
```go
|
||||
func (c *Certificate) RawBytes() []byte
|
||||
```
|
||||
RawBytes returns the entire certificate in []byte form, includes excess payload
|
||||
data.
|
||||
|
||||
#### func (*Certificate) Type
|
||||
|
||||
```go
|
||||
func (c *Certificate) Type() (cert_type int)
|
||||
```
|
||||
Type returns the Certificate type specified in the first byte of the
|
||||
Certificate,
|
88
lib/common/data/date.go
Normal file
88
lib/common/data/date.go
Normal file
@ -0,0 +1,88 @@
|
||||
// Package data implements common data structures used in higher level structures.
|
||||
package data
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/go-i2p/go-i2p/lib/util/logger"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var log = logger.GetGoI2PLogger()
|
||||
|
||||
// DATE_SIZE is the length in bytes of an I2P Date.
|
||||
const DATE_SIZE = 8
|
||||
|
||||
/*
|
||||
[I2P Date]
|
||||
Accurate for version 0.9.49
|
||||
|
||||
Description
|
||||
The number of milliseconds since midnight on Januyar 1, 1970 in the GMT timezone.
|
||||
If the number is 0, the date is undefined or null.
|
||||
|
||||
Contents
|
||||
8 byte Integer
|
||||
*/
|
||||
|
||||
// Date is the represenation of an I2P Date.
|
||||
//
|
||||
// https://geti2p.net/spec/common-structures#date
|
||||
type Date [8]byte
|
||||
|
||||
// Bytes returns the raw []byte content of a Date.
|
||||
func (i Date) Bytes() []byte {
|
||||
return i[:]
|
||||
}
|
||||
|
||||
// Int returns the Date as a Go integer.
|
||||
func (i Date) Int() int {
|
||||
val, _ := intFromBytes(i.Bytes())
|
||||
return val
|
||||
}
|
||||
|
||||
// Time takes the value stored in date as an 8 byte big-endian integer representing the
|
||||
// number of milliseconds since the beginning of unix time and converts it to a Go time.Time
|
||||
// struct.
|
||||
func (date Date) Time() (date_time time.Time) {
|
||||
seconds := Integer(date[:])
|
||||
date_time = time.Unix(0, int64(seconds.Int()*1000000))
|
||||
return
|
||||
}
|
||||
|
||||
// ReadDate creates a Date from []byte using the first DATE_SIZE bytes.
|
||||
// Any data after DATE_SIZE is returned as a remainder.
|
||||
func ReadDate(data []byte) (date Date, remainder []byte, err error) {
|
||||
if len(data) < 8 {
|
||||
log.WithFields(logrus.Fields{
|
||||
"data": data,
|
||||
}).Error("ReadDate: data is too short")
|
||||
err = errors.New("ReadDate: data is too short")
|
||||
return
|
||||
}
|
||||
copy(date[:], data[:8])
|
||||
remainder = data[8:]
|
||||
log.WithFields(logrus.Fields{
|
||||
"date_value": date.Int(),
|
||||
"remainder_length": len(remainder),
|
||||
}).Debug("Successfully read Date from data")
|
||||
return
|
||||
}
|
||||
|
||||
// NewDate creates a new Date from []byte using ReadDate.
|
||||
// Returns a pointer to Date unlike ReadDate.
|
||||
func NewDate(data []byte) (date *Date, remainder []byte, err error) {
|
||||
objdate, remainder, err := ReadDate(data)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to create new Date")
|
||||
return nil, remainder, err
|
||||
}
|
||||
|
||||
date = &objdate
|
||||
log.WithFields(logrus.Fields{
|
||||
"date_value": date.Int(),
|
||||
"remainder_length": len(remainder),
|
||||
}).Debug("Successfully created new Date")
|
||||
return
|
||||
}
|
@ -1,11 +1,12 @@
|
||||
package common
|
||||
package data
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestTimeFromMiliseconds(t *testing.T) {
|
||||
func TestTimeFromMilliseconds(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
next_day := Date{0x00, 0x00, 0x00, 0x00, 0x05, 0x26, 0x5c, 0x00}
|
297
lib/common/data/doc.md
Normal file
297
lib/common/data/doc.md
Normal file
@ -0,0 +1,297 @@
|
||||
# data
|
||||
--
|
||||
import "github.com/go-i2p/go-i2p/lib/common/data"
|
||||
|
||||
Package data implements common data structures used in higher level structures.
|
||||
|
||||
## Usage
|
||||
|
||||
```go
|
||||
const DATE_SIZE = 8
|
||||
```
|
||||
DATE_SIZE is the length in bytes of an I2P Date.
|
||||
|
||||
```go
|
||||
const MAX_INTEGER_SIZE = 8
|
||||
```
|
||||
MAX_INTEGER_SIZE is the maximum length of an I2P integer in bytes.
|
||||
|
||||
```go
|
||||
const STRING_MAX_SIZE = 255
|
||||
```
|
||||
STRING_MAX_SIZE is the maximum number of bytes that can be stored in an I2P
|
||||
string
|
||||
|
||||
#### func PrintErrors
|
||||
|
||||
```go
|
||||
func PrintErrors(errs []error)
|
||||
```
|
||||
PrintErrors prints a formatted list of errors to the console.
|
||||
|
||||
#### func WrapErrors
|
||||
|
||||
```go
|
||||
func WrapErrors(errs []error) error
|
||||
```
|
||||
WrapErrors compiles a slice of errors and returns them wrapped together as a
|
||||
single error.
|
||||
|
||||
#### type Date
|
||||
|
||||
```go
|
||||
type Date [8]byte
|
||||
```
|
||||
|
||||
Date is the represenation of an I2P Date.
|
||||
|
||||
https://geti2p.net/spec/common-structures#date
|
||||
|
||||
#### func NewDate
|
||||
|
||||
```go
|
||||
func NewDate(data []byte) (date *Date, remainder []byte, err error)
|
||||
```
|
||||
NewDate creates a new Date from []byte using ReadDate. Returns a pointer to Date
|
||||
unlike ReadDate.
|
||||
|
||||
#### func ReadDate
|
||||
|
||||
```go
|
||||
func ReadDate(data []byte) (date Date, remainder []byte, err error)
|
||||
```
|
||||
ReadDate creates a Date from []byte using the first DATE_SIZE bytes. Any data
|
||||
after DATE_SIZE is returned as a remainder.
|
||||
|
||||
#### func (Date) Bytes
|
||||
|
||||
```go
|
||||
func (i Date) Bytes() []byte
|
||||
```
|
||||
Bytes returns the raw []byte content of a Date.
|
||||
|
||||
#### func (Date) Int
|
||||
|
||||
```go
|
||||
func (i Date) Int() int
|
||||
```
|
||||
Int returns the Date as a Go integer.
|
||||
|
||||
#### func (Date) Time
|
||||
|
||||
```go
|
||||
func (date Date) Time() (date_time time.Time)
|
||||
```
|
||||
Time takes the value stored in date as an 8 byte big-endian integer representing
|
||||
the number of milliseconds since the beginning of unix time and converts it to a
|
||||
Go time.Time struct.
|
||||
|
||||
#### type Hash
|
||||
|
||||
```go
|
||||
type Hash [32]byte
|
||||
```
|
||||
|
||||
Hash is the represenation of an I2P Hash.
|
||||
|
||||
https://geti2p.net/spec/common-structures#hash
|
||||
|
||||
#### func HashData
|
||||
|
||||
```go
|
||||
func HashData(data []byte) (h Hash)
|
||||
```
|
||||
HashData returns the SHA256 sum of a []byte input as Hash.
|
||||
|
||||
#### func HashReader
|
||||
|
||||
```go
|
||||
func HashReader(r io.Reader) (h Hash, err error)
|
||||
```
|
||||
HashReader returns the SHA256 sum from all data read from an io.Reader. return
|
||||
error if one occurs while reading from reader
|
||||
|
||||
#### func (Hash) Bytes
|
||||
|
||||
```go
|
||||
func (h Hash) Bytes() [32]byte
|
||||
```
|
||||
|
||||
#### type I2PString
|
||||
|
||||
```go
|
||||
type I2PString []byte
|
||||
```
|
||||
|
||||
I2PString is the represenation of an I2P String.
|
||||
|
||||
https://geti2p.net/spec/common-structures#string
|
||||
|
||||
#### func ReadI2PString
|
||||
|
||||
```go
|
||||
func ReadI2PString(data []byte) (str I2PString, remainder []byte, err error)
|
||||
```
|
||||
ReadI2PString returns I2PString from a []byte. The remaining bytes after the
|
||||
specified length are also returned. Returns a list of errors that occurred
|
||||
during parsing.
|
||||
|
||||
#### func ToI2PString
|
||||
|
||||
```go
|
||||
func ToI2PString(data string) (str I2PString, err error)
|
||||
```
|
||||
ToI2PString converts a Go string to an I2PString. Returns error if the string
|
||||
exceeds STRING_MAX_SIZE.
|
||||
|
||||
#### func (I2PString) Data
|
||||
|
||||
```go
|
||||
func (str I2PString) Data() (data string, err error)
|
||||
```
|
||||
Data returns the I2PString content as a string trimmed to the specified length
|
||||
and not including the length byte. Returns error encountered by Length.
|
||||
|
||||
#### func (I2PString) Length
|
||||
|
||||
```go
|
||||
func (str I2PString) Length() (length int, err error)
|
||||
```
|
||||
Length returns the length specified in the first byte. Returns error if the
|
||||
specified does not match the actual length or the string is otherwise invalid.
|
||||
|
||||
#### type Integer
|
||||
|
||||
```go
|
||||
type Integer []byte
|
||||
```
|
||||
|
||||
Integer is the represenation of an I2P Integer.
|
||||
|
||||
https://geti2p.net/spec/common-structures#integer
|
||||
|
||||
#### func NewInteger
|
||||
|
||||
```go
|
||||
func NewInteger(bytes []byte, size int) (integer *Integer, remainder []byte, err error)
|
||||
```
|
||||
NewInteger creates a new Integer from []byte using ReadInteger. Limits the
|
||||
length of the created Integer to MAX_INTEGER_SIZE. Returns a pointer to Integer
|
||||
unlike ReadInteger.
|
||||
|
||||
#### func NewIntegerFromInt
|
||||
|
||||
```go
|
||||
func NewIntegerFromInt(value int, size int) (integer *Integer, err error)
|
||||
```
|
||||
NewIntegerFromInt creates a new Integer from a Go integer of a specified []byte
|
||||
length.
|
||||
|
||||
#### func ReadInteger
|
||||
|
||||
```go
|
||||
func ReadInteger(bytes []byte, size int) (Integer, []byte)
|
||||
```
|
||||
ReadInteger returns an Integer from a []byte of specified length. The remaining
|
||||
bytes after the specified length are also returned.
|
||||
|
||||
#### func (Integer) Bytes
|
||||
|
||||
```go
|
||||
func (i Integer) Bytes() []byte
|
||||
```
|
||||
Bytes returns the raw []byte content of an Integer.
|
||||
|
||||
#### func (Integer) Int
|
||||
|
||||
```go
|
||||
func (i Integer) Int() int
|
||||
```
|
||||
Int returns the Date as a Go integer
|
||||
|
||||
#### type Mapping
|
||||
|
||||
```go
|
||||
type Mapping struct {
|
||||
}
|
||||
```
|
||||
|
||||
Mapping is the represenation of an I2P Mapping.
|
||||
|
||||
https://geti2p.net/spec/common-structures#mapping
|
||||
|
||||
#### func GoMapToMapping
|
||||
|
||||
```go
|
||||
func GoMapToMapping(gomap map[string]string) (mapping *Mapping, err error)
|
||||
```
|
||||
GoMapToMapping converts a Go map of unformatted strings to *Mapping.
|
||||
|
||||
#### func NewMapping
|
||||
|
||||
```go
|
||||
func NewMapping(bytes []byte) (values *Mapping, remainder []byte, err []error)
|
||||
```
|
||||
NewMapping creates a new *Mapping from []byte using ReadMapping. Returns a
|
||||
pointer to Mapping unlike ReadMapping.
|
||||
|
||||
#### func ReadMapping
|
||||
|
||||
```go
|
||||
func ReadMapping(bytes []byte) (mapping Mapping, remainder []byte, err []error)
|
||||
```
|
||||
ReadMapping returns Mapping from a []byte. The remaining bytes after the
|
||||
specified length are also returned. Returns a list of errors that occurred
|
||||
during parsing.
|
||||
|
||||
#### func ValuesToMapping
|
||||
|
||||
```go
|
||||
func ValuesToMapping(values MappingValues) *Mapping
|
||||
```
|
||||
ValuesToMapping creates a *Mapping using MappingValues. The values are sorted in
|
||||
the order defined in mappingOrder.
|
||||
|
||||
#### func (*Mapping) Data
|
||||
|
||||
```go
|
||||
func (mapping *Mapping) Data() []byte
|
||||
```
|
||||
Data returns a Mapping in its []byte form.
|
||||
|
||||
#### func (*Mapping) HasDuplicateKeys
|
||||
|
||||
```go
|
||||
func (mapping *Mapping) HasDuplicateKeys() bool
|
||||
```
|
||||
HasDuplicateKeys returns true if two keys in a mapping are identical.
|
||||
|
||||
#### func (Mapping) Values
|
||||
|
||||
```go
|
||||
func (mapping Mapping) Values() MappingValues
|
||||
```
|
||||
Values returns the values contained in a Mapping as MappingValues.
|
||||
|
||||
#### type MappingValues
|
||||
|
||||
```go
|
||||
type MappingValues [][2]I2PString
|
||||
```
|
||||
|
||||
MappingValues represents the parsed key value pairs inside of an I2P Mapping.
|
||||
|
||||
#### func ReadMappingValues
|
||||
|
||||
```go
|
||||
func ReadMappingValues(remainder []byte, map_length Integer) (values *MappingValues, remainder_bytes []byte, errs []error)
|
||||
```
|
||||
ReadMappingValues returns *MappingValues from a []byte. The remaining bytes
|
||||
after the specified length are also returned. Returns a list of errors that
|
||||
occurred during parsing.
|
||||
|
||||
#### func (MappingValues) Get
|
||||
|
||||
```go
|
||||
func (m MappingValues) Get(key I2PString) I2PString
|
||||
```
|
19
lib/common/data/errors.go
Normal file
19
lib/common/data/errors.go
Normal file
@ -0,0 +1,19 @@
|
||||
package data
|
||||
|
||||
import "fmt"
|
||||
|
||||
// WrapErrors compiles a slice of errors and returns them wrapped together as a single error.
|
||||
func WrapErrors(errs []error) error {
|
||||
var err error
|
||||
for i, e := range errs {
|
||||
err = fmt.Errorf("%v\n\t%d: %v", err, i, e)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// PrintErrors prints a formatted list of errors to the console.
|
||||
func PrintErrors(errs []error) {
|
||||
for i, e := range errs {
|
||||
fmt.Printf("\t%d: %v\n", i, e)
|
||||
}
|
||||
}
|
79
lib/common/data/hash.go
Normal file
79
lib/common/data/hash.go
Normal file
@ -0,0 +1,79 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"crypto/subtle"
|
||||
"errors"
|
||||
"io"
|
||||
)
|
||||
|
||||
/*
|
||||
[I2P Hash]
|
||||
Accurate for version 0.9.49
|
||||
|
||||
Description
|
||||
Represents the SHA256 of some data. Used throughout I2P for data verification
|
||||
and identity representation. Must be compared using constant-time operations
|
||||
to prevent timing attacks.
|
||||
|
||||
Contents
|
||||
32 bytes representing a SHA256 hash value
|
||||
*/
|
||||
|
||||
var (
|
||||
ErrInvalidHashSize = errors.New("invalid hash size")
|
||||
ErrNilReader = errors.New("nil reader")
|
||||
)
|
||||
|
||||
// Hash is the representation of an I2P Hash.
|
||||
// It is always exactly 32 bytes containing a SHA256 sum.
|
||||
//
|
||||
// https://geti2p.net/spec/common-structures#hash
|
||||
type Hash [32]byte
|
||||
|
||||
// Bytes returns a copy of the Hash as a 32-byte array.
|
||||
// This prevents modification of the original hash value.
|
||||
func (h Hash) Bytes() [32]byte {
|
||||
return h
|
||||
}
|
||||
|
||||
// Equal compares two hashes in constant time.
|
||||
// Returns true if the hashes are identical.
|
||||
func (h Hash) Equal(other Hash) bool {
|
||||
return subtle.ConstantTimeCompare(h[:], other[:]) == 1
|
||||
}
|
||||
|
||||
// IsZero returns true if the hash is all zeros.
|
||||
func (h Hash) IsZero() bool {
|
||||
var zero Hash
|
||||
return h.Equal(zero)
|
||||
}
|
||||
|
||||
// HashData returns the SHA256 sum of a []byte input as Hash.
|
||||
// Never returns an error as SHA256 operates on any input length.
|
||||
func HashData(data []byte) Hash {
|
||||
if data == nil {
|
||||
data = []byte{} // Handle nil input gracefully
|
||||
}
|
||||
return sha256.Sum256(data)
|
||||
}
|
||||
|
||||
// HashReader returns the SHA256 sum from all data read from an io.Reader.
|
||||
// Returns an error if one occurs while reading from reader or if reader is nil.
|
||||
func HashReader(r io.Reader) (Hash, error) {
|
||||
var h Hash
|
||||
|
||||
if r == nil {
|
||||
return h, ErrNilReader
|
||||
}
|
||||
|
||||
sha := sha256.New()
|
||||
_, err := io.Copy(sha, r)
|
||||
if err != nil {
|
||||
return h, err
|
||||
}
|
||||
|
||||
sum := sha.Sum(nil)
|
||||
copy(h[:], sum)
|
||||
return h, nil
|
||||
}
|
65
lib/common/data/hash_test.go
Normal file
65
lib/common/data/hash_test.go
Normal file
@ -0,0 +1,65 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHash(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
data []byte
|
||||
want Hash
|
||||
}{
|
||||
{
|
||||
name: "Empty input",
|
||||
data: []byte{},
|
||||
want: HashData([]byte{}),
|
||||
},
|
||||
{
|
||||
name: "Nil input",
|
||||
data: nil,
|
||||
want: HashData([]byte{}),
|
||||
},
|
||||
// Add more test cases
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := HashData(tt.data)
|
||||
if !got.Equal(tt.want) {
|
||||
t.Errorf("HashData() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHashReader(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
reader io.Reader
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Nil reader",
|
||||
reader: nil,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Empty reader",
|
||||
reader: strings.NewReader(""),
|
||||
wantErr: false,
|
||||
},
|
||||
// Add more test cases
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := HashReader(tt.reader)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("HashReader() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
131
lib/common/data/integer.go
Normal file
131
lib/common/data/integer.go
Normal file
@ -0,0 +1,131 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"math"
|
||||
)
|
||||
|
||||
// MAX_INTEGER_SIZE is the maximum length of an I2P integer in bytes.
|
||||
const MAX_INTEGER_SIZE = 8
|
||||
|
||||
/*
|
||||
[I2P Integer]
|
||||
Accurate for version 0.9.49
|
||||
|
||||
Description
|
||||
Represents a non-negative integer.
|
||||
|
||||
Contents
|
||||
1 to 8 bytes in network byte order (big endian) representing an unsigned integer.
|
||||
*/
|
||||
|
||||
var (
|
||||
// ErrInvalidSize indicates the requested integer size is invalid (<=0 or >MAX_INTEGER_SIZE)
|
||||
ErrInvalidSize = errors.New("invalid integer size")
|
||||
// ErrInsufficientData indicates there isn't enough data to read the requested size
|
||||
ErrInsufficientData = errors.New("insufficient data")
|
||||
// ErrNegativeValue indicates an attempt to create an Integer from a negative value
|
||||
ErrNegativeValue = errors.New("negative values not allowed")
|
||||
// ErrIntegerOverflow indicates the value exceeds the maximum allowed size
|
||||
ErrIntegerOverflow = errors.New("integer overflow")
|
||||
)
|
||||
|
||||
// Integer is the representation of an I2P Integer.
|
||||
// It contains 1 to 8 bytes in network byte order (big endian)
|
||||
// representing an unsigned integer value.
|
||||
type Integer []byte
|
||||
|
||||
// Bytes returns the raw []byte content of an Integer.
|
||||
// This represents the big-endian encoded form of the integer.
|
||||
func (i Integer) Bytes() []byte {
|
||||
return i
|
||||
}
|
||||
|
||||
// Int returns the Integer as a Go integer.
|
||||
// Returns an error if the value would overflow on the current platform
|
||||
// or if the encoding is invalid.
|
||||
func (i Integer) Int() int {
|
||||
val, _ := intFromBytes(i)
|
||||
return val
|
||||
}
|
||||
|
||||
// ReadInteger returns an Integer from a []byte of specified length.
|
||||
// The remaining bytes after the specified length are also returned.
|
||||
// Returns an error if size is invalid or there isn't enough data.
|
||||
func ReadInteger(bytes []byte, size int) (Integer, []byte, error) {
|
||||
if size <= 0 {
|
||||
return nil, bytes, ErrInvalidSize
|
||||
}
|
||||
if size > len(bytes) {
|
||||
return nil, bytes, ErrInsufficientData
|
||||
}
|
||||
return Integer(bytes[:size]), bytes[size:], nil
|
||||
}
|
||||
|
||||
// NewInteger creates a new Integer from []byte using ReadInteger.
|
||||
// Limits the length of the created Integer to MAX_INTEGER_SIZE.
|
||||
// Returns a pointer to Integer and the remaining bytes.
|
||||
// Returns an error if size is invalid or there isn't enough data.
|
||||
func NewInteger(bytes []byte, size int) (*Integer, []byte, error) {
|
||||
if size <= 0 || size > MAX_INTEGER_SIZE {
|
||||
return nil, bytes, ErrInvalidSize
|
||||
}
|
||||
if len(bytes) < size {
|
||||
return nil, bytes, ErrInsufficientData
|
||||
}
|
||||
|
||||
integer, remainder, err := ReadInteger(bytes, size)
|
||||
if err != nil {
|
||||
return nil, bytes, err
|
||||
}
|
||||
return &integer, remainder, nil
|
||||
}
|
||||
|
||||
// NewIntegerFromInt creates a new Integer from a Go integer of a specified []byte length.
|
||||
// The value must be non-negative and fit within the specified number of bytes.
|
||||
// Returns an error if the size is invalid or the value cannot be represented.
|
||||
func NewIntegerFromInt(value int, size int) (*Integer, error) {
|
||||
if size <= 0 || size > MAX_INTEGER_SIZE {
|
||||
return nil, ErrInvalidSize
|
||||
}
|
||||
if value < 0 {
|
||||
return nil, ErrNegativeValue
|
||||
}
|
||||
|
||||
// Check if value fits in specified size
|
||||
maxVal := int(math.Pow(2, float64(size*8))) - 1
|
||||
if value > maxVal {
|
||||
return nil, ErrIntegerOverflow
|
||||
}
|
||||
|
||||
buf := make([]byte, MAX_INTEGER_SIZE)
|
||||
binary.BigEndian.PutUint64(buf, uint64(value))
|
||||
|
||||
data := buf[MAX_INTEGER_SIZE-size:]
|
||||
integer := Integer(data)
|
||||
return &integer, nil
|
||||
}
|
||||
|
||||
// intFromBytes interprets a slice of bytes from length 0 to length 8 as a big-endian
|
||||
// integer and returns an int representation.
|
||||
// Returns an error if the value would overflow on the current platform
|
||||
// or if the input is invalid.
|
||||
func intFromBytes(number []byte) (int, error) {
|
||||
if len(number) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
if len(number) > MAX_INTEGER_SIZE {
|
||||
return 0, ErrInvalidSize
|
||||
}
|
||||
|
||||
padded := make([]byte, MAX_INTEGER_SIZE)
|
||||
copy(padded[MAX_INTEGER_SIZE-len(number):], number)
|
||||
|
||||
val := int64(binary.BigEndian.Uint64(padded))
|
||||
if val > math.MaxInt32 || val < math.MinInt32 {
|
||||
return 0, ErrIntegerOverflow
|
||||
}
|
||||
|
||||
return int(val), nil
|
||||
}
|
61
lib/common/data/integer_test.go
Normal file
61
lib/common/data/integer_test.go
Normal file
@ -0,0 +1,61 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestIntegerBigEndian(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
bytes := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}
|
||||
integer := Integer(bytes)
|
||||
|
||||
assert.Equal(integer.Int(), 1, "Integer() did not parse bytes big endian")
|
||||
}
|
||||
|
||||
func TestWorksWithOneByte(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
integer := Integer([]byte{0x01})
|
||||
|
||||
assert.Equal(integer.Int(), 1, "Integer() did not correctly parse single byte slice")
|
||||
}
|
||||
|
||||
func TestIsZeroWithNoData(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
integer := Integer([]byte{})
|
||||
|
||||
assert.Equal(integer.Int(), 0, "Integer() did not correctly parse zero length byte slice")
|
||||
}
|
||||
|
||||
func TestIntegerEdgeCases(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input []byte
|
||||
size int
|
||||
wantErr bool
|
||||
wantInt int
|
||||
}{
|
||||
{"empty input", []byte{}, 1, true, 0},
|
||||
{"zero size", []byte{1}, 0, true, 0},
|
||||
{"oversized", []byte{1}, 9, true, 0},
|
||||
{"valid small", []byte{42}, 1, false, 42},
|
||||
{"valid max", []byte{1, 2, 3, 4, 5, 6, 7, 8}, 8, false, 72623859790382856},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
i, _, err := NewInteger(tt.input, tt.size)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("NewInteger() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if err == nil && i.Int() != tt.wantInt {
|
||||
t.Errorf("Integer.Int() = %v, want %v", i.Int(), tt.wantInt)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
247
lib/common/data/mapping.go
Normal file
247
lib/common/data/mapping.go
Normal file
@ -0,0 +1,247 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
/*
|
||||
[I2P Mapping]
|
||||
Accurate for version 0.9.49
|
||||
|
||||
Description
|
||||
A set of key/value mappings or properties
|
||||
|
||||
|
||||
Contents
|
||||
A 2-byte size Integer followed by a series of String=String; pairs
|
||||
|
||||
+----+----+----+----+----+----+----+----+
|
||||
| size |key_string (len + data) | = |
|
||||
+----+----+----+----+----+----+----+----+
|
||||
| val_string (len + data) | ; | ...
|
||||
+----+----+----+----+----+----+----+
|
||||
size :: Integer
|
||||
length -> 2 bytes
|
||||
Total number of bytes that follow
|
||||
|
||||
key_string :: String
|
||||
A string (one byte length followed by UTF-8 encoded characters)
|
||||
|
||||
= :: A single byte containing '='
|
||||
|
||||
val_string :: String
|
||||
A string (one byte length followed by UTF-8 encoded characters)
|
||||
|
||||
; :: A single byte containing ';'
|
||||
*/
|
||||
|
||||
// Mapping is the represenation of an I2P Mapping.
|
||||
//
|
||||
// https://geti2p.net/spec/common-structures#mapping
|
||||
type Mapping struct {
|
||||
size *Integer
|
||||
vals *MappingValues
|
||||
}
|
||||
|
||||
// Values returns the values contained in a Mapping as MappingValues.
|
||||
func (mapping Mapping) Values() MappingValues {
|
||||
if mapping.vals == nil {
|
||||
log.Debug("Mapping values are nil, returning empty MappingValues")
|
||||
return MappingValues{}
|
||||
}
|
||||
log.WithFields(logrus.Fields{
|
||||
"values_count": len(*mapping.vals),
|
||||
}).Debug("Retrieved Mapping values")
|
||||
return *mapping.vals
|
||||
}
|
||||
|
||||
// Data returns a Mapping in its []byte form.
|
||||
func (mapping *Mapping) Data() []byte {
|
||||
keyOrValIntegerLength := 1
|
||||
bytes := mapping.size.Bytes()
|
||||
for _, pair := range mapping.Values() {
|
||||
klen, _ := pair[0].Length()
|
||||
keylen, _ := NewIntegerFromInt(klen, keyOrValIntegerLength)
|
||||
bytes = append(bytes, keylen.Bytes()...)
|
||||
bytes = append(bytes, pair[0][1:]...)
|
||||
bytes = append(bytes, 0x3d)
|
||||
vlen, _ := pair[1].Length()
|
||||
vallen, _ := NewIntegerFromInt(vlen, keyOrValIntegerLength)
|
||||
bytes = append(bytes, vallen.Bytes()...)
|
||||
bytes = append(bytes, pair[1][1:]...)
|
||||
bytes = append(bytes, 0x3b)
|
||||
}
|
||||
return bytes
|
||||
}
|
||||
|
||||
// HasDuplicateKeys returns true if two keys in a mapping are identical.
|
||||
func (mapping *Mapping) HasDuplicateKeys() bool {
|
||||
log.Debug("Checking for duplicate keys in Mapping")
|
||||
seen_values := make(map[string]bool)
|
||||
values := mapping.Values()
|
||||
for _, pair := range values {
|
||||
key, _ := pair[0].Data()
|
||||
if _, present := seen_values[key]; present {
|
||||
log.WithFields(logrus.Fields{
|
||||
"duplicate_key": key,
|
||||
}).Warn("Found duplicate key in Mapping")
|
||||
return true
|
||||
} else {
|
||||
seen_values[key] = true
|
||||
}
|
||||
}
|
||||
log.Debug("No duplicate keys found in Mapping")
|
||||
return false
|
||||
}
|
||||
|
||||
// GoMapToMapping converts a Go map of unformatted strings to *Mapping.
|
||||
func GoMapToMapping(gomap map[string]string) (*Mapping, error) {
|
||||
map_vals := MappingValues{}
|
||||
for k, v := range gomap {
|
||||
key_str, err := ToI2PString(k)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("key conversion error: %w", err)
|
||||
}
|
||||
val_str, err := ToI2PString(v)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("value conversion error: %w", err)
|
||||
}
|
||||
map_vals = append(map_vals, [2]I2PString{key_str, val_str})
|
||||
}
|
||||
return ValuesToMapping(map_vals), nil
|
||||
}
|
||||
|
||||
// Check if the string parsing error indicates that the Mapping
|
||||
// should no longer be parsed.
|
||||
func stopValueRead(err error) bool {
|
||||
result := err.Error() == "error parsing string: zero length"
|
||||
if result {
|
||||
log.WithError(err).Debug("Stopping value read due to zero length error")
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Determine if the first byte in a slice of bytes is the provided byte.
|
||||
func beginsWith(bytes []byte, chr byte) bool {
|
||||
/*
|
||||
return len(bytes) != 0 &&
|
||||
bytes[0] == chr
|
||||
*/
|
||||
result := len(bytes) != 0 && bytes[0] == chr
|
||||
log.WithFields(logrus.Fields{
|
||||
"bytes_length": len(bytes),
|
||||
"expected_char": string(chr),
|
||||
"result": result,
|
||||
}).Debug("Checked if bytes begin with specific character")
|
||||
return result
|
||||
}
|
||||
|
||||
func (mapping *Mapping) addValue(key, value I2PString) error {
|
||||
for _, pair := range *mapping.vals {
|
||||
existingKey, _ := pair[0].Data()
|
||||
newKey, _ := key.Data()
|
||||
if existingKey == newKey {
|
||||
return fmt.Errorf("duplicate key: %s", newKey)
|
||||
}
|
||||
}
|
||||
*mapping.vals = append(*mapping.vals, [2]I2PString{key, value})
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadMapping returns Mapping from a []byte.
|
||||
// The remaining bytes after the specified length are also returned.
|
||||
// Returns a list of errors that occurred during parsing.
|
||||
const MaxMappingSize = 65535 // Match Java I2P's maximum mapping size
|
||||
|
||||
func ReadMapping(bytes []byte) (mapping Mapping, remainder []byte, err []error) {
|
||||
if len(bytes) < 3 {
|
||||
err = append(err, errors.New("mapping data too short"))
|
||||
return
|
||||
}
|
||||
size, remainder, e := NewInteger(bytes, 2)
|
||||
if e != nil {
|
||||
log.WithError(e).Error("Failed to read Mapping size")
|
||||
err = append(err, e)
|
||||
}
|
||||
if size.Int() > MaxMappingSize {
|
||||
err = append(err, fmt.Errorf("mapping size %d exceeds maximum %d", size.Int(), MaxMappingSize))
|
||||
return
|
||||
}
|
||||
log.WithFields(logrus.Fields{
|
||||
"input_length": len(bytes),
|
||||
}).Debug("Reading Mapping from bytes")
|
||||
if len(bytes) < 3 {
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "ReadMapping",
|
||||
"reason": "zero length",
|
||||
}).Warn("mapping format violation")
|
||||
e := errors.New("zero length")
|
||||
err = append(err, e)
|
||||
return
|
||||
}
|
||||
if size.Int() == 0 {
|
||||
log.Warn("Mapping size is zero")
|
||||
return
|
||||
}
|
||||
mapping.size = size
|
||||
if mapping.size.Int() > len(remainder) {
|
||||
err = append(err, fmt.Errorf("mapping size %d exceeds available data length %d",
|
||||
mapping.size.Int(), len(remainder)))
|
||||
return
|
||||
}
|
||||
map_bytes := remainder[:mapping.size.Int()]
|
||||
remainder = remainder[mapping.size.Int():]
|
||||
if len(remainder) == 0 {
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "ReadMapping",
|
||||
"reason": "zero length",
|
||||
}).Warn("mapping format violation")
|
||||
e := errors.New("zero length")
|
||||
err = append(err, e)
|
||||
}
|
||||
// TODO: this should take the remainder and the length we already parsed above, as a parameter.
|
||||
// Like tomorrow morning.
|
||||
// ReadMappingValues should not attempt to figure out the length of the bytes it's reading over.
|
||||
vals, _, mappingValueErrs := ReadMappingValues(map_bytes, *mapping.size)
|
||||
|
||||
err = append(err, mappingValueErrs...)
|
||||
mapping.vals = vals
|
||||
if len(mappingValueErrs) > 0 {
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "ReadMapping",
|
||||
"reason": "error parsing mapping values",
|
||||
}).Warn("mapping format violation")
|
||||
e := errors.New("error parsing mapping values")
|
||||
err = append(err, e)
|
||||
}
|
||||
|
||||
log.WithFields(logrus.Fields{
|
||||
"mapping_size": mapping.size.Int(),
|
||||
"values_count": len(*mapping.vals),
|
||||
"remainder_length": len(remainder),
|
||||
"error_count": len(err),
|
||||
}).Debug("Finished reading Mapping")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// NewMapping creates a new *Mapping from []byte using ReadMapping.
|
||||
// Returns a pointer to Mapping unlike ReadMapping.
|
||||
func NewMapping(bytes []byte) (values *Mapping, remainder []byte, err []error) {
|
||||
log.WithFields(logrus.Fields{
|
||||
"input_length": len(bytes),
|
||||
}).Debug("Creating new Mapping")
|
||||
|
||||
objvalues, remainder, err := ReadMapping(bytes)
|
||||
values = &objvalues
|
||||
|
||||
log.WithFields(logrus.Fields{
|
||||
"values_count": len(values.Values()),
|
||||
"remainder_length": len(remainder),
|
||||
"error_count": len(err),
|
||||
}).Debug("Finished creating new Mapping")
|
||||
return
|
||||
}
|
192
lib/common/data/mapping_test.go
Normal file
192
lib/common/data/mapping_test.go
Normal file
@ -0,0 +1,192 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestValuesExclusesPairWithBadData(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
bad_key, _, errs := NewMapping([]byte{0x00, 0x0c, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b, 0x00})
|
||||
values := bad_key.Values()
|
||||
|
||||
e := WrapErrors(errs)
|
||||
t.Log(e)
|
||||
|
||||
assert.NotNil(errs, "Values() did not return errors when some values had bad key")
|
||||
|
||||
if assert.Equal(1, len(values), "Values() did not return valid values when some values had bad key") {
|
||||
k := values[0][0]
|
||||
key, _ := k.Data()
|
||||
v := values[0][1]
|
||||
val, _ := v.Data()
|
||||
assert.Equal(key, "a", "Values() returned by data with invalid key contains incorrect present key")
|
||||
assert.Equal(val, "b", "Values() returned by data with invalid key contains incorrect present key")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValuesWarnsMissingData(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
_, _, errs := NewMapping([]byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62})
|
||||
|
||||
if assert.Equal(2, len(errs), "Values() reported wrong error count when mapping had missing data") {
|
||||
assert.Equal(errs[0].Error(), "warning parsing mapping: mapping length exceeds provided data")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValuesWarnsExtraData(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
mapping, _, errs := NewMapping([]byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b, 0x00})
|
||||
values := mapping.Values()
|
||||
|
||||
key, kerr := values[0][0].Data()
|
||||
val, verr := values[0][1].Data()
|
||||
|
||||
assert.Nil(kerr)
|
||||
assert.Nil(verr)
|
||||
assert.Equal(key, "a", "Values() did not return key in valid data")
|
||||
assert.Equal(val, "b", "Values() did not return value in valid data")
|
||||
|
||||
if assert.Equal(2, len(errs), "Values() reported wrong error count when mapping had extra data") {
|
||||
assert.Equal("warning parsing mapping: data exists beyond length of mapping", errs[0].Error(), "correct error message should be returned")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValuesEnforcesEqualDelimitor(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
mapping, _, errs := NewMapping([]byte{0x00, 0x06, 0x01, 0x61, 0x30, 0x01, 0x62, 0x3b})
|
||||
values := mapping.Values()
|
||||
|
||||
if assert.Equal(2, len(errs), "Values() reported wrong error count when mapping had = format error") {
|
||||
assert.Equal("mapping format violation, expected =", errs[0].Error(), "correct error message should be returned")
|
||||
}
|
||||
assert.Equal(0, len(values), "Values() not empty with invalid data due to = format error")
|
||||
}
|
||||
|
||||
func TestValuesEnforcedSemicolonDelimitor(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
mapping, _, errs := NewMapping([]byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x30})
|
||||
values := mapping.Values()
|
||||
|
||||
if assert.Equal(2, len(errs), "Values() reported wrong error count when mapping had ; format error") {
|
||||
assert.Equal("mapping format violation, expected ;", errs[0].Error(), "correct error message should be returned")
|
||||
}
|
||||
assert.Equal(0, len(values), "Values() not empty with invalid data due to ; format error")
|
||||
}
|
||||
|
||||
func TestValuesReturnsValues(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
mapping, _, errs := NewMapping([]byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b})
|
||||
values := mapping.Values()
|
||||
|
||||
key, kerr := values[0][0].Data()
|
||||
val, verr := values[0][1].Data()
|
||||
|
||||
assert.Nil(errs, "Values() returned a errors with parsing valid data")
|
||||
assert.Nil(kerr)
|
||||
assert.Nil(verr)
|
||||
assert.Equal("a", key, "Values() did not return key in valid data")
|
||||
assert.Equal("b", val, "Values() did not return value in valid data")
|
||||
}
|
||||
|
||||
func TestHasDuplicateKeysTrueWhenDuplicates(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
dups, _, _ := NewMapping([]byte{0x00, 0x0c, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b})
|
||||
|
||||
assert.Equal(true, dups.HasDuplicateKeys(), "HasDuplicateKeys() did not report true when duplicate keys present")
|
||||
}
|
||||
|
||||
func TestHasDuplicateKeysFalseWithoutDuplicates(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
mapping, _, _ := NewMapping([]byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b})
|
||||
|
||||
assert.Equal(false, mapping.HasDuplicateKeys(), "HasDuplicateKeys() did not report false when no duplicate keys present")
|
||||
}
|
||||
|
||||
func TestReadMappingHasDuplicateKeys(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
_, _, errs := NewMapping([]byte{0x00, 0x0c, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b})
|
||||
|
||||
assert.Equal("mapping format violation, duplicate key in mapping", errs[0].Error(), "ReadMapping should throw an error when duplicate keys are present.")
|
||||
}
|
||||
|
||||
func TestGoMapToMappingProducesCorrectMapping(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
gomap := map[string]string{"a": "b"}
|
||||
mapping, err := GoMapToMapping(gomap)
|
||||
|
||||
assert.Nil(err, "GoMapToMapping() returned error with valid data")
|
||||
expected := []byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b}
|
||||
if bytes.Compare(mapping.Data(), expected) != 0 {
|
||||
t.Fatal("GoMapToMapping did not produce correct Mapping", mapping, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFullGoMapToMappingProducesCorrectMapping(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
gomap := map[string]string{
|
||||
"a": "b",
|
||||
"c": "d",
|
||||
}
|
||||
mapping, err := GoMapToMapping(gomap)
|
||||
|
||||
assert.Nil(err, "GoMapToMapping() returned error with valid data")
|
||||
expected := []byte{0x00, 0x0c, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b, 0x01, 0x63, 0x3d, 0x01, 0x64, 0x3b}
|
||||
if bytes.Compare(mapping.Data(), expected) != 0 {
|
||||
t.Fatal("GoMapToMapping did not produce correct Mapping", mapping, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStopValueReadTrueWhenCorrectErr(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
status := stopValueRead(errors.New("error parsing string: zero length"))
|
||||
|
||||
assert.Equal(true, status, "stopValueRead() did not return true when String error found")
|
||||
}
|
||||
|
||||
func TestStopValueReadFalseWhenWrongErr(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
status := stopValueRead(errors.New("something else"))
|
||||
|
||||
assert.Equal(false, status, "stopValueRead() did not return false when non String error found")
|
||||
}
|
||||
|
||||
func TestBeginsWithCorrectWhenTrue(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
slice := []byte{0x41}
|
||||
|
||||
assert.Equal(true, beginsWith(slice, 0x41), "beginsWith() did not return true when correct")
|
||||
}
|
||||
|
||||
func TestBeginsWithCorrectWhenFalse(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
slice := []byte{0x00}
|
||||
|
||||
assert.Equal(false, beginsWith(slice, 0x41), "beginsWith() did not false when incorrect")
|
||||
}
|
||||
|
||||
func TestBeginsWithCorrectWhenNil(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
slice := make([]byte, 0)
|
||||
|
||||
assert.Equal(false, beginsWith(slice, 0x41), "beginsWith() did not return false on empty slice")
|
||||
}
|
245
lib/common/data/mapping_values.go
Normal file
245
lib/common/data/mapping_values.go
Normal file
@ -0,0 +1,245 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"sort"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// MappingValues represents the parsed key value pairs inside of an I2P Mapping.
|
||||
type MappingValues [][2]I2PString
|
||||
|
||||
func (m MappingValues) Get(key I2PString) I2PString {
|
||||
if key == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
keyBytes, err := key.Data()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
log.WithFields(logrus.Fields{
|
||||
"key": string(keyBytes),
|
||||
}).Debug("Searching for key in MappingValues")
|
||||
|
||||
for _, pair := range m {
|
||||
if pair[0] == nil {
|
||||
continue
|
||||
}
|
||||
kb, err := pair[0].Data()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if kb == keyBytes {
|
||||
data, _ := pair[1].Data()
|
||||
log.WithFields(logrus.Fields{
|
||||
"key": string(keyBytes),
|
||||
"value": string(data),
|
||||
}).Debug("Found matching key in MappingValues")
|
||||
return pair[1]
|
||||
}
|
||||
}
|
||||
|
||||
log.WithFields(logrus.Fields{
|
||||
"key": string(keyBytes),
|
||||
}).Debug("Key not found in MappingValues")
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValuesToMapping creates a *Mapping using MappingValues.
|
||||
// The values are sorted in the order defined in mappingOrder.
|
||||
func ValuesToMapping(values MappingValues) *Mapping {
|
||||
// Default length to 2 * len
|
||||
// 1 byte for ;
|
||||
// 1 byte for =
|
||||
log.WithFields(logrus.Fields{
|
||||
"values_count": len(values),
|
||||
}).Debug("Converting MappingValues to Mapping")
|
||||
baseLength := 2 * len(values)
|
||||
for _, mappingVals := range values {
|
||||
for _, keyOrVal := range mappingVals {
|
||||
baseLength += len(keyOrVal)
|
||||
}
|
||||
}
|
||||
|
||||
log.WithFields(logrus.Fields{
|
||||
"mapping_size": baseLength,
|
||||
}).Debug("Created Mapping from MappingValues")
|
||||
|
||||
mappingSize, _ := NewIntegerFromInt(baseLength, 2)
|
||||
return &Mapping{
|
||||
size: mappingSize,
|
||||
vals: &values,
|
||||
}
|
||||
}
|
||||
|
||||
// I2P Mappings require consistent order in some cases for cryptographic signing, and sorting
|
||||
// by keys. The Mapping is sorted lexographically by keys. Duplicate keys are allowed in general,
|
||||
// but in implementations where they must be sorted like I2CP SessionConfig duplicate keys are not allowed.
|
||||
// In practice routers do not seem to allow duplicate keys.
|
||||
func mappingOrder(values MappingValues) {
|
||||
sort.SliceStable(values, func(i, j int) bool {
|
||||
data1, err1 := values[i][0].Data()
|
||||
data2, err2 := values[j][0].Data()
|
||||
|
||||
// Handle error cases by treating them as "less than"
|
||||
if err1 != nil || err2 != nil {
|
||||
return err1 == nil
|
||||
}
|
||||
|
||||
return bytes.Compare([]byte(data1), []byte(data2)) < 0
|
||||
})
|
||||
}
|
||||
|
||||
// ReadMappingValues returns *MappingValues from a []byte.
|
||||
// The remaining bytes after the specified length are also returned.
|
||||
// Returns a list of errors that occurred during parsing.
|
||||
func ReadMappingValues(remainder []byte, map_length Integer) (values *MappingValues, remainder_bytes []byte, errs []error) {
|
||||
// mapping := remainder
|
||||
// var remainder = mapping
|
||||
// var err error
|
||||
log.WithFields(logrus.Fields{
|
||||
"input_length": len(remainder),
|
||||
"map_length": map_length.Int(),
|
||||
}).Debug("Reading MappingValues")
|
||||
|
||||
if remainder == nil || len(remainder) < 1 {
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "(Mapping) Values",
|
||||
"reason": "data shorter than expected",
|
||||
}).Error("mapping contained no data")
|
||||
errs = []error{errors.New("mapping contained no data")}
|
||||
return
|
||||
}
|
||||
map_values := make(MappingValues, 0)
|
||||
int_map_length := map_length.Int()
|
||||
mapping_len := len(remainder)
|
||||
if mapping_len > int_map_length {
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "(Mapping) Values",
|
||||
"mapping_bytes_length": mapping_len,
|
||||
"mapping_length_field": int_map_length,
|
||||
"reason": "data longer than expected",
|
||||
}).Warn("mapping format warning")
|
||||
errs = append(errs, errors.New("warning parsing mapping: data exists beyond length of mapping"))
|
||||
} else if int_map_length > mapping_len {
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "(Mapping) Values",
|
||||
"mapping_bytes_length": mapping_len,
|
||||
"mapping_length_field": int_map_length,
|
||||
"reason": "data shorter than expected",
|
||||
}).Warn("mapping format warning")
|
||||
errs = append(errs, errors.New("warning parsing mapping: mapping length exceeds provided data"))
|
||||
}
|
||||
|
||||
encounteredKeysMap := map[string]bool{}
|
||||
// pop off length bytes before parsing kv pairs
|
||||
// remainder = remainder[2:]
|
||||
|
||||
for {
|
||||
// Read a key, breaking on fatal errors
|
||||
// and appending warnings
|
||||
|
||||
// Minimum byte length required for another KV pair.
|
||||
// Two bytes for each string length
|
||||
// At least 1 byte per string
|
||||
// One byte for =
|
||||
// One byte for ;
|
||||
if len(remainder) < 6 {
|
||||
// Not returning an error here as the issue is already flagged by mapping length being wrong.
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "(Mapping) Values",
|
||||
"reason": "mapping format violation",
|
||||
}).Warn("mapping format violation, too few bytes for a kv pair")
|
||||
break
|
||||
}
|
||||
|
||||
key_str, more, err := ReadI2PString(remainder)
|
||||
if err != nil {
|
||||
if stopValueRead(err) {
|
||||
errs = append(errs, err)
|
||||
// return
|
||||
}
|
||||
}
|
||||
// overwriting remainder with more as another var to prevent memory weirdness in loops
|
||||
remainder = more
|
||||
// log.Printf("(MAPPING VALUES DEBUG) Remainder: %s\n", remainder)
|
||||
|
||||
// Check if key has already been encountered in this mapping
|
||||
keyBytes, _ := key_str.Data()
|
||||
keyAsString := string(keyBytes)
|
||||
_, ok := encounteredKeysMap[keyAsString]
|
||||
if ok {
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "(Mapping) Values",
|
||||
"reason": "duplicate key in mapping",
|
||||
"key": string(key_str),
|
||||
}).Error("mapping format violation")
|
||||
log.Printf("DUPE: %s", key_str)
|
||||
errs = append(errs, errors.New("mapping format violation, duplicate key in mapping"))
|
||||
// Based on other implementations this does not seem to happen often?
|
||||
// Java throws an exception in this case, the base object is a Hashmap so the value is overwritten and an exception is thrown.
|
||||
// i2pd as far as I can tell just overwrites the original value
|
||||
// Continue on, we can check if the Mapping contains duplicate keys later.
|
||||
}
|
||||
|
||||
if !beginsWith(remainder, 0x3d) {
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "(Mapping) Values",
|
||||
"reason": "expected =",
|
||||
"value:": string(remainder),
|
||||
}).Warn("mapping format violation")
|
||||
errs = append(errs, errors.New("mapping format violation, expected ="))
|
||||
log.Printf("ERRVAL: %s", remainder)
|
||||
break
|
||||
} else {
|
||||
remainder = remainder[1:]
|
||||
}
|
||||
|
||||
// Read a value, breaking on fatal errors
|
||||
// and appending warnings
|
||||
val_str, more, err := ReadI2PString(remainder)
|
||||
if err != nil {
|
||||
if stopValueRead(err) {
|
||||
errs = append(errs, err)
|
||||
// return
|
||||
}
|
||||
}
|
||||
// overwriting remainder with more as another var to prevent memory weirdness in loops
|
||||
remainder = more
|
||||
// log.Printf("(MAPPING VALUES DEBUG) Remainder: %s\n", remainder)
|
||||
// log.Printf("(MAPPING VALUES DEBUG) String: value: %s", val_str)
|
||||
if !beginsWith(remainder, 0x3b) {
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "(Mapping) Values",
|
||||
"reason": "expected ;",
|
||||
"value:": string(remainder),
|
||||
}).Warn("mapping format violation")
|
||||
errs = append(errs, errors.New("mapping format violation, expected ;"))
|
||||
break
|
||||
} else {
|
||||
remainder = remainder[1:]
|
||||
}
|
||||
|
||||
// Append the key-value pair and break if there is no more data to read
|
||||
map_values = append(map_values, [2]I2PString{key_str, val_str})
|
||||
if len(remainder) == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
// Store the encountered key with arbitrary data
|
||||
encounteredKeysMap[keyAsString] = true
|
||||
}
|
||||
values = &map_values
|
||||
|
||||
log.WithFields(logrus.Fields{
|
||||
"values_count": len(map_values),
|
||||
"remainder_length": len(remainder_bytes),
|
||||
"error_count": len(errs),
|
||||
}).Debug("Finished reading MappingValues")
|
||||
remainder_bytes = remainder
|
||||
return
|
||||
}
|
86
lib/common/data/mapping_values_test.go
Normal file
86
lib/common/data/mapping_values_test.go
Normal file
@ -0,0 +1,86 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMappingOrderSortsValuesThenKeys(t *testing.T) {
|
||||
a, _ := ToI2PString("a")
|
||||
b, _ := ToI2PString("b")
|
||||
aa, _ := ToI2PString("aa")
|
||||
ab, _ := ToI2PString("ab")
|
||||
ac, _ := ToI2PString("ac")
|
||||
values := MappingValues{
|
||||
[2]I2PString{b, b},
|
||||
[2]I2PString{ac, a},
|
||||
[2]I2PString{ab, b},
|
||||
[2]I2PString{aa, a},
|
||||
[2]I2PString{a, a},
|
||||
}
|
||||
mappingOrder(values)
|
||||
for i, pair := range values {
|
||||
key, _ := pair[0].Data()
|
||||
switch i {
|
||||
case 0:
|
||||
if !(key == "a") {
|
||||
t.Fatal(fmt.Sprintf("mappingOrder expected key a, got %s at index", key), i)
|
||||
}
|
||||
case 1:
|
||||
if !(key == "aa") {
|
||||
t.Fatal(fmt.Sprintf("mappingOrder expected key aa, got %s at index", key), i)
|
||||
}
|
||||
case 2:
|
||||
if !(key == "ab") {
|
||||
t.Fatal(fmt.Sprintf("mappingOrder expected key ab, got %s at index", key), i)
|
||||
}
|
||||
case 3:
|
||||
if !(key == "ac") {
|
||||
t.Fatal(fmt.Sprintf("mappingOrder expected key ac, got %s at index", key), i)
|
||||
}
|
||||
case 4:
|
||||
if !(key == "b") {
|
||||
t.Fatal(fmt.Sprintf("mappingOrder expected key b, got %s at index", key), i)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMappingValuesEdgeCases(t *testing.T) {
|
||||
k1, _ := ToI2PString("test")
|
||||
tests := []struct {
|
||||
name string
|
||||
mv MappingValues
|
||||
key I2PString
|
||||
want I2PString
|
||||
}{
|
||||
{
|
||||
name: "nil key",
|
||||
mv: MappingValues{},
|
||||
key: nil,
|
||||
want: nil,
|
||||
},
|
||||
{
|
||||
name: "empty mapping",
|
||||
mv: MappingValues{},
|
||||
key: k1,
|
||||
want: nil,
|
||||
},
|
||||
{
|
||||
name: "nil value in pair",
|
||||
mv: MappingValues{{k1, nil}},
|
||||
key: k1,
|
||||
want: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := tt.mv.Get(tt.key)
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("MappingValues.Get() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
180
lib/common/data/string.go
Normal file
180
lib/common/data/string.go
Normal file
@ -0,0 +1,180 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// STRING_MAX_SIZE is the maximum number of bytes that can be stored in an I2P string
|
||||
const STRING_MAX_SIZE = 255
|
||||
|
||||
/*
|
||||
[I2P String]
|
||||
Accurate for version 0.9.49
|
||||
|
||||
Description
|
||||
Represents a UTF-8 encoded string.
|
||||
|
||||
Contents
|
||||
1 or more bytes where the first byte is the number of bytes (not characters!) in the string
|
||||
and the remaining 0-255 bytes are the non-null terminated UTF-8 encoded character array.
|
||||
Length limit is 255 bytes (not characters). Length may be 0.
|
||||
*/
|
||||
|
||||
// I2PString is the represenation of an I2P String.
|
||||
//
|
||||
// https://geti2p.net/spec/common-structures#string
|
||||
type I2PString []byte
|
||||
|
||||
// Length returns the length specified in the first byte.
|
||||
// Returns error if the specified does not match the actual length or the string is otherwise invalid.
|
||||
func (str I2PString) Length() (length int, err error) {
|
||||
if len(str) == 0 {
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "(I2PString) Length",
|
||||
"reason": "no data",
|
||||
}).Error("error parsing string")
|
||||
err = errors.New("error parsing string: zero length")
|
||||
return
|
||||
}
|
||||
l, _, err := NewInteger(str[:], 1)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to create Integer from I2PString")
|
||||
return l.Int(), err
|
||||
}
|
||||
length = l.Int()
|
||||
str_len := len(str)
|
||||
if length > str_len {
|
||||
/*log.WithFields(log.Fields{
|
||||
"at": "(I2PString) Length",
|
||||
"string_bytes_length": str_len,
|
||||
"string_length_field": length,
|
||||
"data": string(str),
|
||||
"reason": "data less than specified by length",
|
||||
}).Error("string format warning")*/
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "(I2PString) Length",
|
||||
"string_bytes_length": str_len,
|
||||
"string_length_field": length,
|
||||
"reason": "data less than specified by length",
|
||||
}).Warn("string format warning")
|
||||
err = errors.New("string parsing warning: string data is shorter than specified by length")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Data returns the I2PString content as a string trimmed to the specified length and not including the length byte.
|
||||
// Returns error encountered by Length.
|
||||
func (str I2PString) Data() (data string, err error) {
|
||||
length, err := str.Length()
|
||||
if err != nil {
|
||||
switch err.Error() {
|
||||
case "error parsing string: zero length":
|
||||
log.WithError(err).Warn("Zero length I2PString")
|
||||
return
|
||||
case "string parsing warning: string data is shorter than specified by length":
|
||||
log.WithError(err).Warn("I2PString data shorter than specified length")
|
||||
if is, e := ToI2PString(string(str[:])); e != nil {
|
||||
log.WithError(e).Error("Failed to convert short I2PString")
|
||||
return "", e
|
||||
} else {
|
||||
return is.Data()
|
||||
}
|
||||
case "string parsing warning: string contains data beyond length":
|
||||
log.WithError(err).Warn("I2PString contains data beyond specified length")
|
||||
data = string(str[1:])
|
||||
return
|
||||
}
|
||||
}
|
||||
if length == 0 {
|
||||
log.Debug("I2PString is empty")
|
||||
return
|
||||
}
|
||||
data = string(str[1 : length+1])
|
||||
log.WithFields(logrus.Fields{
|
||||
"data_length": len(data),
|
||||
}).Debug("Retrieved I2PString data")
|
||||
return
|
||||
}
|
||||
|
||||
// ToI2PString converts a Go string to an I2PString.
|
||||
// Returns error if the string exceeds STRING_MAX_SIZE.
|
||||
func ToI2PString(data string) (str I2PString, err error) {
|
||||
log.WithFields(logrus.Fields{
|
||||
"input_length": len(data),
|
||||
}).Debug("Converting string to I2PString")
|
||||
data_len := len(data)
|
||||
if data_len > STRING_MAX_SIZE {
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "ToI2PI2PString",
|
||||
"string_len": data_len,
|
||||
"max_len": STRING_MAX_SIZE,
|
||||
"reason": "too much data",
|
||||
}).Error("cannot create I2P string")
|
||||
err = errors.New("cannot store that much data in I2P string")
|
||||
return
|
||||
}
|
||||
i2p_string := []byte{byte(data_len)}
|
||||
i2p_string = append(i2p_string, []byte(data)...)
|
||||
str = I2PString(i2p_string)
|
||||
log.WithFields(logrus.Fields{
|
||||
"i2pstring_length": len(str),
|
||||
}).Debug("Successfully converted string to I2PString")
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Read a string from a slice of bytes, returning any extra data on the end
|
||||
// of the slice and any errors encountered parsing the I2PString.
|
||||
//
|
||||
|
||||
// ReadI2PString returns I2PString from a []byte.
|
||||
// The remaining bytes after the specified length are also returned.
|
||||
// Returns a list of errors that occurred during parsing.
|
||||
func ReadI2PString(data []byte) (str I2PString, remainder []byte, err error) {
|
||||
if len(data) == 0 {
|
||||
err = errors.New("data slice is empty")
|
||||
log.WithError(err).Error("Passed data with len == 0")
|
||||
return
|
||||
}
|
||||
log.WithFields(logrus.Fields{
|
||||
"input_length": len(data),
|
||||
}).Debug("Reading I2PString from bytes")
|
||||
length, _, err := NewInteger(data, 1)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to read I2PString length")
|
||||
return
|
||||
}
|
||||
data_len := length.Int() + 1
|
||||
if data_len > len(data) {
|
||||
err = fmt.Errorf("I2PString length %d exceeds available data %d", data_len-1, len(data)-1)
|
||||
log.WithError(err).Error("Failed to read I2PString")
|
||||
return
|
||||
}
|
||||
str = data[:data_len]
|
||||
remainder = data[data_len:]
|
||||
l, err := str.Length()
|
||||
if l != data_len-1 {
|
||||
err = fmt.Errorf("error reading I2P string, length does not match data")
|
||||
log.WithFields(logrus.Fields{
|
||||
"expected_length": data_len - 1,
|
||||
"actual_length": l,
|
||||
}).Error("I2PString length mismatch")
|
||||
return
|
||||
}
|
||||
log.WithFields(logrus.Fields{
|
||||
"string_length": l,
|
||||
"remainder_length": len(remainder),
|
||||
}).Debug("Successfully read I2PString from bytes")
|
||||
return
|
||||
}
|
||||
|
||||
// NewI2PString creates a new *I2PString from []byte using ReadI2PString.
|
||||
// Returns a pointer to I2PString unlike ReadI2PString.
|
||||
/*func NewI2PString(data []byte) (str *I2PString, remainder []byte, err error) {
|
||||
objstr, remainder, err := ReadI2PString(data)
|
||||
str = &objstr
|
||||
return
|
||||
}*/
|
@ -1,23 +1,24 @@
|
||||
package common
|
||||
package data
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestStringReportsCorrectLength(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
str_len, err := String([]byte{0x02, 0x00, 0x00}).Length()
|
||||
str_len, err := I2PString([]byte{0x02, 0x00, 0x00}).Length()
|
||||
|
||||
assert.Equal(str_len, 2, "Length() did not report correct length")
|
||||
assert.Nil(err, "Length() reported an error on valid string")
|
||||
}
|
||||
|
||||
func TestStringReportsLengthZeroError(t *testing.T) {
|
||||
func TestI2PStringReportsLengthZeroError(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
str_len, err := String(make([]byte, 0)).Length()
|
||||
str_len, err := I2PString(make([]byte, 0)).Length()
|
||||
|
||||
assert.Equal(str_len, 0, "Length() reported non-zero length on empty slice")
|
||||
if assert.NotNil(err) {
|
||||
@ -25,10 +26,10 @@ func TestStringReportsLengthZeroError(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringReportsExtraDataError(t *testing.T) {
|
||||
func TestI2PStringReportsExtraDataError(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
str_len, err := String([]byte{0x01, 0x00, 0x00}).Length()
|
||||
str_len, err := I2PString([]byte{0x01, 0x00, 0x00}).Length()
|
||||
|
||||
assert.Equal(str_len, 1, "Length() reported wrong size when extra data present")
|
||||
if assert.NotNil(err) {
|
||||
@ -36,10 +37,10 @@ func TestStringReportsExtraDataError(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringDataReportsLengthZeroError(t *testing.T) {
|
||||
func TestI2PStringDataReportsLengthZeroError(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
str_len, err := String([]byte{0x01}).Length()
|
||||
str_len, err := I2PString([]byte{0x01}).Length()
|
||||
|
||||
assert.Equal(str_len, 1, "Length() reported wrong size with missing data")
|
||||
if assert.NotNil(err) {
|
||||
@ -47,10 +48,10 @@ func TestStringDataReportsLengthZeroError(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringDataReportsExtraDataError(t *testing.T) {
|
||||
func TestI2PStringDataReportsExtraDataError(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
data, err := String([]byte{0x01, 0x00, 0x01}).Data()
|
||||
data, err := I2PString([]byte{0x01, 0x00, 0x01}).Data()
|
||||
data_len := len(data)
|
||||
|
||||
assert.Equal(data_len, 1, "Data() reported wrong size on string with extra data")
|
||||
@ -59,10 +60,10 @@ func TestStringDataReportsExtraDataError(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringDataEmptyWhenZeroLength(t *testing.T) {
|
||||
func TestI2PStringDataEmptyWhenZeroLength(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
data, err := String(make([]byte, 0)).Data()
|
||||
data, err := I2PString(make([]byte, 0)).Data()
|
||||
|
||||
assert.Equal(len(data), 0, "Data() returned data when none was present:")
|
||||
if assert.NotNil(err) {
|
||||
@ -70,10 +71,10 @@ func TestStringDataEmptyWhenZeroLength(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringDataErrorWhenNonZeroLengthOnly(t *testing.T) {
|
||||
func TestI2PStringDataErrorWhenNonZeroLengthOnly(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
data, err := String([]byte{0x01}).Data()
|
||||
data, err := I2PString([]byte{0x01}).Data()
|
||||
|
||||
assert.Equal(len(data), 0, "Data() returned data when only length was present")
|
||||
if assert.NotNil(err) {
|
||||
@ -81,7 +82,7 @@ func TestStringDataErrorWhenNonZeroLengthOnly(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestToI2PStringFormatsCorrectly(t *testing.T) {
|
||||
func TestToI2PI2PStringFormatsCorrectly(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
i2p_string, err := ToI2PString(string([]byte{0x08, 0x09}))
|
||||
@ -111,38 +112,38 @@ func TestReadStringReadsLength(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
bytes := []byte{0x01, 0x04, 0x06}
|
||||
str, remainder, err := ReadString(bytes)
|
||||
str, remainder, err := ReadI2PString(bytes)
|
||||
|
||||
assert.Nil(err, "ReadString() returned error reading string with extra data")
|
||||
assert.Equal(len(str), 2, "ReadString() did not return correct string length")
|
||||
assert.Equal(1, int(str[0]), "ReadString() did not return correct string")
|
||||
assert.Equal(4, int(str[1]), "ReadString() did not return correct string")
|
||||
assert.Equal(len(remainder), 1, "ReadString() did not return correct remainder length")
|
||||
assert.Equal(6, int(remainder[0]), "ReadString() did not return correct remainder")
|
||||
assert.Nil(err, "ReadI2PString() returned error reading string with extra data")
|
||||
assert.Equal(len(str), 2, "ReadI2PString() did not return correct string length")
|
||||
assert.Equal(1, int(str[0]), "ReadI2PString() did not return correct string")
|
||||
assert.Equal(4, int(str[1]), "ReadI2PString() did not return correct string")
|
||||
assert.Equal(len(remainder), 1, "ReadI2PString() did not return correct remainder length")
|
||||
assert.Equal(6, int(remainder[0]), "ReadI2PString() did not return correct remainder")
|
||||
}
|
||||
|
||||
func TestReadStringErrWhenEmptySlice(t *testing.T) {
|
||||
func TestReadI2PStringErrWhenEmptySlice(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
bytes := make([]byte, 0)
|
||||
_, _, err := ReadString(bytes)
|
||||
_, _, err := ReadI2PString(bytes)
|
||||
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal(err.Error(), "error parsing string: zero length", "correct error message should be returned")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadStringErrWhenDataTooShort(t *testing.T) {
|
||||
func TestReadI2PStringErrWhenDataTooShort(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
short_str := []byte{0x03, 0x01}
|
||||
str, remainder, err := ReadString(short_str)
|
||||
str, remainder, err := ReadI2PString(short_str)
|
||||
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal(err.Error(), "string parsing warning: string data is shorter than specified by length", "correct error message should be returned")
|
||||
}
|
||||
assert.Equal(len(str), 2, "ReadString() did not return the slice as string when too long")
|
||||
assert.Equal(3, int(str[0]), "ReadString() did not return the correct partial string")
|
||||
assert.Equal(1, int(str[1]), "ReadString() did not return the correct partial string")
|
||||
assert.Equal(len(remainder), 0, "ReadString() returned a remainder when the string data was too short")
|
||||
assert.Equal(len(str), 2, "ReadI2PString() did not return the slice as string when too long")
|
||||
assert.Equal(3, int(str[0]), "ReadI2PString() did not return the correct partial string")
|
||||
assert.Equal(1, int(str[1]), "ReadI2PString() did not return the correct partial string")
|
||||
assert.Equal(len(remainder), 0, "ReadI2PString() returned a remainder when the string data was too short")
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
package common
|
||||
|
||||
/*
|
||||
I2P Date
|
||||
https://geti2p.net/spec/common-structures#date
|
||||
Accurate for version 0.9.24
|
||||
*/
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Date [8]byte
|
||||
|
||||
//
|
||||
// Time takes the value stored in date as an 8 byte big-endian integer representing the
|
||||
// number of milliseconds since the beginning of unix time and converts it to a Go time.Time
|
||||
// struct.
|
||||
//
|
||||
func (date Date) Time() (date_time time.Time) {
|
||||
seconds := Integer(date[:])
|
||||
date_time = time.Unix(0, int64(seconds*1000000))
|
||||
return
|
||||
}
|
@ -1,57 +0,0 @@
|
||||
package common
|
||||
|
||||
/*
|
||||
I2P Destination
|
||||
https://geti2p.net/spec/common-structures#destination
|
||||
Accurate for version 0.9.24
|
||||
|
||||
Identical to KeysAndCert
|
||||
*/
|
||||
|
||||
import (
|
||||
"github.com/go-i2p/go-i2p/lib/common/base32"
|
||||
"github.com/go-i2p/go-i2p/lib/common/base64"
|
||||
"github.com/go-i2p/go-i2p/lib/crypto"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//
|
||||
// A Destination is a KeysAndCert with functionallity
|
||||
// for generating base32 and base64 addresses.
|
||||
//
|
||||
type Destination []byte
|
||||
|
||||
func (destination Destination) PublicKey() (crypto.PublicKey, error) {
|
||||
return KeysAndCert(destination).PublicKey()
|
||||
}
|
||||
|
||||
func (destination Destination) SigningPublicKey() (crypto.SigningPublicKey, error) {
|
||||
return KeysAndCert(destination).SigningPublicKey()
|
||||
}
|
||||
|
||||
func (destination Destination) Certificate() (Certificate, error) {
|
||||
return KeysAndCert(destination).Certificate()
|
||||
}
|
||||
|
||||
func ReadDestination(data []byte) (destination Destination, remainder []byte, err error) {
|
||||
keys_and_cert, remainder, err := ReadKeysAndCert(data)
|
||||
destination = Destination(keys_and_cert)
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Generate the I2P base32 address for this Destination.
|
||||
//
|
||||
func (destination Destination) Base32Address() (str string) {
|
||||
hash := crypto.SHA256(destination)
|
||||
str = strings.Trim(base32.EncodeToString(hash[:]), "=")
|
||||
str = str + ".b32.i2p"
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Generate the I2P base64 address for this Destination.
|
||||
//
|
||||
func (destination Destination) Base64() string {
|
||||
return base64.EncodeToString(destination)
|
||||
}
|
87
lib/common/destination/destination.go
Normal file
87
lib/common/destination/destination.go
Normal file
@ -0,0 +1,87 @@
|
||||
// Package destination implements the I2P Destination common data structure
|
||||
package destination
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/go-i2p/go-i2p/lib/util/logger"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
. "github.com/go-i2p/go-i2p/lib/common/keys_and_cert"
|
||||
|
||||
"github.com/go-i2p/go-i2p/lib/common/base32"
|
||||
"github.com/go-i2p/go-i2p/lib/common/base64"
|
||||
"github.com/go-i2p/go-i2p/lib/crypto"
|
||||
)
|
||||
|
||||
var log = logger.GetGoI2PLogger()
|
||||
|
||||
/*
|
||||
[Destination]
|
||||
Accurate for version 0.9.49
|
||||
|
||||
Description
|
||||
A Destination defines a particular endpoint to which messages can be directed for secure delivery.
|
||||
|
||||
Contents
|
||||
Identical to KeysAndCert.
|
||||
*/
|
||||
|
||||
// Destination is the represenation of an I2P Destination.
|
||||
//
|
||||
// https://geti2p.net/spec/common-structures#destination
|
||||
type Destination struct {
|
||||
KeysAndCert
|
||||
}
|
||||
|
||||
// Base32Address returns the I2P base32 address for this Destination.
|
||||
func (destination Destination) Base32Address() (str string) {
|
||||
log.Debug("Generating Base32 address for Destination")
|
||||
|
||||
cert := destination.KeysAndCert.Certificate()
|
||||
dest := cert.Bytes()
|
||||
hash := crypto.SHA256(dest)
|
||||
str = strings.Trim(base32.EncodeToString(hash[:]), "=")
|
||||
str = str + ".b32.i2p"
|
||||
|
||||
log.WithFields(logrus.Fields{
|
||||
"base32_address": str,
|
||||
}).Debug("Generated Base32 address for Destination")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Base64 returns the I2P base64 address for this Destination.
|
||||
func (destination Destination) Base64() string {
|
||||
log.Debug("Generating Base64 address for Destination")
|
||||
|
||||
cert := destination.KeysAndCert.Certificate()
|
||||
dest := cert.Bytes()
|
||||
base64Address := base64.EncodeToString(dest)
|
||||
|
||||
log.WithFields(logrus.Fields{
|
||||
"base64_address_length": len(base64Address),
|
||||
}).Debug("Generated Base64 address for Destination")
|
||||
|
||||
return base64Address
|
||||
}
|
||||
|
||||
// ReadDestination returns Destination from a []byte.
|
||||
// The remaining bytes after the specified length are also returned.
|
||||
// Returns a list of errors that occurred during parsing.
|
||||
func ReadDestination(data []byte) (destination Destination, remainder []byte, err error) {
|
||||
log.WithFields(logrus.Fields{
|
||||
"input_length": len(data),
|
||||
}).Debug("Reading Destination from bytes")
|
||||
|
||||
keys_and_cert, remainder, err := ReadKeysAndCert(data)
|
||||
destination = Destination{
|
||||
keys_and_cert,
|
||||
}
|
||||
|
||||
log.WithFields(logrus.Fields{
|
||||
"remainder_length": len(remainder),
|
||||
}).Debug("Successfully read Destination from bytes")
|
||||
|
||||
return
|
||||
}
|
42
lib/common/destination/doc.md
Normal file
42
lib/common/destination/doc.md
Normal file
@ -0,0 +1,42 @@
|
||||
# destination
|
||||
--
|
||||
import "github.com/go-i2p/go-i2p/lib/common/destination"
|
||||
|
||||
Package destination implements the I2P Destination common data structure
|
||||
|
||||
## Usage
|
||||
|
||||
#### type Destination
|
||||
|
||||
```go
|
||||
type Destination struct {
|
||||
KeysAndCert
|
||||
}
|
||||
```
|
||||
|
||||
Destination is the represenation of an I2P Destination.
|
||||
|
||||
https://geti2p.net/spec/common-structures#destination
|
||||
|
||||
#### func ReadDestination
|
||||
|
||||
```go
|
||||
func ReadDestination(data []byte) (destination Destination, remainder []byte, err error)
|
||||
```
|
||||
ReadDestination returns Destination from a []byte. The remaining bytes after the
|
||||
specified length are also returned. Returns a list of errors that occurred
|
||||
during parsing.
|
||||
|
||||
#### func (Destination) Base32Address
|
||||
|
||||
```go
|
||||
func (destination Destination) Base32Address() (str string)
|
||||
```
|
||||
Base32Address returns the I2P base32 address for this Destination.
|
||||
|
||||
#### func (Destination) Base64
|
||||
|
||||
```go
|
||||
func (destination Destination) Base64() string
|
||||
```
|
||||
Base64 returns the I2P base64 address for this Destination.
|
@ -1,8 +1,7 @@
|
||||
FROM golang
|
||||
|
||||
RUN apt-get update && \
|
||||
apt-get upgrade -y && \
|
||||
apt-get install libsodium-dev -y
|
||||
apt-get upgrade -y
|
||||
|
||||
RUN go get github.com/dvyukov/go-fuzz/go-fuzz
|
||||
RUN go get github.com/dvyukov/go-fuzz/go-fuzz-build
|
||||
|
12
lib/common/fuzz/certificate/doc.md
Normal file
12
lib/common/fuzz/certificate/doc.md
Normal file
@ -0,0 +1,12 @@
|
||||
# exportable
|
||||
--
|
||||
import "github.com/go-i2p/go-i2p/lib/common/fuzz/certificate"
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
#### func Fuzz
|
||||
|
||||
```go
|
||||
func Fuzz(data []byte) int
|
||||
```
|
@ -1,9 +1,9 @@
|
||||
package exportable
|
||||
|
||||
import "github.com/go-i2p/go-i2p/lib/common"
|
||||
import common "github.com/go-i2p/go-i2p/lib/common/certificate"
|
||||
|
||||
func Fuzz(data []byte) int {
|
||||
cert := common.Certificate(data)
|
||||
cert, _, _ := common.ReadCertificate(data)
|
||||
cert.Data()
|
||||
cert.Length()
|
||||
cert.Type()
|
||||
|
12
lib/common/fuzz/destination/doc.md
Normal file
12
lib/common/fuzz/destination/doc.md
Normal file
@ -0,0 +1,12 @@
|
||||
# exportable
|
||||
--
|
||||
import "github.com/go-i2p/go-i2p/lib/common/fuzz/destination"
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
#### func Fuzz
|
||||
|
||||
```go
|
||||
func Fuzz(data []byte) int
|
||||
```
|
@ -1,9 +1,9 @@
|
||||
package exportable
|
||||
|
||||
import "github.com/go-i2p/go-i2p/lib/common"
|
||||
import common "github.com/go-i2p/go-i2p/lib/common/destination"
|
||||
|
||||
func Fuzz(data []byte) int {
|
||||
destination := common.Destination(data)
|
||||
destination, _, _ := common.ReadDestination(data)
|
||||
destination.Base32Address()
|
||||
destination.Base64()
|
||||
return 0
|
||||
|
12
lib/common/fuzz/keys_and_cert/doc.md
Normal file
12
lib/common/fuzz/keys_and_cert/doc.md
Normal file
@ -0,0 +1,12 @@
|
||||
# exportable
|
||||
--
|
||||
import "github.com/go-i2p/go-i2p/lib/common/fuzz/keys_and_cert"
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
#### func Fuzz
|
||||
|
||||
```go
|
||||
func Fuzz(data []byte) int
|
||||
```
|
@ -1,6 +1,6 @@
|
||||
package exportable
|
||||
|
||||
import "github.com/go-i2p/go-i2p/lib/common"
|
||||
import common "github.com/go-i2p/go-i2p/lib/common/keys_and_cert"
|
||||
|
||||
func Fuzz(data []byte) int {
|
||||
keys_and_cert, _, _ := common.ReadKeysAndCert(data)
|
||||
|
12
lib/common/fuzz/router_address/doc.md
Normal file
12
lib/common/fuzz/router_address/doc.md
Normal file
@ -0,0 +1,12 @@
|
||||
# exportable
|
||||
--
|
||||
import "github.com/go-i2p/go-i2p/lib/common/fuzz/router_address"
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
#### func Fuzz
|
||||
|
||||
```go
|
||||
func Fuzz(data []byte) int
|
||||
```
|
@ -1,6 +1,6 @@
|
||||
package exportable
|
||||
|
||||
import "github.com/go-i2p/go-i2p/lib/common"
|
||||
import common "github.com/go-i2p/go-i2p/lib/common/router_address"
|
||||
|
||||
func Fuzz(data []byte) int {
|
||||
router_address, _, _ := common.ReadRouterAddress(data)
|
||||
|
12
lib/common/fuzz/router_identity/doc.md
Normal file
12
lib/common/fuzz/router_identity/doc.md
Normal file
@ -0,0 +1,12 @@
|
||||
# exportable
|
||||
--
|
||||
import "github.com/go-i2p/go-i2p/lib/common/fuzz/router_identity"
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
#### func Fuzz
|
||||
|
||||
```go
|
||||
func Fuzz(data []byte) int
|
||||
```
|
@ -1,11 +1,11 @@
|
||||
package exportable
|
||||
|
||||
import "github.com/go-i2p/go-i2p/lib/common"
|
||||
import common "github.com/go-i2p/go-i2p/lib/common/router_identity"
|
||||
|
||||
func Fuzz(data []byte) int {
|
||||
router_identity, _, _ := common.ReadRouterIdentity(data)
|
||||
router_identity.Certificate()
|
||||
router_identity.PublicKey()
|
||||
router_identity.SigningPublicKey()
|
||||
// router_identity.publicKey()
|
||||
// router_identity.signingPublicKey()
|
||||
return 0
|
||||
}
|
||||
|
12
lib/common/fuzz/string/doc.md
Normal file
12
lib/common/fuzz/string/doc.md
Normal file
@ -0,0 +1,12 @@
|
||||
# exportable
|
||||
--
|
||||
import "github.com/go-i2p/go-i2p/lib/common/fuzz/string"
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
#### func Fuzz
|
||||
|
||||
```go
|
||||
func Fuzz(data []byte) int
|
||||
```
|
@ -1,9 +1,9 @@
|
||||
package exportable
|
||||
|
||||
import "github.com/go-i2p/go-i2p/lib/common"
|
||||
import common "github.com/go-i2p/go-i2p/lib/common/data"
|
||||
|
||||
func Fuzz(data []byte) int {
|
||||
str, _, _ := common.ReadString(data)
|
||||
str := common.I2PString(data)
|
||||
str.Data()
|
||||
str.Length()
|
||||
str, _ = common.ToI2PString(string(data))
|
||||
|
@ -1,27 +0,0 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"io"
|
||||
)
|
||||
|
||||
// sha256 hash of some data
|
||||
type Hash [32]byte
|
||||
|
||||
// calculate sha256 of a byte slice
|
||||
func HashData(data []byte) (h Hash) {
|
||||
h = sha256.Sum256(data)
|
||||
return
|
||||
}
|
||||
|
||||
// calulate sha256 of all data being read from an io.Reader
|
||||
// return error if one occurs while reading from reader
|
||||
func HashReader(r io.Reader) (h Hash, err error) {
|
||||
sha := sha256.New()
|
||||
_, err = io.Copy(sha, r)
|
||||
if err == nil {
|
||||
d := sha.Sum(nil)
|
||||
copy(h[:], d)
|
||||
}
|
||||
return
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
package common
|
||||
|
||||
/*
|
||||
I2P Integer
|
||||
https://geti2p.net/spec/common-structures#integer
|
||||
Accurate for version 0.9.24
|
||||
*/
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
)
|
||||
|
||||
// Total byte length of an I2P integer
|
||||
const (
|
||||
INTEGER_SIZE = 8
|
||||
)
|
||||
|
||||
//
|
||||
// Interpret a slice of bytes from length 0 to length 8 as a big-endian
|
||||
// integer and return an int representation.
|
||||
//
|
||||
func Integer(number []byte) (value int) {
|
||||
num_len := len(number)
|
||||
if num_len < INTEGER_SIZE {
|
||||
number = append(
|
||||
make([]byte, INTEGER_SIZE-num_len),
|
||||
number...,
|
||||
)
|
||||
}
|
||||
value = int(binary.BigEndian.Uint64(number))
|
||||
return
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIntegerBigEndian(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
bytes := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}
|
||||
integer := Integer(bytes)
|
||||
|
||||
assert.Equal(integer, 1, "Integer() did not parse bytes big endian")
|
||||
}
|
||||
|
||||
func TestWorksWithOneByte(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
integer := Integer([]byte{0x01})
|
||||
|
||||
assert.Equal(integer, 1, "Integer() did not correctly parse single byte slice")
|
||||
}
|
||||
|
||||
func TestIsZeroWithNoData(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
integer := Integer([]byte{})
|
||||
|
||||
assert.Equal(integer, 0, "Integer() did not correctly parse zero length byte slice")
|
||||
}
|
@ -1,246 +0,0 @@
|
||||
package common
|
||||
|
||||
/*
|
||||
I2P Key Certificate
|
||||
https://geti2p.net/spec/common-structures#certificate
|
||||
Accurate for version 0.9.24
|
||||
|
||||
+----+----+----+----+----+-//
|
||||
|type| length | payload
|
||||
+----+----+----+----+----+-//
|
||||
|
||||
type :: Integer
|
||||
length -> 1 byte
|
||||
|
||||
case 0 -> NULL
|
||||
case 1 -> HASHCASH
|
||||
case 2 -> HIDDEN
|
||||
case 3 -> SIGNED
|
||||
case 4 -> MULTIPLE
|
||||
case 5 -> KEY
|
||||
|
||||
length :: Integer
|
||||
length -> 2 bytes
|
||||
|
||||
payload :: data
|
||||
length -> $length bytes
|
||||
*/
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/go-i2p/go-i2p/lib/crypto"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Key Certificate Signing Key Types
|
||||
const (
|
||||
KEYCERT_SIGN_DSA_SHA1 = iota
|
||||
KEYCERT_SIGN_P256
|
||||
KEYCERT_SIGN_P384
|
||||
KEYCERT_SIGN_P521
|
||||
KEYCERT_SIGN_RSA2048
|
||||
KEYCERT_SIGN_RSA3072
|
||||
KEYCERT_SIGN_RSA4096
|
||||
KEYCERT_SIGN_ED25519
|
||||
KEYCERT_SIGN_ED25519PH
|
||||
)
|
||||
|
||||
// Key Certificate Public Key Types
|
||||
const (
|
||||
KEYCERT_CRYPTO_ELG = iota
|
||||
)
|
||||
|
||||
// SigningPublicKey sizes for Signing Key Types
|
||||
const (
|
||||
KEYCERT_SIGN_DSA_SHA1_SIZE = 128
|
||||
KEYCERT_SIGN_P256_SIZE = 64
|
||||
KEYCERT_SIGN_P384_SIZE = 96
|
||||
KEYCERT_SIGN_P521_SIZE = 132
|
||||
KEYCERT_SIGN_RSA2048_SIZE = 256
|
||||
KEYCERT_SIGN_RSA3072_SIZE = 384
|
||||
KEYCERT_SIGN_RSA4096_SIZE = 512
|
||||
KEYCERT_SIGN_ED25519_SIZE = 32
|
||||
KEYCERT_SIGN_ED25519PH_SIZE = 32
|
||||
)
|
||||
|
||||
// PublicKey sizes for Public Key Types
|
||||
const (
|
||||
KEYCERT_CRYPTO_ELG_SIZE = 256
|
||||
)
|
||||
|
||||
// Sizes of structures in KeyCertificates
|
||||
const (
|
||||
KEYCERT_PUBKEY_SIZE = 256
|
||||
KEYCERT_SPK_SIZE = 128
|
||||
)
|
||||
|
||||
type KeyCertificate []byte
|
||||
|
||||
//
|
||||
// The data contained in the Key Certificate.
|
||||
//
|
||||
func (key_certificate KeyCertificate) Data() ([]byte, error) {
|
||||
return Certificate(key_certificate).Data()
|
||||
}
|
||||
|
||||
//
|
||||
// The SigningPublicKey type this Key Certificate describes and any errors encountered
|
||||
// parsing the KeyCertificate.
|
||||
//
|
||||
func (key_certificate KeyCertificate) SigningPublicKeyType() (signing_pubkey_type int, err error) {
|
||||
data, err := key_certificate.Data()
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(KeyCertificate) SigningPublicKeyType",
|
||||
"reason": err.Error(),
|
||||
}).Error("error getting signing public key")
|
||||
return
|
||||
}
|
||||
data_len := len(data)
|
||||
if data_len < 2 {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(KeyCertificate) SigningPublicKeyType",
|
||||
"data_len": data_len,
|
||||
"required_len": 2,
|
||||
"reason": "not enough data",
|
||||
}).Error("error parsing key certificate")
|
||||
err = errors.New("error parsing key certificate: not enough data")
|
||||
return
|
||||
}
|
||||
signing_pubkey_type = Integer(data[:2])
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// The PublicKey type this Key Certificate describes and any errors encountered parsing
|
||||
// this KeyCertificate.
|
||||
//
|
||||
func (key_certificate KeyCertificate) PublicKeyType() (pubkey_type int, err error) {
|
||||
data, err := key_certificate.Data()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
data_len := len(data)
|
||||
if data_len < 4 {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(KeyCertificate) PublicKeyType",
|
||||
"data_len": data_len,
|
||||
"required_len": 4,
|
||||
"reason": "not enough data",
|
||||
}).Error("error parsing key certificate")
|
||||
err = errors.New("error parsing key certificate: not enough data")
|
||||
return
|
||||
}
|
||||
pubkey_type = Integer(data[2:4])
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Given some bytes, build a PublicKey using any excess data that may be stored in the KeyCertificate and return
|
||||
// it along with any errors encountered constructing the PublicKey.
|
||||
//
|
||||
func (key_certificate KeyCertificate) ConstructPublicKey(data []byte) (public_key crypto.PublicKey, err error) {
|
||||
key_type, err := key_certificate.PublicKeyType()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
data_len := len(data)
|
||||
if data_len < KEYCERT_PUBKEY_SIZE {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(KeyCertificate) ConstructPublicKey",
|
||||
"data_len": data_len,
|
||||
"required_len": KEYCERT_PUBKEY_SIZE,
|
||||
"reason": "not enough data",
|
||||
}).Error("error constructing public key")
|
||||
err = errors.New("error constructing public key: not enough data")
|
||||
return
|
||||
}
|
||||
switch key_type {
|
||||
case KEYCERT_CRYPTO_ELG:
|
||||
var elg_key crypto.ElgPublicKey
|
||||
copy(elg_key[:], data[KEYCERT_PUBKEY_SIZE-KEYCERT_CRYPTO_ELG_SIZE:KEYCERT_PUBKEY_SIZE])
|
||||
public_key = elg_key
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Given some bytes, build a SigningPublicKey using any excess data that may be stored in the KeyCertificate and return
|
||||
// it along with any errors encountered constructing the SigningPublicKey.
|
||||
//
|
||||
func (key_certificate KeyCertificate) ConstructSigningPublicKey(data []byte) (signing_public_key crypto.SigningPublicKey, err error) {
|
||||
signing_key_type, err := key_certificate.PublicKeyType()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
data_len := len(data)
|
||||
if data_len < KEYCERT_SPK_SIZE {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(KeyCertificate) ConstructSigningPublicKey",
|
||||
"data_len": data_len,
|
||||
"required_len": KEYCERT_SPK_SIZE,
|
||||
"reason": "not enough data",
|
||||
}).Error("error constructing signing public key")
|
||||
err = errors.New("error constructing signing public key: not enough data")
|
||||
return
|
||||
}
|
||||
switch signing_key_type {
|
||||
case KEYCERT_SIGN_DSA_SHA1:
|
||||
var dsa_key crypto.DSAPublicKey
|
||||
copy(dsa_key[:], data[KEYCERT_SPK_SIZE-KEYCERT_SIGN_DSA_SHA1_SIZE:KEYCERT_SPK_SIZE])
|
||||
signing_public_key = dsa_key
|
||||
case KEYCERT_SIGN_P256:
|
||||
var ec_key crypto.ECP256PublicKey
|
||||
copy(ec_key[:], data[KEYCERT_SPK_SIZE-KEYCERT_SIGN_P256_SIZE:KEYCERT_SPK_SIZE])
|
||||
signing_public_key = ec_key
|
||||
case KEYCERT_SIGN_P384:
|
||||
var ec_key crypto.ECP384PublicKey
|
||||
copy(ec_key[:], data[KEYCERT_SPK_SIZE-KEYCERT_SIGN_P384_SIZE:KEYCERT_SPK_SIZE])
|
||||
signing_public_key = ec_key
|
||||
case KEYCERT_SIGN_P521:
|
||||
var ec_key crypto.ECP521PublicKey
|
||||
extra := KEYCERT_SIGN_P521_SIZE - KEYCERT_SPK_SIZE
|
||||
copy(ec_key[:], data)
|
||||
copy(ec_key[KEYCERT_SPK_SIZE:], key_certificate[4:4+extra])
|
||||
signing_public_key = ec_key
|
||||
case KEYCERT_SIGN_RSA2048:
|
||||
//var rsa_key crypto.RSA2048PublicKey
|
||||
//extra := KEYCERT_SIGN_RSA2048_SIZE - 128
|
||||
//copy(rsa_key[:], data)
|
||||
//copy(rsa_key[128:], key_certificate[4:4+extra])
|
||||
//signing_public_key = rsa_key
|
||||
case KEYCERT_SIGN_RSA3072:
|
||||
case KEYCERT_SIGN_RSA4096:
|
||||
case KEYCERT_SIGN_ED25519:
|
||||
case KEYCERT_SIGN_ED25519PH:
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Return the size of a Signature corresponding to the Key Certificate's
|
||||
// SigningPublicKey type.
|
||||
//
|
||||
func (key_certificate KeyCertificate) SignatureSize() (size int) {
|
||||
sizes := map[int]int{
|
||||
KEYCERT_SIGN_DSA_SHA1: 40,
|
||||
KEYCERT_SIGN_P256: 64,
|
||||
KEYCERT_SIGN_P384: 96,
|
||||
KEYCERT_SIGN_P521: 132,
|
||||
KEYCERT_SIGN_RSA2048: 256,
|
||||
KEYCERT_SIGN_RSA3072: 384,
|
||||
KEYCERT_SIGN_RSA4096: 512,
|
||||
KEYCERT_SIGN_ED25519: 64,
|
||||
KEYCERT_SIGN_ED25519PH: 64,
|
||||
}
|
||||
key_type, err := key_certificate.SigningPublicKeyType()
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(KeyCertificate) SignatureSize",
|
||||
"key_type": key_type,
|
||||
"reason": "failed to read signing public key type",
|
||||
}).Error("error getting signature size")
|
||||
return 0
|
||||
}
|
||||
return sizes[int(key_type)]
|
||||
}
|
154
lib/common/key_certificate/doc.md
Normal file
154
lib/common/key_certificate/doc.md
Normal file
@ -0,0 +1,154 @@
|
||||
# key_certificate
|
||||
--
|
||||
import "github.com/go-i2p/go-i2p/lib/common/key_certificate"
|
||||
|
||||
Package key_certificate implements the I2P Destination common data structure
|
||||
|
||||
## Usage
|
||||
|
||||
```go
|
||||
const (
|
||||
KEYCERT_SIGN_DSA_SHA1 = iota
|
||||
KEYCERT_SIGN_P256
|
||||
KEYCERT_SIGN_P384
|
||||
KEYCERT_SIGN_P521
|
||||
KEYCERT_SIGN_RSA2048
|
||||
KEYCERT_SIGN_RSA3072
|
||||
KEYCERT_SIGN_RSA4096
|
||||
KEYCERT_SIGN_ED25519
|
||||
KEYCERT_SIGN_ED25519PH
|
||||
)
|
||||
```
|
||||
Key Certificate Signing Key Types
|
||||
|
||||
```go
|
||||
const (
|
||||
KEYCERT_CRYPTO_ELG = iota
|
||||
KEYCERT_CRYPTO_P256
|
||||
KEYCERT_CRYPTO_P384
|
||||
KEYCERT_CRYPTO_P521
|
||||
KEYCERT_CRYPTO_X25519
|
||||
)
|
||||
```
|
||||
Key Certificate Public Key Types
|
||||
|
||||
```go
|
||||
const (
|
||||
KEYCERT_SIGN_DSA_SHA1_SIZE = 128
|
||||
KEYCERT_SIGN_P256_SIZE = 64
|
||||
KEYCERT_SIGN_P384_SIZE = 96
|
||||
KEYCERT_SIGN_P521_SIZE = 132
|
||||
KEYCERT_SIGN_RSA2048_SIZE = 256
|
||||
KEYCERT_SIGN_RSA3072_SIZE = 384
|
||||
KEYCERT_SIGN_RSA4096_SIZE = 512
|
||||
KEYCERT_SIGN_ED25519_SIZE = 32
|
||||
KEYCERT_SIGN_ED25519PH_SIZE = 32
|
||||
)
|
||||
```
|
||||
SigningPublicKey sizes for Signing Key Types
|
||||
|
||||
```go
|
||||
const (
|
||||
KEYCERT_CRYPTO_ELG_SIZE = 256
|
||||
KEYCERT_CRYPTO_P256_SIZE = 64
|
||||
KEYCERT_CRYPTO_P384_SIZE = 96
|
||||
KEYCERT_CRYPTO_P521_SIZE = 132
|
||||
KEYCERT_CRYPTO_X25519_SIZE = 32
|
||||
)
|
||||
```
|
||||
PublicKey sizes for Public Key Types
|
||||
|
||||
```go
|
||||
const (
|
||||
KEYCERT_PUBKEY_SIZE = 256
|
||||
KEYCERT_SPK_SIZE = 128
|
||||
)
|
||||
```
|
||||
Sizes of structures in KeyCertificates
|
||||
|
||||
```go
|
||||
const (
|
||||
KEYCERT_MIN_SIZE = 7
|
||||
)
|
||||
```
|
||||
|
||||
#### type KeyCertificate
|
||||
|
||||
```go
|
||||
type KeyCertificate struct {
|
||||
Certificate
|
||||
}
|
||||
```
|
||||
|
||||
type KeyCertificate []byte
|
||||
|
||||
#### func KeyCertificateFromCertificate
|
||||
|
||||
```go
|
||||
func KeyCertificateFromCertificate(certificate Certificate) *KeyCertificate
|
||||
```
|
||||
KeyCertificateFromCertificate returns a *KeyCertificate from a *Certificate.
|
||||
|
||||
#### func NewKeyCertificate
|
||||
|
||||
```go
|
||||
func NewKeyCertificate(bytes []byte) (key_certificate *KeyCertificate, remainder []byte, err error)
|
||||
```
|
||||
NewKeyCertificate creates a new *KeyCertificate from []byte using
|
||||
ReadCertificate. The remaining bytes after the specified length are also
|
||||
returned. Returns a list of errors that occurred during parsing.
|
||||
|
||||
#### func (KeyCertificate) ConstructPublicKey
|
||||
|
||||
```go
|
||||
func (key_certificate KeyCertificate) ConstructPublicKey(data []byte) (public_key crypto.PublicKey, err error)
|
||||
```
|
||||
ConstructPublicKey returns a PublicKey constructed using any excess data that
|
||||
may be stored in the KeyCertififcate. Returns enr errors encountered while
|
||||
parsing.
|
||||
|
||||
#### func (KeyCertificate) ConstructSigningPublicKey
|
||||
|
||||
```go
|
||||
func (key_certificate KeyCertificate) ConstructSigningPublicKey(data []byte) (signing_public_key crypto.SigningPublicKey, err error)
|
||||
```
|
||||
ConstructSigningPublicKey returns a SingingPublicKey constructed using any
|
||||
excess data that may be stored in the KeyCertificate. Returns any errors
|
||||
encountered while parsing.
|
||||
|
||||
#### func (KeyCertificate) CryptoSize
|
||||
|
||||
```go
|
||||
func (key_certificate KeyCertificate) CryptoSize() (size int)
|
||||
```
|
||||
CryptoSize return the size of a Public Key corresponding to the Key
|
||||
Certificate's PublicKey type.
|
||||
|
||||
#### func (KeyCertificate) Data
|
||||
|
||||
```go
|
||||
func (key_certificate KeyCertificate) Data() ([]byte, error)
|
||||
```
|
||||
Data returns the raw []byte contained in the Certificate.
|
||||
|
||||
#### func (KeyCertificate) PublicKeyType
|
||||
|
||||
```go
|
||||
func (key_certificate KeyCertificate) PublicKeyType() (pubkey_type int)
|
||||
```
|
||||
PublicKeyType returns the PublicKey type as a Go integer.
|
||||
|
||||
#### func (KeyCertificate) SignatureSize
|
||||
|
||||
```go
|
||||
func (key_certificate KeyCertificate) SignatureSize() (size int)
|
||||
```
|
||||
SignatureSize return the size of a Signature corresponding to the Key
|
||||
Certificate's SigningPublicKey type.
|
||||
|
||||
#### func (KeyCertificate) SigningPublicKeyType
|
||||
|
||||
```go
|
||||
func (key_certificate KeyCertificate) SigningPublicKeyType() (signing_pubkey_type int)
|
||||
```
|
||||
SigningPublicKeyType returns the SigningPublicKey type as a Go integer.
|
337
lib/common/key_certificate/key_certificate.go
Normal file
337
lib/common/key_certificate/key_certificate.go
Normal file
@ -0,0 +1,337 @@
|
||||
// Package key_certificate implements the I2P Destination common data structure
|
||||
package key_certificate
|
||||
|
||||
/*
|
||||
I2P Key Certificate
|
||||
https://geti2p.net/spec/common-structures#certificate
|
||||
Accurate for version 0.9.24
|
||||
|
||||
+----+----+----+----+----+-//
|
||||
|type| length | payload
|
||||
+----+----+----+----+----+-//
|
||||
|
||||
type :: Integer
|
||||
length -> 1 byte
|
||||
|
||||
case 0 -> NULL
|
||||
case 1 -> HASHCASH
|
||||
case 2 -> HIDDEN
|
||||
case 3 -> SIGNED
|
||||
case 4 -> MULTIPLE
|
||||
case 5 -> KEY
|
||||
|
||||
length :: Integer
|
||||
length -> 2 bytes
|
||||
|
||||
payload :: data
|
||||
length -> $length bytes
|
||||
*/
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/go-i2p/go-i2p/lib/util/logger"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
. "github.com/go-i2p/go-i2p/lib/common/certificate"
|
||||
. "github.com/go-i2p/go-i2p/lib/common/data"
|
||||
"github.com/go-i2p/go-i2p/lib/crypto"
|
||||
)
|
||||
|
||||
var log = logger.GetGoI2PLogger()
|
||||
|
||||
// Key Certificate Signing Key Types
|
||||
const (
|
||||
KEYCERT_SIGN_DSA_SHA1 = iota
|
||||
KEYCERT_SIGN_P256
|
||||
KEYCERT_SIGN_P384
|
||||
KEYCERT_SIGN_P521
|
||||
KEYCERT_SIGN_RSA2048
|
||||
KEYCERT_SIGN_RSA3072
|
||||
KEYCERT_SIGN_RSA4096
|
||||
KEYCERT_SIGN_ED25519
|
||||
KEYCERT_SIGN_ED25519PH
|
||||
)
|
||||
|
||||
// Key Certificate Public Key Types
|
||||
const (
|
||||
KEYCERT_CRYPTO_ELG = iota
|
||||
KEYCERT_CRYPTO_P256
|
||||
KEYCERT_CRYPTO_P384
|
||||
KEYCERT_CRYPTO_P521
|
||||
KEYCERT_CRYPTO_X25519
|
||||
)
|
||||
|
||||
const (
|
||||
KEYCERT_MIN_SIZE = 7
|
||||
)
|
||||
|
||||
// signingPublicKey sizes for Signing Key Types
|
||||
const (
|
||||
KEYCERT_SIGN_DSA_SHA1_SIZE = 128
|
||||
KEYCERT_SIGN_P256_SIZE = 64
|
||||
KEYCERT_SIGN_P384_SIZE = 96
|
||||
KEYCERT_SIGN_P521_SIZE = 132
|
||||
KEYCERT_SIGN_RSA2048_SIZE = 256
|
||||
KEYCERT_SIGN_RSA3072_SIZE = 384
|
||||
KEYCERT_SIGN_RSA4096_SIZE = 512
|
||||
KEYCERT_SIGN_ED25519_SIZE = 32
|
||||
KEYCERT_SIGN_ED25519PH_SIZE = 32
|
||||
)
|
||||
|
||||
// publicKey sizes for Public Key Types
|
||||
const (
|
||||
KEYCERT_CRYPTO_ELG_SIZE = 256
|
||||
KEYCERT_CRYPTO_P256_SIZE = 64
|
||||
KEYCERT_CRYPTO_P384_SIZE = 96
|
||||
KEYCERT_CRYPTO_P521_SIZE = 132
|
||||
KEYCERT_CRYPTO_X25519_SIZE = 32
|
||||
)
|
||||
|
||||
// Sizes of structures in KeyCertificates
|
||||
const (
|
||||
KEYCERT_PUBKEY_SIZE = 256
|
||||
KEYCERT_SPK_SIZE = 128
|
||||
)
|
||||
|
||||
// type KeyCertificate []byte
|
||||
type KeyCertificate struct {
|
||||
Certificate
|
||||
spkType Integer
|
||||
cpkType Integer
|
||||
}
|
||||
|
||||
// Data returns the raw []byte contained in the Certificate.
|
||||
func (key_certificate KeyCertificate) Data() ([]byte, error) {
|
||||
data := key_certificate.Certificate.RawBytes()
|
||||
log.WithFields(logrus.Fields{
|
||||
"data_length": len(data),
|
||||
}).Debug("Retrieved raw data from keyCertificate")
|
||||
return key_certificate.Certificate.RawBytes(), nil
|
||||
}
|
||||
|
||||
// SigningPublicKeyType returns the signingPublicKey type as a Go integer.
|
||||
func (key_certificate KeyCertificate) SigningPublicKeyType() int {
|
||||
spk_type := key_certificate.spkType.Int()
|
||||
log.WithFields(logrus.Fields{
|
||||
"signing_pubkey_type": spk_type,
|
||||
}).Debug("Retrieved signingPublicKey type")
|
||||
return spk_type
|
||||
}
|
||||
|
||||
func (key_certificate KeyCertificate) CryptoSize() int {
|
||||
switch key_certificate.PublicKeyType() {
|
||||
case KEYCERT_CRYPTO_ELG:
|
||||
return KEYCERT_CRYPTO_ELG_SIZE
|
||||
case KEYCERT_CRYPTO_P256:
|
||||
return KEYCERT_CRYPTO_P256_SIZE
|
||||
case KEYCERT_CRYPTO_P384:
|
||||
return KEYCERT_CRYPTO_P384_SIZE
|
||||
case KEYCERT_CRYPTO_P521:
|
||||
return KEYCERT_CRYPTO_P521_SIZE
|
||||
case KEYCERT_CRYPTO_X25519:
|
||||
return KEYCERT_CRYPTO_X25519_SIZE
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func (key_certificate KeyCertificate) ConstructPublicKey(data []byte) (public_key crypto.PublicKey, err error) {
|
||||
log.WithFields(logrus.Fields{
|
||||
"input_length": len(data),
|
||||
}).Debug("Constructing publicKey from keyCertificate")
|
||||
|
||||
key_type := key_certificate.PublicKeyType()
|
||||
|
||||
data_len := len(data)
|
||||
if data_len < key_certificate.CryptoSize() {
|
||||
return nil, errors.New("error constructing public key: not enough data")
|
||||
}
|
||||
|
||||
// Implementation missing here - needs to construct appropriate key type
|
||||
switch key_type {
|
||||
case KEYCERT_CRYPTO_ELG:
|
||||
var elg_key crypto.ElgPublicKey
|
||||
copy(elg_key[:], data[KEYCERT_PUBKEY_SIZE-KEYCERT_CRYPTO_ELG_SIZE:KEYCERT_PUBKEY_SIZE])
|
||||
public_key = elg_key
|
||||
log.Debug("Constructed ElgPublicKey")
|
||||
case KEYCERT_CRYPTO_X25519:
|
||||
var ed25519_key crypto.Ed25519PublicKey
|
||||
copy(ed25519_key[:], data[KEYCERT_PUBKEY_SIZE-KEYCERT_CRYPTO_ELG_SIZE:KEYCERT_PUBKEY_SIZE])
|
||||
public_key = ed25519_key
|
||||
log.Debug("Constructed Ed25519PublicKey")
|
||||
case KEYCERT_CRYPTO_P256:
|
||||
//return crypto.CreatePublicKey(data[:KEYCERT_CRYPTO_P256_SIZE])
|
||||
case KEYCERT_CRYPTO_P384:
|
||||
//return crypto.CreatePublicKey(data[:KEYCERT_CRYPTO_P384_SIZE])
|
||||
case KEYCERT_CRYPTO_P521:
|
||||
//return crypto.CreatePublicKey(data[:KEYCERT_CRYPTO_P521_SIZE])
|
||||
default:
|
||||
return nil, errors.New("error constructing public key: unknown key type")
|
||||
}
|
||||
return nil, errors.New("error constructing public key: unknown key type")
|
||||
}
|
||||
|
||||
// PublicKeyType returns the publicKey type as a Go integer.
|
||||
func (key_certificate KeyCertificate) PublicKeyType() int {
|
||||
pk_type := key_certificate.cpkType.Int()
|
||||
log.WithFields(logrus.Fields{
|
||||
"pubkey_type": pk_type,
|
||||
}).Debug("Retrieved publicKey type")
|
||||
return pk_type
|
||||
}
|
||||
|
||||
// ConstructSigningPublicKey returns a SingingPublicKey constructed using any excess data that may be stored in the KeyCertificate.
|
||||
// Returns any errors encountered while parsing.
|
||||
func (key_certificate KeyCertificate) ConstructSigningPublicKey(data []byte) (signing_public_key crypto.SigningPublicKey, err error) {
|
||||
log.WithFields(logrus.Fields{
|
||||
"input_length": len(data),
|
||||
}).Debug("Constructing signingPublicKey from keyCertificate")
|
||||
signing_key_type := key_certificate.PublicKeyType()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
data_len := len(data)
|
||||
if data_len < key_certificate.SignatureSize() {
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "(keyCertificate) ConstructSigningPublicKey",
|
||||
"data_len": data_len,
|
||||
"required_len": KEYCERT_SPK_SIZE,
|
||||
"reason": "not enough data",
|
||||
}).Error("error constructing signing public key")
|
||||
err = errors.New("error constructing signing public key: not enough data")
|
||||
return
|
||||
}
|
||||
switch signing_key_type {
|
||||
case KEYCERT_SIGN_DSA_SHA1:
|
||||
var dsa_key crypto.DSAPublicKey
|
||||
copy(dsa_key[:], data[KEYCERT_SPK_SIZE-KEYCERT_SIGN_DSA_SHA1_SIZE:KEYCERT_SPK_SIZE])
|
||||
signing_public_key = dsa_key
|
||||
log.Debug("Constructed DSAPublicKey")
|
||||
case KEYCERT_SIGN_P256:
|
||||
var ec_key crypto.ECP256PublicKey
|
||||
copy(ec_key[:], data[KEYCERT_SPK_SIZE-KEYCERT_SIGN_P256_SIZE:KEYCERT_SPK_SIZE])
|
||||
signing_public_key = ec_key
|
||||
log.Debug("Constructed ECP256PublicKey")
|
||||
case KEYCERT_SIGN_P384:
|
||||
var ec_key crypto.ECP384PublicKey
|
||||
copy(ec_key[:], data[KEYCERT_SPK_SIZE-KEYCERT_SIGN_P384_SIZE:KEYCERT_SPK_SIZE])
|
||||
signing_public_key = ec_key
|
||||
log.Debug("Constructed ECP384PublicKey")
|
||||
case KEYCERT_SIGN_P521:
|
||||
var ec_key crypto.ECP521PublicKey
|
||||
extra := KEYCERT_SIGN_P521_SIZE - KEYCERT_SPK_SIZE
|
||||
copy(ec_key[:], data)
|
||||
copy(ec_key[KEYCERT_SPK_SIZE:], key_certificate.Certificate.RawBytes()[4:4+extra])
|
||||
signing_public_key = ec_key
|
||||
log.Debug("Constructed ECP521PublicKey")
|
||||
case KEYCERT_SIGN_RSA2048:
|
||||
// var rsa_key crypto.RSA2048PublicKey
|
||||
// extra := KEYCERT_SIGN_RSA2048_SIZE - 128
|
||||
// copy(rsa_key[:], data)
|
||||
// copy(rsa_key[128:], key_certificate[4:4+extra])
|
||||
// signing_public_key = rsa_key
|
||||
log.WithFields(logrus.Fields{
|
||||
"signing_key_type": signing_key_type,
|
||||
}).Warn("Signing key type KEYCERT_SIGN_RSA2048 not implemented")
|
||||
case KEYCERT_SIGN_RSA3072:
|
||||
log.WithFields(logrus.Fields{
|
||||
"signing_key_type": signing_key_type,
|
||||
}).Warn("Signing key type KEYCERT_SIGN_RSA3072 not implemented")
|
||||
case KEYCERT_SIGN_RSA4096:
|
||||
log.WithFields(logrus.Fields{
|
||||
"signing_key_type": signing_key_type,
|
||||
}).Warn("Signing key type KEYCERT_SIGN_RSA4096 not implemented")
|
||||
case KEYCERT_SIGN_ED25519:
|
||||
log.WithFields(logrus.Fields{
|
||||
"signing_key_type": signing_key_type,
|
||||
}).Warn("Signing key type KEYCERT_SIGN_ED25519 not implemented")
|
||||
case KEYCERT_SIGN_ED25519PH:
|
||||
log.WithFields(logrus.Fields{
|
||||
"signing_key_type": signing_key_type,
|
||||
}).Warn("Signing key type KEYCERT_SIGN_ED25519PH not implemented")
|
||||
default:
|
||||
log.WithFields(logrus.Fields{
|
||||
"signing_key_type": signing_key_type,
|
||||
}).Warn("Unknown signing key type")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// SignatureSize return the size of a Signature corresponding to the Key Certificate's signingPublicKey type.
|
||||
func (key_certificate KeyCertificate) SignatureSize() (size int) {
|
||||
sizes := map[int]int{
|
||||
KEYCERT_SIGN_DSA_SHA1: KEYCERT_SIGN_DSA_SHA1_SIZE,
|
||||
KEYCERT_SIGN_P256: KEYCERT_SIGN_P256_SIZE,
|
||||
KEYCERT_SIGN_P384: KEYCERT_SIGN_P384_SIZE,
|
||||
KEYCERT_SIGN_P521: KEYCERT_SIGN_P521_SIZE,
|
||||
KEYCERT_SIGN_RSA2048: KEYCERT_SIGN_RSA2048_SIZE,
|
||||
KEYCERT_SIGN_RSA3072: KEYCERT_SIGN_RSA3072_SIZE,
|
||||
KEYCERT_SIGN_RSA4096: KEYCERT_SIGN_RSA4096_SIZE,
|
||||
KEYCERT_SIGN_ED25519: KEYCERT_SIGN_ED25519_SIZE,
|
||||
KEYCERT_SIGN_ED25519PH: KEYCERT_SIGN_ED25519PH_SIZE,
|
||||
}
|
||||
key_type := key_certificate.SigningPublicKeyType()
|
||||
size = sizes[int(key_type)]
|
||||
log.WithFields(logrus.Fields{
|
||||
"key_type": key_type,
|
||||
"signature_size": size,
|
||||
}).Debug("Retrieved signature size")
|
||||
return sizes[int(key_type)]
|
||||
}
|
||||
|
||||
// NewKeyCertificate creates a new *KeyCertificate from []byte using ReadCertificate.
|
||||
// The remaining bytes after the specified length are also returned.
|
||||
// Returns a list of errors that occurred during parsing.
|
||||
func NewKeyCertificate(bytes []byte) (key_certificate *KeyCertificate, remainder []byte, err error) {
|
||||
log.WithFields(logrus.Fields{
|
||||
"input_length": len(bytes),
|
||||
}).Debug("Creating new keyCertificate")
|
||||
|
||||
var certificate Certificate
|
||||
certificate, remainder, err = ReadCertificate(bytes)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to read Certificate")
|
||||
return
|
||||
}
|
||||
|
||||
if certificate.Type() != 5 { // Key certificate type must be 5
|
||||
return nil, nil, errors.New("error parsing key certificate: invalid certificate type")
|
||||
}
|
||||
if len(bytes) < KEYCERT_MIN_SIZE {
|
||||
log.WithError(err).Error("keyCertificate data too short")
|
||||
err = errors.New("error parsing key certificate: not enough data")
|
||||
remainder = bytes[KEYCERT_MIN_SIZE:]
|
||||
}
|
||||
key_certificate = &KeyCertificate{
|
||||
Certificate: certificate,
|
||||
}
|
||||
if len(bytes) >= 5 {
|
||||
key_certificate.spkType = Integer(bytes[4:5])
|
||||
}
|
||||
if len(bytes) >= 7 {
|
||||
key_certificate.cpkType = Integer(bytes[6:7])
|
||||
}
|
||||
|
||||
log.WithFields(logrus.Fields{
|
||||
"spk_type": key_certificate.spkType.Int(),
|
||||
"cpk_type": key_certificate.cpkType.Int(),
|
||||
"remainder_length": len(remainder),
|
||||
}).Debug("Successfully created new keyCertificate")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// KeyCertificateFromCertificate returns a *KeyCertificate from a *Certificate.
|
||||
func KeyCertificateFromCertificate(certificate Certificate) *KeyCertificate {
|
||||
log.Debug("Creating keyCertificate from Certificate")
|
||||
// k, _, _ := NewKeyCertificate(certificate.RawBytes())
|
||||
k, _, err := NewKeyCertificate(certificate.RawBytes())
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to create keyCertificate from Certificate")
|
||||
} else {
|
||||
log.Debug("Successfully created keyCertificate from Certificate")
|
||||
}
|
||||
return k
|
||||
}
|
@ -1,15 +1,16 @@
|
||||
package common
|
||||
package key_certificate
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSingingPublicKeyTypeReturnsCorrectInteger(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
key_cert := KeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x03, 0x00, 0x00})
|
||||
pk_type, err := key_cert.SigningPublicKeyType()
|
||||
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x03, 0x00, 0x00})
|
||||
pk_type := key_cert.SigningPublicKeyType()
|
||||
|
||||
assert.Nil(err, "SigningPublicKeyType() returned error with valid data")
|
||||
assert.Equal(pk_type, KEYCERT_SIGN_P521, "SigningPublicKeyType() did not return correct typec")
|
||||
@ -18,8 +19,10 @@ func TestSingingPublicKeyTypeReturnsCorrectInteger(t *testing.T) {
|
||||
func TestSingingPublicKeyTypeReportsWhenDataTooSmall(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
key_cert := KeyCertificate([]byte{0x05, 0x00, 0x01, 0x00})
|
||||
_, err := key_cert.SigningPublicKeyType()
|
||||
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x01, 0x00})
|
||||
sk_type := key_cert.SigningPublicKeyType()
|
||||
|
||||
assert.Equal(sk_type, 0, "SigningPublicKeyType() did not return correct typec")
|
||||
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("error parsing key certificate: not enough data", err.Error(), "correct error message should be returned")
|
||||
@ -29,30 +32,31 @@ func TestSingingPublicKeyTypeReportsWhenDataTooSmall(t *testing.T) {
|
||||
func TestPublicKeyTypeReturnsCorrectInteger(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
key_cert := KeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03})
|
||||
pk_type, err := key_cert.PublicKeyType()
|
||||
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03})
|
||||
pk_type := key_cert.PublicKeyType()
|
||||
|
||||
assert.Nil(err, "PublicKey() returned error with valid data")
|
||||
assert.Nil(err, "publicKey() returned error with valid data")
|
||||
assert.Equal(pk_type, KEYCERT_SIGN_P521, "PublicKeyType() did not return correct typec")
|
||||
}
|
||||
|
||||
func TestPublicKeyTypeReportsWhenDataTooSmall(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
key_cert := KeyCertificate([]byte{0x05, 0x00, 0x02, 0x00, 0x00})
|
||||
_, err := key_cert.PublicKeyType()
|
||||
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x02, 0x00, 0x00})
|
||||
pk_type := key_cert.PublicKeyType()
|
||||
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("error parsing key certificate: not enough data", err.Error(), "correct error message should be returned")
|
||||
}
|
||||
assert.Equal(pk_type, 0, "PublicKeyType() did not return correct typec")
|
||||
}
|
||||
|
||||
func TestConstructPublicKeyReportsWhenDataTooSmall(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
key_cert := KeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00})
|
||||
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00})
|
||||
data := make([]byte, 255)
|
||||
_, err := key_cert.ConstructPublicKey(data)
|
||||
_, err = key_cert.ConstructPublicKey(data)
|
||||
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("error constructing public key: not enough data", err.Error(), "correct error message should be returned")
|
||||
@ -62,7 +66,7 @@ func TestConstructPublicKeyReportsWhenDataTooSmall(t *testing.T) {
|
||||
func TestConstructPublicKeyReturnsCorrectDataWithElg(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
key_cert := KeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00})
|
||||
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00})
|
||||
data := make([]byte, 256)
|
||||
pk, err := key_cert.ConstructPublicKey(data)
|
||||
|
||||
@ -73,9 +77,9 @@ func TestConstructPublicKeyReturnsCorrectDataWithElg(t *testing.T) {
|
||||
func TestConstructSigningPublicKeyReportsWhenDataTooSmall(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
key_cert := KeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00})
|
||||
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00})
|
||||
data := make([]byte, 127)
|
||||
_, err := key_cert.ConstructSigningPublicKey(data)
|
||||
_, err = key_cert.ConstructSigningPublicKey(data)
|
||||
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("error constructing signing public key: not enough data", err.Error(), "correct error message should be returned")
|
||||
@ -85,43 +89,43 @@ func TestConstructSigningPublicKeyReportsWhenDataTooSmall(t *testing.T) {
|
||||
func TestConstructSigningPublicKeyWithDSASHA1(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
key_cert := KeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00})
|
||||
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00})
|
||||
data := make([]byte, 128)
|
||||
spk, err := key_cert.ConstructSigningPublicKey(data)
|
||||
|
||||
assert.Nil(err, "ConstructSigningPublicKey() with DSA SHA1 returned error with valid data")
|
||||
assert.Equal(spk.Len(), KEYCERT_SIGN_DSA_SHA1_SIZE, "ConstructSigningPublicKey() with DSA SHA1 returned incorrect SigningPublicKey length")
|
||||
assert.Equal(spk.Len(), KEYCERT_SIGN_DSA_SHA1_SIZE, "ConstructSigningPublicKey() with DSA SHA1 returned incorrect signingPublicKey length")
|
||||
}
|
||||
|
||||
func TestConstructSigningPublicKeyWithP256(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
key_cert := KeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x01, 0x00, 0x01})
|
||||
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x01, 0x00, 0x01})
|
||||
data := make([]byte, 128)
|
||||
spk, err := key_cert.ConstructSigningPublicKey(data)
|
||||
|
||||
assert.Nil(err, "ConstructSigningPublicKey() with P256 returned err on valid data")
|
||||
assert.Equal(spk.Len(), KEYCERT_SIGN_P256_SIZE, "ConstructSigningPublicKey() with P256 returned incorrect SigningPublicKey length")
|
||||
assert.Equal(spk.Len(), KEYCERT_SIGN_P256_SIZE, "ConstructSigningPublicKey() with P256 returned incorrect signingPublicKey length")
|
||||
}
|
||||
|
||||
func TestConstructSigningPublicKeyWithP384(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
key_cert := KeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x02, 0x00, 0x02})
|
||||
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x04, 0x00, 0x02, 0x00, 0x02})
|
||||
data := make([]byte, 128)
|
||||
spk, err := key_cert.ConstructSigningPublicKey(data)
|
||||
|
||||
assert.Nil(err, "ConstructSigningPublicKey() with P384 returned err on valid data")
|
||||
assert.Equal(spk.Len(), KEYCERT_SIGN_P384_SIZE, "ConstructSigningPublicKey() with P384 returned incorrect SigningPublicKey length")
|
||||
assert.Equal(spk.Len(), KEYCERT_SIGN_P384_SIZE, "ConstructSigningPublicKey() with P384 returned incorrect signingPublicKey length")
|
||||
}
|
||||
|
||||
func TestConstructSigningPublicKeyWithP521(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
key_cert := KeyCertificate([]byte{0x05, 0x00, 0x08, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00})
|
||||
key_cert, _, err := NewKeyCertificate([]byte{0x05, 0x00, 0x08, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00})
|
||||
data := make([]byte, 128)
|
||||
spk, err := key_cert.ConstructSigningPublicKey(data)
|
||||
|
||||
assert.Nil(err, "ConstructSigningPublicKey() with P521 returned err on valid data")
|
||||
assert.Equal(spk.Len(), KEYCERT_SIGN_P521_SIZE, "ConstructSigningPublicKey() with P521 returned incorrect SigningPublicKey length")
|
||||
assert.Equal(spk.Len(), KEYCERT_SIGN_P521_SIZE, "ConstructSigningPublicKey() with P521 returned incorrect signingPublicKey length")
|
||||
}
|
@ -1,203 +0,0 @@
|
||||
package common
|
||||
|
||||
/*
|
||||
I2P KeysAndCert
|
||||
https://geti2p.net/spec/common-structures#keysandcert
|
||||
Accurate for version 0.9.24
|
||||
|
||||
+----+----+----+----+----+----+----+----+
|
||||
| public_key |
|
||||
+ +
|
||||
| |
|
||||
~ ~
|
||||
~ ~
|
||||
| |
|
||||
+----+----+----+----+----+----+----+----+
|
||||
| padding (optional) |
|
||||
~ ~
|
||||
~ ~
|
||||
| |
|
||||
+----+----+----+----+----+----+----+----+
|
||||
| signing_key |
|
||||
+ +
|
||||
| |
|
||||
~ ~
|
||||
~ ~
|
||||
| |
|
||||
+----+----+----+----+----+----+----+----+
|
||||
| certificate |
|
||||
+----+----+----+-//
|
||||
|
||||
public_key :: PublicKey (partial or full)
|
||||
length -> 256 bytes or as specified in key certificate
|
||||
|
||||
padding :: random data
|
||||
length -> 0 bytes or as specified in key certificate
|
||||
padding length + signing_key length == KEYS_AND_CERT_SPK_SIZE bytes
|
||||
|
||||
signing__key :: SigningPublicKey (partial or full)
|
||||
length -> 128 bytes or as specified in key certificate
|
||||
padding length + signing_key length == KEYS_AND_CERT_SPK_SIZE bytes
|
||||
|
||||
certificate :: Certificate
|
||||
length -> >= 3 bytes
|
||||
|
||||
total length: 387+ bytes
|
||||
*/
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/go-i2p/go-i2p/lib/crypto"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Sizes of various KeysAndCert structures and requirements
|
||||
const (
|
||||
KEYS_AND_CERT_PUBKEY_SIZE = 256
|
||||
KEYS_AND_CERT_SPK_SIZE = 128
|
||||
KEYS_AND_CERT_MIN_SIZE = 387
|
||||
KEYS_AND_CERT_DATA_SIZE = 384
|
||||
)
|
||||
|
||||
type KeysAndCert []byte
|
||||
|
||||
//
|
||||
// Return the PublicKey for this KeysAndCert, reading from the Key Certificate if it is present to
|
||||
// determine correct lengths.
|
||||
//
|
||||
func (keys_and_cert KeysAndCert) PublicKey() (key crypto.PublicKey, err error) {
|
||||
cert, err := keys_and_cert.Certificate()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
cert_len, err := cert.Length()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if cert_len == 0 {
|
||||
// No Certificate is present, return the KEYS_AND_CERT_PUBKEY_SIZE byte
|
||||
// PublicKey space as ElgPublicKey.
|
||||
var elg_key crypto.ElgPublicKey
|
||||
copy(keys_and_cert[:KEYS_AND_CERT_PUBKEY_SIZE], elg_key[:])
|
||||
key = elg_key
|
||||
} else {
|
||||
// A Certificate is present in this KeysAndCert
|
||||
cert_type, _ := cert.Type()
|
||||
if cert_type == CERT_KEY {
|
||||
// This KeysAndCert contains a Key Certificate, construct
|
||||
// a PublicKey from the data in the KeysAndCert and
|
||||
// any additional data in the Certificate.
|
||||
key, err = KeyCertificate(cert).ConstructPublicKey(
|
||||
keys_and_cert[:KEYS_AND_CERT_PUBKEY_SIZE],
|
||||
)
|
||||
} else {
|
||||
// Key Certificate is not present, return the KEYS_AND_CERT_PUBKEY_SIZE byte
|
||||
// PublicKey space as ElgPublicKey. No other Certificate
|
||||
// types are currently in use.
|
||||
var elg_key crypto.ElgPublicKey
|
||||
copy(keys_and_cert[:KEYS_AND_CERT_PUBKEY_SIZE], elg_key[:])
|
||||
key = elg_key
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(KeysAndCert) PublicKey",
|
||||
"cert_type": cert_type,
|
||||
}).Warn("unused certificate type observed")
|
||||
}
|
||||
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Return the SigningPublicKey for this KeysAndCert, reading from the Key Certificate if it is present to
|
||||
// determine correct lengths.
|
||||
//
|
||||
func (keys_and_cert KeysAndCert) SigningPublicKey() (signing_public_key crypto.SigningPublicKey, err error) {
|
||||
cert, err := keys_and_cert.Certificate()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
cert_len, err := cert.Length()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if cert_len == 0 {
|
||||
// No Certificate is present, return the KEYS_AND_CERT_SPK_SIZE byte
|
||||
// SigningPublicKey space as legacy DSA SHA1 SigningPublicKey.
|
||||
var dsa_pk crypto.DSAPublicKey
|
||||
copy(dsa_pk[:], keys_and_cert[KEYS_AND_CERT_PUBKEY_SIZE:KEYS_AND_CERT_PUBKEY_SIZE+KEYS_AND_CERT_SPK_SIZE])
|
||||
signing_public_key = dsa_pk
|
||||
} else {
|
||||
// A Certificate is present in this KeysAndCert
|
||||
cert_type, _ := cert.Type()
|
||||
if cert_type == CERT_KEY {
|
||||
// This KeysAndCert contains a Key Certificate, construct
|
||||
// a SigningPublicKey from the data in the KeysAndCert and
|
||||
// any additional data in the Certificate.
|
||||
signing_public_key, err = KeyCertificate(cert).ConstructSigningPublicKey(
|
||||
keys_and_cert[KEYS_AND_CERT_PUBKEY_SIZE : KEYS_AND_CERT_PUBKEY_SIZE+KEYS_AND_CERT_SPK_SIZE],
|
||||
)
|
||||
} else {
|
||||
// Key Certificate is not present, return the KEYS_AND_CERT_SPK_SIZE byte
|
||||
// SigningPublicKey space as legacy SHA DSA1 SigningPublicKey.
|
||||
// No other Certificate types are currently in use.
|
||||
var dsa_pk crypto.DSAPublicKey
|
||||
copy(dsa_pk[:], keys_and_cert[KEYS_AND_CERT_PUBKEY_SIZE:KEYS_AND_CERT_PUBKEY_SIZE+KEYS_AND_CERT_SPK_SIZE])
|
||||
signing_public_key = dsa_pk
|
||||
}
|
||||
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Return the Certificate contained in the KeysAndCert and any errors encountered while parsing the
|
||||
// KeysAndCert or Certificate.
|
||||
//
|
||||
func (keys_and_cert KeysAndCert) Certificate() (cert Certificate, err error) {
|
||||
keys_cert_len := len(keys_and_cert)
|
||||
if keys_cert_len < KEYS_AND_CERT_MIN_SIZE {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(KeysAndCert) Certificate",
|
||||
"data_len": keys_cert_len,
|
||||
"required_len": KEYS_AND_CERT_MIN_SIZE,
|
||||
"reason": "not enough data",
|
||||
}).Error("error parsing keys and cert")
|
||||
err = errors.New("error parsing KeysAndCert: data is smaller than minimum valid size")
|
||||
return
|
||||
}
|
||||
cert, _, err = ReadCertificate(keys_and_cert[KEYS_AND_CERT_DATA_SIZE:])
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Read a KeysAndCert from a slice of bytes, retuning it and the remaining data as well as any errors
|
||||
// encoutered parsing the KeysAndCert.
|
||||
//
|
||||
func ReadKeysAndCert(data []byte) (keys_and_cert KeysAndCert, remainder []byte, err error) {
|
||||
data_len := len(data)
|
||||
if data_len < KEYS_AND_CERT_MIN_SIZE {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "ReadKeysAndCert",
|
||||
"data_len": data_len,
|
||||
"required_len": KEYS_AND_CERT_MIN_SIZE,
|
||||
"reason": "not enough data",
|
||||
}).Error("error parsing keys and cert")
|
||||
err = errors.New("error parsing KeysAndCert: data is smaller than minimum valid size")
|
||||
return
|
||||
}
|
||||
keys_and_cert = KeysAndCert(data[:KEYS_AND_CERT_MIN_SIZE])
|
||||
cert, _ := keys_and_cert.Certificate()
|
||||
cert_len, cert_len_err := cert.Length()
|
||||
if cert_len == 0 {
|
||||
remainder = data[KEYS_AND_CERT_MIN_SIZE:]
|
||||
return
|
||||
}
|
||||
if data_len < KEYS_AND_CERT_MIN_SIZE+cert_len {
|
||||
keys_and_cert = append(keys_and_cert, data[KEYS_AND_CERT_MIN_SIZE:]...)
|
||||
err = cert_len_err
|
||||
} else {
|
||||
keys_and_cert = append(keys_and_cert, data[KEYS_AND_CERT_MIN_SIZE:KEYS_AND_CERT_MIN_SIZE+cert_len]...)
|
||||
remainder = data[KEYS_AND_CERT_MIN_SIZE+cert_len:]
|
||||
}
|
||||
return
|
||||
}
|
66
lib/common/keys_and_cert/doc.md
Normal file
66
lib/common/keys_and_cert/doc.md
Normal file
@ -0,0 +1,66 @@
|
||||
# keys_and_cert
|
||||
--
|
||||
import "github.com/go-i2p/go-i2p/lib/common/keys_and_cert"
|
||||
|
||||
Package keys_and_cert implements the I2P KeysAndCert common data structure
|
||||
|
||||
## Usage
|
||||
|
||||
```go
|
||||
const (
|
||||
KEYS_AND_CERT_PUBKEY_SIZE = 256
|
||||
KEYS_AND_CERT_SPK_SIZE = 128
|
||||
KEYS_AND_CERT_MIN_SIZE = 387
|
||||
KEYS_AND_CERT_DATA_SIZE = 384
|
||||
)
|
||||
```
|
||||
Sizes of various KeysAndCert structures and requirements
|
||||
|
||||
#### type KeysAndCert
|
||||
|
||||
```go
|
||||
type KeysAndCert struct {
|
||||
KeyCertificate *KeyCertificate
|
||||
}
|
||||
```
|
||||
|
||||
KeysAndCert is the represenation of an I2P KeysAndCert.
|
||||
|
||||
https://geti2p.net/spec/common-structures#keysandcert
|
||||
|
||||
#### func ReadKeysAndCert
|
||||
|
||||
```go
|
||||
func ReadKeysAndCert(data []byte) (keys_and_cert KeysAndCert, remainder []byte, err error)
|
||||
```
|
||||
ReadKeysAndCert creates a new *KeysAndCert from []byte using ReadKeysAndCert.
|
||||
Returns a pointer to KeysAndCert unlike ReadKeysAndCert.
|
||||
|
||||
#### func (KeysAndCert) Bytes
|
||||
|
||||
```go
|
||||
func (keys_and_cert KeysAndCert) Bytes() []byte
|
||||
```
|
||||
Bytes returns the entire KeyCertificate in []byte form, trims payload to
|
||||
specified length.
|
||||
|
||||
#### func (*KeysAndCert) Certificate
|
||||
|
||||
```go
|
||||
func (keys_and_cert *KeysAndCert) Certificate() (cert Certificate)
|
||||
```
|
||||
Certfificate returns the certificate.
|
||||
|
||||
#### func (*KeysAndCert) PublicKey
|
||||
|
||||
```go
|
||||
func (keys_and_cert *KeysAndCert) PublicKey() (key crypto.PublicKey)
|
||||
```
|
||||
PublicKey returns the public key as a crypto.PublicKey.
|
||||
|
||||
#### func (*KeysAndCert) SigningPublicKey
|
||||
|
||||
```go
|
||||
func (keys_and_cert *KeysAndCert) SigningPublicKey() (signing_public_key crypto.SigningPublicKey)
|
||||
```
|
||||
SigningPublicKey returns the signing public key.
|
252
lib/common/keys_and_cert/keys_and_cert.go
Normal file
252
lib/common/keys_and_cert/keys_and_cert.go
Normal file
@ -0,0 +1,252 @@
|
||||
// Package keys_and_cert implements the I2P KeysAndCert common data structure
|
||||
package keys_and_cert
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
|
||||
"github.com/go-i2p/go-i2p/lib/util/logger"
|
||||
|
||||
. "github.com/go-i2p/go-i2p/lib/common/certificate"
|
||||
. "github.com/go-i2p/go-i2p/lib/common/key_certificate"
|
||||
"github.com/go-i2p/go-i2p/lib/crypto"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var log = logger.GetGoI2PLogger()
|
||||
|
||||
// Sizes of various KeysAndCert structures and requirements
|
||||
const (
|
||||
KEYS_AND_CERT_PUBKEY_SIZE = 256
|
||||
KEYS_AND_CERT_SPK_SIZE = 128
|
||||
KEYS_AND_CERT_MIN_SIZE = 387
|
||||
KEYS_AND_CERT_DATA_SIZE = 384
|
||||
)
|
||||
|
||||
/*
|
||||
[KeysAndCert]
|
||||
Accurate for version 0.9.49
|
||||
|
||||
Description
|
||||
An encryption public key, a signing public key, and a certificate, used as either a RouterIdentity or a Destination.
|
||||
|
||||
Contents
|
||||
A publicKey followed by a signingPublicKey and then a Certificate.
|
||||
|
||||
+----+----+----+----+----+----+----+----+
|
||||
| public_key |
|
||||
+ +
|
||||
| |
|
||||
~ ~
|
||||
~ ~
|
||||
| |
|
||||
+----+----+----+----+----+----+----+----+
|
||||
| padding (optional) |
|
||||
~ ~
|
||||
~ ~
|
||||
| |
|
||||
+----+----+----+----+----+----+----+----+
|
||||
| signing_key |
|
||||
+ +
|
||||
| |
|
||||
~ ~
|
||||
~ ~
|
||||
| |
|
||||
+----+----+----+----+----+----+----+----+
|
||||
| certificate |
|
||||
+----+----+----+-//
|
||||
|
||||
public_key :: publicKey (partial or full)
|
||||
length -> 256 bytes or as specified in key certificate
|
||||
|
||||
padding :: random data
|
||||
length -> 0 bytes or as specified in key certificate
|
||||
padding length + signing_key length == 128 bytes
|
||||
|
||||
signing__key :: signingPublicKey (partial or full)
|
||||
length -> 128 bytes or as specified in key certificate
|
||||
padding length + signing_key length == 128 bytes
|
||||
|
||||
certificate :: Certificate
|
||||
length -> >= 3 bytes
|
||||
|
||||
total length: 387+ bytes
|
||||
*/
|
||||
|
||||
// KeysAndCert is the represenation of an I2P KeysAndCert.
|
||||
//
|
||||
// https://geti2p.net/spec/common-structures#keysandcert
|
||||
type KeysAndCert struct {
|
||||
keyCertificate *KeyCertificate
|
||||
publicKey crypto.PublicKey
|
||||
Padding []byte
|
||||
signingPublicKey crypto.SigningPublicKey
|
||||
}
|
||||
|
||||
// Bytes returns the entire keyCertificate in []byte form, trims payload to specified length.
|
||||
func (keys_and_cert KeysAndCert) Bytes() []byte {
|
||||
bytes := keys_and_cert.publicKey.Bytes()
|
||||
bytes = append(bytes, keys_and_cert.Padding...)
|
||||
bytes = append(bytes, keys_and_cert.signingPublicKey.Bytes()...)
|
||||
bytes = append(bytes, keys_and_cert.keyCertificate.Bytes()...)
|
||||
log.WithFields(logrus.Fields{
|
||||
"bytes_length": len(bytes),
|
||||
"pk_bytes_length": len(keys_and_cert.publicKey.Bytes()),
|
||||
"padding_bytes_length": len(keys_and_cert.Padding),
|
||||
"spk_bytes_length": len(keys_and_cert.signingPublicKey.Bytes()),
|
||||
"cert_bytes_length": len(keys_and_cert.keyCertificate.Bytes()),
|
||||
}).Debug("Retrieved bytes from KeysAndCert")
|
||||
return bytes
|
||||
}
|
||||
|
||||
// publicKey returns the public key as a crypto.publicKey.
|
||||
func (keys_and_cert *KeysAndCert) PublicKey() (key crypto.PublicKey) {
|
||||
return keys_and_cert.publicKey
|
||||
}
|
||||
|
||||
// signingPublicKey returns the signing public key.
|
||||
func (keys_and_cert *KeysAndCert) SigningPublicKey() (signing_public_key crypto.SigningPublicKey) {
|
||||
return keys_and_cert.signingPublicKey
|
||||
}
|
||||
|
||||
// Certfificate returns the certificate.
|
||||
func (keys_and_cert *KeysAndCert) Certificate() (cert Certificate) {
|
||||
return keys_and_cert.keyCertificate.Certificate
|
||||
}
|
||||
|
||||
// ReadKeysAndCert creates a new *KeysAndCert from []byte using ReadKeysAndCert.
|
||||
// Returns a pointer to KeysAndCert unlike ReadKeysAndCert.
|
||||
func ReadKeysAndCert(data []byte) (keys_and_cert KeysAndCert, remainder []byte, err error) {
|
||||
log.WithFields(logrus.Fields{
|
||||
"input_length": len(data),
|
||||
}).Debug("Reading KeysAndCert from data")
|
||||
|
||||
data_len := len(data)
|
||||
// keys_and_cert = KeysAndCert{}
|
||||
if data_len < KEYS_AND_CERT_MIN_SIZE && data_len > KEYS_AND_CERT_DATA_SIZE {
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "ReadKeysAndCert",
|
||||
"data_len": data_len,
|
||||
"required_len": KEYS_AND_CERT_MIN_SIZE,
|
||||
"reason": "not enough data",
|
||||
}).Error("error parsing keys and cert")
|
||||
err = errors.New("error parsing KeysAndCert: data is smaller than minimum valid size")
|
||||
keys_and_cert.keyCertificate, remainder, _ = NewKeyCertificate(data[KEYS_AND_CERT_DATA_SIZE:])
|
||||
return
|
||||
} else if data_len < KEYS_AND_CERT_DATA_SIZE {
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "ReadKeysAndCert",
|
||||
"data_len": data_len,
|
||||
"required_len": KEYS_AND_CERT_MIN_SIZE,
|
||||
"reason": "not enough data",
|
||||
}).Error("error parsing keys and cert")
|
||||
err = errors.New("error parsing KeysAndCert: data is smaller than minimum valid size")
|
||||
return
|
||||
}
|
||||
keys_and_cert.keyCertificate, remainder, err = NewKeyCertificate(data[KEYS_AND_CERT_DATA_SIZE:])
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to create keyCertificate")
|
||||
return
|
||||
}
|
||||
// TODO: this only supports one key type right now and it's the old key type, but the layout is the same.
|
||||
// a case-switch which sets the size of the SPK and the PK should be used to replace the referenced KEYS_AND_CERT_PUBKEY_SIZE
|
||||
// and KEYS_AND_CERT_SPK_SIZE constants in the future.
|
||||
keys_and_cert.publicKey, err = keys_and_cert.keyCertificate.ConstructPublicKey(data[:keys_and_cert.keyCertificate.CryptoSize()])
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to construct publicKey")
|
||||
return
|
||||
}
|
||||
keys_and_cert.signingPublicKey, err = keys_and_cert.keyCertificate.ConstructSigningPublicKey(data[KEYS_AND_CERT_DATA_SIZE-keys_and_cert.keyCertificate.SignatureSize() : KEYS_AND_CERT_DATA_SIZE])
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to construct signingPublicKey")
|
||||
return
|
||||
}
|
||||
padding := data[KEYS_AND_CERT_PUBKEY_SIZE : KEYS_AND_CERT_DATA_SIZE-KEYS_AND_CERT_SPK_SIZE]
|
||||
keys_and_cert.Padding = padding
|
||||
|
||||
log.WithFields(logrus.Fields{
|
||||
"public_key_type": keys_and_cert.keyCertificate.PublicKeyType(),
|
||||
"signing_public_key_type": keys_and_cert.keyCertificate.SigningPublicKeyType(),
|
||||
"padding_length": len(padding),
|
||||
"remainder_length": len(remainder),
|
||||
}).Debug("Successfully read KeysAndCert")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// NewKeysAndCert creates a new KeysAndCert instance with the provided parameters.
|
||||
// It validates the sizes of the provided keys and padding before assembling the struct.
|
||||
func NewKeysAndCert(
|
||||
keyCertificate *KeyCertificate,
|
||||
publicKey crypto.PublicKey,
|
||||
padding []byte,
|
||||
signingPublicKey crypto.SigningPublicKey,
|
||||
) (*KeysAndCert, error) {
|
||||
log.Debug("Creating new KeysAndCert with provided parameters")
|
||||
|
||||
// 1. Validate keyCertificate
|
||||
if keyCertificate == nil {
|
||||
log.Error("KeyCertificate is nil")
|
||||
return nil, errors.New("KeyCertificate cannot be nil")
|
||||
}
|
||||
|
||||
// 2. Validate publicKey size
|
||||
if publicKey.Len() != KEYS_AND_CERT_PUBKEY_SIZE {
|
||||
log.WithFields(logrus.Fields{
|
||||
"expected_size": KEYS_AND_CERT_PUBKEY_SIZE,
|
||||
"actual_size": publicKey.Len(),
|
||||
}).Error("Invalid publicKey size")
|
||||
return nil, errors.New("publicKey has an invalid size")
|
||||
}
|
||||
|
||||
/*
|
||||
// 3. Validate signingPublicKey size
|
||||
if signingPublicKey.Len() != KEYS_AND_CERT_SPK_SIZE {
|
||||
log.WithFields(logrus.Fields{
|
||||
"expected_size": KEYS_AND_CERT_SPK_SIZE,
|
||||
"actual_size": signingPublicKey.Len(),
|
||||
}).Error("Invalid signingPublicKey size")
|
||||
return nil, errors.New("signingPublicKey has an invalid size")
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
// 4. Validate padding size
|
||||
publicKeyLength := publicKey.Len()
|
||||
signingPublicKeyLength := signingPublicKey.Len()
|
||||
totalKeysSize := publicKeyLength + signingPublicKeyLength
|
||||
expectedPaddingSize := KEYS_AND_CERT_DATA_SIZE - totalKeysSize
|
||||
if len(padding) != expectedPaddingSize {
|
||||
log.WithFields(logrus.Fields{
|
||||
"expected_size": expectedPaddingSize,
|
||||
"actual_size": len(padding),
|
||||
}).Warn("Invalid padding size")
|
||||
// generate some random padding and continue
|
||||
padding = make([]byte, expectedPaddingSize)
|
||||
_, err := rand.Read(padding)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to generate random padding")
|
||||
return nil, err
|
||||
}
|
||||
log.WithFields(logrus.Fields{
|
||||
"expected_size": expectedPaddingSize,
|
||||
"actual_size": len(padding),
|
||||
}).Warn("Generated random padding")
|
||||
}
|
||||
|
||||
// 5. Assemble KeysAndCert
|
||||
keysAndCert := &KeysAndCert{
|
||||
keyCertificate: keyCertificate,
|
||||
publicKey: publicKey,
|
||||
Padding: padding,
|
||||
signingPublicKey: signingPublicKey,
|
||||
}
|
||||
|
||||
log.WithFields(logrus.Fields{
|
||||
"public_key_length": publicKey.Len(),
|
||||
"signing_public_key_length": signingPublicKey.Len(),
|
||||
"padding_length": len(padding),
|
||||
}).Debug("Successfully created KeysAndCert")
|
||||
|
||||
return keysAndCert, nil
|
||||
}
|
@ -1,27 +1,22 @@
|
||||
package common
|
||||
package keys_and_cert
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCertificateWithMissingData(t *testing.T) {
|
||||
/*func TestCertificateWithMissingData(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
cert_data := []byte{0x05, 0x00, 0x04, 0x00, 0x01}
|
||||
data := make([]byte, 128+256)
|
||||
data = append(data, cert_data...)
|
||||
keys_and_cert := KeysAndCert(data)
|
||||
|
||||
cert, err := keys_and_cert.Certificate()
|
||||
_, _, err := NewKeysAndCert(data)
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("certificate parsing warning: certificate data is shorter than specified by length", err.Error())
|
||||
}
|
||||
cert_bytes := []byte(cert)
|
||||
if assert.Equal(len(cert_data), len(cert_bytes)) {
|
||||
assert.Equal(cert_bytes, cert_data, "keys_and_cert.Certificate() did not return available data when cert was missing some data")
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
func TestCertificateWithValidData(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
@ -29,11 +24,12 @@ func TestCertificateWithValidData(t *testing.T) {
|
||||
cert_data := []byte{0x05, 0x00, 0x04, 0x00, 0x01, 0x00, 0x00}
|
||||
data := make([]byte, 128+256)
|
||||
data = append(data, cert_data...)
|
||||
keys_and_cert := KeysAndCert(data)
|
||||
|
||||
cert, err := keys_and_cert.Certificate()
|
||||
keys_and_cert, _, err := ReadKeysAndCert(data)
|
||||
assert.Nil(err)
|
||||
cert_bytes := []byte(cert)
|
||||
|
||||
cert := keys_and_cert.Certificate()
|
||||
|
||||
cert_bytes := cert.Bytes()
|
||||
if assert.Equal(len(cert_data), len(cert_bytes)) {
|
||||
assert.Equal(cert_bytes, cert_data, "keys_and_cert.Certificate() did not return correct data with valid cert")
|
||||
}
|
||||
@ -47,9 +43,9 @@ func TestPublicKeyWithBadData(t *testing.T) {
|
||||
data := make([]byte, 128)
|
||||
data = append(data, pub_key_data...)
|
||||
data = append(data, cert_data...)
|
||||
keys_and_cert := KeysAndCert(data)
|
||||
keys_and_cert, _, err := ReadKeysAndCert(data)
|
||||
|
||||
pub_key, err := keys_and_cert.PublicKey()
|
||||
pub_key := keys_and_cert.PublicKey()
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("error parsing KeysAndCert: data is smaller than minimum valid size", err.Error())
|
||||
}
|
||||
@ -64,9 +60,9 @@ func TestPublicKeyWithBadCertificate(t *testing.T) {
|
||||
data := make([]byte, 128)
|
||||
data = append(data, pub_key_data...)
|
||||
data = append(data, cert_data...)
|
||||
keys_and_cert := KeysAndCert(data)
|
||||
keys_and_cert, _, err := ReadKeysAndCert(data)
|
||||
|
||||
pub_key, err := keys_and_cert.PublicKey()
|
||||
pub_key := keys_and_cert.PublicKey()
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("certificate parsing warning: certificate data is shorter than specified by length", err.Error())
|
||||
}
|
||||
@ -81,9 +77,9 @@ func TestPublicKeyWithNullCertificate(t *testing.T) {
|
||||
data := make([]byte, 128)
|
||||
data = append(data, pub_key_data...)
|
||||
data = append(data, cert_data...)
|
||||
keys_and_cert := KeysAndCert(data)
|
||||
keys_and_cert, _, err := ReadKeysAndCert(data)
|
||||
|
||||
pub_key, err := keys_and_cert.PublicKey()
|
||||
pub_key := keys_and_cert.PublicKey()
|
||||
assert.Nil(err)
|
||||
assert.Equal(len(pub_key_data), pub_key.Len())
|
||||
}
|
||||
@ -96,9 +92,9 @@ func TestPublicKeyWithKeyCertificate(t *testing.T) {
|
||||
data := make([]byte, 128)
|
||||
data = append(data, pub_key_data...)
|
||||
data = append(data, cert_data...)
|
||||
keys_and_cert := KeysAndCert(data)
|
||||
keys_and_cert, _, err := ReadKeysAndCert(data)
|
||||
|
||||
pub_key, err := keys_and_cert.PublicKey()
|
||||
pub_key := keys_and_cert.PublicKey()
|
||||
assert.Nil(err)
|
||||
assert.Equal(len(pub_key_data), pub_key.Len())
|
||||
}
|
||||
@ -111,9 +107,9 @@ func TestSigningPublicKeyWithBadData(t *testing.T) {
|
||||
data := make([]byte, 93)
|
||||
data = append(data, pub_key_data...)
|
||||
data = append(data, cert_data...)
|
||||
keys_and_cert := KeysAndCert(data)
|
||||
keys_and_cert, _, err := ReadKeysAndCert(data)
|
||||
|
||||
signing_pub_key, err := keys_and_cert.SigningPublicKey()
|
||||
signing_pub_key := keys_and_cert.SigningPublicKey()
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("error parsing KeysAndCert: data is smaller than minimum valid size", err.Error())
|
||||
}
|
||||
@ -128,9 +124,9 @@ func TestSigningPublicKeyWithBadCertificate(t *testing.T) {
|
||||
data := make([]byte, 128)
|
||||
data = append(data, pub_key_data...)
|
||||
data = append(data, cert_data...)
|
||||
keys_and_cert := KeysAndCert(data)
|
||||
keys_and_cert, _, err := ReadKeysAndCert(data)
|
||||
|
||||
signing_pub_key, err := keys_and_cert.SigningPublicKey()
|
||||
signing_pub_key := keys_and_cert.SigningPublicKey()
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("certificate parsing warning: certificate data is shorter than specified by length", err.Error())
|
||||
}
|
||||
@ -145,9 +141,9 @@ func TestSigningPublicKeyWithNullCertificate(t *testing.T) {
|
||||
signing_pub_key_data := make([]byte, 128)
|
||||
data := append(pub_key_data, signing_pub_key_data...)
|
||||
data = append(data, cert_data...)
|
||||
keys_and_cert := KeysAndCert(data)
|
||||
keys_and_cert, _, err := ReadKeysAndCert(data)
|
||||
|
||||
signing_pub_key, err := keys_and_cert.SigningPublicKey()
|
||||
signing_pub_key := keys_and_cert.SigningPublicKey()
|
||||
assert.Nil(err)
|
||||
assert.Equal(len(signing_pub_key_data), signing_pub_key.Len())
|
||||
}
|
||||
@ -160,130 +156,76 @@ func TestSigningPublicKeyWithKeyCertificate(t *testing.T) {
|
||||
signing_pub_key_data := make([]byte, 128)
|
||||
data := append(pub_key_data, signing_pub_key_data...)
|
||||
data = append(data, cert_data...)
|
||||
keys_and_cert := KeysAndCert(data)
|
||||
keys_and_cert, _, err := ReadKeysAndCert(data)
|
||||
|
||||
signing_pub_key, err := keys_and_cert.SigningPublicKey()
|
||||
signing_pub_key := keys_and_cert.SigningPublicKey()
|
||||
assert.Nil(err)
|
||||
assert.Equal(len(signing_pub_key_data), signing_pub_key.Len())
|
||||
}
|
||||
|
||||
func TestReadKeysAndCertWithMissingData(t *testing.T) {
|
||||
func TestNewKeysAndCertWithMissingData(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
cert_data := make([]byte, 128)
|
||||
keys_and_cert, remainder, err := ReadKeysAndCert(cert_data)
|
||||
_, remainder, err := ReadKeysAndCert(cert_data)
|
||||
assert.Equal(0, len(remainder))
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("error parsing KeysAndCert: data is smaller than minimum valid size", err.Error())
|
||||
}
|
||||
|
||||
_, err = keys_and_cert.PublicKey()
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("error parsing KeysAndCert: data is smaller than minimum valid size", err.Error())
|
||||
}
|
||||
_, err = keys_and_cert.SigningPublicKey()
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("error parsing KeysAndCert: data is smaller than minimum valid size", err.Error())
|
||||
}
|
||||
_, err = keys_and_cert.Certificate()
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("error parsing KeysAndCert: data is smaller than minimum valid size", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadKeysAndCertWithMissingCertData(t *testing.T) {
|
||||
func TestNewKeysAndCertWithMissingCertData(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
cert_data := make([]byte, 128+256)
|
||||
cert_data = append(cert_data, []byte{0x05, 0x00, 0x04, 0x00, 0x01}...)
|
||||
keys_and_cert, remainder, err := ReadKeysAndCert(cert_data)
|
||||
_, remainder, err := ReadKeysAndCert(cert_data)
|
||||
assert.Equal(0, len(remainder))
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("certificate parsing warning: certificate data is shorter than specified by length", err.Error())
|
||||
}
|
||||
|
||||
_, err = keys_and_cert.PublicKey()
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("certificate parsing warning: certificate data is shorter than specified by length", err.Error())
|
||||
}
|
||||
_, err = keys_and_cert.SigningPublicKey()
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("certificate parsing warning: certificate data is shorter than specified by length", err.Error())
|
||||
}
|
||||
_, err = keys_and_cert.Certificate()
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal("certificate parsing warning: certificate data is shorter than specified by length", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadKeysAndCertWithValidDataWithCertificate(t *testing.T) {
|
||||
func TestNewKeysAndCertWithValidDataWithCertificate(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
cert_data := make([]byte, 128+256)
|
||||
cert_data = append(cert_data, []byte{0x05, 0x00, 0x04, 0x00, 0x01, 0x00, 0x00}...)
|
||||
keys_and_cert, remainder, err := ReadKeysAndCert(cert_data)
|
||||
_, remainder, err := ReadKeysAndCert(cert_data)
|
||||
assert.Equal(0, len(remainder))
|
||||
assert.Nil(err)
|
||||
|
||||
_, err = keys_and_cert.PublicKey()
|
||||
assert.Nil(err, "keys_and_cert.PublicKey() returned error with valid data containing certificate")
|
||||
_, err = keys_and_cert.SigningPublicKey()
|
||||
assert.Nil(err, "keys_and_cert.SigningPublicKey() returned error with valid data containing certificate")
|
||||
_, err = keys_and_cert.Certificate()
|
||||
assert.Nil(err, "keys_and_cert.Certificate() returned error with valid data containing certificate")
|
||||
}
|
||||
|
||||
func TestReadKeysAndCertWithValidDataWithoutCertificate(t *testing.T) {
|
||||
func TestNewKeysAndCertWithValidDataWithoutCertificate(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
cert_data := make([]byte, 128+256)
|
||||
cert_data = append(cert_data, []byte{0x00, 0x00, 0x00}...)
|
||||
keys_and_cert, remainder, err := ReadKeysAndCert(cert_data)
|
||||
_, remainder, err := ReadKeysAndCert(cert_data)
|
||||
assert.Equal(0, len(remainder))
|
||||
assert.Nil(err)
|
||||
|
||||
_, err = keys_and_cert.PublicKey()
|
||||
assert.Nil(err, "keys_and_cert.PublicKey() returned error with valid data not containing certificate")
|
||||
_, err = keys_and_cert.SigningPublicKey()
|
||||
assert.Nil(err, "keys_and_cert.SigningPublicKey() returned error with valid data not containing certificate")
|
||||
_, err = keys_and_cert.Certificate()
|
||||
assert.Nil(err, "keys_and_cert.Certificate() returned error with valid data not containing certificate")
|
||||
}
|
||||
|
||||
func TestReadKeysAndCertWithValidDataWithCertificateAndRemainder(t *testing.T) {
|
||||
func TestNewKeysAndCertWithValidDataWithCertificateAndRemainder(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
cert_data := make([]byte, 128+256)
|
||||
cert_data = append(cert_data, []byte{0x05, 0x00, 0x04, 0x00, 0x01, 0x00, 0x00, 0x41}...)
|
||||
keys_and_cert, remainder, err := ReadKeysAndCert(cert_data)
|
||||
_, remainder, err := ReadKeysAndCert(cert_data)
|
||||
if assert.Equal(1, len(remainder)) {
|
||||
assert.Equal("A", string(remainder[0]))
|
||||
}
|
||||
assert.Nil(err)
|
||||
|
||||
_, err = keys_and_cert.PublicKey()
|
||||
assert.Nil(err, "keys_and_cert.PublicKey() returned error with valid data containing certificate")
|
||||
_, err = keys_and_cert.SigningPublicKey()
|
||||
assert.Nil(err, "keys_and_cert.SigningPublicKey() returned error with valid data containing certificate")
|
||||
_, err = keys_and_cert.Certificate()
|
||||
assert.Nil(err, "keys_and_cert.Certificate() returned error with valid data containing certificate")
|
||||
}
|
||||
|
||||
func TestReadKeysAndCertWithValidDataWithoutCertificateAndRemainder(t *testing.T) {
|
||||
func TestNewKeysAndCertWithValidDataWithoutCertificateAndRemainder(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
cert_data := make([]byte, 128+256)
|
||||
cert_data = append(cert_data, []byte{0x00, 0x00, 0x00, 0x41}...)
|
||||
keys_and_cert, remainder, err := ReadKeysAndCert(cert_data)
|
||||
_, remainder, err := ReadKeysAndCert(cert_data)
|
||||
if assert.Equal(1, len(remainder)) {
|
||||
assert.Equal("A", string(remainder[0]))
|
||||
}
|
||||
assert.Nil(err)
|
||||
|
||||
_, err = keys_and_cert.PublicKey()
|
||||
assert.Nil(err, "keys_and_cert.PublicKey() returned error with valid data not containing certificate")
|
||||
_, err = keys_and_cert.SigningPublicKey()
|
||||
assert.Nil(err, "keys_and_cert.SigningPublicKey() returned error with valid data not containing certificate")
|
||||
_, err = keys_and_cert.Certificate()
|
||||
assert.Nil(err, "keys_and_cert.Certificate() returned error with valid data not containing certificate")
|
||||
}
|
18
lib/common/keys_and_cert/private_keys_and_cert.go
Normal file
18
lib/common/keys_and_cert/private_keys_and_cert.go
Normal file
@ -0,0 +1,18 @@
|
||||
package keys_and_cert
|
||||
|
||||
import "crypto"
|
||||
|
||||
// PrivateKeysAndCert contains a KeysAndCert along with the corresponding private keys for the
|
||||
// Public Key and the Signing Public Key
|
||||
type PrivateKeysAndCert struct {
|
||||
KeysAndCert
|
||||
PK_KEY crypto.PrivateKey
|
||||
SPK_KEY crypto.PrivateKey
|
||||
}
|
||||
|
||||
func NewPrivateKeysAndCert() (*PrivateKeysAndCert, error) {
|
||||
var pkc PrivateKeysAndCert
|
||||
var err error
|
||||
// pkc.PK_KEY, err =
|
||||
return &pkc, err
|
||||
}
|
@ -1,64 +0,0 @@
|
||||
package common
|
||||
|
||||
/*
|
||||
I2P Lease
|
||||
https://geti2p.net/spec/common-structures#lease
|
||||
Accurate for version 0.9.24
|
||||
|
||||
+----+----+----+----+----+----+----+----+
|
||||
| tunnel_gw |
|
||||
+ +
|
||||
| |
|
||||
+ +
|
||||
| |
|
||||
+ +
|
||||
| |
|
||||
+----+----+----+----+----+----+----+----+
|
||||
| tunnel_id | end_date
|
||||
+----+----+----+----+----+----+----+----+
|
||||
|
|
||||
+----+----+----+----+
|
||||
|
||||
tunnel_gw :: Hash of the RouterIdentity of the tunnel gateway
|
||||
length -> 32 bytes
|
||||
|
||||
tunnel_id :: TunnelId
|
||||
length -> 4 bytes
|
||||
|
||||
end_date :: Date
|
||||
length -> 8 bytes
|
||||
*/
|
||||
|
||||
// Sizes or various components of a Lease
|
||||
const (
|
||||
LEASE_SIZE = 44
|
||||
LEASE_HASH_SIZE = 32
|
||||
LEASE_TUNNEL_ID_SIZE = 4
|
||||
)
|
||||
|
||||
type Lease [LEASE_SIZE]byte
|
||||
|
||||
//
|
||||
// Return the first 32 bytes of the Lease as a Hash.
|
||||
//
|
||||
func (lease Lease) TunnelGateway() (hash Hash) {
|
||||
copy(hash[:], lease[:LEASE_HASH_SIZE])
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Parse the TunnelID Integer in the Lease.
|
||||
//
|
||||
func (lease Lease) TunnelID() uint32 {
|
||||
return uint32(
|
||||
Integer(lease[LEASE_HASH_SIZE : LEASE_HASH_SIZE+LEASE_TUNNEL_ID_SIZE]),
|
||||
)
|
||||
}
|
||||
|
||||
//
|
||||
// Return the Date inside the Lease.
|
||||
//
|
||||
func (lease Lease) Date() (date Date) {
|
||||
copy(date[:], lease[LEASE_HASH_SIZE+LEASE_TUNNEL_ID_SIZE:])
|
||||
return
|
||||
}
|
63
lib/common/lease/doc.md
Normal file
63
lib/common/lease/doc.md
Normal file
@ -0,0 +1,63 @@
|
||||
# lease
|
||||
--
|
||||
import "github.com/go-i2p/go-i2p/lib/common/lease"
|
||||
|
||||
Package lease implements the I2P lease common data structure
|
||||
|
||||
## Usage
|
||||
|
||||
```go
|
||||
const (
|
||||
LEASE_SIZE = 44
|
||||
LEASE_HASH_SIZE = 32
|
||||
LEASE_TUNNEL_ID_SIZE = 4
|
||||
)
|
||||
```
|
||||
Sizes in bytes of various components of a Lease
|
||||
|
||||
#### type Lease
|
||||
|
||||
```go
|
||||
type Lease [LEASE_SIZE]byte
|
||||
```
|
||||
|
||||
Lease is the represenation of an I2P Lease.
|
||||
|
||||
https://geti2p.net/spec/common-structures#lease
|
||||
|
||||
#### func NewLease
|
||||
|
||||
```go
|
||||
func NewLease(data []byte) (lease *Lease, remainder []byte, err error)
|
||||
```
|
||||
NewLease creates a new *NewLease from []byte using ReadLease. Returns a pointer
|
||||
to KeysAndCert unlike ReadLease.
|
||||
|
||||
#### func ReadLease
|
||||
|
||||
```go
|
||||
func ReadLease(data []byte) (lease Lease, remainder []byte, err error)
|
||||
```
|
||||
ReadLease returns Lease from a []byte. The remaining bytes after the specified
|
||||
length are also returned. Returns a list of errors that occurred during parsing.
|
||||
|
||||
#### func (Lease) Date
|
||||
|
||||
```go
|
||||
func (lease Lease) Date() (date Date)
|
||||
```
|
||||
Date returns the date as an I2P Date.
|
||||
|
||||
#### func (Lease) TunnelGateway
|
||||
|
||||
```go
|
||||
func (lease Lease) TunnelGateway() (hash Hash)
|
||||
```
|
||||
TunnelGateway returns the tunnel gateway as a Hash.
|
||||
|
||||
#### func (Lease) TunnelID
|
||||
|
||||
```go
|
||||
func (lease Lease) TunnelID() uint32
|
||||
```
|
||||
TunnelID returns the tunnel id as a uint23.
|
85
lib/common/lease/lease.go
Normal file
85
lib/common/lease/lease.go
Normal file
@ -0,0 +1,85 @@
|
||||
// Package lease implements the I2P lease common data structure
|
||||
package lease
|
||||
|
||||
import . "github.com/go-i2p/go-i2p/lib/common/data"
|
||||
|
||||
// Sizes in bytes of various components of a Lease
|
||||
const (
|
||||
LEASE_SIZE = 44
|
||||
LEASE_HASH_SIZE = 32
|
||||
LEASE_TUNNEL_ID_SIZE = 4
|
||||
)
|
||||
|
||||
/*
|
||||
[Lease]
|
||||
Accurate for version 0.9.49
|
||||
|
||||
Description
|
||||
Defines the authorization for a particular tunnel to receive messages targeting a Destination.
|
||||
|
||||
Contents
|
||||
SHA256 Hash of the RouterIdentity of the gateway router, then the TunnelId and finally an end Date.
|
||||
|
||||
+----+----+----+----+----+----+----+----+
|
||||
| tunnel_gw |
|
||||
+ +
|
||||
| |
|
||||
+ +
|
||||
| |
|
||||
+ +
|
||||
| |
|
||||
+----+----+----+----+----+----+----+----+
|
||||
| tunnel_id | end_date
|
||||
+----+----+----+----+----+----+----+----+
|
||||
|
|
||||
+----+----+----+----+
|
||||
|
||||
tunnel_gw :: Hash of the RouterIdentity of the tunnel gateway
|
||||
length -> 32 bytes
|
||||
|
||||
tunnel_id :: TunnelId
|
||||
length -> 4 bytes
|
||||
|
||||
end_date :: Date
|
||||
length -> 8 bytes
|
||||
*/
|
||||
|
||||
// Lease is the represenation of an I2P Lease.
|
||||
//
|
||||
// https://geti2p.net/spec/common-structures#lease
|
||||
type Lease [LEASE_SIZE]byte
|
||||
|
||||
// TunnelGateway returns the tunnel gateway as a Hash.
|
||||
func (lease Lease) TunnelGateway() (hash Hash) {
|
||||
copy(hash[:], lease[:LEASE_HASH_SIZE])
|
||||
return
|
||||
}
|
||||
|
||||
// TunnelID returns the tunnel id as a uint23.
|
||||
func (lease Lease) TunnelID() uint32 {
|
||||
i := Integer(lease[LEASE_HASH_SIZE : LEASE_HASH_SIZE+LEASE_TUNNEL_ID_SIZE])
|
||||
return uint32(
|
||||
i.Int(),
|
||||
)
|
||||
}
|
||||
|
||||
// Date returns the date as an I2P Date.
|
||||
func (lease Lease) Date() (date Date) {
|
||||
copy(date[:], lease[LEASE_HASH_SIZE+LEASE_TUNNEL_ID_SIZE:])
|
||||
return
|
||||
}
|
||||
|
||||
// ReadLease returns Lease from a []byte.
|
||||
// The remaining bytes after the specified length are also returned.
|
||||
// Returns a list of errors that occurred during parsing.
|
||||
func ReadLease(data []byte) (lease Lease, remainder []byte, err error) {
|
||||
// TODO: stub
|
||||
return
|
||||
}
|
||||
|
||||
// NewLease creates a new *NewLease from []byte using ReadLease.
|
||||
// Returns a pointer to KeysAndCert unlike ReadLease.
|
||||
func NewLease(data []byte) (lease *Lease, remainder []byte, err error) {
|
||||
// TODO: stub
|
||||
return
|
||||
}
|
95
lib/common/lease_set/doc.md
Normal file
95
lib/common/lease_set/doc.md
Normal file
@ -0,0 +1,95 @@
|
||||
# lease_set
|
||||
--
|
||||
import "github.com/go-i2p/go-i2p/lib/common/lease_set"
|
||||
|
||||
Package lease_set implements the I2P LeastSet common data structure
|
||||
|
||||
## Usage
|
||||
|
||||
```go
|
||||
const (
|
||||
LEASE_SET_PUBKEY_SIZE = 256
|
||||
LEASE_SET_SPK_SIZE = 128
|
||||
LEASE_SET_SIG_SIZE = 40
|
||||
)
|
||||
```
|
||||
Sizes of various structures in an I2P LeaseSet
|
||||
|
||||
#### type LeaseSet
|
||||
|
||||
```go
|
||||
type LeaseSet []byte
|
||||
```
|
||||
|
||||
LeaseSet is the represenation of an I2P LeaseSet.
|
||||
|
||||
https://geti2p.net/spec/common-structures#leaseset
|
||||
|
||||
#### func (LeaseSet) Destination
|
||||
|
||||
```go
|
||||
func (lease_set LeaseSet) Destination() (destination Destination, err error)
|
||||
```
|
||||
Destination returns the Destination as []byte.
|
||||
|
||||
#### func (LeaseSet) LeaseCount
|
||||
|
||||
```go
|
||||
func (lease_set LeaseSet) LeaseCount() (count int, err error)
|
||||
```
|
||||
LeaseCount returns the numbert of leases specified by the LeaseCount value as
|
||||
int. returns errors encountered during parsing.
|
||||
|
||||
#### func (LeaseSet) Leases
|
||||
|
||||
```go
|
||||
func (lease_set LeaseSet) Leases() (leases []Lease, err error)
|
||||
```
|
||||
Leases returns the leases as []Lease. returns errors encountered during parsing.
|
||||
|
||||
#### func (LeaseSet) NewestExpiration
|
||||
|
||||
```go
|
||||
func (lease_set LeaseSet) NewestExpiration() (newest Date, err error)
|
||||
```
|
||||
NewestExpiration returns the newest lease expiration as an I2P Date. Returns
|
||||
errors encountered during parsing.
|
||||
|
||||
#### func (LeaseSet) OldestExpiration
|
||||
|
||||
```go
|
||||
func (lease_set LeaseSet) OldestExpiration() (earliest Date, err error)
|
||||
```
|
||||
OldestExpiration returns the oldest lease expiration as an I2P Date. Returns
|
||||
errors encountered during parsing.
|
||||
|
||||
#### func (LeaseSet) PublicKey
|
||||
|
||||
```go
|
||||
func (lease_set LeaseSet) PublicKey() (public_key crypto.ElgPublicKey, err error)
|
||||
```
|
||||
PublicKey returns the public key as crypto.ElgPublicKey. Returns errors
|
||||
encountered during parsing.
|
||||
|
||||
#### func (LeaseSet) Signature
|
||||
|
||||
```go
|
||||
func (lease_set LeaseSet) Signature() (signature Signature, err error)
|
||||
```
|
||||
Signature returns the signature as Signature. returns errors encountered during
|
||||
parsing.
|
||||
|
||||
#### func (LeaseSet) SigningKey
|
||||
|
||||
```go
|
||||
func (lease_set LeaseSet) SigningKey() (signing_public_key crypto.SigningPublicKey, err error)
|
||||
```
|
||||
SigningKey returns the signing public key as crypto.SigningPublicKey. returns
|
||||
errors encountered during parsing.
|
||||
|
||||
#### func (LeaseSet) Verify
|
||||
|
||||
```go
|
||||
func (lease_set LeaseSet) Verify() error
|
||||
```
|
||||
Verify returns nil
|
@ -1,9 +1,47 @@
|
||||
package common
|
||||
// Package lease_set implements the I2P LeastSet common data structure
|
||||
package lease_set
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/go-i2p/go-i2p/lib/util/logger"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
. "github.com/go-i2p/go-i2p/lib/common/certificate"
|
||||
. "github.com/go-i2p/go-i2p/lib/common/data"
|
||||
. "github.com/go-i2p/go-i2p/lib/common/destination"
|
||||
. "github.com/go-i2p/go-i2p/lib/common/key_certificate"
|
||||
. "github.com/go-i2p/go-i2p/lib/common/keys_and_cert"
|
||||
. "github.com/go-i2p/go-i2p/lib/common/lease"
|
||||
. "github.com/go-i2p/go-i2p/lib/common/signature"
|
||||
"github.com/go-i2p/go-i2p/lib/crypto"
|
||||
)
|
||||
|
||||
var log = logger.GetGoI2PLogger()
|
||||
|
||||
// Sizes of various structures in an I2P LeaseSet
|
||||
const (
|
||||
LEASE_SET_PUBKEY_SIZE = 256
|
||||
LEASE_SET_SPK_SIZE = 128
|
||||
LEASE_SET_SIG_SIZE = 40
|
||||
)
|
||||
|
||||
/*
|
||||
I2P LeaseSet
|
||||
https://geti2p.net/spec/common-structures#leaseset
|
||||
Accurate for version 0.9.24
|
||||
[LeaseSet]
|
||||
Accurate for version 0.9.49
|
||||
|
||||
Description
|
||||
Contains all of the currently authorized Leases for a particular Destination, the
|
||||
publicKey to which garlic messages can be encrypted, and then the signingPublicKey
|
||||
that can be used to revoke this particular version of the structure. The LeaseSet is one
|
||||
of the two structures stored in the network database (the other being RouterInfo), and
|
||||
is kered under the SHA256 of the contained Destination.
|
||||
|
||||
Contents
|
||||
Destination, followed by a publicKey for encryption, then a signingPublicKey which
|
||||
can be used to revoke this version of the LeaseSet, then a 1 byte Integer specifying how
|
||||
many Lease structures are in the set, followed by the actual Lease structures and
|
||||
finally a Signature of the previous bytes signed by the Destination's SigningPrivateKey.
|
||||
|
||||
+----+----+----+----+----+----+----+----+
|
||||
| destination |
|
||||
@ -62,10 +100,10 @@ Accurate for version 0.9.24
|
||||
destination :: Destination
|
||||
length -> >= 387 bytes
|
||||
|
||||
encryption_key :: PublicKey
|
||||
encryption_key :: publicKey
|
||||
length -> 256 bytes
|
||||
|
||||
signing_key :: SigningPublicKey
|
||||
signing_key :: signingPublicKey
|
||||
length -> 128 bytes or as specified in destination's key certificate
|
||||
|
||||
num :: Integer
|
||||
@ -80,39 +118,46 @@ signature :: Signature
|
||||
length -> 40 bytes or as specified in destination's key certificate
|
||||
*/
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/go-i2p/go-i2p/lib/crypto"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Sizes of various structures in an I2P LeaseSet
|
||||
const (
|
||||
LEASE_SET_PUBKEY_SIZE = 256
|
||||
LEASE_SET_SPK_SIZE = 128
|
||||
LEASE_SET_SIG_SIZE = 40
|
||||
)
|
||||
|
||||
// LeaseSet is the represenation of an I2P LeaseSet.
|
||||
//
|
||||
// https://geti2p.net/spec/common-structures#leaseset
|
||||
type LeaseSet []byte
|
||||
|
||||
//
|
||||
// Read a Destination from the LeaseSet.
|
||||
//
|
||||
/*
|
||||
type LeaseSet struct {
|
||||
Destination *Destination
|
||||
EncryptionKey *crypto.ElgPublicKey
|
||||
SigningKey *crypto.ElgPublicKey
|
||||
Size *Integer
|
||||
Leases []*Lease
|
||||
Signature *Signature
|
||||
}
|
||||
*/
|
||||
|
||||
// Destination returns the Destination as []byte.
|
||||
func (lease_set LeaseSet) Destination() (destination Destination, err error) {
|
||||
keys_and_cert, _, err := ReadKeysAndCert(lease_set)
|
||||
destination = Destination(keys_and_cert)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to read KeysAndCert from LeaseSet")
|
||||
return
|
||||
}
|
||||
destination, _, err = ReadDestination(keys_and_cert.Bytes())
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to read Destination from KeysAndCert")
|
||||
} else {
|
||||
log.Debug("Successfully retrieved Destination from LeaseSet")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Return the PublicKey in this LeaseSet and any errors ancountered parsing the LeaseSet.
|
||||
//
|
||||
// PublicKey returns the public key as crypto.ElgPublicKey.
|
||||
// Returns errors encountered during parsing.
|
||||
func (lease_set LeaseSet) PublicKey() (public_key crypto.ElgPublicKey, err error) {
|
||||
_, remainder, err := ReadKeysAndCert(lease_set)
|
||||
remainder_len := len(remainder)
|
||||
if remainder_len < LEASE_SET_PUBKEY_SIZE {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(LeaseSet) PublicKey",
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "(LeaseSet) publicKey",
|
||||
"data_len": remainder_len,
|
||||
"required_len": LEASE_SET_PUBKEY_SIZE,
|
||||
"reason": "not enough data",
|
||||
@ -122,30 +167,29 @@ func (lease_set LeaseSet) PublicKey() (public_key crypto.ElgPublicKey, err error
|
||||
return
|
||||
}
|
||||
copy(public_key[:], remainder[:LEASE_SET_PUBKEY_SIZE])
|
||||
log.Debug("Successfully retrieved publicKey from LeaseSet")
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Return the SigningPublicKey, as specified in the LeaseSet's Destination's Key Certificate if
|
||||
// present, or a legacy DSA key.
|
||||
//
|
||||
// SigningKey returns the signing public key as crypto.SigningPublicKey.
|
||||
// returns errors encountered during parsing.
|
||||
func (lease_set LeaseSet) SigningKey() (signing_public_key crypto.SigningPublicKey, err error) {
|
||||
log.Debug("Retrieving SigningKey from LeaseSet")
|
||||
destination, err := lease_set.Destination()
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to retrieve Destination for SigningKey")
|
||||
return
|
||||
}
|
||||
offset := len(destination) + LEASE_SET_PUBKEY_SIZE
|
||||
cert, err := destination.Certificate()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
cert_len, err := cert.Length()
|
||||
offset := len(destination.Bytes()) + LEASE_SET_PUBKEY_SIZE
|
||||
cert := destination.Certificate()
|
||||
cert_len := cert.Length()
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to get Certificate length")
|
||||
return
|
||||
}
|
||||
lease_set_len := len(lease_set)
|
||||
if lease_set_len < offset+LEASE_SET_SPK_SIZE {
|
||||
log.WithFields(log.Fields{
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "(LeaseSet) SigningKey",
|
||||
"data_len": lease_set_len,
|
||||
"required_len": offset + LEASE_SET_SPK_SIZE,
|
||||
@ -156,43 +200,50 @@ func (lease_set LeaseSet) SigningKey() (signing_public_key crypto.SigningPublicK
|
||||
}
|
||||
if cert_len == 0 {
|
||||
// No Certificate is present, return the LEASE_SET_SPK_SIZE byte
|
||||
// SigningPublicKey space as legacy DSA SHA1 SigningPublicKey.
|
||||
// signingPublicKey space as legacy DSA SHA1 signingPublicKey.
|
||||
var dsa_pk crypto.DSAPublicKey
|
||||
copy(dsa_pk[:], lease_set[offset:offset+LEASE_SET_SPK_SIZE])
|
||||
signing_public_key = dsa_pk
|
||||
log.Debug("Retrieved legacy DSA SHA1 signingPublicKey")
|
||||
} else {
|
||||
// A Certificate is present in this LeaseSet's Destination
|
||||
cert_type, _ := cert.Type()
|
||||
cert_type := cert.Type()
|
||||
if cert_type == CERT_KEY {
|
||||
// This LeaseSet's Destination's Certificate is a Key Certificate,
|
||||
// create the signing publickey key using any data that might be
|
||||
// contained in the key certificate.
|
||||
signing_public_key, err = KeyCertificate(cert).ConstructSigningPublicKey(
|
||||
signing_public_key, err = KeyCertificateFromCertificate(cert).ConstructSigningPublicKey(
|
||||
lease_set[offset : offset+LEASE_SET_SPK_SIZE],
|
||||
)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to construct signingPublicKey from keyCertificate")
|
||||
} else {
|
||||
log.Debug("Retrieved signingPublicKey from keyCertificate")
|
||||
}
|
||||
} else {
|
||||
// No Certificate is present, return the LEASE_SET_SPK_SIZE byte
|
||||
// SigningPublicKey space as legacy DSA SHA1 SigningPublicKey.
|
||||
// signingPublicKey space as legacy DSA SHA1 signingPublicKey.
|
||||
var dsa_pk crypto.DSAPublicKey
|
||||
copy(dsa_pk[:], lease_set[offset:offset+LEASE_SET_SPK_SIZE])
|
||||
signing_public_key = dsa_pk
|
||||
log.Debug("Retrieved legacy DSA SHA1 signingPublicKey (Certificate present but not Key Certificate)")
|
||||
}
|
||||
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Return the number of Leases specified by the LeaseCount value in this LeaseSet.
|
||||
//
|
||||
// LeaseCount returns the numbert of leases specified by the LeaseCount value as int.
|
||||
// returns errors encountered during parsing.
|
||||
func (lease_set LeaseSet) LeaseCount() (count int, err error) {
|
||||
log.Debug("Retrieving LeaseCount from LeaseSet")
|
||||
_, remainder, err := ReadKeysAndCert(lease_set)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to read KeysAndCert for LeaseCount")
|
||||
return
|
||||
}
|
||||
remainder_len := len(remainder)
|
||||
if remainder_len < LEASE_SET_PUBKEY_SIZE+LEASE_SET_SPK_SIZE+1 {
|
||||
log.WithFields(log.Fields{
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "(LeaseSet) LeaseCount",
|
||||
"data_len": remainder_len,
|
||||
"required_len": LEASE_SET_PUBKEY_SIZE + LEASE_SET_SPK_SIZE + 1,
|
||||
@ -201,29 +252,34 @@ func (lease_set LeaseSet) LeaseCount() (count int, err error) {
|
||||
err = errors.New("error parsing lease count: not enough data")
|
||||
return
|
||||
}
|
||||
count = Integer([]byte{remainder[LEASE_SET_PUBKEY_SIZE+LEASE_SET_SPK_SIZE]})
|
||||
c := Integer([]byte{remainder[LEASE_SET_PUBKEY_SIZE+LEASE_SET_SPK_SIZE]})
|
||||
count = c.Int()
|
||||
if count > 16 {
|
||||
log.WithFields(log.Fields{
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "(LeaseSet) LeaseCount",
|
||||
"lease_count": count,
|
||||
"reason": "more than 16 leases",
|
||||
}).Warn("invalid lease set")
|
||||
err = errors.New("invalid lease set: more than 16 leases")
|
||||
} else {
|
||||
log.WithField("lease_count", count).Debug("Retrieved LeaseCount from LeaseSet")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Read the Leases in this LeaseSet, returning a partial set if there is insufficient data.
|
||||
//
|
||||
// Leases returns the leases as []Lease.
|
||||
// returns errors encountered during parsing.
|
||||
func (lease_set LeaseSet) Leases() (leases []Lease, err error) {
|
||||
log.Debug("Retrieving Leases from LeaseSet")
|
||||
destination, err := lease_set.Destination()
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to retrieve Destination for Leases")
|
||||
return
|
||||
}
|
||||
offset := len(destination) + LEASE_SET_PUBKEY_SIZE + LEASE_SET_SPK_SIZE + 1
|
||||
offset := len(destination.Bytes()) + LEASE_SET_PUBKEY_SIZE + LEASE_SET_SPK_SIZE + 1
|
||||
count, err := lease_set.LeaseCount()
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to retrieve LeaseCount for Leases")
|
||||
return
|
||||
}
|
||||
for i := 0; i < count; i++ {
|
||||
@ -231,7 +287,7 @@ func (lease_set LeaseSet) Leases() (leases []Lease, err error) {
|
||||
end := start + LEASE_SIZE
|
||||
lease_set_len := len(lease_set)
|
||||
if lease_set_len < end {
|
||||
log.WithFields(log.Fields{
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "(LeaseSet) Leases",
|
||||
"data_len": lease_set_len,
|
||||
"required_len": end,
|
||||
@ -244,41 +300,40 @@ func (lease_set LeaseSet) Leases() (leases []Lease, err error) {
|
||||
copy(lease[:], lease_set[start:end])
|
||||
leases = append(leases, lease)
|
||||
}
|
||||
log.WithField("lease_count", len(leases)).Debug("Retrieved Leases from LeaseSet")
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Return the Signature data for the LeaseSet, as specified in the Destination's
|
||||
// Key Certificate if present or the 40 bytes following the Leases.
|
||||
//
|
||||
// Signature returns the signature as Signature.
|
||||
// returns errors encountered during parsing.
|
||||
func (lease_set LeaseSet) Signature() (signature Signature, err error) {
|
||||
log.Debug("Retrieving Signature from LeaseSet")
|
||||
destination, err := lease_set.Destination()
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to retrieve Destination for Signature")
|
||||
return
|
||||
}
|
||||
lease_count, err := lease_set.LeaseCount()
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to retrieve LeaseCount for Signature")
|
||||
return
|
||||
}
|
||||
start := len(destination) +
|
||||
start := len(destination.Bytes()) +
|
||||
LEASE_SET_PUBKEY_SIZE +
|
||||
LEASE_SET_SPK_SIZE +
|
||||
1 +
|
||||
(LEASE_SIZE * lease_count)
|
||||
cert, err := destination.Certificate()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
cert_type, _ := cert.Type()
|
||||
cert := destination.Certificate()
|
||||
cert_type := cert.Type()
|
||||
var end int
|
||||
if cert_type == CERT_KEY {
|
||||
end = start + KeyCertificate(cert).SignatureSize()
|
||||
end = start + KeyCertificateFromCertificate(cert).SignatureSize()
|
||||
} else {
|
||||
end = start + LEASE_SET_SIG_SIZE
|
||||
}
|
||||
lease_set_len := len(lease_set)
|
||||
if lease_set_len < end {
|
||||
log.WithFields(log.Fields{
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "(LeaseSet) Signature",
|
||||
"data_len": lease_set_len,
|
||||
"required_len": end,
|
||||
@ -288,13 +343,13 @@ func (lease_set LeaseSet) Signature() (signature Signature, err error) {
|
||||
return
|
||||
}
|
||||
signature = []byte(lease_set[start:end])
|
||||
log.WithField("signature_length", len(signature)).Debug("Retrieved Signature from LeaseSet")
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
//
|
||||
//
|
||||
// Verify returns nil
|
||||
func (lease_set LeaseSet) Verify() error {
|
||||
log.Debug("Verifying LeaseSet")
|
||||
//data_end := len(destination) +
|
||||
// LEASE_SET_PUBKEY_SIZE +
|
||||
// LEASE_SET_SPK_SIZE +
|
||||
@ -303,38 +358,42 @@ func (lease_set LeaseSet) Verify() error {
|
||||
//data := lease_set[:data_end]
|
||||
//spk, _ := lease_set.
|
||||
// Destination().
|
||||
// SigningPublicKey()
|
||||
// signingPublicKey()
|
||||
//verifier, err := spk.NewVerifier()
|
||||
//if err != nil {
|
||||
// return err
|
||||
//}
|
||||
log.Warn("LeaseSet verification not implemented")
|
||||
return nil // verifier.Verify(data, lease_set.Signature())
|
||||
}
|
||||
|
||||
//
|
||||
// Return the oldest date from all the Leases in the LeaseSet.
|
||||
//
|
||||
func (lease_set LeaseSet) NewestExpiration() (oldest Date, err error) {
|
||||
// NewestExpiration returns the newest lease expiration as an I2P Date.
|
||||
// Returns errors encountered during parsing.
|
||||
func (lease_set LeaseSet) NewestExpiration() (newest Date, err error) {
|
||||
log.Debug("Finding newest expiration in LeaseSet")
|
||||
leases, err := lease_set.Leases()
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to retrieve Leases for NewestExpiration")
|
||||
return
|
||||
}
|
||||
oldest = Date{0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
|
||||
newest = Date{0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
|
||||
for _, lease := range leases {
|
||||
date := lease.Date()
|
||||
if date.Time().After(oldest.Time()) {
|
||||
oldest = date
|
||||
if date.Time().After(newest.Time()) {
|
||||
newest = date
|
||||
}
|
||||
}
|
||||
log.WithField("newest_expiration", newest.Time()).Debug("Found newest expiration in LeaseSet")
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Return the oldest date from all the Leases in the LeaseSet.
|
||||
//
|
||||
// OldestExpiration returns the oldest lease expiration as an I2P Date.
|
||||
// Returns errors encountered during parsing.
|
||||
func (lease_set LeaseSet) OldestExpiration() (earliest Date, err error) {
|
||||
log.Debug("Finding oldest expiration in LeaseSet")
|
||||
leases, err := lease_set.Leases()
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to retrieve Leases for OldestExpiration")
|
||||
return
|
||||
}
|
||||
earliest = Date{0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
|
||||
@ -344,5 +403,6 @@ func (lease_set LeaseSet) OldestExpiration() (earliest Date, err error) {
|
||||
earliest = date
|
||||
}
|
||||
}
|
||||
log.WithField("oldest_expiration", earliest.Time()).Debug("Found oldest expiration in LeaseSet")
|
||||
return
|
||||
}
|
@ -1,15 +1,22 @@
|
||||
package common
|
||||
package lease_set
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
|
||||
"github.com/go-i2p/go-i2p/lib/common/certificate"
|
||||
common "github.com/go-i2p/go-i2p/lib/common/data"
|
||||
"github.com/go-i2p/go-i2p/lib/common/lease"
|
||||
"github.com/go-i2p/go-i2p/lib/common/router_identity"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func buildDestination() RouterIdentity {
|
||||
func buildDestination() *router_identity.RouterIdentity {
|
||||
router_ident_data := make([]byte, 128+256)
|
||||
router_ident_data = append(router_ident_data, []byte{0x05, 0x00, 0x04, 0x00, 0x01, 0x00, 0x00}...)
|
||||
return RouterIdentity(router_ident_data)
|
||||
ident, _, err := router_identity.ReadRouterIdentity(router_ident_data)
|
||||
panic(err)
|
||||
return &ident
|
||||
}
|
||||
|
||||
func buildPublicKey() []byte {
|
||||
@ -31,15 +38,15 @@ func buildSigningKey() []byte {
|
||||
func buildLease(n int) []byte {
|
||||
data := make([]byte, 0)
|
||||
for i := 0; i < n; i++ {
|
||||
lease := make([]byte, LEASE_SIZE)
|
||||
for p := range lease {
|
||||
lease[p] = byte(i)
|
||||
l := make([]byte, lease.LEASE_SIZE)
|
||||
for p := range l {
|
||||
l[p] = byte(i)
|
||||
}
|
||||
for q := LEASE_SIZE - 9; q < LEASE_SIZE-1; q++ {
|
||||
lease[q] = 0x00
|
||||
for q := lease.LEASE_SIZE - 9; q < lease.LEASE_SIZE-1; q++ {
|
||||
l[q] = 0x00
|
||||
}
|
||||
lease[LEASE_SIZE-1] = byte(i + 10)
|
||||
data = append(data, lease...)
|
||||
l[lease.LEASE_SIZE-1] = byte(i + 10)
|
||||
data = append(data, l...)
|
||||
}
|
||||
return data
|
||||
}
|
||||
@ -54,7 +61,7 @@ func buildSignature(size int) []byte {
|
||||
|
||||
func buildFullLeaseSet(n int) LeaseSet {
|
||||
lease_set_data := make([]byte, 0)
|
||||
lease_set_data = append(lease_set_data, buildDestination()...)
|
||||
lease_set_data = append(lease_set_data, buildDestination().KeysAndCert.Bytes()...)
|
||||
lease_set_data = append(lease_set_data, buildPublicKey()...)
|
||||
lease_set_data = append(lease_set_data, buildSigningKey()...)
|
||||
lease_set_data = append(lease_set_data, byte(n))
|
||||
@ -69,11 +76,11 @@ func TestDestinationIsCorrect(t *testing.T) {
|
||||
lease_set := buildFullLeaseSet(1)
|
||||
dest, err := lease_set.Destination()
|
||||
assert.Nil(err)
|
||||
dest_cert, err := dest.Certificate()
|
||||
dest_cert := dest.Certificate()
|
||||
// assert.Nil(err)
|
||||
cert_type := dest_cert.Type()
|
||||
assert.Nil(err)
|
||||
cert_type, err := dest_cert.Type()
|
||||
assert.Nil(err)
|
||||
assert.Equal(CERT_KEY, cert_type)
|
||||
assert.Equal(certificate.CERT_KEY, cert_type)
|
||||
}
|
||||
|
||||
func TestPublicKeyIsCorrect(t *testing.T) {
|
||||
@ -142,18 +149,18 @@ func TestLeasesHaveCorrectData(t *testing.T) {
|
||||
leases, err := lease_set.Leases()
|
||||
if assert.Nil(err) {
|
||||
for i := 0; i < count; i++ {
|
||||
lease := make([]byte, LEASE_SIZE)
|
||||
for p := range lease {
|
||||
lease[p] = byte(i)
|
||||
l := make([]byte, lease.LEASE_SIZE)
|
||||
for p := range l {
|
||||
l[p] = byte(i)
|
||||
}
|
||||
for q := LEASE_SIZE - 9; q < LEASE_SIZE-1; q++ {
|
||||
lease[q] = 0x00
|
||||
for q := lease.LEASE_SIZE - 9; q < lease.LEASE_SIZE-1; q++ {
|
||||
l[q] = 0x00
|
||||
}
|
||||
lease[LEASE_SIZE-1] = byte(i + 10)
|
||||
l[lease.LEASE_SIZE-1] = byte(i + 10)
|
||||
assert.Equal(
|
||||
0,
|
||||
bytes.Compare(
|
||||
lease,
|
||||
l,
|
||||
leases[i][:],
|
||||
),
|
||||
)
|
||||
@ -184,8 +191,9 @@ func TestNewestExpirationIsCorrect(t *testing.T) {
|
||||
lease_set := buildFullLeaseSet(5)
|
||||
latest, err := lease_set.NewestExpiration()
|
||||
assert.Nil(err)
|
||||
Date, _, err := common.NewDate([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, byte(4 + 10)})
|
||||
assert.Equal(
|
||||
Date{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, byte(4 + 10)},
|
||||
Date,
|
||||
latest,
|
||||
)
|
||||
}
|
||||
@ -196,8 +204,9 @@ func TestOldestExpirationIsCorrect(t *testing.T) {
|
||||
lease_set := buildFullLeaseSet(5)
|
||||
latest, err := lease_set.OldestExpiration()
|
||||
assert.Nil(err)
|
||||
Date, _, err := common.NewDate([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a})
|
||||
assert.Equal(
|
||||
Date{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a},
|
||||
Date,
|
||||
latest,
|
||||
)
|
||||
}
|
@ -1,228 +0,0 @@
|
||||
package common
|
||||
|
||||
/*
|
||||
I2P Mapping
|
||||
https://geti2p.net/spec/common-structures#mapping
|
||||
Accurate for version 0.9.24
|
||||
|
||||
+----+----+----+----+----+----+----+----+
|
||||
| size |key_string (len + data) | = |
|
||||
+----+----+----+----+----+----+----+----+
|
||||
| val_string (len + data) | ; | ...
|
||||
+----+----+----+----+----+----+----+
|
||||
size :: Integer
|
||||
length -> 2 bytes
|
||||
Total number of bytes that follow
|
||||
|
||||
key_string :: String
|
||||
A string (one byte length followed by UTF-8 encoded characters)
|
||||
|
||||
= :: A single byte containing '='
|
||||
|
||||
val_string :: String
|
||||
A string (one byte length followed by UTF-8 encoded characters)
|
||||
|
||||
; :: A single byte containing ';'
|
||||
*/
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"sort"
|
||||
)
|
||||
|
||||
type Mapping []byte
|
||||
|
||||
// Parsed key-values pairs inside a Mapping.
|
||||
type MappingValues [][2]String
|
||||
|
||||
//
|
||||
// Returns the values contained in a Mapping in the form of a MappingValues.
|
||||
//
|
||||
func (mapping Mapping) Values() (map_values MappingValues, errs []error) {
|
||||
var str String
|
||||
var remainder = mapping
|
||||
var err error
|
||||
|
||||
length := Integer(remainder[:2])
|
||||
inferred_length := length + 2
|
||||
remainder = remainder[2:]
|
||||
mapping_len := len(mapping)
|
||||
if mapping_len > inferred_length {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(Mapping) Values",
|
||||
"mappnig_bytes_length": mapping_len,
|
||||
"mapping_length_field": length,
|
||||
"expected_bytes_length": inferred_length,
|
||||
"reason": "data longer than expected",
|
||||
}).Warn("mapping format warning")
|
||||
errs = append(errs, errors.New("warning parsing mapping: data exists beyond length of mapping"))
|
||||
} else if inferred_length > mapping_len {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(Mapping) Values",
|
||||
"mappnig_bytes_length": mapping_len,
|
||||
"mapping_length_field": length,
|
||||
"expected_bytes_length": inferred_length,
|
||||
"reason": "data shorter than expected",
|
||||
}).Warn("mapping format warning")
|
||||
errs = append(errs, errors.New("warning parsing mapping: mapping length exceeds provided data"))
|
||||
}
|
||||
|
||||
for {
|
||||
// Read a key, breaking on fatal errors
|
||||
// and appending warnings
|
||||
str, remainder, err = ReadString(remainder)
|
||||
key_str := str
|
||||
if err != nil {
|
||||
if stopValueRead(err) {
|
||||
errs = append(errs, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
if !beginsWith(remainder, 0x3d) {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(Mapping) Values",
|
||||
"reason": "expected =",
|
||||
}).Warn("mapping format violation")
|
||||
errs = append(errs, errors.New("mapping format violation, expected ="))
|
||||
return
|
||||
}
|
||||
remainder = remainder[1:]
|
||||
|
||||
// Read a value, breaking on fatal errors
|
||||
// and appending warnings
|
||||
str, remainder, err = ReadString(remainder)
|
||||
val_str := str
|
||||
if err != nil {
|
||||
if stopValueRead(err) {
|
||||
errs = append(errs, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
if !beginsWith(remainder, 0x3b) {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(Mapping) Values",
|
||||
"reason": "expected ;",
|
||||
}).Warn("mapping format violation")
|
||||
errs = append(errs, errors.New("mapping format violation, expected ;"))
|
||||
return
|
||||
}
|
||||
remainder = remainder[1:]
|
||||
|
||||
// Append the key-value pair and break if there is no more data to read
|
||||
map_values = append(map_values, [2]String{key_str, val_str})
|
||||
if len(remainder) == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Return true if two keys in a mapping are identical.
|
||||
//
|
||||
func (mapping Mapping) HasDuplicateKeys() bool {
|
||||
seen_values := make(map[string]bool)
|
||||
values, _ := mapping.Values()
|
||||
for _, pair := range values {
|
||||
key, _ := pair[0].Data()
|
||||
if _, present := seen_values[key]; present {
|
||||
return true
|
||||
} else {
|
||||
seen_values[key] = true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
//
|
||||
// Convert a MappingValue struct to a Mapping. The values are first
|
||||
// sorted in the order defined in mappingOrder.
|
||||
//
|
||||
func ValuesToMapping(values MappingValues) (mapping Mapping) {
|
||||
mappingOrder(values)
|
||||
for _, kv_pair := range values {
|
||||
key_string := kv_pair[0]
|
||||
key_string = append(key_string, []byte("=")[0])
|
||||
key_value := kv_pair[1]
|
||||
key_value = append(key_value, []byte(";")[0])
|
||||
mapping = append(append(mapping, key_string...), key_value...)
|
||||
}
|
||||
map_len := len(mapping)
|
||||
len_bytes := make([]byte, 2)
|
||||
binary.BigEndian.PutUint16(len_bytes, uint16(map_len))
|
||||
mapping = append(len_bytes, mapping...)
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Convert a Go map of unformatted strings to a sorted Mapping.
|
||||
//
|
||||
func GoMapToMapping(gomap map[string]string) (mapping Mapping, err error) {
|
||||
map_vals := MappingValues{}
|
||||
for k, v := range gomap {
|
||||
key_str, kerr := ToI2PString(k)
|
||||
if kerr != nil {
|
||||
err = kerr
|
||||
return
|
||||
}
|
||||
val_str, verr := ToI2PString(v)
|
||||
if verr != nil {
|
||||
err = verr
|
||||
return
|
||||
}
|
||||
map_vals = append(
|
||||
map_vals,
|
||||
[2]String{key_str, val_str},
|
||||
)
|
||||
}
|
||||
mapping = ValuesToMapping(map_vals)
|
||||
return
|
||||
}
|
||||
|
||||
type byValue MappingValues
|
||||
|
||||
func (set byValue) Len() int { return len(set) }
|
||||
func (set byValue) Swap(i, j int) { set[i], set[j] = set[j], set[i] }
|
||||
func (set byValue) Less(i, j int) bool {
|
||||
data1, _ := set[i][1].Data()
|
||||
data2, _ := set[j][1].Data()
|
||||
return data1 < data2
|
||||
}
|
||||
|
||||
type byKey MappingValues
|
||||
|
||||
func (set byKey) Len() int { return len(set) }
|
||||
func (set byKey) Swap(i, j int) { set[i], set[j] = set[j], set[i] }
|
||||
func (set byKey) Less(i, j int) bool {
|
||||
data1, _ := set[i][0].Data()
|
||||
data2, _ := set[j][0].Data()
|
||||
return data1 < data2
|
||||
}
|
||||
|
||||
//
|
||||
// I2P Mappings require consistent order for for cryptographic signing, and sorting
|
||||
// by keys. When new Mappings are created, they are stable sorted first by values
|
||||
// than by keys to ensure a consistent order.
|
||||
//
|
||||
func mappingOrder(values MappingValues) {
|
||||
sort.Stable(byValue(values))
|
||||
sort.Stable(byKey(values))
|
||||
}
|
||||
|
||||
//
|
||||
// Check if the string parsing error indicates that the Mapping
|
||||
// should no longer be parsed.
|
||||
//
|
||||
func stopValueRead(err error) bool {
|
||||
return err.Error() == "error parsing string: zero length"
|
||||
}
|
||||
|
||||
//
|
||||
// Determine if the first byte in a slice of bytes is the provided byte.
|
||||
//
|
||||
func beginsWith(bytes []byte, chr byte) bool {
|
||||
return len(bytes) != 0 &&
|
||||
bytes[0] == chr
|
||||
}
|
@ -1,187 +0,0 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestValuesExclusesPairWithBadData(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
bad_key := Mapping([]byte{0x00, 0x0c, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b, 0x00})
|
||||
values, errs := bad_key.Values()
|
||||
|
||||
if assert.Equal(len(values), 1, "Values() did not return valid values when some values had bad key") {
|
||||
key, _ := values[0][0].Data()
|
||||
val, _ := values[0][1].Data()
|
||||
assert.Equal(key, "a", "Values() returned by data with invalid key contains incorrect present key")
|
||||
assert.Equal(val, "b", "Values() returned by data with invalid key contains incorrect present key")
|
||||
}
|
||||
assert.Equal(len(errs), 2, "Values() reported wrong error count when some values had invalid data")
|
||||
}
|
||||
|
||||
func TestValuesWarnsMissingData(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
mapping := Mapping([]byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62})
|
||||
_, errs := mapping.Values()
|
||||
|
||||
if assert.Equal(len(errs), 2, "Values() reported wrong error count when mapping had missing data") {
|
||||
assert.Equal(errs[0].Error(), "warning parsing mapping: mapping length exceeds provided data", "correct error message should be returned")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValuesWarnsExtraData(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
mapping := Mapping([]byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b, 0x00})
|
||||
_, errs := mapping.Values()
|
||||
|
||||
if assert.Equal(len(errs), 2, "Values() reported wrong error count when mapping had extra data") {
|
||||
assert.Equal(errs[0].Error(), "warning parsing mapping: data exists beyond length of mapping", "correct error message should be returned")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValuesEnforcesEqualDelimitor(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
mapping := Mapping([]byte{0x00, 0x06, 0x01, 0x61, 0x30, 0x01, 0x62, 0x3b})
|
||||
values, errs := mapping.Values()
|
||||
|
||||
if assert.Equal(len(errs), 1, "Values() reported wrong error count when mapping had = format error") {
|
||||
assert.Equal(errs[0].Error(), "mapping format violation, expected =", "correct error message should be returned")
|
||||
}
|
||||
assert.Equal(len(values), 0, "Values() not empty with invalid data due to = format error")
|
||||
}
|
||||
|
||||
func TestValuesEnforcedSemicolonDelimitor(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
mapping := Mapping([]byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x30})
|
||||
values, errs := mapping.Values()
|
||||
|
||||
if assert.Equal(len(errs), 1, "Values() reported wrong error count when mapping had ; format error") {
|
||||
assert.Equal(errs[0].Error(), "mapping format violation, expected ;", "correct error message should be returned")
|
||||
}
|
||||
assert.Equal(len(values), 0, "Values() not empty with invalid data due to ; format error")
|
||||
}
|
||||
|
||||
func TestValuesReturnsValues(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
mapping := Mapping([]byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b})
|
||||
values, errs := mapping.Values()
|
||||
key, kerr := values[0][0].Data()
|
||||
val, verr := values[0][1].Data()
|
||||
|
||||
assert.Nil(errs, "Values() returned a errors with parsing valid data")
|
||||
assert.Nil(kerr)
|
||||
assert.Nil(verr)
|
||||
assert.Equal(key, "a", "Values() did not return key in valid data")
|
||||
assert.Equal(val, "b", "Values() did not return value in valid data")
|
||||
}
|
||||
|
||||
func TestHasDuplicateKeysTrueWhenDuplicates(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
dups := Mapping([]byte{0x00, 0x0c, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b})
|
||||
|
||||
assert.Equal(dups.HasDuplicateKeys(), true, "HasDuplicateKeys() did not report true when duplicate keys present")
|
||||
}
|
||||
|
||||
func TestHasDuplicateKeysFalseWithoutDuplicates(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
mapping := Mapping([]byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b})
|
||||
|
||||
assert.Equal(mapping.HasDuplicateKeys(), false, "HasDuplicateKeys() did not report false when no duplicate keys present")
|
||||
}
|
||||
|
||||
func TestGoMapToMappingProducesCorrectMapping(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
gomap := map[string]string{"a": "b"}
|
||||
mapping, err := GoMapToMapping(gomap)
|
||||
|
||||
assert.Nil(err, "GoMapToMapping() returned error with valid data")
|
||||
expected := []byte{0x00, 0x06, 0x01, 0x61, 0x3d, 0x01, 0x62, 0x3b}
|
||||
if bytes.Compare(mapping, expected) != 0 {
|
||||
t.Fatal("GoMapToMapping did not produce correct Mapping", mapping, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMappingOrderSortsValuesThenKeys(t *testing.T) {
|
||||
a, _ := ToI2PString("a")
|
||||
b, _ := ToI2PString("b")
|
||||
values := MappingValues{
|
||||
[2]String{b, b},
|
||||
[2]String{b, a},
|
||||
[2]String{a, b},
|
||||
[2]String{a, a},
|
||||
}
|
||||
mappingOrder(values)
|
||||
for i, pair := range values {
|
||||
key, _ := pair[0].Data()
|
||||
value, _ := pair[1].Data()
|
||||
switch i {
|
||||
case 0:
|
||||
if !(key == "a" && value == "a") {
|
||||
t.Fatal("mappingOrder produced incorrect sort output at", i)
|
||||
}
|
||||
case 1:
|
||||
if !(key == "a" && value == "b") {
|
||||
t.Fatal("mappingOrder produced incorrect sort output at", i)
|
||||
}
|
||||
case 2:
|
||||
if !(key == "b" && value == "a") {
|
||||
t.Fatal("mappingOrder produced incorrect sort output at", i)
|
||||
}
|
||||
case 3:
|
||||
if !(key == "b" && value == "b") {
|
||||
t.Fatal("mappingOrder produced incorrect sort output at", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStopValueReadTrueWhenCorrectErr(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
status := stopValueRead(errors.New("error parsing string: zero length"))
|
||||
|
||||
assert.Equal(status, true, "stopValueRead() did not return true when String error found")
|
||||
}
|
||||
|
||||
func TestStopValueReadFalseWhenWrongErr(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
status := stopValueRead(errors.New("something else"))
|
||||
|
||||
assert.Equal(status, false, "stopValueRead() did not return false when non String error found")
|
||||
}
|
||||
|
||||
func TestBeginsWithCorrectWhenTrue(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
slice := []byte{0x41}
|
||||
|
||||
assert.Equal(beginsWith(slice, 0x41), true, "beginsWith() did not return true when correct")
|
||||
}
|
||||
|
||||
func TestBeginsWithCorrectWhenFalse(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
slice := []byte{0x00}
|
||||
|
||||
assert.Equal(beginsWith(slice, 0x41), false, "beginsWith() did not false when incorrect")
|
||||
}
|
||||
|
||||
func TestBeginsWithCorrectWhenNil(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
slice := make([]byte, 0)
|
||||
|
||||
assert.Equal(beginsWith(slice, 0x41), false, "beginsWith() did not return false on empty slice")
|
||||
}
|
@ -1,161 +0,0 @@
|
||||
package common
|
||||
|
||||
/*
|
||||
I2P RouterAddress
|
||||
https://geti2p.net/spec/common-structures#routeraddress
|
||||
Accurate for version 0.9.24
|
||||
|
||||
+----+----+----+----+----+----+----+----+
|
||||
|cost| expiration
|
||||
+----+----+----+----+----+----+----+----+
|
||||
| transport_style |
|
||||
+----+----+----+----+-//-+----+----+----+
|
||||
| |
|
||||
+ +
|
||||
| options |
|
||||
~ ~
|
||||
~ ~
|
||||
| |
|
||||
+----+----+----+----+----+----+----+----+
|
||||
|
||||
cost :: Integer
|
||||
length -> 1 byte
|
||||
|
||||
case 0 -> free
|
||||
case 255 -> expensive
|
||||
|
||||
expiration :: Date (must be all zeros, see notes below)
|
||||
length -> 8 bytes
|
||||
|
||||
case null -> never expires
|
||||
|
||||
transport_style :: String
|
||||
length -> 1-256 bytes
|
||||
|
||||
options :: Mapping
|
||||
*/
|
||||
|
||||
import (
|
||||
"errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Minimum number of bytes in a valid RouterAddress
|
||||
const (
|
||||
ROUTER_ADDRESS_MIN_SIZE = 9
|
||||
)
|
||||
|
||||
type RouterAddress []byte
|
||||
|
||||
//
|
||||
// Return the cost integer for this RouterAddress and any errors encountered
|
||||
// parsing the RouterAddress.
|
||||
//
|
||||
func (router_address RouterAddress) Cost() (cost int, err error) {
|
||||
err, exit := router_address.checkValid()
|
||||
if exit {
|
||||
return
|
||||
}
|
||||
cost = Integer([]byte{router_address[0]})
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Return the Date this RouterAddress expires and any errors encountered
|
||||
// parsing the RouterAddress.
|
||||
//
|
||||
func (router_address RouterAddress) Expiration() (date Date, err error) {
|
||||
err, exit := router_address.checkValid()
|
||||
if exit {
|
||||
return
|
||||
}
|
||||
copy(date[:], router_address[1:ROUTER_ADDRESS_MIN_SIZE])
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Return the Transport type for this RouterAddress and any errors encountered
|
||||
// parsing the RouterAddress.
|
||||
//
|
||||
func (router_address RouterAddress) TransportStyle() (str String, err error) {
|
||||
err, exit := router_address.checkValid()
|
||||
if exit {
|
||||
return
|
||||
}
|
||||
str, _, err = ReadString(router_address[ROUTER_ADDRESS_MIN_SIZE:])
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Return the Mapping containing the options for this RouterAddress and any
|
||||
// errors encountered parsing the RouterAddress.
|
||||
//
|
||||
func (router_address RouterAddress) Options() (mapping Mapping, err error) {
|
||||
err, exit := router_address.checkValid()
|
||||
if exit {
|
||||
return
|
||||
}
|
||||
_, remainder, err := ReadString(router_address[ROUTER_ADDRESS_MIN_SIZE:])
|
||||
if len(remainder) == 0 {
|
||||
return
|
||||
}
|
||||
mapping = Mapping(remainder)
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Check if the RouterAddress is empty or if it is too small to contain valid data.
|
||||
//
|
||||
func (router_address RouterAddress) checkValid() (err error, exit bool) {
|
||||
addr_len := len(router_address)
|
||||
exit = false
|
||||
if addr_len == 0 {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(RouterAddress) checkValid",
|
||||
"reason": "no data",
|
||||
}).Error("invalid router address")
|
||||
err = errors.New("error parsing RouterAddress: no data")
|
||||
exit = true
|
||||
} else if addr_len < ROUTER_ADDRESS_MIN_SIZE {
|
||||
log.WithFields(log.Fields{
|
||||
"at": "(RouterAddress) checkValid",
|
||||
"reason": "data too small (len < ROUTER_ADDRESS_MIN_SIZE)",
|
||||
}).Warn("router address format warning")
|
||||
err = errors.New("warning parsing RouterAddress: data too small")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//
|
||||
// Given a slice of bytes, read a RouterAddress, returning the remaining bytes and any
|
||||
// errors encountered parsing the RouterAddress.
|
||||
//
|
||||
func ReadRouterAddress(data []byte) (router_address RouterAddress, remainder []byte, err error) {
|
||||
test_address := RouterAddress(data)
|
||||
err, _ = test_address.checkValid()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
router_address = append(router_address, data[:ROUTER_ADDRESS_MIN_SIZE]...)
|
||||
str, remainder, err := ReadString(data[ROUTER_ADDRESS_MIN_SIZE:])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
router_address = append(router_address, str...)
|
||||
map_size := 0
|
||||
mapping := make([]byte, 0)
|
||||
if len(remainder) >= 2 {
|
||||
map_size = Integer(remainder[:2])
|
||||
if len(remainder) < map_size+2 {
|
||||
err = errors.New("not enough data for map inside router address")
|
||||
router_address = RouterAddress([]byte{})
|
||||
remainder = []byte{}
|
||||
return
|
||||
}
|
||||
mapping = remainder[:map_size+2]
|
||||
router_address = append(router_address, mapping...)
|
||||
}
|
||||
|
||||
remainder = data[ROUTER_ADDRESS_MIN_SIZE+len(str)+len(mapping):]
|
||||
return
|
||||
}
|
179
lib/common/router_address/doc.md
Normal file
179
lib/common/router_address/doc.md
Normal file
@ -0,0 +1,179 @@
|
||||
# router_address
|
||||
--
|
||||
import "github.com/go-i2p/go-i2p/lib/common/router_address"
|
||||
|
||||
Package router_address implements the I2P RouterAddress common data structure
|
||||
|
||||
## Usage
|
||||
|
||||
```go
|
||||
const (
|
||||
ROUTER_ADDRESS_MIN_SIZE = 9
|
||||
)
|
||||
```
|
||||
Minimum number of bytes in a valid RouterAddress
|
||||
|
||||
#### type RouterAddress
|
||||
|
||||
```go
|
||||
type RouterAddress struct {
|
||||
TransportCost *Integer
|
||||
ExpirationDate *Date
|
||||
TransportType I2PString
|
||||
TransportOptions *Mapping
|
||||
}
|
||||
```
|
||||
|
||||
RouterAddress is the represenation of an I2P RouterAddress.
|
||||
|
||||
https://geti2p.net/spec/common-structures#routeraddress
|
||||
|
||||
#### func ReadRouterAddress
|
||||
|
||||
```go
|
||||
func ReadRouterAddress(data []byte) (router_address RouterAddress, remainder []byte, err error)
|
||||
```
|
||||
ReadRouterAddress returns RouterAddress from a []byte. The remaining bytes after
|
||||
the specified length are also returned. Returns a list of errors that occurred
|
||||
during parsing.
|
||||
|
||||
#### func (RouterAddress) Bytes
|
||||
|
||||
```go
|
||||
func (router_address RouterAddress) Bytes() []byte
|
||||
```
|
||||
Bytes returns the router address as a []byte.
|
||||
|
||||
#### func (RouterAddress) Cost
|
||||
|
||||
```go
|
||||
func (router_address RouterAddress) Cost() int
|
||||
```
|
||||
Cost returns the cost for this RouterAddress as a Go integer.
|
||||
|
||||
#### func (RouterAddress) Expiration
|
||||
|
||||
```go
|
||||
func (router_address RouterAddress) Expiration() Date
|
||||
```
|
||||
Expiration returns the expiration for this RouterAddress as an I2P Date.
|
||||
|
||||
#### func (RouterAddress) GetOption
|
||||
|
||||
```go
|
||||
func (router_address RouterAddress) GetOption(key I2PString) I2PString
|
||||
```
|
||||
GetOption returns the value of the option specified by the key
|
||||
|
||||
#### func (RouterAddress) Host
|
||||
|
||||
```go
|
||||
func (router_address RouterAddress) Host() (net.Addr, error)
|
||||
```
|
||||
|
||||
#### func (RouterAddress) HostString
|
||||
|
||||
```go
|
||||
func (router_address RouterAddress) HostString() I2PString
|
||||
```
|
||||
|
||||
#### func (RouterAddress) InitializationVector
|
||||
|
||||
```go
|
||||
func (router_address RouterAddress) InitializationVector() ([32]byte, error)
|
||||
```
|
||||
|
||||
#### func (RouterAddress) InitializationVectorString
|
||||
|
||||
```go
|
||||
func (router_address RouterAddress) InitializationVectorString() I2PString
|
||||
```
|
||||
|
||||
#### func (RouterAddress) IntroducerExpirationString
|
||||
|
||||
```go
|
||||
func (router_address RouterAddress) IntroducerExpirationString(num int) I2PString
|
||||
```
|
||||
|
||||
#### func (RouterAddress) IntroducerHashString
|
||||
|
||||
```go
|
||||
func (router_address RouterAddress) IntroducerHashString(num int) I2PString
|
||||
```
|
||||
|
||||
#### func (RouterAddress) IntroducerTagString
|
||||
|
||||
```go
|
||||
func (router_address RouterAddress) IntroducerTagString(num int) I2PString
|
||||
```
|
||||
|
||||
#### func (*RouterAddress) Network
|
||||
|
||||
```go
|
||||
func (router_address *RouterAddress) Network() string
|
||||
```
|
||||
Network implements net.Addr. It returns the transport type
|
||||
|
||||
#### func (RouterAddress) Options
|
||||
|
||||
```go
|
||||
func (router_address RouterAddress) Options() Mapping
|
||||
```
|
||||
Options returns the options for this RouterAddress as an I2P Mapping.
|
||||
|
||||
#### func (RouterAddress) Port
|
||||
|
||||
```go
|
||||
func (router_address RouterAddress) Port() (string, error)
|
||||
```
|
||||
|
||||
#### func (RouterAddress) PortString
|
||||
|
||||
```go
|
||||
func (router_address RouterAddress) PortString() I2PString
|
||||
```
|
||||
|
||||
#### func (RouterAddress) ProtocolVersion
|
||||
|
||||
```go
|
||||
func (router_address RouterAddress) ProtocolVersion() (string, error)
|
||||
```
|
||||
|
||||
#### func (RouterAddress) ProtocolVersionString
|
||||
|
||||
```go
|
||||
func (router_address RouterAddress) ProtocolVersionString() I2PString
|
||||
```
|
||||
|
||||
#### func (RouterAddress) StaticKey
|
||||
|
||||
```go
|
||||
func (router_address RouterAddress) StaticKey() ([32]byte, error)
|
||||
```
|
||||
|
||||
#### func (RouterAddress) StaticKeyString
|
||||
|
||||
```go
|
||||
func (router_address RouterAddress) StaticKeyString() I2PString
|
||||
```
|
||||
|
||||
#### func (*RouterAddress) String
|
||||
|
||||
```go
|
||||
func (router_address *RouterAddress) String() string
|
||||
```
|
||||
String implements net.Addr. It returns the IP address, followed by the options
|
||||
|
||||
#### func (RouterAddress) TransportStyle
|
||||
|
||||
```go
|
||||
func (router_address RouterAddress) TransportStyle() I2PString
|
||||
```
|
||||
TransportStyle returns the transport style for this RouterAddress as an
|
||||
I2PString.
|
||||
|
||||
#### func (*RouterAddress) UDP
|
||||
|
||||
```go
|
||||
func (router_address *RouterAddress) UDP() bool
|
||||
```
|
410
lib/common/router_address/router_address.go
Normal file
410
lib/common/router_address/router_address.go
Normal file
@ -0,0 +1,410 @@
|
||||
// Package router_address implements the I2P RouterAddress common data structure
|
||||
package router_address
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-i2p/go-i2p/lib/util/logger"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
. "github.com/go-i2p/go-i2p/lib/common/data"
|
||||
)
|
||||
|
||||
// Minimum number of bytes in a valid RouterAddress
|
||||
const (
|
||||
ROUTER_ADDRESS_MIN_SIZE = 9
|
||||
)
|
||||
|
||||
var log = logger.GetGoI2PLogger()
|
||||
|
||||
/*
|
||||
[RouterAddress]
|
||||
Accurate for version 0.9.49
|
||||
|
||||
Description
|
||||
This structure defines the means to contact a router through a transport protocol.
|
||||
|
||||
Contents
|
||||
1 byte Integer defining the relative cost of using the address, where 0 is free and 255 is
|
||||
expensive, followed by the expiration Date after which the address should not be used,
|
||||
of if null, the address never expires. After that comes a String defining the transport
|
||||
protocol this router address uses. Finally there is a Mapping containing all of the
|
||||
transport specific options necessary to establish the connection, such as IP address,
|
||||
port number, email address, URL, etc.
|
||||
|
||||
|
||||
+----+----+----+----+----+----+----+----+
|
||||
|cost| expiration
|
||||
+----+----+----+----+----+----+----+----+
|
||||
| transport_style |
|
||||
+----+----+----+----+-//-+----+----+----+
|
||||
| |
|
||||
+ +
|
||||
| options |
|
||||
~ ~
|
||||
~ ~
|
||||
| |
|
||||
+----+----+----+----+----+----+----+----+
|
||||
|
||||
cost :: Integer
|
||||
length -> 1 byte
|
||||
|
||||
case 0 -> free
|
||||
case 255 -> expensive
|
||||
|
||||
expiration :: Date (must be all zeros, see notes below)
|
||||
length -> 8 bytes
|
||||
|
||||
case null -> never expires
|
||||
|
||||
transport_style :: String
|
||||
length -> 1-256 bytes
|
||||
|
||||
options :: Mapping
|
||||
*/
|
||||
|
||||
// RouterAddress is the represenation of an I2P RouterAddress.
|
||||
//
|
||||
// https://geti2p.net/spec/common-structures#routeraddress
|
||||
type RouterAddress struct {
|
||||
TransportCost *Integer
|
||||
ExpirationDate *Date
|
||||
TransportType I2PString
|
||||
TransportOptions *Mapping
|
||||
}
|
||||
|
||||
// Network implements net.Addr. It returns the transport type plus 4 or 6
|
||||
func (router_address *RouterAddress) Network() string {
|
||||
log.Debug("Getting network for RouterAddress")
|
||||
if router_address.TransportType == nil {
|
||||
log.Warn("TransportType is nil in RouterAddress")
|
||||
return ""
|
||||
}
|
||||
str, err := router_address.TransportType.Data()
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to get TransportType data")
|
||||
return ""
|
||||
}
|
||||
network := string(str) + router_address.IPVersion()
|
||||
log.WithField("network", network).Debug("Retrieved network for RouterAddress")
|
||||
return network
|
||||
}
|
||||
|
||||
// IPVersion returns a string "4" for IPv4 or 6 for IPv6
|
||||
func (router_address *RouterAddress) IPVersion() string {
|
||||
log.Debug("Getting IP version for RouterAddress")
|
||||
str, err := router_address.CapsString().Data()
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to get CapsString data")
|
||||
return ""
|
||||
}
|
||||
if strings.HasSuffix(str, "6") {
|
||||
log.Debug("IP version is IPv6")
|
||||
return "6"
|
||||
}
|
||||
log.Debug("IP version is IPv4")
|
||||
return "4"
|
||||
}
|
||||
|
||||
func (router_address *RouterAddress) UDP() bool {
|
||||
// return strings.HasPrefix(strings.ToLower(router_address.Network()), "ssu")
|
||||
log.Debug("Checking if RouterAddress is UDP")
|
||||
isUDP := strings.HasPrefix(strings.ToLower(router_address.Network()), "ssu")
|
||||
log.WithField("is_udp", isUDP).Debug("Checked if RouterAddress is UDP")
|
||||
return isUDP
|
||||
}
|
||||
|
||||
// String implements net.Addr. It returns the IP address, followed by the options
|
||||
func (router_address *RouterAddress) String() string {
|
||||
log.Debug("Converting RouterAddress to string")
|
||||
var rv []string
|
||||
rv = append(rv, string(router_address.TransportStyle()))
|
||||
rv = append(rv, string(router_address.HostString()))
|
||||
rv = append(rv, string(router_address.PortString()))
|
||||
rv = append(rv, string(router_address.StaticKeyString()))
|
||||
rv = append(rv, string(router_address.InitializationVectorString()))
|
||||
rv = append(rv, string(router_address.ProtocolVersionString()))
|
||||
if router_address.UDP() {
|
||||
rv = append(rv, string(router_address.IntroducerHashString(0)))
|
||||
rv = append(rv, string(router_address.IntroducerExpirationString(0)))
|
||||
rv = append(rv, string(router_address.IntroducerTagString(0)))
|
||||
rv = append(rv, string(router_address.IntroducerHashString(1)))
|
||||
rv = append(rv, string(router_address.IntroducerExpirationString(1)))
|
||||
rv = append(rv, string(router_address.IntroducerTagString(1)))
|
||||
rv = append(rv, string(router_address.IntroducerHashString(2)))
|
||||
rv = append(rv, string(router_address.IntroducerExpirationString(2)))
|
||||
rv = append(rv, string(router_address.IntroducerTagString(2)))
|
||||
}
|
||||
str := strings.TrimSpace(strings.Join(rv, " "))
|
||||
log.WithField("router_address_string", str).Debug("Converted RouterAddress to string")
|
||||
return str
|
||||
}
|
||||
|
||||
var ex_addr net.Addr = &RouterAddress{}
|
||||
|
||||
// Bytes returns the router address as a []byte.
|
||||
func (router_address RouterAddress) Bytes() []byte {
|
||||
log.Debug("Converting RouterAddress to bytes")
|
||||
bytes := make([]byte, 0)
|
||||
bytes = append(bytes, router_address.TransportCost.Bytes()...)
|
||||
bytes = append(bytes, router_address.ExpirationDate.Bytes()...)
|
||||
bytes = append(bytes, router_address.TransportType...)
|
||||
bytes = append(bytes, router_address.TransportOptions.Data()...)
|
||||
log.WithField("bytes_length", len(bytes)).Debug("Converted RouterAddress to bytes")
|
||||
return bytes
|
||||
}
|
||||
|
||||
// Cost returns the cost for this RouterAddress as a Go integer.
|
||||
func (router_address RouterAddress) Cost() int {
|
||||
return router_address.TransportCost.Int()
|
||||
}
|
||||
|
||||
// Expiration returns the expiration for this RouterAddress as an I2P Date.
|
||||
func (router_address RouterAddress) Expiration() Date {
|
||||
return *router_address.ExpirationDate
|
||||
}
|
||||
|
||||
// TransportStyle returns the transport style for this RouterAddress as an I2PString.
|
||||
func (router_address RouterAddress) TransportStyle() I2PString {
|
||||
return router_address.TransportType
|
||||
}
|
||||
|
||||
// GetOption returns the value of the option specified by the key
|
||||
func (router_address RouterAddress) GetOption(key I2PString) I2PString {
|
||||
return router_address.Options().Values().Get(key)
|
||||
}
|
||||
|
||||
func (router_address RouterAddress) HostString() I2PString {
|
||||
host, _ := ToI2PString("host")
|
||||
return router_address.GetOption(host)
|
||||
}
|
||||
|
||||
func (router_address RouterAddress) PortString() I2PString {
|
||||
port, _ := ToI2PString("port")
|
||||
return router_address.GetOption(port)
|
||||
}
|
||||
|
||||
func (router_address RouterAddress) CapsString() I2PString {
|
||||
caps, _ := ToI2PString("caps")
|
||||
return router_address.GetOption(caps)
|
||||
}
|
||||
|
||||
func (router_address RouterAddress) StaticKeyString() I2PString {
|
||||
sk, _ := ToI2PString("s")
|
||||
return router_address.GetOption(sk)
|
||||
}
|
||||
|
||||
func (router_address RouterAddress) InitializationVectorString() I2PString {
|
||||
iv, _ := ToI2PString("i")
|
||||
return router_address.GetOption(iv)
|
||||
}
|
||||
|
||||
func (router_address RouterAddress) ProtocolVersionString() I2PString {
|
||||
v, _ := ToI2PString("v")
|
||||
return router_address.GetOption(v)
|
||||
}
|
||||
|
||||
func (router_address RouterAddress) IntroducerHashString(num int) I2PString {
|
||||
if num >= 0 && num <= 2 {
|
||||
val := strconv.Itoa(num)
|
||||
v, _ := ToI2PString("ih" + val)
|
||||
return router_address.GetOption(v)
|
||||
}
|
||||
v, _ := ToI2PString("ih0")
|
||||
return router_address.GetOption(v)
|
||||
}
|
||||
|
||||
func (router_address RouterAddress) IntroducerExpirationString(num int) I2PString {
|
||||
if num >= 0 && num <= 2 {
|
||||
val := strconv.Itoa(num)
|
||||
v, _ := ToI2PString("iexp" + val)
|
||||
return router_address.GetOption(v)
|
||||
}
|
||||
v, _ := ToI2PString("iexp0")
|
||||
return router_address.GetOption(v)
|
||||
}
|
||||
|
||||
func (router_address RouterAddress) IntroducerTagString(num int) I2PString {
|
||||
if num >= 0 && num <= 2 {
|
||||
val := strconv.Itoa(num)
|
||||
v, _ := ToI2PString("itag" + val)
|
||||
return router_address.GetOption(v)
|
||||
}
|
||||
v, _ := ToI2PString("itag0")
|
||||
return router_address.GetOption(v)
|
||||
}
|
||||
|
||||
func (router_address RouterAddress) Host() (net.Addr, error) {
|
||||
log.Debug("Getting host from RouterAddress")
|
||||
host := router_address.HostString()
|
||||
hostBytes, err := host.Data()
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to get host data")
|
||||
return nil, err
|
||||
}
|
||||
ip := net.ParseIP(hostBytes)
|
||||
if ip == nil {
|
||||
log.Error("Failed to parse IP address")
|
||||
return nil, fmt.Errorf("null host error")
|
||||
}
|
||||
// return net.ResolveIPAddr("", ip.String())
|
||||
addr, err := net.ResolveIPAddr("", ip.String())
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to resolve IP address")
|
||||
} else {
|
||||
log.WithField("addr", addr).Debug("Retrieved host from RouterAddress")
|
||||
}
|
||||
return addr, err
|
||||
}
|
||||
|
||||
func (router_address RouterAddress) Port() (string, error) {
|
||||
log.Debug("Getting port from RouterAddress")
|
||||
port := router_address.PortString()
|
||||
portBytes, err := port.Data()
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to get port data")
|
||||
return "", err
|
||||
}
|
||||
val, err := strconv.Atoi(portBytes)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to convert port to integer")
|
||||
return "", err
|
||||
}
|
||||
// return strconv.Itoa(val), nil
|
||||
portStr := strconv.Itoa(val)
|
||||
log.WithField("port", portStr).Debug("Retrieved port from RouterAddress")
|
||||
return portStr, nil
|
||||
}
|
||||
|
||||
func (router_address RouterAddress) StaticKey() ([32]byte, error) {
|
||||
sk := router_address.StaticKeyString()
|
||||
if len([]byte(sk)) != 32 {
|
||||
return [32]byte{}, fmt.Errorf("error: invalid static key")
|
||||
}
|
||||
return [32]byte(sk), nil
|
||||
}
|
||||
|
||||
func (router_address RouterAddress) InitializationVector() ([32]byte, error) {
|
||||
iv := router_address.InitializationVectorString()
|
||||
if len([]byte(iv)) != 32 {
|
||||
return [32]byte{}, fmt.Errorf("error: invalid static key")
|
||||
}
|
||||
return [32]byte(iv), nil
|
||||
}
|
||||
|
||||
func (router_address RouterAddress) ProtocolVersion() (string, error) {
|
||||
return router_address.ProtocolVersionString().Data()
|
||||
}
|
||||
|
||||
// Options returns the options for this RouterAddress as an I2P Mapping.
|
||||
func (router_address RouterAddress) Options() Mapping {
|
||||
return *router_address.TransportOptions
|
||||
}
|
||||
|
||||
// Check if the RouterAddress is empty or if it is too small to contain valid data.
|
||||
func (router_address RouterAddress) checkValid() (err error, exit bool) {
|
||||
return
|
||||
}
|
||||
|
||||
// ReadRouterAddress returns RouterAddress from a []byte.
|
||||
// The remaining bytes after the specified length are also returned.
|
||||
// Returns a list of errors that occurred during parsing.
|
||||
func ReadRouterAddress(data []byte) (router_address RouterAddress, remainder []byte, err error) {
|
||||
log.WithField("data_length", len(data)).Debug("Reading RouterAddress from data")
|
||||
if len(data) == 0 {
|
||||
log.WithField("at", "(RouterAddress) ReadRouterAddress").Error("error parsing RouterAddress: no data")
|
||||
err = errors.New("error parsing RouterAddress: no data")
|
||||
return
|
||||
}
|
||||
router_address.TransportCost, remainder, err = NewInteger(data, 1)
|
||||
if err != nil {
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "(RouterAddress) ReadNewRouterAddress",
|
||||
"reason": "error parsing cost",
|
||||
}).Warn("error parsing RouterAddress")
|
||||
}
|
||||
router_address.ExpirationDate, remainder, err = NewDate(remainder)
|
||||
if err != nil {
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "(RouterAddress) ReadNewRouterAddress",
|
||||
"reason": "error parsing expiration",
|
||||
}).Error("error parsing RouterAddress")
|
||||
}
|
||||
router_address.TransportType, remainder, err = ReadI2PString(remainder)
|
||||
if err != nil {
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "(RouterAddress) ReadNewRouterAddress",
|
||||
"reason": "error parsing transport_style",
|
||||
}).Error("error parsing RouterAddress")
|
||||
}
|
||||
var errs []error
|
||||
router_address.TransportOptions, remainder, errs = NewMapping(remainder)
|
||||
for _, err := range errs {
|
||||
log.WithFields(logrus.Fields{
|
||||
"at": "(RouterAddress) ReadNewRouterAddress",
|
||||
"reason": "error parsing options",
|
||||
"error": err,
|
||||
}).Error("error parsing RozuterAddress")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// NewRouterAddress creates a new RouterAddress with the provided parameters.
|
||||
// Returns a pointer to RouterAddress.
|
||||
func NewRouterAddress(cost uint8, expiration time.Time, transportType string, options map[string]string) (*RouterAddress, error) {
|
||||
log.Debug("Creating new RouterAddress")
|
||||
|
||||
// Create TransportCost as an Integer (1 byte)
|
||||
transportCost, err := NewIntegerFromInt(int(cost), 1)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to create TransportCost Integer")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Create ExpirationDate as a Date
|
||||
millis := expiration.UnixNano() / int64(time.Millisecond)
|
||||
dateBytes := make([]byte, DATE_SIZE)
|
||||
binary.BigEndian.PutUint64(dateBytes, uint64(millis))
|
||||
expirationDate, _, err := NewDate(dateBytes)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to create ExpirationDate")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Create TransportType as an I2PString
|
||||
transportTypeStr, err := ToI2PString(transportType)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to create TransportType I2PString")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Create TransportOptions as a Mapping
|
||||
transportOptions, err := GoMapToMapping(options)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to create TransportOptions Mapping")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Create RouterAddress
|
||||
ra := &RouterAddress{
|
||||
TransportCost: transportCost,
|
||||
ExpirationDate: expirationDate,
|
||||
TransportType: transportTypeStr,
|
||||
TransportOptions: transportOptions,
|
||||
}
|
||||
|
||||
log.WithFields(logrus.Fields{
|
||||
"cost": cost,
|
||||
"expiration": expiration,
|
||||
"transportType": transportType,
|
||||
"options": options,
|
||||
}).Debug("Successfully created new RouterAddress")
|
||||
|
||||
return ra, nil
|
||||
}
|
@ -1,41 +1,46 @@
|
||||
package common
|
||||
package router_address
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
|
||||
. "github.com/go-i2p/go-i2p/lib/common/data"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCheckValidReportsEmptySlice(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
router_address := RouterAddress([]byte{})
|
||||
err, exit := router_address.checkValid()
|
||||
router_address, _, err := ReadRouterAddress([]byte{})
|
||||
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal(err.Error(), "error parsing RouterAddress: no data", "correct error message should be returned")
|
||||
}
|
||||
err, exit := router_address.checkValid()
|
||||
assert.Equal(exit, true, "checkValid did not indicate to stop parsing on empty slice")
|
||||
}
|
||||
|
||||
func TestCheckRouterAddressValidReportsDataMissing(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
router_address := RouterAddress([]byte{0x01})
|
||||
err, exit := router_address.checkValid()
|
||||
router_address, _, err := ReadRouterAddress([]byte{0x01})
|
||||
|
||||
if assert.NotNil(err) {
|
||||
assert.Equal(err.Error(), "warning parsing RouterAddress: data too small", "correct error message should be returned")
|
||||
}
|
||||
|
||||
err, exit := router_address.checkValid()
|
||||
assert.Equal(exit, false, "checkValid indicates to stop parsing when some fields may be present")
|
||||
}
|
||||
|
||||
func TestCheckRouterAddressValidNoErrWithValidData(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
router_address := RouterAddress([]byte{0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00})
|
||||
mapping, _ := GoMapToMapping(map[string]string{"host": "127.0.0.1", "port": "4567"})
|
||||
router_address = append(router_address, mapping...)
|
||||
router_address, _, _ := ReadRouterAddress([]byte{0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00})
|
||||
mapping, err := GoMapToMapping(map[string]string{"host": "127.0.0.1", "port": "4567"})
|
||||
assert.Nil(err, "GoMapToMapping() returned error with valid data")
|
||||
router_address.TransportOptions = mapping
|
||||
// router_address = append(router_address, mapping...)
|
||||
err, exit := router_address.checkValid()
|
||||
|
||||
assert.Nil(err, "checkValid() reported error with valid data")
|
||||
@ -45,8 +50,8 @@ func TestCheckRouterAddressValidNoErrWithValidData(t *testing.T) {
|
||||
func TestRouterAddressCostReturnsFirstByte(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
router_address := RouterAddress([]byte{0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00})
|
||||
cost, err := router_address.Cost()
|
||||
router_address, _, err := ReadRouterAddress([]byte{0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00})
|
||||
cost := router_address.Cost()
|
||||
|
||||
assert.Nil(err, "Cost() returned error with valid data")
|
||||
assert.Equal(cost, 6, "Cost() returned wrong cost")
|
||||
@ -55,8 +60,8 @@ func TestRouterAddressCostReturnsFirstByte(t *testing.T) {
|
||||
func TestRouterAddressExpirationReturnsCorrectData(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
router_address := RouterAddress([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00})
|
||||
expiration, err := router_address.Expiration()
|
||||
router_address, _, err := ReadRouterAddress([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00})
|
||||
expiration := router_address.Expiration()
|
||||
|
||||
assert.Nil(err, "Expiration() returned error with valid data")
|
||||
if bytes.Compare(expiration[:], []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}) != 0 {
|
||||
@ -71,7 +76,7 @@ func TestReadRouterAddressReturnsCorrectRemainderWithoutError(t *testing.T) {
|
||||
str, _ := ToI2PString("foo")
|
||||
mapping, _ := GoMapToMapping(map[string]string{"host": "127.0.0.1", "port": "4567"})
|
||||
router_address_bytes = append(router_address_bytes, []byte(str)...)
|
||||
router_address_bytes = append(router_address_bytes, mapping...)
|
||||
router_address_bytes = append(router_address_bytes, mapping.Data()...)
|
||||
router_address_bytes = append(router_address_bytes, []byte{0x01, 0x02, 0x03}...)
|
||||
router_address, remainder, err := ReadRouterAddress(router_address_bytes)
|
||||
|
@ -1,36 +0,0 @@
|
||||
package common
|
||||
|
||||
/*
|
||||
I2P RouterIdentity
|
||||
https://geti2p.net/spec/common-structures#routeridentity
|
||||
Accurate for version 0.9.24
|
||||
|
||||
Identical to KeysAndCert
|
||||
*/
|
||||
|
||||
import (
|
||||
"github.com/go-i2p/go-i2p/lib/crypto"
|
||||
)
|
||||
|
||||
//
|
||||
// A RouterIdentity is identical to KeysAndCert.
|
||||
//
|
||||
type RouterIdentity []byte
|
||||
|
||||
func (router_identity RouterIdentity) PublicKey() (crypto.PublicKey, error) {
|
||||
return KeysAndCert(router_identity).PublicKey()
|
||||
}
|
||||
|
||||
func (router_identity RouterIdentity) SigningPublicKey() (crypto.SigningPublicKey, error) {
|
||||
return KeysAndCert(router_identity).SigningPublicKey()
|
||||
}
|
||||
|
||||
func (router_identity RouterIdentity) Certificate() (Certificate, error) {
|
||||
return KeysAndCert(router_identity).Certificate()
|
||||
}
|
||||
|
||||
func ReadRouterIdentity(data []byte) (router_identity RouterIdentity, remainder []byte, err error) {
|
||||
keys_and_cert, remainder, err := ReadKeysAndCert(data)
|
||||
router_identity = RouterIdentity(keys_and_cert)
|
||||
return
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user