Skip to content

Commit

Permalink
Start refactoring KeyOps
Browse files Browse the repository at this point in the history
  • Loading branch information
masomel committed Aug 7, 2016
1 parent 9598520 commit 2f535b4
Show file tree
Hide file tree
Showing 10 changed files with 277 additions and 131 deletions.
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ env:
global:
secure: B5ZEHKFRa11V7e9JZwr4vGFSdo1lvnbxijd00mm41qbvwT5YFSMZhEQLRVlDmxhsqcc6guC84Q49LsY3/4rlzspHe8fkByY2sQUM3aXuK13/lJJT8CeDXxYAicWMPBLvybpt3ML1wXcbskQzv5C7oNLgkeMwvpd2zw5pYMV2dHwRhRkfsYgH7hzlVgCOMnPLHfPiDgjuQK6KJUr1qlO4Y9vmFsCiCi94fLHIxu+MsvFy3XRnnlTERTGQCeBD2GGHTH5wjiwTL0GHdKX/ntpODyWZAnbYs8OYsLuCq7QQHJDk4uBVCnMIj1zt6lwxa0iiz+uuvxnL6msSeF/BkIWG/yxRkBjdVtdJL0WBEIDmfzeEugdNFke1I2dsH68Fe+R2syUwIjJ654HSoLZ/xL9Dbnxo8wmix2cO46ictXN3WVNrWwB4KUV5N+aGW/hB0KRqSsO2iUmXqweifJz7qfWm20Y//pgtMtVTnZVTneiSCPucY1QWQmiOgC0jcseV+oIbpFjbYArxGhhPhqJqAWYhJeR/7uX872zMwPqpyHRF3y1XQ+C2xI9IWC2SqjinCsgUnZei1pu7A4Qd34Isd8uCICrLMu8JPe98+Dc+jksGrH/SgOcDI/pffnD2rk/ablNU7JY8yE2t5me9B6SAxOEOyle60i5WiRHcM+pZukBwvwU=

script: mvn test
script:
# TODO check style
- mvn test

