Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
6
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Switch to GitLab Next
Sign in / Register
Toggle navigation
B
bunqapi
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Iterations
Merge Requests
0
Merge Requests
0
Requirements
Requirements
List
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Test Cases
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issue
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Sybren A. Stüvel
bunqapi
Commits
6b228730
Commit
6b228730
authored
May 23, 2019
by
Sybren A. Stüvel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added server signature verification
parent
e61590f6
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
114 additions
and
16 deletions
+114
-16
bunq_credentials.go
bunq_credentials.go
+7
-1
client.go
client.go
+2
-1
consts.go
consts.go
+1
-0
device_server.go
device_server.go
+2
-4
mergetest/struct_merge.go
mergetest/struct_merge.go
+1
-1
monetary_account.go
monetary_account.go
+3
-5
security.go
security.go
+36
-0
session.go
session.go
+1
-1
signing.go
signing.go
+61
-3
No files found.
bunq_credentials.go
View file @
6b228730
package
bunqapi
import
(
"crypto/rsa"
"io/ioutil"
"net/url"
"path/filepath"
...
...
@@ -18,7 +19,8 @@ type Credentials struct {
InstallationToken
string
`yaml:"installationToken"`
ServerPublicKey
string
`yaml:"serverPublicKey"`
DeviceID
int
`yaml:"deviceID"`
serverPublicKey
*
rsa
.
PublicKey
DeviceID
int
`yaml:"deviceID"`
}
func
LoadCredentials
()
Credentials
{
...
...
@@ -55,6 +57,10 @@ func LoadCredentials() Credentials {
}
logger
.
WithField
(
"url"
,
creds
.
apiURL
.
String
())
.
Debug
(
"set API URL based on API mode"
)
if
creds
.
ServerPublicKey
!=
""
{
creds
.
serverPublicKey
=
parsePublicRSAKeyString
(
creds
.
ServerPublicKey
)
}
return
creds
}
...
...
client.go
View file @
6b228730
...
...
@@ -102,7 +102,8 @@ func (c *Client) DoRequest(method, endpoint string, payload interface{}, respons
logger
.
WithError
(
err
)
.
Fatal
(
"unable to perform HTTP call"
)
}
// TODO: verify server signature
c
.
VerifyResponse
(
resp
)
respBody
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
logger
.
WithError
(
err
)
.
Fatal
(
"unable to read HTTP body"
)
...
...
consts.go
View file @
6b228730
...
...
@@ -30,6 +30,7 @@ const (
headerXBunqGeolocation
=
"X-Bunq-Geolocation"
headerXBunqLanguage
=
"X-Bunq-Language"
headerXBunqRegion
=
"X-Bunq-Region"
headerXBunqServerSignature
=
"X-Bunq-Server-Signature"
)
// Default header values
...
...
device_server.go
View file @
6b228730
package
bunqapi
import
"github.com/sirupsen/logrus"
type
deviceServerRequest
struct
{
// The description of the DeviceServer. This is only for your own reference when reading the DeviceServer again.
Description
string
`json:"description"`
...
...
@@ -20,7 +18,7 @@ type deviceServerResponse struct {
func
(
c
*
Client
)
CheckDeviceServer
(
description
string
,
permittedIPs
[]
string
)
{
if
c
.
creds
.
DeviceID
!=
0
{
log
rus
.
WithField
(
"deviceID"
,
c
.
creds
.
DeviceID
)
.
Debug
(
"device ID is known"
)
log
.
WithField
(
"deviceID"
,
c
.
creds
.
DeviceID
)
.
Debug
(
"device ID is known"
)
return
}
c
.
PostDeviceServer
(
description
,
permittedIPs
)
...
...
@@ -42,6 +40,6 @@ func (c *Client) PostDeviceServer(description string, permittedIPs []string) {
MergeStructs
(
wrappedResponse
.
Response
,
&
response
)
c
.
creds
.
DeviceID
=
response
.
ID
.
ID
log
rus
.
WithField
(
"id"
,
c
.
creds
.
DeviceID
)
.
Info
(
"registered device device ID"
)
log
.
WithField
(
"id"
,
c
.
creds
.
DeviceID
)
.
Info
(
"registered device device ID"
)
c
.
creds
.
Save
()
}
mergetest/struct_merge.go
View file @
6b228730
...
...
@@ -39,7 +39,7 @@ func main() {
bunqapi
.
MergeStructs
(
fromBunq
.
Response
,
&
merged
)
out
,
err
:=
json
.
MarshalIndent
(
merged
,
""
,
" "
)
if
err
!=
nil
{
log
rus
.
WithError
(
err
)
.
Fatal
(
"unable to marshal result to JSON"
)
log
.
WithError
(
err
)
.
Fatal
(
"unable to marshal result to JSON"
)
}
os
.
Stdout
.
Write
(
out
)
os
.
Stdout
.
WriteString
(
"
\n
"
)
...
...
monetary_account.go
View file @
6b228730
...
...
@@ -2,8 +2,6 @@ package bunqapi
import
(
"fmt"
"github.com/sirupsen/logrus"
)
type
wrappedMonetaryAccountBankResponse
struct
{
...
...
@@ -54,7 +52,7 @@ func (c *Client) GetMonetaryAccountBankList() {
url
:=
fmt
.
Sprintf
(
"user/%d/monetary-account-bank"
,
c
.
session
.
UserPerson
.
ID
)
errResp
:=
c
.
DoRequest
(
"GET"
,
url
,
nil
,
&
wrappedResponse
)
if
errResp
!=
nil
{
log
rus
.
WithFields
(
errResp
.
LogFields
())
.
Fatal
(
"error performing session creation request"
)
log
.
WithFields
(
errResp
.
LogFields
())
.
Fatal
(
"error performing session creation request"
)
}
}
...
...
@@ -63,11 +61,11 @@ func (c *Client) CreateMonetaryAccountBank(account MonetaryAccountBank) {
url
:=
fmt
.
Sprintf
(
"user/%d/monetary-account-bank"
,
c
.
session
.
UserPerson
.
ID
)
errResp
:=
c
.
DoRequest
(
"POST"
,
url
,
&
account
,
&
wrappedResponse
)
if
errResp
!=
nil
{
log
rus
.
WithFields
(
errResp
.
LogFields
())
.
Fatal
(
"error performing session creation request"
)
log
.
WithFields
(
errResp
.
LogFields
())
.
Fatal
(
"error performing session creation request"
)
}
response
:=
monetaryAccountBankCreateResponse
{}
MergeStructs
(
wrappedResponse
.
Response
,
&
response
)
log
rus
.
WithField
(
"bankAccountID"
,
response
.
ID
.
ID
)
.
Info
(
"bank account created"
)
log
.
WithField
(
"bankAccountID"
,
response
.
ID
.
ID
)
.
Info
(
"bank account created"
)
}
security.go
View file @
6b228730
...
...
@@ -45,6 +45,21 @@ func readPEM(filename, pemType string) ([]byte, error) {
}
}
func
parsePEM
(
pemString
,
pemType
string
)
([]
byte
,
error
)
{
pemdata
:=
[]
byte
(
pemString
)
var
block
*
pem
.
Block
for
{
block
,
pemdata
=
pem
.
Decode
(
pemdata
)
if
block
==
nil
{
log
.
WithField
(
"expectedHeader"
,
pemType
)
.
Panic
(
"unable to find expected block of PEM data"
)
}
if
block
.
Type
==
pemType
{
return
block
.
Bytes
,
nil
}
}
}
func
generateNewPrivateKey
()
*
rsa
.
PrivateKey
{
privkey
,
err
:=
rsa
.
GenerateKey
(
rand
.
Reader
,
2048
)
if
err
!=
nil
{
...
...
@@ -82,3 +97,24 @@ func ensureOwnRSAKey() *rsa.PrivateKey {
return
privkey
}
func
parsePublicRSAKeyString
(
pemString
string
)
*
rsa
.
PublicKey
{
keyBytes
,
err
:=
parsePEM
(
pemString
,
"PUBLIC KEY"
)
if
err
!=
nil
{
log
.
WithError
(
err
)
.
Error
(
"unable to parse public key, assuming it's not there"
)
return
nil
}
pubkey
,
err
:=
x509
.
ParsePKIXPublicKey
(
keyBytes
)
if
err
!=
nil
{
log
.
WithError
(
err
)
.
Fatal
(
"unable to parse public RSA key"
)
}
rsaPubkey
,
ok
:=
pubkey
.
(
*
rsa
.
PublicKey
)
if
!
ok
{
log
.
Fatal
(
"public key is not an RSA key"
)
}
log
.
WithField
(
"keySizeBits"
,
rsaPubkey
.
Size
()
*
8
)
.
Debug
(
"loaded public RSA key"
)
return
rsaPubkey
}
session.go
View file @
6b228730
...
...
@@ -45,7 +45,7 @@ func (c *Client) SessionStart() {
c
.
session
.
UserCompany
=
response
.
UserCompany
c
.
session
.
UserPerson
=
response
.
UserPerson
log
rus
.
WithFields
(
logrus
.
Fields
{
log
.
WithFields
(
logrus
.
Fields
{
"firstName"
:
c
.
session
.
UserPerson
.
FirstName
,
"lastName"
:
c
.
session
.
UserPerson
.
LastName
,
"session"
:
c
.
session
.
Token
,
...
...
signing.go
View file @
6b228730
...
...
@@ -10,6 +10,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"net/textproto"
"sort"
"strings"
...
...
@@ -22,9 +23,9 @@ var headersToSign = map[string]bool{
headerUserAgent
:
true
,
}
func
sortedHeaderKeys
(
r
*
http
.
Request
)
[]
string
{
func
sortedHeaderKeys
(
header
http
.
Header
)
[]
string
{
headerKeys
:=
[]
string
{}
for
key
:=
range
r
.
H
eader
{
for
key
:=
range
h
eader
{
headerKeys
=
append
(
headerKeys
,
key
)
}
sort
.
Strings
(
headerKeys
)
...
...
@@ -43,7 +44,7 @@ func (c *Client) SignRequest(r *http.Request) error {
}
hash
(
fmt
.
Sprintf
(
"%s %s
\n
"
,
r
.
Method
,
r
.
URL
.
Path
))
headerKeys
:=
sortedHeaderKeys
(
r
)
headerKeys
:=
sortedHeaderKeys
(
r
.
Header
)
for
_
,
key
:=
range
headerKeys
{
if
!
headersToSign
[
key
]
&&
!
strings
.
HasPrefix
(
strings
.
ToLower
(
key
),
"x-bunq-"
)
{
continue
...
...
@@ -85,3 +86,60 @@ func (c *Client) SignRequest(r *http.Request) error {
r
.
Header
.
Set
(
headerXBunqClientSignature
,
encodedSig
)
return
nil
}
func
(
c
*
Client
)
VerifyResponse
(
r
*
http
.
Response
)
error
{
// TODO: merge common code between this function and SignRequest().
serverSignatureB64
:=
r
.
Header
.
Get
(
headerXBunqServerSignature
)
if
serverSignatureB64
==
""
{
log
.
Panic
(
"this response has not been signed"
)
}
hasher
:=
sha256
.
New
()
hash
:=
func
(
value
string
)
{
log
.
WithField
(
"value"
,
value
)
.
Debug
(
"writing to hasher"
)
hasher
.
Write
([]
byte
(
value
))
}
hash
(
fmt
.
Sprintf
(
"%d
\n
"
,
r
.
StatusCode
))
headerKeys
:=
sortedHeaderKeys
(
r
.
Header
)
serverSigLowerKey
:=
strings
.
ToLower
(
headerXBunqServerSignature
)
for
_
,
key
:=
range
headerKeys
{
lowerKey
:=
strings
.
ToLower
(
key
)
if
!
strings
.
HasPrefix
(
lowerKey
,
"x-bunq-"
)
||
lowerKey
==
serverSigLowerKey
{
continue
}
for
_
,
value
:=
range
r
.
Header
[
key
]
{
hash
(
fmt
.
Sprintf
(
"%s: %s
\n
"
,
textproto
.
CanonicalMIMEHeaderKey
(
key
),
value
))
}
}
hash
(
"
\n
"
)
bodyBytes
,
err
:=
ioutil
.
ReadAll
(
r
.
Body
)
if
err
!=
nil
{
log
.
WithError
(
err
)
.
Panic
(
"unable to read response body for signing"
)
}
hasher
.
Write
(
bodyBytes
)
log
.
WithField
(
"value"
,
string
(
bodyBytes
))
.
Debug
(
"writing body to hasher"
)
r
.
Body
=
ioutil
.
NopCloser
(
bytes
.
NewReader
(
bodyBytes
))
sum
:=
hasher
.
Sum
(
nil
)
// Verify the signature just to be sure ;-)
serverSignature
,
err
:=
base64
.
StdEncoding
.
DecodeString
(
serverSignatureB64
)
if
err
!=
nil
{
log
.
WithError
(
err
)
.
Fatal
(
"unable to base64-decode the server signature"
)
}
if
err
:=
rsa
.
VerifyPKCS1v15
(
c
.
creds
.
serverPublicKey
,
crypto
.
SHA256
,
sum
,
serverSignature
);
err
!=
nil
{
log
.
WithError
(
err
)
.
Fatal
(
"unable to verify server signature with public RSA key"
)
}
encodedSig
:=
base64
.
StdEncoding
.
EncodeToString
(
serverSignature
)
log
.
WithFields
(
logrus
.
Fields
{
"shaSum"
:
fmt
.
Sprintf
(
"%x"
,
sum
),
"signature"
:
encodedSig
,
})
.
Debug
(
"verified server response signature"
)
return
nil
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment