[ 
https://issues.apache.org/jira/browse/ARTEMIS-3106?focusedWorklogId=570385&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-570385
 ]

ASF GitHub Bot logged work on ARTEMIS-3106:
-------------------------------------------

                Author: ASF GitHub Bot
            Created on: 23/Mar/21 12:08
            Start Date: 23/Mar/21 12:08
    Worklog Time Spent: 10m 
      Work Description: gemmellr commented on a change in pull request #3470:
URL: https://github.com/apache/activemq-artemis/pull/3470#discussion_r599509032



##########
File path: 
artemis-server/src/main/java/org/apache/activemq/artemis/spi/core/security/jaas/SCRAMPropertiesLoginModule.java
##########
@@ -0,0 +1,202 @@
+/*
+ * 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.spi.core.security.jaas;
+
+import java.io.IOException;
+import java.security.GeneralSecurityException;
+import java.security.MessageDigest;
+import java.security.Principal;
+import java.security.SecureRandom;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+
+import javax.crypto.Mac;
+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.FailedLoginException;
+import javax.security.auth.login.LoginException;
+
+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.ScramUtils;
+import org.apache.activemq.artemis.spi.core.security.scram.UserData;
+import org.apache.activemq.artemis.utils.PasswordMaskingUtil;
+
+/**
+ * Login modules that uses properties files similar to the {@link 
PropertiesLoginModule}. It can
+ * either store the username-password in plain text or in an encrypted/hashed 
form. the
+ * {@link #main(String[])} method provides a way to prepare unencrypted data 
to be encrypted/hashed.
+ */
+public class SCRAMPropertiesLoginModule extends PropertiesLoader implements 
AuditLoginModule {
+
+   private static final String SEPARATOR = ":";
+   private static final int MIN_ITERATIONS = 4096;
+   private Subject subject;
+   private CallbackHandler callbackHandler;
+   private Properties users;
+   private Map<String, Set<String>> roles;
+   private UserData userData;
+   private String user;
+   private final Set<Principal> principals = new HashSet<>();
+
+   @Override
+   public void initialize(Subject subject, CallbackHandler callbackHandler, 
Map<String, ?> sharedState,
+                          Map<String, ?> options) {
+      this.subject = subject;
+      this.callbackHandler = callbackHandler;
+
+      init(options);
+      users = load(PropertiesLoginModule.USER_FILE_PROP_NAME, "user", 
options).getProps();
+      roles = load(PropertiesLoginModule.ROLE_FILE_PROP_NAME, "role", 
options).invertedPropertiesValuesMap();
+
+   }
+
+   @Override
+   public boolean login() throws LoginException {
+      NameCallback nameCallback = new NameCallback("Username: ");
+      executeCallbacks(new Callback[] {nameCallback});
+      user = nameCallback.getName();
+      if (user == null) {
+         throw new FailedLoginException("User is null");
+      }
+      String password = users.getProperty(user);
+      if (password == null) {
+         throw new FailedLoginException("User does not exist: " + user);
+      }
+      if (PasswordMaskingUtil.isEncMasked(password)) {
+         String[] unwrap = 
PasswordMaskingUtil.unwrap(password).split(SEPARATOR);
+         userData = new UserData(unwrap[0], Integer.parseInt(unwrap[1]), 
unwrap[2], unwrap[3]);
+      } else {
+         DigestCallback digestCallback = new DigestCallback();
+         HmacCallback hmacCallback = new HmacCallback();
+         executeCallbacks(new Callback[] {digestCallback, hmacCallback});
+         byte[] salt = generateSalt();
+         try {
+            ScramUtils.NewPasswordStringData data =
+                     
ScramUtils.byteArrayToStringData(ScramUtils.newPassword(password, salt, 4096,
+                                                                             
digestCallback.getDigest(),
+                                                                             
hmacCallback.getHmac()));
+            userData = new UserData(data.salt, data.iterations, 
data.serverKey, data.storedKey);
+         } catch (ScramException e) {
+            throw new LoginException();
+         }
+      }
+      return true;
+   }
+
+   private static byte[] generateSalt() {
+      byte[] salt = new byte[24];

Review comment:
       I mainly just thought 24 seemed unusual is all. I belive Qpid Broker-J 
uses 32, I'd perhaps do the same as as nice power of 2.




-- 
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:
us...@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 570385)
    Time Spent: 11h 10m  (was: 11h)

> Support for SASL-SCRAM
> ----------------------
>
>                 Key: ARTEMIS-3106
>                 URL: https://issues.apache.org/jira/browse/ARTEMIS-3106
>             Project: ActiveMQ Artemis
>          Issue Type: New Feature
>          Components: AMQP
>            Reporter: Christoph Läubrich
>            Priority: Major
>          Time Spent: 11h 10m
>  Remaining Estimate: 0h
>
> With the enhancements in ARTEMIS-33 / [PR 
> 3432|https://github.com/apache/activemq-artemis/pull/3432] it would be now 
> possible to plug-in new SASL mechanism.
> One popular one is 
> [SASL-SCRAM|https://en.wikipedia.org/wiki/Salted_Challenge_Response_Authentication_Mechanism]
>  because it allows channelbinding together with secure storage of 
> user-credential.
> I have created an [implementation of this for Artemis 
> AMQP|https://github.com/laeubi/scram-sasl/tree/artemis/artemis] based on the 
> [SCRAM SASL authentication for Java|https://github.com/ogrebgr/scram-sasl] 
> code with some enhancements/cleanups to the original.
> As the source is already Apache licensed I'd like to propose to include this 
> in the Artemis code-base. This would greatly enhance the interoperability 
> with other implementations e.g. Apache QPID. 



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to