notifications:
# Email notifications are disabled to not annoy anybody.
# See http://about.travis-ci.org/docs/user/build-configuration/ to learn more
# about configuring notification recipients and more.
email: false
7 changes: 0 additions & 7 deletions coniks_common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@
<description>Common message spec and encoding library for CONIKS.</description>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
Expand Down
7 changes: 0 additions & 7 deletions coniks_server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@
</description>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.coniks.coniks_common</groupId>
<artifactId>coniks_common</artifactId>
Expand Down
58 changes: 29 additions & 29 deletions coniks_server/src/main/java/org/coniks/coniks_server/KeyOps.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
/*
Copyright (c) 2015-16, Princeton University.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Princeton University nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/

Expand All @@ -45,7 +45,7 @@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,

import org.coniks.coniks_common.C2SProtos.DSAPublicKeyProto;

/** Implements all encryption-key related operations that a
/** Implements all encryption-key related operations that a
* CONIKS server must perform.
* Current encryption/signing algorithm used: RSA with SHA-256.
*
Expand All @@ -70,14 +70,14 @@ public static RSAPrivateKey loadSigningKey(){

// get user password and file input stream
char[] ks_password = ServerConfig.getKeystorePassword().toCharArray();

FileInputStream fis = null;

fis = new FileInputStream(ServerConfig.getKeystorePath());
ks.load(fis, ks_password);

if(ks.isKeyEntry(ServerConfig.getName())){
KeyStore.ProtectionParameter protParam =
KeyStore.ProtectionParameter protParam =
new KeyStore.PasswordProtection(ks_password);

KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry)
Expand All @@ -92,13 +92,13 @@ public static RSAPrivateKey loadSigningKey(){
}
catch(IOException e){
TimerLogger.error("KeyOps:loadSigningKey: Problem loading the keystore");
}
}
catch(NoSuchAlgorithmException e){
TimerLogger.error("KeyOps:loadSigningKey: Problem with integrity check algorithm");
}
catch(CertificateException e){
TimerLogger.error("KeyOps:loadSigningKey: Problem with the cert(s) in keystore");
}
}
catch(KeyStoreException e){
TimerLogger.error("KeyOps:loadSigningKey: Problem getting Keystore instance");
}
Expand All @@ -111,7 +111,7 @@ public static RSAPrivateKey loadSigningKey(){
/** Load the given server {@code keyOwner}'s public key from the truststore
* indicated in <i>this</i> server's configuration {@code config}.
*
*@return The {@code keyOwner}'s public RSA key, or {@code null} in
*@return The {@code keyOwner}'s public RSA key, or {@code null} in
* the case of an Exception.
*/
public static RSAPublicKey loadPublicKey(String keyOwner){
Expand All @@ -123,14 +123,14 @@ public static RSAPublicKey loadPublicKey(String keyOwner){
ks = KeyStore.getInstance(KeyStore.getDefaultType());

char[] ts_password = ServerConfig.getTruststorePassword().toCharArray();

FileInputStream fis = null;

fis = new FileInputStream(ServerConfig.getTruststorePath());
ks.load(fis, ts_password);

if(ks.isKeyEntry(keyOwner)){
KeyStore.ProtectionParameter protParam =
KeyStore.ProtectionParameter protParam =
new KeyStore.PasswordProtection(ts_password);

KeyStore.TrustedCertificateEntry pkEntry = (KeyStore.TrustedCertificateEntry)
Expand All @@ -145,13 +145,13 @@ public static RSAPublicKey loadPublicKey(String keyOwner){
}
catch(IOException e){
ServerLogger.error("KeyOps:loadPublicKey: Problem loading the keystore");
}
}
catch(NoSuchAlgorithmException e){
ServerLogger.error("KeyOps:loadPublicKey: Problem with integrity check algorithm");
}
catch(CertificateException e){
ServerLogger.error("KeyOps:loadPublicKey: Problem with the cert(s) in keystore");
}
}
catch(KeyStoreException e){
ServerLogger.error("KeyOps:loadPublicKey: Problem getting Keystore instance");
}
Expand Down
7 changes: 0 additions & 7 deletions coniks_test_client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@
</description>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.coniks.coniks_common</groupId>
<artifactId>coniks_common</artifactId>
Expand Down
22 changes: 0 additions & 22 deletions crypto/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,6 @@
Cryptographic algorithms and operations library for CONIKS.
</description>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<useFile>false</useFile>
</configuration>
</plugin>
</plugins>
</build>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Expand Down
134 changes: 134 additions & 0 deletions crypto/src/main/java/org/coniks/crypto/Keys.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
Copyright (c) 2015-16, Princeton University.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Princeton University nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/

package org.coniks.crypto;

import java.security.*;
import java.security.interfaces.*;
import java.security.spec.*;
import java.math.BigInteger;

/** Implements all encryption-key related operations that a
* CONIKS server must perform.
* Current encryption/signing algorithm used: RSA with SHA-256.
*
*@author Marcela S. Melara ([email protected])
*@author Michael Rochlin
*/
public class Keys {

/** Generates a DSA key pair.
*
*@return the DSA key pair or null in case of an error.
*/
public static KeyPair generateDSAKeyPair()
throws NoSuchAlgorithmException {
KeyPairGenerator gen = KeyPairGenerator.getInstance("DSA");
gen.initialize(1024);

KeyPair pair = gen.generateKeyPair();

return pair;
}

/** Get the private key from the DSA key pair.
*
*@param kp The DSA KeyPair.
*@return the DSA private key.
*/
public static DSAPrivateKey getDSAPrivate(KeyPair kp) {
return (DSAPrivateKey)kp.getPrivate();
}

/** Get the public key from the DSA key pair.
*
*@param kp The DSA KeyPair.
*@return the DSA public key.
*/
public static DSAPublicKey getDSAPublic(KeyPair kp) {
return (DSAPublicKey)kp.getPublic();
}

/** Makes a {@link DSAPublicKey} from its {@code p}, {@code q},
* {@code g} and {@code y} parameters.
*
*@return the DSAPublicKey, or {@code null} in case of an error.
*/
public static DSAPublicKey getDSAPublicFromParams(BigInteger p, BigInteger q,
BigInteger g, BigInteger y) {

DSAPublicKey pk = null;
try {
KeyFactory keyFactory = KeyFactory.getInstance("DSA");
KeySpec publicKeySpec = new DSAPublicKeySpec(y, p, q, g);
pk = (DSAPublicKey)keyFactory.generatePublic(publicKeySpec);
}
// let's panic if an exception occurs
finally {
return pk;
}
}

/** Generates an RSA key pair.
*
*@return the RSA key pair or null in case of an error.
*/
public static KeyPair generateRSAKeyPair()
throws NoSuchAlgorithmException {
KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
gen.initialize(2048);

KeyPair pair = gen.generateKeyPair();

return pair;
}

/** Get the private key from the RSA key pair.
*
*@param kp The RSA KeyPair.
*@return the RSA private key.
*/
public static RSAPrivateKey getRSAPrivate(KeyPair kp) {
return (RSAPrivateKey)kp.getPrivate();
}

/** Get the public key from the RSA key pair.
*
*@param kp The RSA KeyPair.
*@return the RSA public key.
*/
public static RSAPublicKey getRSAPublic(KeyPair kp) {
return (RSAPublicKey)kp.getPublic();
}

}
Loading

0 comments on commit 2f535b4

Please sign in to comment.