2011-12-20 10:20:44 -08:00
|
|
|
/**
|
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2013-11-10 04:15:29 -08:00
|
|
|
* Copyright (C) 2013 Open Whisper Systems
|
2011-12-20 10:20:44 -08:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2013-08-17 18:37:18 -07:00
|
|
|
package org.whispersystems.textsecure.crypto;
|
2011-12-20 10:20:44 -08:00
|
|
|
|
|
|
|
import android.os.Parcel;
|
|
|
|
import android.os.Parcelable;
|
|
|
|
|
2013-11-10 04:15:29 -08:00
|
|
|
import org.whispersystems.textsecure.crypto.ecc.Curve;
|
|
|
|
import org.whispersystems.textsecure.crypto.ecc.ECPublicKey;
|
2013-08-17 18:37:18 -07:00
|
|
|
import org.whispersystems.textsecure.util.Hex;
|
2013-11-10 04:15:29 -08:00
|
|
|
import org.whispersystems.textsecure.util.Util;
|
2013-08-15 08:25:30 -07:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
/**
|
|
|
|
* A class for representing an identity key.
|
|
|
|
*
|
|
|
|
* @author Moxie Marlinspike
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class IdentityKey implements Parcelable, SerializableKey {
|
|
|
|
|
|
|
|
public static final Parcelable.Creator<IdentityKey> CREATOR = new Parcelable.Creator<IdentityKey>() {
|
|
|
|
public IdentityKey createFromParcel(Parcel in) {
|
|
|
|
try {
|
|
|
|
return new IdentityKey(in);
|
|
|
|
} catch (InvalidKeyException e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public IdentityKey[] newArray(int size) {
|
|
|
|
return new IdentityKey[size];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-11-10 04:15:29 -08:00
|
|
|
public static final int SIZE = 1 + ECPublicKey.KEY_SIZE;
|
|
|
|
private static final int CURRENT_VESION = 1;
|
|
|
|
|
|
|
|
private ECPublicKey publicKey;
|
|
|
|
|
|
|
|
public IdentityKey(ECPublicKey publicKey) {
|
2011-12-20 10:20:44 -08:00
|
|
|
this.publicKey = publicKey;
|
|
|
|
}
|
2013-11-10 04:15:29 -08:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
public IdentityKey(Parcel in) throws InvalidKeyException {
|
|
|
|
int length = in.readInt();
|
|
|
|
byte[] serialized = new byte[length];
|
|
|
|
|
|
|
|
in.readByteArray(serialized);
|
|
|
|
initializeFromSerialized(serialized, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public IdentityKey(byte[] bytes, int offset) throws InvalidKeyException {
|
|
|
|
initializeFromSerialized(bytes, offset);
|
|
|
|
}
|
2013-11-10 04:15:29 -08:00
|
|
|
|
|
|
|
public ECPublicKey getPublicKey() {
|
|
|
|
return publicKey;
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
2013-11-10 04:15:29 -08:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
private void initializeFromSerialized(byte[] bytes, int offset) throws InvalidKeyException {
|
2013-11-10 04:15:29 -08:00
|
|
|
int version = bytes[offset] & 0xff;
|
2011-12-20 10:20:44 -08:00
|
|
|
|
2013-11-10 04:15:29 -08:00
|
|
|
if (version > CURRENT_VESION)
|
2011-12-20 10:20:44 -08:00
|
|
|
throw new InvalidKeyException("Unsupported key version: " + version);
|
2013-08-15 08:25:30 -07:00
|
|
|
|
2013-11-10 04:15:29 -08:00
|
|
|
this.publicKey = Curve.decodePoint(bytes, offset + 1);
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] serialize() {
|
2013-11-10 04:15:29 -08:00
|
|
|
byte[] versionBytes = {(byte)CURRENT_VESION};
|
|
|
|
byte[] encodedKey = publicKey.serialize();
|
|
|
|
|
|
|
|
return Util.combine(versionBytes, encodedKey);
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
2013-11-10 04:15:29 -08:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
public String getFingerprint() {
|
2013-11-10 04:15:29 -08:00
|
|
|
return Hex.toString(publicKey.serialize());
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object other) {
|
2013-11-10 04:15:29 -08:00
|
|
|
if (other == null) return false;
|
2011-12-20 10:20:44 -08:00
|
|
|
if (!(other instanceof IdentityKey)) return false;
|
2013-11-10 04:15:29 -08:00
|
|
|
|
|
|
|
return publicKey.equals(((IdentityKey) other).getPublicKey());
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
2013-11-10 04:15:29 -08:00
|
|
|
return publicKey.hashCode();
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public int describeContents() {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void writeToParcel(Parcel dest, int flags) {
|
|
|
|
byte[] serialized = this.serialize();
|
|
|
|
dest.writeInt(serialized.length);
|
|
|
|
dest.writeByteArray(serialized);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|