diff options
Diffstat (limited to 'repackaged_platform/bcprov/src/main/java/com/android/internal/org/bouncycastle/jcajce/spec/OpenSSHPublicKeySpec.java')
-rw-r--r-- | repackaged_platform/bcprov/src/main/java/com/android/internal/org/bouncycastle/jcajce/spec/OpenSSHPublicKeySpec.java | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/repackaged_platform/bcprov/src/main/java/com/android/internal/org/bouncycastle/jcajce/spec/OpenSSHPublicKeySpec.java b/repackaged_platform/bcprov/src/main/java/com/android/internal/org/bouncycastle/jcajce/spec/OpenSSHPublicKeySpec.java new file mode 100644 index 00000000..93f553a8 --- /dev/null +++ b/repackaged_platform/bcprov/src/main/java/com/android/internal/org/bouncycastle/jcajce/spec/OpenSSHPublicKeySpec.java @@ -0,0 +1,79 @@ +/* GENERATED SOURCE. DO NOT MODIFY. */ +package com.android.internal.org.bouncycastle.jcajce.spec; + +import java.security.spec.EncodedKeySpec; + +import com.android.internal.org.bouncycastle.util.Arrays; +import com.android.internal.org.bouncycastle.util.Strings; + +/** + * Holds an OpenSSH encoded public key. + * @hide This class is not part of the Android public SDK API + */ +public class OpenSSHPublicKeySpec + extends EncodedKeySpec +{ + private static final String[] allowedTypes = new String[]{"ssh-rsa", "ssh-ed25519", "ssh-dss"}; + private final String type; + + + /** + * Construct and instance and determine the OpenSSH public key type. + * The current types are ssh-rsa, ssh-ed25519, ssh-dss and ecdsa-* + * <p> + * It does not validate the key beyond identifying the type. + * + * @param encodedKey + */ + public OpenSSHPublicKeySpec(byte[] encodedKey) + { + super(encodedKey); + + // + // The type is encoded at the start of the blob. + // + int pos = 0; + int i = (encodedKey[pos++] & 0xFF) << 24; + i |= (encodedKey[pos++] & 0xFF) << 16; + i |= (encodedKey[pos++] & 0xFF) << 8; + i |= (encodedKey[pos++] & 0xFF); + + if ((pos + i) >= encodedKey.length) + { + throw new IllegalArgumentException("invalid public key blob: type field longer than blob"); + } + + this.type = Strings.fromByteArray(Arrays.copyOfRange(encodedKey, pos, pos + i)); + + if (type.startsWith("ecdsa")) + { + return; // These have a curve name and digest in them and can't be compared exactly. + } + + for (int t = 0; t < allowedTypes.length; t++) + { + if (allowedTypes[t].equals(this.type)) + { + return; + } + } + + throw new IllegalArgumentException("unrecognised public key type " + type); + + } + + public String getFormat() + { + return "OpenSSH"; + } + + /** + * The type of OpenSSH public key. + * + * @return the type. + */ + public String getType() + { + return type; + } +} |