|
| 1 | +package org.bouncycastle.asn1; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | + |
| 5 | +/** |
| 6 | + * Base class for an ASN.1 ApplicationSpecific object |
| 7 | + * |
| 8 | + * @deprecated Will be removed. Change application code to handle as {@link ASN1TaggedObject} only, testing |
| 9 | + * for the expected {@link ASN1TaggedObject#getTagClass() tag class} of |
| 10 | + * {@link BERTags#APPLICATION} in relevant objects before using. If using a |
| 11 | + * {@link ASN1StreamParser stream parser}, handle application-tagged objects using |
| 12 | + * {@link ASN1TaggedObjectParser} in the usual way, again testing for a |
| 13 | + * {@link ASN1TaggedObjectParser#getTagClass() tag class} of {@link BERTags#APPLICATION}. |
| 14 | + */ |
| 15 | +public abstract class ASN1ApplicationSpecific |
| 16 | + extends ASN1TaggedObject |
| 17 | + implements ASN1ApplicationSpecificParser |
| 18 | +{ |
| 19 | + /** |
| 20 | + * Return an ASN1ApplicationSpecific from the passed in object, which may be a byte array, or null. |
| 21 | + * |
| 22 | + * @param obj the object to be converted. |
| 23 | + * @return obj's representation as an ASN1ApplicationSpecific object. |
| 24 | + */ |
| 25 | + public static ASN1TaggedObject getInstance(Object obj) |
| 26 | + { |
| 27 | + if (obj == null || obj instanceof ASN1ApplicationSpecific) |
| 28 | + { |
| 29 | + return (ASN1ApplicationSpecific)obj; |
| 30 | + } |
| 31 | + else if (obj instanceof byte[]) |
| 32 | + { |
| 33 | + try |
| 34 | + { |
| 35 | + return getInstance(ASN1Primitive.fromByteArray((byte[])obj)); |
| 36 | + } |
| 37 | + catch (IOException e) |
| 38 | + { |
| 39 | + throw new IllegalArgumentException("Failed to construct object from byte[]: " + e.getMessage()); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + throw new IllegalArgumentException("unknown object in getInstance: " + obj.getClass().getName()); |
| 44 | + } |
| 45 | + |
| 46 | + final ASN1TaggedObject taggedObject; |
| 47 | + |
| 48 | + ASN1ApplicationSpecific(ASN1TaggedObject taggedObject) |
| 49 | + { |
| 50 | + super(taggedObject.explicitness, checkTagClass(taggedObject.tagClass), taggedObject.tagNo, taggedObject.obj); |
| 51 | + |
| 52 | + this.taggedObject = taggedObject; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Return the tag number associated with this object, |
| 57 | + * |
| 58 | + * @return the application tag number. |
| 59 | + */ |
| 60 | + public int getApplicationTag() |
| 61 | + { |
| 62 | + return taggedObject.getTagNo(); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Return the contents of this object as a byte[] |
| 67 | + * |
| 68 | + * @return the encoded contents of the object. |
| 69 | + */ |
| 70 | + public byte[] getContents() |
| 71 | + { |
| 72 | + return taggedObject.getContents(); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Return the enclosed object assuming explicit tagging. |
| 77 | + * |
| 78 | + * @return the resulting object |
| 79 | + * @throws IOException if reconstruction fails. |
| 80 | + */ |
| 81 | + public ASN1Primitive getEnclosedObject() throws IOException |
| 82 | + { |
| 83 | + return taggedObject.getBaseObject().toASN1Primitive(); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Return the enclosed object assuming implicit tagging. |
| 88 | + * |
| 89 | + * @param tagNo the type tag that should be applied to the object's contents. |
| 90 | + * @return the resulting object |
| 91 | + * @throws IOException if reconstruction fails. |
| 92 | + */ |
| 93 | + public ASN1Primitive getObject(int tagNo) throws IOException |
| 94 | + { |
| 95 | + return taggedObject.getBaseUniversal(false, tagNo); |
| 96 | + } |
| 97 | + |
| 98 | + public ASN1Encodable getObjectParser(int tag, boolean isExplicit) throws IOException |
| 99 | + { |
| 100 | + throw new ASN1Exception("this method only valid for CONTEXT_SPECIFIC tags"); |
| 101 | + } |
| 102 | + |
| 103 | + public ASN1Encodable parseBaseUniversal(boolean declaredExplicit, int baseTagNo) throws IOException |
| 104 | + { |
| 105 | + return taggedObject.parseBaseUniversal(declaredExplicit, baseTagNo); |
| 106 | + } |
| 107 | + |
| 108 | + public ASN1Encodable parseExplicitBaseObject() throws IOException |
| 109 | + { |
| 110 | + return taggedObject.parseExplicitBaseObject(); |
| 111 | + } |
| 112 | + |
| 113 | + public ASN1TaggedObjectParser parseExplicitBaseTagged() throws IOException |
| 114 | + { |
| 115 | + return taggedObject.parseExplicitBaseTagged(); |
| 116 | + } |
| 117 | + |
| 118 | + public ASN1TaggedObjectParser parseImplicitBaseTagged(int baseTagClass, int baseTagNo) throws IOException |
| 119 | + { |
| 120 | + return taggedObject.parseImplicitBaseTagged(baseTagClass, baseTagNo); |
| 121 | + } |
| 122 | + |
| 123 | + public boolean hasApplicationTag(int tagNo) |
| 124 | + { |
| 125 | + return this.tagNo == tagNo; |
| 126 | + } |
| 127 | + |
| 128 | + public boolean hasContextTag(int tagNo) |
| 129 | + { |
| 130 | + return false; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * ASN1ApplicationSpecific uses an internal ASN1TaggedObject for the |
| 135 | + * implementation, and will soon be deprecated in favour of using |
| 136 | + * ASN1TaggedObject with a tag class of {@link BERTags#APPLICATION}. This method |
| 137 | + * lets you get the internal ASN1TaggedObject so that client code can begin the |
| 138 | + * migration. |
| 139 | + */ |
| 140 | + public ASN1TaggedObject getTaggedObject() |
| 141 | + { |
| 142 | + return taggedObject; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Return true if the object is marked as constructed, false otherwise. |
| 147 | + * |
| 148 | + * @return true if constructed, otherwise false. |
| 149 | + */ |
| 150 | + public boolean isConstructed() |
| 151 | + { |
| 152 | + return taggedObject.isConstructed(); |
| 153 | + } |
| 154 | + |
| 155 | + public ASN1Encodable readObject() throws IOException |
| 156 | + { |
| 157 | + // NOTE: No way to say you're looking for an implicitly-tagged object via ASN1ApplicationSpecificParser |
| 158 | + return parseExplicitBaseObject(); |
| 159 | + } |
| 160 | + |
| 161 | + boolean encodeConstructed() |
| 162 | + { |
| 163 | + return taggedObject.encodeConstructed(); |
| 164 | + } |
| 165 | + |
| 166 | + int encodedLength(boolean withTag) throws IOException |
| 167 | + { |
| 168 | + return taggedObject.encodedLength(withTag); |
| 169 | + } |
| 170 | + |
| 171 | + void encode(ASN1OutputStream out, boolean withTag) throws IOException |
| 172 | + { |
| 173 | + taggedObject.encode(out, withTag); |
| 174 | + } |
| 175 | + |
| 176 | + String getASN1Encoding() |
| 177 | + { |
| 178 | + return taggedObject.getASN1Encoding(); |
| 179 | + } |
| 180 | + |
| 181 | + ASN1Sequence rebuildConstructed(ASN1Primitive primitive) |
| 182 | + { |
| 183 | + return taggedObject.rebuildConstructed(primitive); |
| 184 | + } |
| 185 | + |
| 186 | + ASN1TaggedObject replaceTag(int tagClass, int tagNo) |
| 187 | + { |
| 188 | + return taggedObject.replaceTag(tagClass, tagNo); |
| 189 | + } |
| 190 | + |
| 191 | + ASN1Primitive toDERObject() |
| 192 | + { |
| 193 | + return new DERApplicationSpecific((ASN1TaggedObject)taggedObject.toDERObject()); |
| 194 | + } |
| 195 | + |
| 196 | + ASN1Primitive toDLObject() |
| 197 | + { |
| 198 | + return new DLApplicationSpecific((ASN1TaggedObject)taggedObject.toDLObject()); |
| 199 | + } |
| 200 | + |
| 201 | + private static int checkTagClass(int tagClass) |
| 202 | + { |
| 203 | + if (BERTags.APPLICATION != tagClass) |
| 204 | + { |
| 205 | + throw new IllegalArgumentException(); |
| 206 | + } |
| 207 | + return tagClass; |
| 208 | + } |
| 209 | +} |
0 commit comments