Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Overview

* **httpsig-ssh-jsch**: Alternative implementation of SSH key support using the JSch API.

* **httpsig-hmac**: Provides a HMAC implementation with support for SHA-256 and SHA-512.

* **httpsig-test-common**: Provides several public/private SSH key pairs and utility methods for writing unit tests.

API
Expand Down
32 changes: 32 additions & 0 deletions hmac/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Overview
========

Provides a HMAC implementation with support for SHA-256 and SHA-512.


API
===

Signer
------

To create a Signer, you must provide both a HMAC key and a KeyId. For example:

Signer signer = new Signer(
new HmacKey("keyId", "ssh. its a secret"),
new HmacKeyId()
);

Verifier
--------

The Verifier is the mechanism used by a server to verify the signature provided in
the Authorization header against the Request and the Challenge defined for the server.

To create a Verifier, you must provide a HMAC and a KeyId. For example:

Verifier verifier = new Verifier(
new DefaultKeychain(Arrays.asList(new HmacKey("keyId", "ssh. its a secret"))),
new HmacKeyId()
);

161 changes: 161 additions & 0 deletions hmac/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ This is free and unencumbered software released into the public domain.
~
~ Anyone is free to copy, modify, publish, use, compile, sell, or
~ distribute this software, either in source code form or as a compiled
~ binary, for any purpose, commercial or non-commercial, and by any
~ means.
~
~ In jurisdictions that recognize copyright laws, the author or authors
~ of this software dedicate any and all copyright interest in the
~ software to the public domain. We make this dedication for the benefit
~ of the public at large and to the detriment of our heirs and
~ successors. We intend this dedication to be an overt act of
~ relinquishment in perpetuity of all present and future rights to this
~ software under copyright law.
~
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
~ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
~ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
~ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
~ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
~ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
~ OTHER DEALINGS IN THE SOFTWARE.
~
~ For more information, please refer to <http://unlicense.org />
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>net.adamcin.httpsig</groupId>
<artifactId>httpsig</artifactId>
<version>1.1.1-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>

<artifactId>httpsig-hmac</artifactId>
<packaging>jar</packaging>

<name>${project.artifactId}</name>
<description>
Library providing a HMAC based implementation of the httpsig API
</description>

<inceptionYear>2014</inceptionYear>

<scm>
<url>https://github.com/adamcin/httpsig-java</url>
<developerConnection>scm:git:[email protected]:adamcin/httpsig-java.git</developerConnection>
<connection>scm:git://github.com/adamcin/httpsig-java.git</connection>
<tag>HEAD</tag>
</scm>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>templating-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.github.github</groupId>
<artifactId>site-maven-plugin</artifactId>
<configuration>
<message>Creating site for ${project.artifactId} ${project.version}</message>
<noJekyll>true</noJekyll>
<repositoryOwner>adamcin</repositoryOwner>
<repositoryName>httpsig-java</repositoryName>
<path>${project.artifactId}</path>
<merge>true</merge>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>itests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.14</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

<dependencies>
<dependency>
<groupId>net.adamcin.httpsig</groupId>
<artifactId>httpsig-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>biz.aQute.bnd</groupId>
<artifactId>bndlib</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.adamcin.httpsig</groupId>
<artifactId>httpsig-test-common</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.adamcin.commons</groupId>
<artifactId>net.adamcin.commons.testing</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>

</dependencies>

<developers>
<developer>
<id>niallmccullagh</id>
<name>Niall McCullagh</name>
<url>https://github.com/niallmccullagh</url>
</developer>
</developers>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to <http://unlicense.org/>
*/

@Version("${package-info.version}")
package net.adamcin.httpsig.hmac;

import aQute.bnd.annotation.Version;
150 changes: 150 additions & 0 deletions hmac/src/main/java/net/adamcin/httpsig/hmac/HmacKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to <http://unlicense.org/>
*/

