You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@activemq.apache.org by GitBox <gi...@apache.org> on 2021/03/18 14:36:16 UTC

[GitHub] [activemq-artemis] gtully commented on a change in pull request #3470: ARTEMIS-3106 - Support for SASL-SCRAM

gtully commented on a change in pull request #3470:
URL: https://github.com/apache/activemq-artemis/pull/3470#discussion_r596931243



##########
File path: artemis-protocols/artemis-amqp-protocol/src/main/java/org/apache/activemq/artemis/protocol/amqp/sasl/scram/SCRAMServerSASLFactory.java
##########
@@ -0,0 +1,241 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.activemq.artemis.protocol.amqp.sasl.scram;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.security.GeneralSecurityException;
+import java.security.NoSuchAlgorithmException;
+import java.security.Principal;
+import java.util.Iterator;
+import java.util.UUID;
+
+import javax.security.auth.Subject;
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.auth.callback.NameCallback;
+import javax.security.auth.callback.UnsupportedCallbackException;
+import javax.security.auth.login.LoginContext;
+
+import org.apache.activemq.artemis.core.server.ActiveMQServer;
+import org.apache.activemq.artemis.protocol.amqp.broker.AmqpInterceptor;
+import org.apache.activemq.artemis.protocol.amqp.broker.ProtonProtocolManager;
+import org.apache.activemq.artemis.protocol.amqp.sasl.SASLResult;
+import org.apache.activemq.artemis.protocol.amqp.sasl.ServerSASL;
+import org.apache.activemq.artemis.protocol.amqp.sasl.ServerSASLFactory;
+import org.apache.activemq.artemis.spi.core.protocol.ProtocolManager;
+import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection;
+import org.apache.activemq.artemis.spi.core.remoting.Connection;
+import org.apache.activemq.artemis.spi.core.security.jaas.DigestCallback;
+import org.apache.activemq.artemis.spi.core.security.jaas.HmacCallback;
+import org.apache.activemq.artemis.spi.core.security.jaas.SCRAMMechanismCallback;
+import org.apache.activemq.artemis.spi.core.security.scram.SCRAM;
+import org.apache.activemq.artemis.spi.core.security.scram.ScramException;
+import org.apache.activemq.artemis.spi.core.security.scram.UserData;
+import org.jboss.logging.Logger;
+
+/**
+ * abstract class that implements the SASL-SCRAM authentication scheme, concrete implementations
+ * must supply the {@link SCRAM} type to use and be register via SPI
+ */
+public abstract class SCRAMServerSASLFactory implements ServerSASLFactory {
+
+   private final Logger logger = Logger.getLogger(getClass());
+   private final SCRAM scramType;
+
+   public SCRAMServerSASLFactory(SCRAM scram) {
+      this.scramType = scram;
+   }
+
+   @Override
+   public String getMechanism() {
+      return scramType.getName();
+   }
+
+   @Override
+   public boolean isDefaultPermitted() {
+      return false;
+   }
+
+   @Override
+   public ServerSASL create(ActiveMQServer server, ProtocolManager<AmqpInterceptor> manager, Connection connection,
+                            RemotingConnection remotingConnection) {
+      try {
+         if (manager instanceof ProtonProtocolManager) {
+            ScramServerFunctionalityImpl scram =
+                     new ScramServerFunctionalityImpl(scramType.getDigest(), scramType.getHmac(),
+                                                      UUID.randomUUID().toString());
+            String loginConfigScope = ((ProtonProtocolManager) manager).getSaslLoginConfigScope();
+            return new SCRAMServerSASL(scramType.getName(), scram, loginConfigScope, logger);
+         }
+      } catch (NoSuchAlgorithmException e) {
+         // can't be used then...
+      }
+      return null;
+   }
+
+   private static final class SCRAMServerSASL implements ServerSASL {
+
+      private final String name;
+      private final ScramServerFunctionality scram;
+      private SASLResult result;
+      private final String loginConfigScope;
+      private final Logger logger;
+
+      SCRAMServerSASL(String name, ScramServerFunctionality scram, String loginConfigScope, Logger logger) {
+         this.name = name;
+         this.scram = scram;
+         this.loginConfigScope = loginConfigScope;
+         this.logger = logger;
+      }
+
+      @Override
+      public String getName() {
+         return name;
+      }
+
+      @Override
+      public byte[] processSASL(byte[] bytes) {
+         String message = new String(bytes, StandardCharsets.US_ASCII);
+         try {
+            switch (scram.getState()) {
+               case INITIAL: {
+                  String userName = scram.handleClientFirstMessage(message);
+                  if (userName != null) {
+
+                     LoginContext loginContext = new LoginContext(loginConfigScope, new CallbackHandler() {
+
+                        @Override
+                        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
+                           for (Callback callback : callbacks) {
+                              if (callback instanceof NameCallback) {
+                                 ((NameCallback) callback).setName(userName);
+                              } else if (callback instanceof SCRAMMechanismCallback) {
+                                 ((SCRAMMechanismCallback) callback).setMechanism(name);
+                              } else if (callback instanceof DigestCallback) {
+                                 ((DigestCallback) callback).setDigest(scram.getDigest());
+                              } else if (callback instanceof HmacCallback) {
+                                 ((HmacCallback) callback).setHmac(scram.getHmac());
+                              } else {
+                                 throw new UnsupportedCallbackException(callback, "Unrecognized Callback " +
+                                          callback.getClass().getSimpleName());
+                              }
+                           }
+                        }
+                     });
+                     loginContext.login();

Review comment:
       Christoph, this login call has me wondering.... here we have a successfully authenticated subject from a JAAS perspective but have not yet completed the SASL exchange. Could this be a problem? If there are any other login modules in that realm the semantics may be broken a little.
   When I thought of doing something similar, I imagined the callback handlers would provide the mechanism to perform challenge/response - ie: it would all happen as part of the login call. Either through a fancy handler that would expose the state machine or some series/chain of handlers that represent the state transitions.
   I would need to do a little proof of concept to verify that would work... but is that something you considered?
   I am not certain that the current use of login it is a problem, but it does seem odd.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org