MCPcopy Create free account
hub / github.com/crewjam/saml / validateRSAKeyIfPresent

Function validateRSAKeyIfPresent

xmlenc/decrypt.go:108–142  ·  view source on GitHub ↗
(key interface{}, encryptedKey *etree.Element)

Source from the content-addressed store, hash-verified

106}
107
108func validateRSAKeyIfPresent(key interface{}, encryptedKey *etree.Element) (*rsa.PrivateKey, error) {
109 rsaKey, ok := key.(*rsa.PrivateKey)
110 if !ok {
111 return nil, errors.New("expected key to be a *rsa.PrivateKey")
112 }
113
114 // extract and verify that the public key matches the certificate
115 // this section is included to either let the service know up front
116 // if the key will work, or let the service provider know which key
117 // to use to decrypt the message. Either way, verification is not
118 // security-critical.
119 //nolint:revive,staticcheck // Keep the later empty branch so that we know to address this at a later date.
120 if el := encryptedKey.FindElement("./KeyInfo/X509Data/X509Certificate"); el != nil {
121 certPEMbuf := el.Text()
122 certPEMbuf = "-----BEGIN CERTIFICATE-----\n" + certPEMbuf + "\n-----END CERTIFICATE-----\n"
123 certPEM, _ := pem.Decode([]byte(certPEMbuf))
124 if certPEM == nil {
125 return nil, fmt.Errorf("invalid certificate")
126 }
127 cert, err := x509.ParseCertificate(certPEM.Bytes)
128 if err != nil {
129 return nil, err
130 }
131 pubKey, ok := cert.PublicKey.(*rsa.PublicKey)
132 if !ok {
133 return nil, fmt.Errorf("expected certificate to be an *rsa.PublicKey")
134 }
135 if rsaKey.N.Cmp(pubKey.N) != 0 || rsaKey.E != pubKey.E {
136 return nil, fmt.Errorf("certificate does not match provided key")
137 }
138 } else if el = encryptedKey.FindElement("./KeyInfo/X509Data/X509IssuerSerial"); el != nil {
139 // TODO: determine how to validate the issuer serial information
140 }
141 return rsaKey, nil
142}

Callers 2

DecryptMethod · 0.85

Calls 2

NewMethod · 0.65
DecodeMethod · 0.65

Tested by 1