package net.adamcin.httpsig.hmac;

import net.adamcin.httpsig.api.Algorithm;
import net.adamcin.httpsig.api.Key;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

public class HmacKey implements Key {

private final String keyId;
private final String secret;

private static final byte[] EMPTY_BYTES = new byte[0];
private static final Logger LOGGER = LoggerFactory.getLogger(HmacKey.class);

/**
* Instantiates a new HMAC key with an identifier and a secret used to sign
* @param keyId The keys identifier
* @param secret The secret used to sign
*/
public HmacKey(String keyId, String secret) {
this.keyId = Objects.requireNonNull(keyId, "keyId must not be null");
this.secret = Objects.requireNonNull(secret, "secret must not be null");
}

/**
* @return the {@link net.adamcin.httpsig.api.Key}'s self-identification. This may end up not being unique within a keychain.
*/
public String getId() {
return keyId;
}

/**
* @return the {@link java.util.Set} of Signature {@link net.adamcin.httpsig.api.Algorithm}s supported by this key.
*/
public Set<Algorithm> getAlgorithms() {
Set<Algorithm> algorithms = new HashSet<Algorithm>();

algorithms.add(Algorithm.HMAC_SHA512);
algorithms.add(Algorithm.HMAC_SHA256);

return algorithms;
}

/**
* HMAC Keys can always be used to verify
* @return always true
*/
public boolean canVerify() {
return true;
}

/**
* Verifies the {@code signatureBytes} against the {@code challengeHash} using an underlying public key
* @param algorithm the selected Signature {@link net.adamcin.httpsig.api.Algorithm}
* @param contentBytes the result of {@link net.adamcin.httpsig.api.RequestContent#getContent(java.util.List, java.nio.charset.Charset)}
* @param signatureBytes the result of {@link net.adamcin.httpsig.api.Authorization#getSignatureBytes()}
* @return true if signature is valid
*/
public boolean verify(Algorithm algorithm, byte[] contentBytes, byte[] signatureBytes) {
if(getAlgorithms().contains(algorithm)){
byte[] generatedSig = sign(algorithm, contentBytes);
return Arrays.equals(generatedSig, signatureBytes);
}

return false;
}

/**
* Maps http-signatures spec algorithm names to Java
* @param algorithm http-signature spec algorithm name
* @return Java algorithm name
*/
private String mapAlgorithm(Algorithm algorithm) {
if("hmac-sha512".equalsIgnoreCase(algorithm.getName())){
return "HmacSHA512";
}else if("hmac-sha256".equalsIgnoreCase(algorithm.getName())){
return "HmacSHA256";
}else{
throw new IllegalArgumentException(String.format("Unsupported algorithm: %s", algorithm.getName()));
}
}

/**
* HMAC Keys can always be used to sign
* @return always true
*/
public boolean canSign() {
return true;
}

/**
* Signs the {@code challengeHash} using the specified signature {@link net.adamcin.httpsig.api.Algorithm}
* @param algorithm the selected Signature {@link net.adamcin.httpsig.api.Algorithm}
* @param contentBytes the result of {@link net.adamcin.httpsig.api.RequestContent#getContent(java.util.List, java.nio.charset.Charset)}
* @return byte array containing the challengeHash signature or null if a signature could not be generated.
*/
public byte[] sign(Algorithm algorithm, byte[] contentBytes) {
try{

SecretKeySpec secretKeySpec = new SecretKeySpec(secret.getBytes(), mapAlgorithm(algorithm));
Mac mac = Mac.getInstance(mapAlgorithm(algorithm));
mac.init(secretKeySpec);
return mac.doFinal(contentBytes);

} catch (NoSuchAlgorithmException e) {
LOGGER.error("[sign] failed to sign content.", e);
} catch (InvalidKeyException e) {
LOGGER.error("[sign] failed to sign content.", e);
}

return EMPTY_BYTES;
}
}
Loading