use of com.github.zhenwei.core.asn1.DERUTF8String in project ddf by codice.
the class SignerCondition method getIdentifyFromBytes.
// this code was grabbed online and should be correct
// we don't have any tests for this so be wary of changing this code
private String getIdentifyFromBytes(byte[] itemBytes) {
try (ASN1InputStream decoder = new ASN1InputStream(itemBytes)) {
ASN1Encodable encoded = decoder.readObject();
encoded = ((DERSequence) encoded).getObjectAt(1);
encoded = ((DERTaggedObject) encoded).getObject();
encoded = ((DERTaggedObject) encoded).getObject();
return ((DERUTF8String) encoded).getString();
} catch (IOException e) {
return "";
}
}
use of com.github.zhenwei.core.asn1.DERUTF8String in project LinLong-Java by zhenwei1108.
the class CMSTimeStampedGenerator method setMetaData.
/**
* Set the MetaData for the generated message.
*
* @param hashProtected true if the MetaData should be included in first imprint calculation,
* false otherwise.
* @param fileName optional file name, may be null.
* @param mediaType optional media type, may be null.
* @param attributes optional attributes, may be null.
*/
public void setMetaData(boolean hashProtected, String fileName, String mediaType, Attributes attributes) {
ASN1UTF8String asn1FileName = null;
if (fileName != null) {
asn1FileName = new DERUTF8String(fileName);
}
ASN1IA5String asn1MediaType = null;
if (mediaType != null) {
asn1MediaType = new DERIA5String(mediaType);
}
setMetaData(hashProtected, asn1FileName, asn1MediaType, attributes);
}
use of com.github.zhenwei.core.asn1.DERUTF8String in project LinLong-Java by zhenwei1108.
the class OERInputStream method parse.
public ASN1Object parse(OERDefinition.Element element) throws Exception {
switch(element.baseType) {
case SEQ_OF:
{
int l = readLength().intLength();
// This l is the number of bytes that holds the actual length of the sequence of.
byte[] lenEnc = allocateArray(l);
if (Streams.readFully(this, lenEnc) != lenEnc.length) {
throw new IOException("could not read all of count of seq-of values");
}
//
// The actual number of elements encoded into this seq-of.
//
int j = BigIntegers.fromUnsignedByteArray(lenEnc).intValue();
debugPrint(element.appendLabel("(len = " + j + ")"));
ASN1EncodableVector avec = new ASN1EncodableVector();
for (int n = 0; n < j; n++) {
avec.add(parse(element.children.get(0)));
}
return new DERSequence(avec);
}
case SEQ:
{
Sequence sequence = sequence(countOptionalChildTypes(element), element.hasDefaultChildren(), element.extensionsInDefinition);
debugPrint(element.appendLabel(sequence.toString()));
ASN1EncodableVector avec = new ASN1EncodableVector();
for (int t = 0; t < element.children.size(); t++) {
OERDefinition.Element child = element.children.get(t);
if (child.explicit) {
// debugPrint(child.appendLabel("E[" + t + "]"));
avec.add(parse(child));
} else {
if (sequence.hasOptional(element.optionalOrDefaultChildrenInOrder().indexOf(child))) {
// debugPrint(child.appendLabel("O[" + t + "]"));
avec.add(OEROptional.getInstance(parse(child)));
} else {
if (child.getDefaultValue() != null) {
avec.add(child.defaultValue);
debugPrint("Using default.");
} else {
avec.add(absent(child));
}
}
}
}
return new DERSequence(avec);
}
case CHOICE:
{
Choice choice = choice();
debugPrint(element.appendLabel(choice.toString()));
if (choice.isContextSpecific()) {
OERDefinition.Element item = element.children.get(choice.getTag());
return new DERTaggedObject(choice.tag, parse(element.children.get(choice.getTag())));
} else if (choice.isApplicationTagClass()) {
throw new IllegalStateException("Unimplemented tag type");
} else if (choice.isPrivateTagClass()) {
throw new IllegalStateException("Unimplemented tag type");
} else if (choice.isUniversalTagClass()) {
switch(choice.getTag()) {
}
} else {
throw new IllegalStateException("Unimplemented tag type");
}
}
case ENUM:
{
BigInteger bi = enumeration();
debugPrint(element.appendLabel("ENUM(" + bi + ") = " + element.children.get(bi.intValue()).label));
return new ASN1Enumerated(bi);
}
case INT:
{
byte[] data;
BigInteger bi;
//
// Special fixed width cases used for signed and unsigned 8/16/24/32/64 bit numbers.
//
int bytesToRead = element.intBytesForRange();
if (// Fixed width
bytesToRead != 0) {
data = allocateArray(Math.abs(bytesToRead));
Streams.readFully(this, data);
switch(data.length) {
case 1:
bi = BigInteger.valueOf(data[0]);
break;
case 2:
bi = BigInteger.valueOf(Pack.bigEndianToShort(data, 0));
break;
case 4:
bi = BigInteger.valueOf(Pack.bigEndianToInt(data, 0));
break;
case 8:
bi = BigInteger.valueOf(Pack.bigEndianToLong(data, 0));
break;
default:
throw new IllegalStateException("Unknown size");
}
} else if (// INTEGER(0 ... MAX) or INTEGER (0 ... n)
element.isLowerRangeZero()) {
LengthInfo lengthInfo = readLength();
data = allocateArray(lengthInfo.intLength());
Streams.readFully(this, data);
if (data.length == 0) {
bi = BigInteger.ZERO;
} else {
bi = BigIntegers.fromUnsignedByteArray(data);
}
} else {
//
// Classic twos compliment.
//
LengthInfo lengthInfo = readLength();
data = allocateArray(lengthInfo.intLength());
Streams.readFully(this, data);
if (data.length == 0) {
bi = BigInteger.ZERO;
} else {
bi = new BigInteger(data);
}
}
if (debugOutput != null) {
debugPrint(element.appendLabel("INTEGER(" + data.length + " " + bi.toString(16) + ")"));
}
return new ASN1Integer(bi);
}
case OCTET_STRING:
{
byte[] data;
int readSize = 0;
if (element.upperBound != null && element.upperBound.equals(element.lowerBound)) {
// Fixed length there is no range.
readSize = element.upperBound.intValue();
} else {
readSize = readLength().intLength();
}
data = allocateArray(readSize);
if (Streams.readFully(this, data) != readSize) {
throw new IOException("did not read all of " + element.label);
}
if (debugOutput != null) {
debugPrint(element.appendLabel("OCTET STRING (" + data.length + ") = " + Hex.toHexString(data, 0, Math.min(data.length, 32))));
}
return new DEROctetString(data);
}
case UTF8_STRING:
{
// 27.3 and 27.4 a length determinant followed by a number of octets.
byte[] data = allocateArray(readLength().intLength());
if (Streams.readFully(this, data) != data.length) {
throw new IOException("could not read all of utf 8 string");
}
String content = Strings.fromUTF8ByteArray(data);
if (debugOutput != null) {
debugPrint(element.appendLabel("UTF8 String (" + data.length + ") = " + content));
}
return new DERUTF8String(content);
}
case BIT_STRING:
{
byte[] data;
if (element.isFixedLength()) {
data = new byte[element.lowerBound.intValue() / 8];
} else if (BigInteger.ZERO.compareTo(element.upperBound) > 0) {
// Fixed size.
data = allocateArray(element.upperBound.intValue() / 8);
} else {
// Length defined.
data = allocateArray(readLength().intLength() / 8);
}
Streams.readFully(this, data);
if (debugOutput != null) {
StringBuffer sb = new StringBuffer();
sb.append("BIT STRING(" + (data.length * 8) + ") = ");
for (int i = 0; i != data.length; i++) {
byte b = data[i];
for (int t = 0; t < 8; t++) {
sb.append((b & 0x80) > 0 ? "1" : "0");
b <<= 1;
}
}
debugPrint(element.appendLabel(sb.toString()));
}
return new DERBitString(data);
}
case NULL:
debugPrint(element.appendLabel("NULL"));
return DERNull.INSTANCE;
case EXTENSION:
LengthInfo li = readLength();
byte[] value = new byte[li.intLength()];
if (Streams.readFully(this, value) != li.intLength()) {
throw new IOException("could not read all of count of open value in choice (...) ");
}
debugPrint("ext " + li.intLength() + " " + Hex.toHexString(value));
return new DEROctetString(value);
}
throw new IllegalStateException("Unhandled type " + element.baseType);
}
use of com.github.zhenwei.core.asn1.DERUTF8String in project X-Road by nordic-institute.
the class DigestList method singleDigest.
/**
* Encodes hash value as SingleDigest data structure.
*/
private static DERSequence singleDigest(String digestMethodUri, byte[] digest) throws Exception {
DEROctetString digestValue = new DEROctetString(digest);
DERUTF8String digestMethod = new DERUTF8String(digestMethodUri);
DERSequence transforms = new DERSequence();
return new DERSequence(new ASN1Encodable[] { digestValue, digestMethod, transforms });
}
use of com.github.zhenwei.core.asn1.DERUTF8String in project Conversations by iNPUTmice.
the class XmppDomainVerifier method parseOtherName.
private static Pair<String, String> parseOtherName(byte[] otherName) {
try {
ASN1Primitive asn1Primitive = ASN1Primitive.fromByteArray(otherName);
if (asn1Primitive instanceof DERTaggedObject) {
ASN1Primitive inner = ((DERTaggedObject) asn1Primitive).getObject();
if (inner instanceof DLSequence) {
DLSequence sequence = (DLSequence) inner;
if (sequence.size() >= 2 && sequence.getObjectAt(1) instanceof DERTaggedObject) {
String oid = sequence.getObjectAt(0).toString();
ASN1Primitive value = ((DERTaggedObject) sequence.getObjectAt(1)).getObject();
if (value instanceof DERUTF8String) {
return new Pair<>(oid, ((DERUTF8String) value).getString());
} else if (value instanceof DERIA5String) {
return new Pair<>(oid, ((DERIA5String) value).getString());
}
}
}
}
return null;
} catch (IOException e) {
return null;
}
}
Aggregations