use of com.github.zhenwei.core.asn1.ASN1ApplicationSpecific in project LinLong-Java by zhenwei1108.
the class ASN1Dump method _dumpAsString.
/**
* dump a DER object as a formatted string with indentation
*
* @param obj the ASN1Primitive to be dumped out.
*/
static void _dumpAsString(String indent, boolean verbose, ASN1Primitive obj, StringBuffer buf) {
String nl = Strings.lineSeparator();
if (obj instanceof ASN1Null) {
buf.append(indent);
buf.append("NULL");
buf.append(nl);
} else if (obj instanceof ASN1Sequence) {
buf.append(indent);
if (obj instanceof BERSequence) {
buf.append("BER Sequence");
} else if (obj instanceof DERSequence) {
buf.append("DER Sequence");
} else {
buf.append("Sequence");
}
buf.append(nl);
ASN1Sequence sequence = (ASN1Sequence) obj;
String elementsIndent = indent + TAB;
for (int i = 0, count = sequence.size(); i < count; ++i) {
_dumpAsString(elementsIndent, verbose, sequence.getObjectAt(i).toASN1Primitive(), buf);
}
} else if (obj instanceof ASN1Set) {
buf.append(indent);
if (obj instanceof BERSet) {
buf.append("BER Set");
} else if (obj instanceof DERSet) {
buf.append("DER Set");
} else {
buf.append("Set");
}
buf.append(nl);
ASN1Set set = (ASN1Set) obj;
String elementsIndent = indent + TAB;
for (int i = 0, count = set.size(); i < count; ++i) {
_dumpAsString(elementsIndent, verbose, set.getObjectAt(i).toASN1Primitive(), buf);
}
} else if (obj instanceof ASN1ApplicationSpecific) {
_dumpAsString(indent, verbose, ((ASN1ApplicationSpecific) obj).getTaggedObject(), buf);
} else if (obj instanceof ASN1TaggedObject) {
buf.append(indent);
if (obj instanceof BERTaggedObject) {
buf.append("BER Tagged ");
} else if (obj instanceof DERTaggedObject) {
buf.append("DER Tagged ");
} else {
buf.append("Tagged ");
}
ASN1TaggedObject o = (ASN1TaggedObject) obj;
buf.append(ASN1Util.getTagText(o));
if (!o.isExplicit()) {
buf.append(" IMPLICIT ");
}
buf.append(nl);
String baseIndent = indent + TAB;
_dumpAsString(baseIndent, verbose, o.getBaseObject().toASN1Primitive(), buf);
} else if (obj instanceof ASN1OctetString) {
ASN1OctetString oct = (ASN1OctetString) obj;
if (obj instanceof BEROctetString) {
buf.append(indent + "BER Constructed Octet String" + "[" + oct.getOctets().length + "] ");
} else {
buf.append(indent + "DER Octet String" + "[" + oct.getOctets().length + "] ");
}
if (verbose) {
buf.append(dumpBinaryDataAsString(indent, oct.getOctets()));
} else {
buf.append(nl);
}
} else if (obj instanceof ASN1ObjectIdentifier) {
buf.append(indent + "ObjectIdentifier(" + ((ASN1ObjectIdentifier) obj).getId() + ")" + nl);
} else if (obj instanceof ASN1RelativeOID) {
buf.append(indent + "RelativeOID(" + ((ASN1RelativeOID) obj).getId() + ")" + nl);
} else if (obj instanceof ASN1Boolean) {
buf.append(indent + "Boolean(" + ((ASN1Boolean) obj).isTrue() + ")" + nl);
} else if (obj instanceof ASN1Integer) {
buf.append(indent + "Integer(" + ((ASN1Integer) obj).getValue() + ")" + nl);
} else if (obj instanceof ASN1BitString) {
ASN1BitString bitString = (ASN1BitString) obj;
byte[] bytes = bitString.getBytes();
int padBits = bitString.getPadBits();
if (bitString instanceof DERBitString) {
buf.append(indent + "DER Bit String" + "[" + bytes.length + ", " + padBits + "] ");
} else if (bitString instanceof DLBitString) {
buf.append(indent + "DL Bit String" + "[" + bytes.length + ", " + padBits + "] ");
} else {
buf.append(indent + "BER Bit String" + "[" + bytes.length + ", " + padBits + "] ");
}
if (verbose) {
buf.append(dumpBinaryDataAsString(indent, bytes));
} else {
buf.append(nl);
}
} else if (obj instanceof ASN1IA5String) {
buf.append(indent + "IA5String(" + ((ASN1IA5String) obj).getString() + ") " + nl);
} else if (obj instanceof ASN1UTF8String) {
buf.append(indent + "UTF8String(" + ((ASN1UTF8String) obj).getString() + ") " + nl);
} else if (obj instanceof ASN1NumericString) {
buf.append(indent + "NumericString(" + ((ASN1NumericString) obj).getString() + ") " + nl);
} else if (obj instanceof ASN1PrintableString) {
buf.append(indent + "PrintableString(" + ((ASN1PrintableString) obj).getString() + ") " + nl);
} else if (obj instanceof ASN1VisibleString) {
buf.append(indent + "VisibleString(" + ((ASN1VisibleString) obj).getString() + ") " + nl);
} else if (obj instanceof ASN1BMPString) {
buf.append(indent + "BMPString(" + ((ASN1BMPString) obj).getString() + ") " + nl);
} else if (obj instanceof ASN1T61String) {
buf.append(indent + "T61String(" + ((ASN1T61String) obj).getString() + ") " + nl);
} else if (obj instanceof ASN1GraphicString) {
buf.append(indent + "GraphicString(" + ((ASN1GraphicString) obj).getString() + ") " + nl);
} else if (obj instanceof ASN1VideotexString) {
buf.append(indent + "VideotexString(" + ((ASN1VideotexString) obj).getString() + ") " + nl);
} else if (obj instanceof ASN1UTCTime) {
buf.append(indent + "UTCTime(" + ((ASN1UTCTime) obj).getTime() + ") " + nl);
} else if (obj instanceof ASN1GeneralizedTime) {
buf.append(indent + "GeneralizedTime(" + ((ASN1GeneralizedTime) obj).getTime() + ") " + nl);
} else if (obj instanceof ASN1Enumerated) {
ASN1Enumerated en = (ASN1Enumerated) obj;
buf.append(indent + "DER Enumerated(" + en.getValue() + ")" + nl);
} else if (obj instanceof ASN1ObjectDescriptor) {
ASN1ObjectDescriptor od = (ASN1ObjectDescriptor) obj;
buf.append(indent + "ObjectDescriptor(" + od.getBaseGraphicString().getString() + ") " + nl);
} else if (obj instanceof ASN1External) {
ASN1External ext = (ASN1External) obj;
buf.append(indent + "External " + nl);
String tab = indent + TAB;
if (ext.getDirectReference() != null) {
buf.append(tab + "Direct Reference: " + ext.getDirectReference().getId() + nl);
}
if (ext.getIndirectReference() != null) {
buf.append(tab + "Indirect Reference: " + ext.getIndirectReference().toString() + nl);
}
if (ext.getDataValueDescriptor() != null) {
_dumpAsString(tab, verbose, ext.getDataValueDescriptor(), buf);
}
buf.append(tab + "Encoding: " + ext.getEncoding() + nl);
_dumpAsString(tab, verbose, ext.getExternalContent(), buf);
} else {
buf.append(indent + obj.toString() + nl);
}
}
use of com.github.zhenwei.core.asn1.ASN1ApplicationSpecific in project LinLong-Java by zhenwei1108.
the class CVCertificateRequest method initCertBody.
private void initCertBody(ASN1ApplicationSpecific request) throws IOException {
if (request.getApplicationTag() == EACTags.CARDHOLDER_CERTIFICATE) {
int valid = 0;
ASN1Sequence seq = ASN1Sequence.getInstance(request.getObject(BERTags.SEQUENCE));
for (Enumeration en = seq.getObjects(); en.hasMoreElements(); ) {
ASN1ApplicationSpecific obj = ASN1ApplicationSpecific.getInstance(en.nextElement());
switch(obj.getApplicationTag()) {
case EACTags.CERTIFICATE_CONTENT_TEMPLATE:
certificateBody = CertificateBody.getInstance(obj);
valid |= bodyValid;
break;
case EACTags.STATIC_INTERNAL_AUTHENTIFICATION_ONE_STEP:
innerSignature = obj.getContents();
valid |= signValid;
break;
default:
throw new IOException("Invalid tag, not an CV Certificate Request element:" + obj.getApplicationTag());
}
}
if ((valid & (bodyValid | signValid)) == 0) {
throw new IOException("Invalid CARDHOLDER_CERTIFICATE in request:" + request.getApplicationTag());
}
} else {
throw new IOException("not a CARDHOLDER_CERTIFICATE in request:" + request.getApplicationTag());
}
}
use of com.github.zhenwei.core.asn1.ASN1ApplicationSpecific in project LinLong-Java by zhenwei1108.
the class CertificateBody method setIso7816CertificateBody.
private void setIso7816CertificateBody(ASN1ApplicationSpecific appSpe) throws IOException {
byte[] content;
if (appSpe.getApplicationTag() == EACTags.CERTIFICATE_CONTENT_TEMPLATE) {
content = appSpe.getContents();
} else {
throw new IOException("Bad tag : not an iso7816 CERTIFICATE_CONTENT_TEMPLATE");
}
ASN1InputStream aIS = new ASN1InputStream(content);
ASN1Primitive obj;
while ((obj = aIS.readObject()) != null) {
ASN1ApplicationSpecific aSpe;
if (obj instanceof ASN1ApplicationSpecific) {
aSpe = (ASN1ApplicationSpecific) obj;
} else {
throw new IOException("Not a valid iso7816 content : not a ASN1ApplicationSpecific Object :" + EACTags.encodeTag(appSpe) + obj.getClass());
}
switch(aSpe.getApplicationTag()) {
case EACTags.INTERCHANGE_PROFILE:
setCertificateProfileIdentifier(aSpe);
break;
case EACTags.ISSUER_IDENTIFICATION_NUMBER:
setCertificationAuthorityReference(aSpe);
break;
case EACTags.CARDHOLDER_PUBLIC_KEY_TEMPLATE:
setPublicKey(PublicKeyDataObject.getInstance(aSpe.getObject(BERTags.SEQUENCE)));
break;
case EACTags.CARDHOLDER_NAME:
setCertificateHolderReference(aSpe);
break;
case EACTags.CERTIFICATE_HOLDER_AUTHORIZATION_TEMPLATE:
setCertificateHolderAuthorization(new CertificateHolderAuthorization(aSpe));
break;
case EACTags.APPLICATION_EFFECTIVE_DATE:
setCertificateEffectiveDate(aSpe);
break;
case EACTags.APPLICATION_EXPIRATION_DATE:
setCertificateExpirationDate(aSpe);
break;
default:
certificateType = 0;
throw new IOException("Not a valid iso7816 ASN1ApplicationSpecific tag " + aSpe.getApplicationTag());
}
}
aIS.close();
}
use of com.github.zhenwei.core.asn1.ASN1ApplicationSpecific in project LinLong-Java by zhenwei1108.
the class CertificateHolderAuthorization method setPrivateData.
private void setPrivateData(ASN1InputStream cha) throws IOException {
ASN1Primitive obj;
obj = cha.readObject();
if (obj instanceof ASN1ObjectIdentifier) {
this.oid = (ASN1ObjectIdentifier) obj;
} else {
throw new IllegalArgumentException("no Oid in CerticateHolderAuthorization");
}
obj = cha.readObject();
if (obj instanceof ASN1ApplicationSpecific) {
this.accessRights = (ASN1ApplicationSpecific) obj;
} else {
throw new IllegalArgumentException("No access rights in CerticateHolderAuthorization");
}
}
use of com.github.zhenwei.core.asn1.ASN1ApplicationSpecific in project LinLong-Java by zhenwei1108.
the class OEROutputStream method write.
public void write(ASN1Encodable encodable, OERDefinition.Element oerElement) throws IOException {
if (encodable == OEROptional.ABSENT) {
return;
} else if (encodable instanceof OEROptional) {
write(((OEROptional) encodable).get(), oerElement);
return;
}
encodable = encodable.toASN1Primitive();
switch(oerElement.baseType) {
case SEQ:
{
ASN1Sequence seq = ASN1Sequence.getInstance(encodable);
// build mask.
int j = 7;
int mask = 0;
if (oerElement.extensionsInDefinition) {
if (oerElement.hasPopulatedExtension()) {
mask |= bits[j];
}
j--;
}
for (int t = 0; t < oerElement.children.size(); t++) {
OERDefinition.Element childOERDescription = oerElement.children.get(t);
if (j < 0) {
out.write(mask);
j = 7;
mask = 0;
}
ASN1Encodable asn1EncodableChild = seq.getObjectAt(t);
if (childOERDescription.explicit && asn1EncodableChild instanceof OEROptional) {
// TODO call stack like definition error.
throw new IllegalStateException("absent sequence element that is required by oer definition");
}
if (!childOERDescription.explicit) {
ASN1Encodable obj = seq.getObjectAt(t);
if (childOERDescription.getDefaultValue() != null) {
if (obj instanceof OEROptional) {
if (((OEROptional) obj).isDefined()) {
if (!((OEROptional) obj).get().equals(childOERDescription.defaultValue)) {
mask |= bits[j];
}
}
} else {
if (!childOERDescription.getDefaultValue().equals(obj)) {
mask |= bits[j];
}
}
} else {
if (asn1EncodableChild != OEROptional.ABSENT) {
mask |= bits[j];
}
}
j--;
}
}
if (j != 7) {
out.write(mask);
}
//
for (int t = 0; t < oerElement.children.size(); t++) {
ASN1Encodable child = seq.getObjectAt(t);
OERDefinition.Element childOERElement = oerElement.children.get(t);
if (childOERElement.getDefaultValue() != null) {
if (childOERElement.getDefaultValue().equals(child)) {
continue;
}
}
write(child, childOERElement);
}
out.flush();
debugPrint(oerElement.appendLabel(""));
}
break;
case SEQ_OF:
//
// Assume this comes in as a sequence.
//
Enumeration e;
if (encodable instanceof ASN1Set) {
e = ((ASN1Set) encodable).getObjects();
encodeQuantity(((ASN1Set) encodable).size());
} else if (encodable instanceof ASN1Sequence) {
e = ((ASN1Sequence) encodable).getObjects();
encodeQuantity(((ASN1Sequence) encodable).size());
} else {
throw new IllegalStateException("encodable at for SEQ_OF is not a container");
}
while (e.hasMoreElements()) {
Object o = e.nextElement();
write((ASN1Encodable) o, oerElement.getFirstChid());
}
out.flush();
debugPrint(oerElement.appendLabel(""));
break;
case CHOICE:
{
ASN1Primitive item = encodable.toASN1Primitive();
BitBuilder bb = new BitBuilder();
int tag;
if (item instanceof ASN1ApplicationSpecific) {
//
// Application specific tag prefix.
//
tag = ((ASN1ApplicationSpecific) item).getApplicationTag();
bb.writeBit(0).writeBit(1);
item = ((ASN1ApplicationSpecific) item).getEnclosedObject();
} else if (item instanceof ASN1TaggedObject) {
ASN1TaggedObject taggedObject = (ASN1TaggedObject) item;
//
// Tag prefix.
//
int tagClass = taggedObject.getTagClass();
bb.writeBit(tagClass & BERTags.CONTEXT_SPECIFIC).writeBit(tagClass & BERTags.APPLICATION);
tag = taggedObject.getTagNo();
item = taggedObject.getBaseObject().toASN1Primitive();
} else {
throw new IllegalStateException("only support tagged objects");
}
// Small tag value encode in remaining bits
if (tag <= 63) {
bb.writeBits(tag, 6);
} else {
// Large tag value variant.
bb.writeBits(0xFF, 6);
// Encode as 7bit bytes where MSB indicated continuing byte.
bb.write7BitBytes(tag);
}
if (debugOutput != null) {
if (item instanceof ASN1ApplicationSpecific) {
debugPrint(oerElement.appendLabel("AS"));
} else if (item instanceof ASN1TaggedObject) {
debugPrint(oerElement.appendLabel("CS"));
}
}
// Save the header.
bb.writeAndClear(out);
write(item, oerElement.children.get(tag));
out.flush();
break;
}
case ENUM:
{
BigInteger ordinal;
if (encodable instanceof ASN1Integer) {
ordinal = ASN1Integer.getInstance(encodable).getValue();
} else {
ordinal = ASN1Enumerated.getInstance(encodable).getValue();
}
for (Iterator it = oerElement.children.iterator(); it.hasNext(); ) {
OERDefinition.Element child = (OERDefinition.Element) it.next();
//
if (child.enumValue.equals(ordinal)) {
if (ordinal.compareTo(BigInteger.valueOf(127)) > 0) {
// Note 2 Section 11.4 of T-REC-X.696-201508-I!!PDF-E.pdf
byte[] val = ordinal.toByteArray();
int l = 0x80 | (val.length & 0xFF);
out.write(l);
out.write(val);
} else {
out.write(ordinal.intValue() & 0x7F);
}
out.flush();
debugPrint(oerElement.appendLabel(oerElement.rangeExpression()));
return;
}
}
throw new IllegalArgumentException("enum value " + ordinal + " " + Hex.toHexString(ordinal.toByteArray()) + " no in defined child list");
}
case INT:
{
ASN1Integer integer = ASN1Integer.getInstance(encodable);
// >0 = positive and <0 = negative
int intBytesForRange = oerElement.intBytesForRange();
if (intBytesForRange > 0) {
//
// For unsigned fixed length 1,2,4,8 byte integers.
//
byte[] encoded = BigIntegers.asUnsignedByteArray(intBytesForRange, integer.getValue());
switch(intBytesForRange) {
case 1:
case 2:
case 4:
case 8:
out.write(encoded);
break;
default:
throw new IllegalStateException("unknown uint length " + intBytesForRange);
}
} else if (intBytesForRange < 0) {
//
// For twos compliment numbers of 1,2,4,8 bytes in encoded length.
//
byte[] encoded;
BigInteger number = integer.getValue();
switch(intBytesForRange) {
case -1:
encoded = new byte[] { BigIntegers.byteValueExact(number) };
break;
case -2:
encoded = Pack.shortToBigEndian(BigIntegers.shortValueExact(number));
break;
case -4:
encoded = Pack.intToBigEndian(BigIntegers.intValueExact(number));
break;
case -8:
encoded = Pack.longToBigEndian(BigIntegers.longValueExact(number));
break;
default:
throw new IllegalStateException("unknown twos compliment length");
}
out.write(encoded);
} else {
// Unbounded at one or both ends and needs length encoding.
byte[] encoded;
if (oerElement.isLowerRangeZero()) {
// Since we have already captured the fixed with unsigned ints.
// Everything is assumed unbounded we need to encode a length and write the value.
encoded = BigIntegers.asUnsignedByteArray(integer.getValue());
} else {
// Twos complement
encoded = integer.getValue().toByteArray();
}
// Deals with long and short forms.
encodeLength(encoded.length);
out.write(encoded);
}
debugPrint(oerElement.appendLabel(oerElement.rangeExpression()));
out.flush();
}
break;
case OCTET_STRING:
{
ASN1OctetString octets = ASN1OctetString.getInstance(encodable);
byte[] bytes = octets.getOctets();
if (oerElement.isFixedLength()) {
out.write(bytes);
} else {
encodeLength(bytes.length);
out.write(bytes);
}
debugPrint(oerElement.appendLabel(oerElement.rangeExpression()));
out.flush();
break;
}
case UTF8_STRING:
{
ASN1UTF8String utf8 = ASN1UTF8String.getInstance(encodable);
byte[] encoded = Strings.toUTF8ByteArray(utf8.getString());
encodeLength(encoded.length);
out.write(encoded);
debugPrint(oerElement.appendLabel(""));
out.flush();
break;
}
case BIT_STRING:
{
DERBitString bitString = DERBitString.getInstance(encodable);
byte[] bytes = bitString.getBytes();
if (oerElement.isFixedLength()) {
out.write(bytes);
debugPrint(oerElement.appendLabel(oerElement.rangeExpression()));
} else {
int padBits = bitString.getPadBits();
// 13.3.1
encodeLength(bytes.length + 1);
// 13.3.2
out.write(padBits);
// 13.3.3
out.write(bytes);
debugPrint(oerElement.appendLabel(oerElement.rangeExpression()));
}
out.flush();
}
break;
case NULL:
// Does not encode in OER.
break;
case EXTENSION:
{
ASN1OctetString octets = ASN1OctetString.getInstance(encodable);
byte[] bytes = octets.getOctets();
if (oerElement.isFixedLength()) {
out.write(bytes);
} else {
encodeLength(bytes.length);
out.write(bytes);
}
debugPrint(oerElement.appendLabel(oerElement.rangeExpression()));
out.flush();
break;
}
case ENUM_ITEM:
// Used to define options does not encode.
break;
case BOOLEAN:
debugPrint(oerElement.label);
ASN1Boolean asn1Boolean = ASN1Boolean.getInstance(encodable);
if (asn1Boolean.isTrue()) {
out.write(255);
} else {
out.write(0);
}
out.flush();
}
}
Aggregations