keycloak-service
静的公開メンバ関数 | 静的非公開メンバ関数 | 全メンバ一覧
org.keycloak.protocol.saml.SamlProtocolUtils クラス
org.keycloak.protocol.saml.SamlProtocolUtils 連携図
Collaboration graph

静的公開メンバ関数

static void verifyDocumentSignature (ClientModel client, Document document) throws VerificationException
 
static void verifyDocumentSignature (Document document, KeyLocator keyLocator) throws VerificationException
 
static PublicKey getSignatureValidationKey (ClientModel client) throws VerificationException
 
static PublicKey getEncryptionKey (ClientModel client) throws VerificationException
 
static PublicKey getPublicKey (ClientModel client, String attribute) throws VerificationException
 
static void verifyRedirectSignature (SAMLDocumentHolder documentHolder, KeyLocator locator, UriInfo uriInformation, String paramKey) throws VerificationException
 

静的非公開メンバ関数

static PublicKey getPublicKey (String certPem) throws VerificationException
 
static String getMessageSigningKeyId (SAML2Object doc)
 

詳解

著者
Bill Burke
バージョン
Revision
1

関数詳解

◆ getEncryptionKey()

static PublicKey org.keycloak.protocol.saml.SamlProtocolUtils.getEncryptionKey ( ClientModel  client) throws VerificationException
inlinestatic

Returns public part of SAML encryption key from the client settings.

引数
client
戻り値
Public key for encryption.
例外
VerificationException
106  {
107  return getPublicKey(client, SamlConfigAttributes.SAML_ENCRYPTION_CERTIFICATE_ATTRIBUTE);
108  }
static PublicKey getPublicKey(ClientModel client, String attribute)
Definition: SamlProtocolUtils.java:110

◆ getMessageSigningKeyId()

static String org.keycloak.protocol.saml.SamlProtocolUtils.getMessageSigningKeyId ( SAML2Object  doc)
inlinestaticprivate
173  {
174  final ExtensionsType extensions;
175  if (doc instanceof RequestAbstractType) {
176  extensions = ((RequestAbstractType) doc).getExtensions();
177  } else if (doc instanceof StatusResponseType) {
178  extensions = ((StatusResponseType) doc).getExtensions();
179  } else {
180  return null;
181  }
182 
183  if (extensions == null) {
184  return null;
185  }
186 
187  for (Object ext : extensions.getAny()) {
188  if (! (ext instanceof Element)) {
189  continue;
190  }
191 
192  String res = KeycloakKeySamlExtensionGenerator.getMessageSigningKeyIdFromElement((Element) ext);
193 
194  if (res != null) {
195  return res;
196  }
197  }
198 
199  return null;
200  }

◆ getPublicKey() [1/2]

static PublicKey org.keycloak.protocol.saml.SamlProtocolUtils.getPublicKey ( ClientModel  client,
String  attribute 
) throws VerificationException
inlinestatic
110  {
111  String certPem = client.getAttribute(attribute);
112  return getPublicKey(certPem);
113  }
static PublicKey getPublicKey(ClientModel client, String attribute)
Definition: SamlProtocolUtils.java:110

◆ getPublicKey() [2/2]

static PublicKey org.keycloak.protocol.saml.SamlProtocolUtils.getPublicKey ( String  certPem) throws VerificationException
inlinestaticprivate
115  {
116  if (certPem == null) throw new VerificationException("Client does not have a public key.");
117  X509Certificate cert = null;
118  try {
119  cert = PemUtils.decodeCertificate(certPem);
120  cert.checkValidity();
121  } catch (CertificateException ex) {
122  throw new VerificationException("Certificate is not valid.");
123  } catch (Exception e) {
124  throw new VerificationException("Could not decode cert", e);
125  }
126  return cert.getPublicKey();
127  }

◆ getSignatureValidationKey()

static PublicKey org.keycloak.protocol.saml.SamlProtocolUtils.getSignatureValidationKey ( ClientModel  client) throws VerificationException
inlinestatic

Returns public part of SAML signing key from the client settings.

引数
client
戻り値
Public key for signature validation.
例外
VerificationException
96  {
97  return getPublicKey(new SamlClient(client).getClientSigningCertificate());
98  }
static PublicKey getPublicKey(ClientModel client, String attribute)
Definition: SamlProtocolUtils.java:110

◆ verifyDocumentSignature() [1/2]

