@@ -96,6 +96,7 @@ type AssertionMaker interface {
96
96
// and password).
97
97
type IdentityProvider struct {
98
98
Key crypto.PrivateKey
99
+ Signer crypto.Signer
99
100
Logger logger.Interface
100
101
Certificate * x509.Certificate
101
102
Intermediates []* x509.Certificate
@@ -831,24 +832,8 @@ const canonicalizerPrefixList = ""
831
832
832
833
// MakeAssertionEl sets `AssertionEl` to a signed, possibly encrypted, version of `Assertion`.
833
834
func (req * IdpAuthnRequest ) MakeAssertionEl () error {
834
- keyPair := tls.Certificate {
835
- Certificate : [][]byte {req .IDP .Certificate .Raw },
836
- PrivateKey : req .IDP .Key ,
837
- Leaf : req .IDP .Certificate ,
838
- }
839
- for _ , cert := range req .IDP .Intermediates {
840
- keyPair .Certificate = append (keyPair .Certificate , cert .Raw )
841
- }
842
- keyStore := dsig .TLSCertKeyStore (keyPair )
843
-
844
- signatureMethod := req .IDP .SignatureMethod
845
- if signatureMethod == "" {
846
- signatureMethod = dsig .RSASHA1SignatureMethod
847
- }
848
-
849
- signingContext := dsig .NewDefaultSigningContext (keyStore )
850
- signingContext .Canonicalizer = dsig .MakeC14N10ExclusiveCanonicalizerWithPrefixList (canonicalizerPrefixList )
851
- if err := signingContext .SetSignatureMethod (signatureMethod ); err != nil {
835
+ signingContext , err := req .signingContext ()
836
+ if err != nil {
852
837
return err
853
838
}
854
839
@@ -1049,24 +1034,8 @@ func (req *IdpAuthnRequest) MakeResponse() error {
1049
1034
1050
1035
// Sign the response element (we've already signed the Assertion element)
1051
1036
{
1052
- keyPair := tls.Certificate {
1053
- Certificate : [][]byte {req .IDP .Certificate .Raw },
1054
- PrivateKey : req .IDP .Key ,
1055
- Leaf : req .IDP .Certificate ,
1056
- }
1057
- for _ , cert := range req .IDP .Intermediates {
1058
- keyPair .Certificate = append (keyPair .Certificate , cert .Raw )
1059
- }
1060
- keyStore := dsig .TLSCertKeyStore (keyPair )
1061
-
1062
- signatureMethod := req .IDP .SignatureMethod
1063
- if signatureMethod == "" {
1064
- signatureMethod = dsig .RSASHA1SignatureMethod
1065
- }
1066
-
1067
- signingContext := dsig .NewDefaultSigningContext (keyStore )
1068
- signingContext .Canonicalizer = dsig .MakeC14N10ExclusiveCanonicalizerWithPrefixList (canonicalizerPrefixList )
1069
- if err := signingContext .SetSignatureMethod (signatureMethod ); err != nil {
1037
+ signingContext , err := req .signingContext ()
1038
+ if err != nil {
1070
1039
return err
1071
1040
}
1072
1041
@@ -1084,3 +1053,44 @@ func (req *IdpAuthnRequest) MakeResponse() error {
1084
1053
req .ResponseEl = responseEl
1085
1054
return nil
1086
1055
}
1056
+
1057
+ // signingContext will create a signing context for the request.
1058
+ func (req * IdpAuthnRequest ) signingContext () (* dsig.SigningContext , error ) {
1059
+ // Create a cert chain based off of the IDP cert and its intermediates.
1060
+ certificates := [][]byte {req .IDP .Certificate .Raw }
1061
+ for _ , cert := range req .IDP .Intermediates {
1062
+ certificates = append (certificates , cert .Raw )
1063
+ }
1064
+
1065
+ var signingContext * dsig.SigningContext
1066
+ var err error
1067
+ // If signer is set, use it instead of the private key.
1068
+ if req .IDP .Signer != nil {
1069
+ signingContext , err = dsig .NewSigningContext (req .IDP .Signer , certificates )
1070
+ if err != nil {
1071
+ return nil , err
1072
+ }
1073
+ } else {
1074
+ keyPair := tls.Certificate {
1075
+ Certificate : certificates ,
1076
+ PrivateKey : req .IDP .Key ,
1077
+ Leaf : req .IDP .Certificate ,
1078
+ }
1079
+ keyStore := dsig .TLSCertKeyStore (keyPair )
1080
+
1081
+ signingContext = dsig .NewDefaultSigningContext (keyStore )
1082
+ }
1083
+
1084
+ // Default to using SHA1 if the signature method isn't set.
1085
+ signatureMethod := req .IDP .SignatureMethod
1086
+ if signatureMethod == "" {
1087
+ signatureMethod = dsig .RSASHA1SignatureMethod
1088
+ }
1089
+
1090
+ signingContext .Canonicalizer = dsig .MakeC14N10ExclusiveCanonicalizerWithPrefixList (canonicalizerPrefixList )
1091
+ if err := signingContext .SetSignatureMethod (signatureMethod ); err != nil {
1092
+ return nil , err
1093
+ }
1094
+
1095
+ return signingContext , nil
1096
+ }
0 commit comments