brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aledsage <...@git.apache.org>
Subject [GitHub] incubator-brooklyn pull request: Jclouds tidy
Date Mon, 26 Jan 2015 11:49:21 GMT
Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/incubator-brooklyn/pull/465#discussion_r23524964
  
    --- Diff: utils/common/src/main/java/brooklyn/util/crypto/AuthorizedKeysParser.java ---
    @@ -0,0 +1,132 @@
    +/*
    + * 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 brooklyn.util.crypto;
    +
    +import java.io.ByteArrayInputStream;
    +import java.io.ByteArrayOutputStream;
    +import java.io.IOException;
    +import java.io.InputStream;
    +import java.io.OutputStream;
    +import java.math.BigInteger;
    +import java.security.KeyFactory;
    +import java.security.PublicKey;
    +import java.security.interfaces.DSAPublicKey;
    +import java.security.interfaces.RSAPublicKey;
    +import java.security.spec.DSAPublicKeySpec;
    +import java.security.spec.RSAPublicKeySpec;
    +
    +import brooklyn.util.exceptions.Exceptions;
    +
    +import com.google.common.io.BaseEncoding;
    +
    +public class AuthorizedKeysParser {
    +
    +    public static PublicKey decodePublicKey(String keyLine) {
    +        try {
    +            ByteArrayInputStream stream = null;
    +
    +            // look for the Base64 encoded part of the line to decode
    +            // both ssh-rsa and ssh-dss begin with "AAAA" due to the length bytes
    +            for (String part : keyLine.split(" ")) {
    +                if (part.startsWith("AAAA")) {
    +                    stream = new ByteArrayInputStream(BaseEncoding.base64().decode(part));
    +                    break;
    +                }
    +            }
    +            if (stream == null)
    +                throw new IllegalArgumentException("Encoded public key should include
phrase beginning AAAA.");
    +
    +            String type = readType(stream);
    +            if (type.equals("ssh-rsa")) {
    +                BigInteger e = readBigInt(stream);
    +                BigInteger m = readBigInt(stream);
    +                RSAPublicKeySpec spec = new RSAPublicKeySpec(m, e);
    +                return KeyFactory.getInstance("RSA").generatePublic(spec);
    +            } else if (type.equals("ssh-dss")) {
    +                BigInteger p = readBigInt(stream);
    +                BigInteger q = readBigInt(stream);
    +                BigInteger g = readBigInt(stream);
    +                BigInteger y = readBigInt(stream);
    +                DSAPublicKeySpec spec = new DSAPublicKeySpec(y, p, q, g);
    +                return KeyFactory.getInstance("DSA").generatePublic(spec);
    +            } else {
    +                throw new IllegalArgumentException("Unknown public key type " + type);
    +            }
    +            
    +        } catch (Exception e) {
    +            Exceptions.propagateIfFatal(e);
    +            throw new IllegalArgumentException("Error parsing authorized_keys/SSH2 format
public key: "+e);
    +        }
    +    }
    +
    +    private static int readInt(InputStream stream) throws IOException {
    +        return ((stream.read() & 0xFF) << 24) | ((stream.read() & 0xFF)
<< 16)
    +            | ((stream.read() & 0xFF) << 8) | (stream.read() & 0xFF);
    +    }
    +    
    +    private static byte[] readBytesWithLength(InputStream stream) throws IOException
{
    +        int len = readInt(stream);
    +        byte[] result = new byte[len];
    +        stream.read(result);
    +        return result;
    +    }
    +    
    +    private static void writeInt(OutputStream stream, int v) throws IOException {
    +        for (int shift = 24; shift >= 0; shift -= 8)
    +            stream.write((v >>> shift) & 0xFF);
    +    }
    +    private static void writeBytesWithLength(OutputStream stream, byte[] buf) throws
IOException {
    +        writeInt(stream, buf.length);
    +        stream.write(buf);
    +    }
    +    
    +    private static String readType(InputStream stream) throws IOException { return new
String(readBytesWithLength(stream)); }
    +    private static BigInteger readBigInt(InputStream stream) throws IOException { return
new BigInteger(readBytesWithLength(stream)); }
    +
    +    public static String encodePublicKey(PublicKey key) {
    --- End diff --
    
    Surprised there's not some utility method in one of our dependencies that already does
this, but I haven't looked at all.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message