Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce common IKey interface #564

Merged
merged 2 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ _Code:_
- JNI libraries are now available as `libthemis-jni` packages for supported Linux systems ([#552](https://github.com/cossacklabs/themis/pull/552), [#553](https://github.com/cossacklabs/themis/pull/553)).
- Fixed a NullPointerException bug in `SecureSocket` initialisation ([#557](https://github.com/cossacklabs/themis/pull/557)).
- Some Themis exceptions have been converted from checked `Exception` to _unchecked_ `RuntimeException`, relaxing requirements for `throws` specifiers ([#563](https://github.com/cossacklabs/themis/pull/563)).
- Introduced `IKey` interface with accessors to raw key data ([#564](https://github.com/cossacklabs/themis/pull/564)).

- **Python**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,20 @@
/**
* Base class for Themis asymmetric keys
*/
public abstract class AsymmetricKey {
public abstract class AsymmetricKey extends KeyBytes {

public static final int KEYTYPE_EC = 0;
public static final int KEYTYPE_RSA = 1;

byte[] key;

/**
* Creates asymmetric key from byte array
* @param [in] key
* Creates asymmetric key from byte array.
*
* @param key byte array
*
* @throws NullArgumentException if `key` is null.
* @throws InvalidArgumentException if `key` is empty.
*/
public AsymmetricKey(byte[] key) {
this.key = key;
}

/**
* Serializes this key to a byte array
* @return key as byte array
*/
public byte[] toByteArray() {
return key.clone();
super(key);
}
}
30 changes: 30 additions & 0 deletions src/wrappers/themis/java/com/cossacklabs/themis/IKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2019 Cossack Labs Limited
*
* Licensed 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 com.cossacklabs.themis;

/**
* Cryptographic key.
*/
public interface IKey {

/**
* Serializes this key to a byte array.
*
* @return key as byte array.
*/
public byte[] toByteArray();
}
50 changes: 50 additions & 0 deletions src/wrappers/themis/java/com/cossacklabs/themis/KeyBytes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2019 Cossack Labs Limited
*
* Licensed 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 com.cossacklabs.themis;

/**
* Key byte storage.
*/
abstract class KeyBytes implements IKey {

final byte[] key;

/**
* Creates a new key from byte array.
*
* @param key byte array
*
* @throws NullArgumentException if `key` is null.
* @throws InvalidArgumentException if `key` is empty.
*/
public KeyBytes(byte[] key) {
if (key == null) {
throw new NullArgumentException("key cannot be null");
}
if (key.length == 0) {
throw new InvalidArgumentException("key cannot be empty");
}
this.key = key;
}

@Override
public byte[] toByteArray() {
// Java arrays are always mutable. Return a copy to the caller
// so that their modifications do not affect the actual key.
return this.key.clone();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
public class PrivateKey extends AsymmetricKey {

/**
* Creates new private key from byte array
* Creates private key from byte array.
*
* @param key byte array
*
* @throws NullArgumentException if `key` is null.
* @throws InvalidArgumentException if `key` is empty.
*/
public PrivateKey(byte[] key) {

super(key);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
public class PublicKey extends AsymmetricKey {

/**
* Creates new public key from byte array
* Creates public key from byte array.
*
* @param key byte array
*
* @throws NullArgumentException if `key` is null.
* @throws InvalidArgumentException if `key` is empty.
*/
public PublicKey(byte[] key) {

super(key);

}

}