Skip to content

Instantly share code, notes, and snippets.

@smoser
Created June 26, 2012 17:01

Revisions

  1. smoser created this gist Jun 26, 2012.
    55 changes: 55 additions & 0 deletions rsa2azure
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    #!/bin/sh
    # vi: ts=4 noexpandtab
    # rsa2azure

    Usage() {
    cat <<EOF
    Usage: ${0##*/} [options] [private-key]
    convert private-key in ssh private key format to the format
    needed by windows azure as described at
    http://utlemming.azurewebsites.net/?p=91
    options:
    -o | --output FILE write output to F. default stdout.
    Default private-key is ~/.ssh/id_rsa)
    EOF
    }
    fail() { echo "$@" 1>&2; exit 1; }

    short_opts="ho:"
    long_opts="output:,help"
    getopt_out=$(getopt --name "${0##*/}" \
    --options "ho:" --long "output:,help" -- "$@") &&
    eval set -- "${getopt_out}" ||
    { Usage 1>&2; fail "bad usage"; }

    umask 066

    outfile=/dev/stdout
    echo $@
    while [ $# -ne 0 ]; do
    case "$1" in
    -h|--help) Usage; exit 0;;
    -o|--output) outfile=${2}; shift;;
    --) shift; break;;
    esac
    shift;
    done

    [ $# -eq 0 -o $# -eq 1 ] || { Usage 1>&2; exit 1; }

    key_in=$1
    if [ $# -eq 0 ]; then
    key_in=~/.ssh/id_rsa
    [ -f "$key_in" ] || fail "no default key found in $key_in";
    fi

    [ "$key_in" = "-" ] && key_in=/dev/stdin
    [ -f "$key_in" ] || fail "${key_in}: not a file"

    out=$(openssl rsa -in "$key_in" -out /dev/stdout 2>/dev/null) ||
    fail "failed to decrypt private key in $key_in"
    printf "%s" "$out" |
    openssl req -new -x509 -nodes -days 999 -batch -outform DER -batch \
    -key /dev/stdin -out "${outfile}" 2>/dev/null