Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
packer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kristopher Ruzic
packer
Commits
abcdd5a6
Commit
abcdd5a6
authored
May 20, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
communicator/ssh: Add SimpleKeychain
parent
0ea19cf8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
75 additions
and
0 deletions
+75
-0
communicator/ssh/keychain.go
communicator/ssh/keychain.go
+54
-0
communicator/ssh/keychain_test.go
communicator/ssh/keychain_test.go
+21
-0
No files found.
communicator/ssh/keychain.go
0 → 100644
View file @
abcdd5a6
package
ssh
import
(
"crypto"
"crypto/dsa"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"io"
)
type
SimpleKeychain
struct
{
keys
[]
interface
{}
}
// AddPEMKey adds a simple PEM encoded private key to the keychain.
func
(
k
*
SimpleKeychain
)
AddPEMKey
(
key
string
)
(
err
error
)
{
block
,
_
:=
pem
.
Decode
([]
byte
(
key
))
rsakey
,
err
:=
x509
.
ParsePKCS1PrivateKey
(
block
.
Bytes
)
if
err
!=
nil
{
return
}
k
.
keys
=
append
(
k
.
keys
,
rsakey
)
return
}
// Key method for ssh.ClientKeyring interface
func
(
k
*
SimpleKeychain
)
Key
(
i
int
)
(
interface
{},
error
)
{
if
i
<
0
||
i
>=
len
(
k
.
keys
)
{
return
nil
,
nil
}
switch
key
:=
k
.
keys
[
i
]
.
(
type
)
{
case
*
rsa
.
PrivateKey
:
return
&
key
.
PublicKey
,
nil
case
*
dsa
.
PrivateKey
:
return
&
key
.
PublicKey
,
nil
}
panic
(
"unknown key type"
)
}
// Sign method for ssh.ClientKeyring interface
func
(
k
*
SimpleKeychain
)
Sign
(
i
int
,
rand
io
.
Reader
,
data
[]
byte
)
(
sig
[]
byte
,
err
error
)
{
hashFunc
:=
crypto
.
SHA1
h
:=
hashFunc
.
New
()
h
.
Write
(
data
)
digest
:=
h
.
Sum
(
nil
)
switch
key
:=
k
.
keys
[
i
]
.
(
type
)
{
case
*
rsa
.
PrivateKey
:
return
rsa
.
SignPKCS1v15
(
rand
,
key
,
hashFunc
,
digest
)
}
return
nil
,
errors
.
New
(
"ssh: unknown key type"
)
}
communicator/ssh/keychain_test.go
0 → 100644
View file @
abcdd5a6
package
ssh
import
"testing"
const
testPrivateKey
=
`-----BEGIN RSA PRIVATE KEY-----
MIIBOwIBAAJBALdGZxkXDAjsYk10ihwU6Id2KeILz1TAJuoq4tOgDWxEEGeTrcld
r/ZwVaFzjWzxaf6zQIJbfaSEAhqD5yo72+sCAwEAAQJBAK8PEVU23Wj8mV0QjwcJ
tZ4GcTUYQL7cF4+ezTCE9a1NrGnCP2RuQkHEKxuTVrxXt+6OF15/1/fuXnxKjmJC
nxkCIQDaXvPPBi0c7vAxGwNY9726x01/dNbHCE0CBtcotobxpwIhANbbQbh3JHVW
2haQh4fAG5mhesZKAGcxTyv4mQ7uMSQdAiAj+4dzMpJWdSzQ+qGHlHMIBvVHLkqB
y2VdEyF7DPCZewIhAI7GOI/6LDIFOvtPo6Bj2nNmyQ1HU6k/LRtNIXi4c9NJAiAr
rrxx26itVhJmcvoUhOjwuzSlP2bE5VHAvkGB352YBg==
-----END RSA PRIVATE KEY-----`
func
TestAddPEMKey
(
t
*
testing
.
T
)
{
k
:=
&
SimpleKeychain
{}
err
:=
k
.
AddPEMKey
(
testPrivateKey
)
if
err
!=
nil
{
t
.
Fatalf
(
"error while adding key: %s"
,
err
)
}
}
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