forked from InVisionApp/saml
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0ee85b6
commit e0c9879
Showing
2 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/amboss-mededu/saml | ||
|
||
go 1.13 | ||
go 1.16 | ||
|
||
require ( | ||
github.com/beevik/etree v1.1.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package xmlenc | ||
|
||
import ( | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/rand" | ||
"fmt" | ||
"github.com/beevik/etree" | ||
"io" | ||
) | ||
|
||
// struct implements Decrypter and Encrypter for block ciphers in struct mode | ||
type GCM struct { | ||
keySize int | ||
algorithm string | ||
cipher func([]byte) (cipher.Block, error) | ||
} | ||
|
||
// KeySize returns the length of the key required. | ||
func (e GCM) KeySize() int { | ||
return e.keySize | ||
} | ||
|
||
// Algorithm returns the name of the algorithm, as will be found | ||
// in an xenc:EncryptionMethod element. | ||
func (e GCM) Algorithm() string { | ||
return e.algorithm | ||
} | ||
|
||
func (e GCM) Encrypt(key interface{}, plaintext []byte) (*etree.Element, error) { | ||
return nil, nil | ||
} | ||
|
||
// Decrypt decrypts an encrypted element with key. If the ciphertext contains an | ||
// EncryptedKey element, then the type of `key` is determined by the registered | ||
// Decryptor for the EncryptedKey element. Otherwise, `key` must be a []byte of | ||
// length KeySize(). | ||
func (e GCM) Decrypt(key interface{}, ciphertextEl *etree.Element) ([]byte, error) { | ||
fmt.Printf("gcm ciphertextEl Type: %T\n", ciphertextEl) | ||
fmt.Printf("gcm ciphertextEl Value: %v\n", ciphertextEl) | ||
if encryptedKeyEl := ciphertextEl.FindElement("./KeyInfo/EncryptedKey"); encryptedKeyEl != nil { | ||
var err error | ||
key, err = Decrypt(key, encryptedKeyEl) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
keyBuf, ok := key.([]byte) | ||
|
||
if !ok { | ||
return nil, ErrIncorrectKeyType("[]byte") | ||
} | ||
if len(keyBuf) != e.KeySize() { | ||
return nil, ErrIncorrectKeyLength(e.KeySize()) | ||
} | ||
|
||
block, err := e.cipher(keyBuf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
aesgcm, err := cipher.NewGCM(block) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
nonce := make([]byte, aesgcm.NonceSize()) | ||
if _, err := io.ReadFull(rand.Reader, nonce); err != nil { | ||
return nil, err | ||
} | ||
|
||
plainText, err := aesgcm.Open(nonce, nonce, keyBuf, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return plainText, nil | ||
} | ||
|
||
var ( | ||
// AES128GCM implements AES128-GCM mode for encryption and decryption | ||
AES128GCM BlockCipher = GCM{ | ||
keySize: 16, | ||
algorithm: "http://www.w3.org/2009/xmlenc11#aes128-gcm", | ||
cipher: aes.NewCipher, | ||
} | ||
) | ||
|
||
func init() { | ||
RegisterDecrypter(AES128GCM) | ||
} |