static void org.keycloak.protocol.saml.SamlProtocolUtils.verifyDocumentSignature ( ClientModel  client,
Document  document 
) throws VerificationException
inlinestatic

Verifies a signature of the given SAML document using settings for the given client. Throws an exception if the client signature is expected to be present as per the client settings and it is invalid, otherwise returns back to the caller.

引数
client
document
例外
VerificationException
62  {
63  SamlClient samlClient = new SamlClient(client);
64  if (!samlClient.requiresClientSignature()) {
65  return;
66  }
67  PublicKey publicKey = getSignatureValidationKey(client);
68  verifyDocumentSignature(document, new HardcodedKeyLocator(publicKey));
69  }
static void verifyDocumentSignature(ClientModel client, Document document)
Definition: SamlProtocolUtils.java:62
static PublicKey getSignatureValidationKey(ClientModel client)
Definition: SamlProtocolUtils.java:96

◆ verifyDocumentSignature() [2/2]

static void org.keycloak.protocol.saml.SamlProtocolUtils.verifyDocumentSignature ( Document  document,
KeyLocator  keyLocator 
) throws VerificationException
inlinestatic

Verifies a signature of the given SAML document using keys obtained from the given key locator. Throws an exception if the client signature is invalid, otherwise returns back to the caller.

引数
document
keyLocator
例外
VerificationException
79  {
80  SAML2Signature saml2Signature = new SAML2Signature();
81  try {
82  if (!saml2Signature.validate(document, keyLocator)) {
83  throw new VerificationException("Invalid signature on document");
84  }
85  } catch (ProcessingException e) {
86  throw new VerificationException("Error validating signature", e);
87  }
88  }

◆ verifyRedirectSignature()

static void org.keycloak.protocol.saml.SamlProtocolUtils.verifyRedirectSignature ( SAMLDocumentHolder  documentHolder,
KeyLocator  locator,
UriInfo  uriInformation,
String  paramKey 
) throws VerificationException
inlinestatic
129  {
130  MultivaluedMap<String, String> encodedParams = uriInformation.getQueryParameters(false);
131  String request = encodedParams.getFirst(paramKey);
132  String algorithm = encodedParams.getFirst(GeneralConstants.SAML_SIG_ALG_REQUEST_KEY);
133  String signature = encodedParams.getFirst(GeneralConstants.SAML_SIGNATURE_REQUEST_KEY);
134  String relayState = encodedParams.getFirst(GeneralConstants.RELAY_STATE);
135  String decodedAlgorithm = uriInformation.getQueryParameters(true).getFirst(GeneralConstants.SAML_SIG_ALG_REQUEST_KEY);
136 
137  if (request == null) throw new VerificationException("SAM was null");
138  if (algorithm == null) throw new VerificationException("SigAlg was null");
139  if (signature == null) throw new VerificationException("Signature was null");
140 
141  String keyId = getMessageSigningKeyId(documentHolder.getSamlObject());
142 
143  // Shibboleth doesn't sign the document for redirect binding.
144  // todo maybe a flag?
145 
146  StringBuilder rawQueryBuilder = new StringBuilder().append(paramKey).append("=").append(request);
147  if (encodedParams.containsKey(GeneralConstants.RELAY_STATE)) {
148  rawQueryBuilder.append("&" + GeneralConstants.RELAY_STATE + "=").append(relayState);
149  }
150  rawQueryBuilder.append("&" + GeneralConstants.SAML_SIG_ALG_REQUEST_KEY + "=").append(algorithm);
151  String rawQuery = rawQueryBuilder.toString();
152 
153  try {
154  byte[] decodedSignature = RedirectBindingUtil.urlBase64Decode(signature);
155 
156  SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.getFromXmlMethod(decodedAlgorithm);
157  Signature validator = signatureAlgorithm.createSignature(); // todo plugin signature alg
158  Key key = locator.getKey(keyId);
159  if (key instanceof PublicKey) {
160  validator.initVerify((PublicKey) key);
161  validator.update(rawQuery.getBytes("UTF-8"));
162  } else {
163  throw new VerificationException("Invalid key locator for signature verification");
164  }
165  if (!validator.verify(decodedSignature)) {
166  throw new VerificationException("Invalid query param signature");
167  }
168  } catch (Exception e) {
169  throw new VerificationException(e);
170  }
171  }
static String getMessageSigningKeyId(SAML2Object doc)
Definition: SamlProtocolUtils.java:173

このクラス詳解は次のファイルから抽出されました: