Top Description Inners Fields Constructors Methods
io.vertx.core.net.impl.pkcs1

public Class PrivateKeyParser

extends Object
Class Inheritance
Imports
java.io.UnsupportedEncodingException, java.math.BigInteger, java.security.GeneralSecurityException, .KeyPairGenerator, java.security.interfaces.ECPublicKey, java.security.spec.ECGenParameterSpec, .ECParameterSpec, .ECPrivateKeySpec, .RSAPrivateCrtKeySpec, java.util.Arrays, io.vertx.core.VertxException, io.vertx.core.buffer.Buffer

This code is copies and modifies over from net.oauth.java.jmeter:ApacheJMeter_oauth

Source is from https://web.archive.org/web/20110827064643/http://oauth.googlecode.com:80/svn/code/branches/jmeter/jmeter/src/main/java/org/apache/jmeter/protocol/oauth/sampler/PrivateKeyReader.java which is licensed under the APL (see also https://web.archive.org/web/20120202095848/http://code.google.com:80/p/jmeter-oauth/)

All credits go to the original author Zhihong Zhang (zhihong@gmail)

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
pack-priv static class
PrivateKeyParser.DerParser

A bare-minimum ASN.1 DER decoder, just having enough functions to decode PKCS#1 private keys.

Field Summary

Modifier and TypeField and Description
private static final byte[]
OID_EC_PUBLIC_KEY

ASN.1 OID for EC public key.

private static final byte[]
OID_RSA_PUBLIC_KEY

ASN.1 OID for RSA public key.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static ECPrivateKeySpec

Returns:

The spec that can be used to instantiate the private key.
getECKeySpec
(byte[]
The encoded key.
keyBytes
)

Converts a DER encoded ECPrivateKey into a Java ECPrivateKeySpec.

private static ECParameterSpec
public static String

Returns:

The algorithm name, either RSA or EC, depending on the algorithm identifier found in the encoded key.
getPKCS8EncodedKeyAlgorithm
(byte[]
The encoded private key.
encodedKey
)

Gets the algorithm used by a PKCS#8 encoded private key.

public static RSAPrivateCrtKeySpec

Returns:

KeySpec
getRSAKeySpec
(byte[]
PKCS#1 encoded key
keyBytes
)

Convert PKCS#1 encoded private key into RSAPrivateCrtKeySpec.

private static String
oidToString(byte[] oid)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

OID_EC_PUBLIC_KEYback to summary
private static final byte[] OID_EC_PUBLIC_KEY

ASN.1 OID for EC public key.

OID_RSA_PUBLIC_KEYback to summary
private static final byte[] OID_RSA_PUBLIC_KEY

ASN.1 OID for RSA public key.

Constructor Detail

PrivateKeyParserback to summary
public PrivateKeyParser()

Method Detail

getECKeySpecback to summary
public static ECPrivateKeySpec getECKeySpec(byte[] keyBytes) throws VertxException

Converts a DER encoded ECPrivateKey into a Java ECPrivateKeySpec.

RFC 5915 defines the following ASN.1 syntax for an EC private key:

ECPrivateKey ::= SEQUENCE {
  version        INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
  privateKey     OCTET STRING,
  parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
  publicKey  [1] BIT STRING OPTIONAL
}

A private key encoded like this will most often be found in a PEM file which will contain the Base64 encoded DER-encoding of an ECPrivateKey sandwiched between

-----BEGIN EC PRIVATE KEY-----
-----END EC PRIVATE KEY-----
as described in RFC 5915, Section 4
Parameters
keyBytes:byte[]

The encoded key.

Returns:ECPrivateKeySpec

The spec that can be used to instantiate the private key.

Exceptions
VertxException:
if the byte array does not represent an ASN.1 ECPrivateKey structure.
See Also
https://datatracker.ietf.org/doc/html/rfc5915
getECParameterSpecback to summary
private static ECParameterSpec getECParameterSpec(String curveName) throws VertxException
getPKCS8EncodedKeyAlgorithmback to summary
public static String getPKCS8EncodedKeyAlgorithm(byte[] encodedKey)

Gets the algorithm used by a PKCS#8 encoded private key.

Parameters
encodedKey:byte[]

The encoded private key.

Returns:String

The algorithm name, either RSA or EC, depending on the algorithm identifier found in the encoded key.

Exceptions
VertxException:
if the key is not PKCS#8 encoded or uses an unsupported algorithm.
getRSAKeySpecback to summary
public static RSAPrivateCrtKeySpec getRSAKeySpec(byte[] keyBytes) throws VertxException

Convert PKCS#1 encoded private key into RSAPrivateCrtKeySpec.

The ASN.1 syntax for the private key with CRT is

--
-- Representation of RSA private key with information for the CRT algorithm.
--
RSAPrivateKey ::= SEQUENCE {
  version           Version,
  modulus           INTEGER,  -- n
  publicExponent    INTEGER,  -- e
  privateExponent   INTEGER,  -- d
  prime1            INTEGER,  -- p
  prime2            INTEGER,  -- q
  exponent1         INTEGER,  -- d mod (p-1)
  exponent2         INTEGER,  -- d mod (q-1)
  coefficient       INTEGER,  -- (inverse of q) mod p
  otherPrimeInfos   OtherPrimeInfos OPTIONAL
}
Parameters
keyBytes:byte[]

PKCS#1 encoded key

Returns:RSAPrivateCrtKeySpec

KeySpec

oidToStringback to summary
private static String oidToString(byte[] oid)
io.vertx.core.net.impl.pkcs1 back to summary

pack-priv Class PrivateKeyParser.Asn1Object

extends Object
Class Inheritance

An ASN.1 TLV. The object is not parsed. It can only handle integers and strings.
Author
zhang

Field Summary

Modifier and TypeField and Description
protected final int
protected final int
protected final int
protected final byte[]

Constructor Summary

AccessConstructor and Description
public
Asn1Object(int
Tag or Identifier
tag
,
int
Length of the field
length
,
byte[]
Encoded octet string for the field.
value
)

Construct a ASN.1 TLV.

Method Summary

Modifier and TypeMethod and Description
public BigInteger

Returns:

BigInteger
getInteger
()

Get the value as integer

public int
public byte[]
public PrivateKeyParser.DerParser

Returns:

A parser for the construct.
getParser
()

For constructed field, return a parser for its content.

public String

Returns:

Java string
getString
()

Get value as string.

public int
public byte[]
public boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

lengthback to summary
protected final int length
tagback to summary
protected final int tag
typeback to summary
protected final int type
valueback to summary
protected final byte[] value

Constructor Detail

Asn1Objectback to summary
public Asn1Object(int tag, int length, byte[] value)

Construct a ASN.1 TLV. The TLV could be either a constructed or primitive entity.

The first byte in DER encoding is made of following fields,

-------------------------------------------------
|Bit 8|Bit 7|Bit 6|Bit 5|Bit 4|Bit 3|Bit 2|Bit 1|
-------------------------------------------------
|  Class    | CF  |     +      Type             |
-------------------------------------------------
  • Class: Universal, Application, Context or Private
  • CF: Constructed flag. If 1, the field is constructed.
  • Type: This is actually called tag in ASN.1. It indicates data type (Integer, String) or a construct (sequence, choice, set).
Parameters
tag:int

Tag or Identifier

length:int

Length of the field

value:byte[]

Encoded octet string for the field.

Method Detail

getIntegerback to summary
public BigInteger getInteger() throws VertxException

Get the value as integer

Returns:BigInteger

BigInteger

getLengthback to summary
public int getLength()
getObjectIdentifierback to summary
public byte[] getObjectIdentifier() throws VertxException
getParserback to summary
public PrivateKeyParser.DerParser getParser() throws VertxException

For constructed field, return a parser for its content.

Returns:PrivateKeyParser.DerParser

A parser for the construct.

getStringback to summary
public String getString() throws VertxException

Get value as string. Most strings are treated as Latin-1.

Returns:String

Java string

getTypeback to summary
public int getType()
getValueback to summary
public byte[] getValue()
isConstructedback to summary
public boolean isConstructed()
io.vertx.core.net.impl.pkcs1 back to summary

pack-priv Class PrivateKeyParser.DerParser

extends Object
Class Inheritance

A bare-minimum ASN.1 DER decoder, just having enough functions to decode PKCS#1 private keys. Especially, it doesn't handle explicitly tagged types with an outer tag.

This parser can only handle one layer. To parse nested constructs, get a new parser for each layer using Asn1Object.getParser().

There are many DER decoders in JRE but using them will tie this program to a specific JCE/JVM.

Author
zhang

Field Summary

Modifier and TypeField and Description
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private Buffer
in

References Deprecated Buffer is deprecated or references (maybe indirectly) at least one deprecated element.
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int
private static final int

Constructor Summary

AccessConstructor and Description
pack-priv
DerParser(Buffer
The DER encoded stream
in
)
References Deprecated Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

Create a new DER decoder from an input stream.

pack-priv
DerParser(byte[]
The encoded bytes
bytes
)

Create a new DER decoder from a byte array.

Method Summary

Modifier and TypeMethod and Description
private int

Returns:

The length as integer
getLength
()

Decode the length of the field.

public PrivateKeyParser.Asn1Object

Returns:

A object
read
()

Read next object.

private int
private byte[]
readBytes(int len)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ANYback to summary
private static final int ANY
APPLICATIONback to summary
private static final int APPLICATION
BIT_STRINGback to summary
private static final int BIT_STRING
BMP_STRINGback to summary
private static final int BMP_STRING
BOOLEANback to summary
private static final int BOOLEAN
CONSTRUCTEDback to summary
private static final int CONSTRUCTED
CONTEXTback to summary
private static final int CONTEXT
ENUMERATEDback to summary
private static final int ENUMERATED
GENERAL_STRINGback to summary
private static final int GENERAL_STRING
GRAPHIC_STRINGback to summary
private static final int GRAPHIC_STRING
IA5_STRINGback to summary
private static final int IA5_STRING
inback to summary
private Buffer in

References Deprecated

Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

INTEGERback to summary
private static final int INTEGER
ISO646_STRINGback to summary
private static final int ISO646_STRING
NULLback to summary
private static final int NULL
NUMERIC_STRINGback to summary
private static final int NUMERIC_STRING
OBJECT_IDENTIFIERback to summary
private static final int OBJECT_IDENTIFIER
OCTET_STRINGback to summary
private static final int OCTET_STRING
posback to summary
private int pos
PRINTABLE_STRINGback to summary
private static final int PRINTABLE_STRING
PRIVATEback to summary
private static final int PRIVATE
REALback to summary
private static final int REAL
SEQUENCEback to summary
private static final int SEQUENCE
SETback to summary
private static final int SET
UNIVERSALback to summary
private static final int UNIVERSAL
UNIVERSAL_STRINGback to summary
private static final int UNIVERSAL_STRING
UTC_TIMEback to summary
private static final int UTC_TIME
UTF8_STRINGback to summary
private static final int UTF8_STRING
VIDEOTEX_STRINGback to summary
private static final int VIDEOTEX_STRING

Constructor Detail

DerParserback to summary
pack-priv DerParser(Buffer in) throws VertxException

References Deprecated

Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Create a new DER decoder from an input stream.

Parameters
in:Buffer

The DER encoded stream

DerParserback to summary
pack-priv DerParser(byte[] bytes) throws VertxException

Create a new DER decoder from a byte array.

Parameters
bytes:byte[]

The encoded bytes

Method Detail

getLengthback to summary
private int getLength() throws VertxException

Decode the length of the field. Can only support length encoding up to 4 octets.

In BER/DER encoding, length can be encoded in 2 forms,

  • Short form. One octet. Bit 8 has value "0" and bits 7-1 give the length.
  • Long form. Two to 127 octets (only 4 is supported here). Bit 8 of first octet has value "1" and bits 7-1 give the number of additional length octets. Second and following octets give the length, base 256, most significant digit first.
Returns:int

The length as integer

readback to summary
public PrivateKeyParser.Asn1Object read() throws VertxException

Read next object. If it's constructed, the value holds encoded content and it should be parsed by a new parser from Asn1Object.getParser.

Returns:PrivateKeyParser.Asn1Object

A object

readByteback to summary
private int readByte() throws VertxException
readBytesback to summary
private byte[] readBytes(int len) throws VertxException