Java Code Examples for org.bouncycastle.asn1.ASN1TaggedObject#getTagNo()

The following examples show how to use org.bouncycastle.asn1.ASN1TaggedObject#getTagNo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CertifiedAttributesV2.java    From dss with GNU Lesser General Public License v2.1 6 votes vote down vote up
@SuppressWarnings("rawtypes")
private CertifiedAttributesV2(ASN1Sequence seq) {
	int index = 0;
	values = new Object[seq.size()];

	for (Enumeration e = seq.getObjects(); e.hasMoreElements();) {
		ASN1TaggedObject taggedObject = ASN1TaggedObject.getInstance(e.nextElement());

		if (taggedObject.getTagNo() == 0) {
			values[index] = AttributeCertificate.getInstance(ASN1Sequence.getInstance(taggedObject, true));
		} else if (taggedObject.getTagNo() == 1) {
			LOG.info("OtherAttributeCertificate detected");
		} else {
			throw new IllegalArgumentException("illegal tag: " + taggedObject.getTagNo());
		}
		index++;
	}
}
 
Example 2
Source File: ProxyMessage.java    From xipki with Apache License 2.0 6 votes vote down vote up
private NewObjectControl(ASN1Sequence seq) throws BadAsn1ObjectException {
  final int size = seq.size();
  Args.min(size, "seq.size", 1);
  String label = DERUTF8String.getInstance(seq.getObjectAt(0)).getString();
  byte[] id = null;

  for (int i = 1; i < size; i++) {
    ASN1Encodable obj = seq.getObjectAt(i);
    if (obj instanceof ASN1TaggedObject) {
      continue;
    }

    ASN1TaggedObject tagObj = (ASN1TaggedObject) obj;
    int tagNo = tagObj.getTagNo();
    if (tagNo == 0) {
      id = DEROctetString.getInstance(tagObj.getObject()).getOctets();
    }
  }

  this.control = new P11NewKeyControl(id, label);
}
 
Example 3
Source File: ProxyMessage.java    From xipki with Apache License 2.0 6 votes vote down vote up
private IdentityId(ASN1Sequence seq) throws BadAsn1ObjectException {
  requireRange(seq, 2, 4);
  P11SlotIdentifier slotId =
      SlotIdentifier.getInstance(seq.getObjectAt(0)).getValue();
  P11ObjectIdentifier keyId =
      ObjectIdentifier.getInstance(seq.getObjectAt(1)).getValue();
  String publicKeyLabel = null;
  String certLabel = null;

  final int n = seq.size();
  for (int i = 2; i < n; i++) {
    ASN1Encodable asn1 = seq.getObjectAt(i);
    if (asn1 instanceof ASN1TaggedObject) {
      ASN1TaggedObject tagAsn1 = (ASN1TaggedObject) asn1;
      int tag = tagAsn1.getTagNo();
      if (tag == 1) {
        publicKeyLabel = DERUTF8String.getInstance(tagAsn1.getObject()).getString();
      } else if (tag == 2) {
        certLabel = DERUTF8String.getInstance(tagAsn1.getObject()).getString();
      }
    }

  }

  this.value = new P11IdentityId(slotId, keyId, publicKeyLabel, certLabel);
}
 
Example 4
Source File: SignerAttributeV2.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
@SuppressWarnings("rawtypes")
private SignerAttributeV2(ASN1Sequence seq) {
	int index = 0;
	values = new Object[seq.size()];

	for (Enumeration e = seq.getObjects(); e.hasMoreElements();) {
		ASN1TaggedObject taggedObject = ASN1TaggedObject.getInstance(e.nextElement());

		if (taggedObject.getTagNo() == 0) {
			ASN1Sequence attrs = ASN1Sequence.getInstance(taggedObject, true);
			Attribute[] attributes = new Attribute[attrs.size()];

			for (int i = 0; i != attributes.length; i++) {
				attributes[i] = Attribute.getInstance(attrs.getObjectAt(i));
			}
			values[index] = attributes;
		} else if (taggedObject.getTagNo() == 1) {
			values[index] = CertifiedAttributesV2.getInstance(ASN1Sequence.getInstance(taggedObject, true));
		} else if (taggedObject.getTagNo() == 2) {
		    	LOG.info("SAML assertion detected");
			values[index] = SignedAssertions.getInstance(ASN1Sequence.getInstance(taggedObject, true));
		} else {
			throw new IllegalArgumentException("illegal tag: " + taggedObject.getTagNo());
		}
		index++;
	}
}
 
Example 5
Source File: RevocationInfoArchival.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
private RevocationInfoArchival(ASN1Sequence seq)
{
    if (seq.size() > 3)
    {
        throw new IllegalArgumentException("Bad sequence size: "
            + seq.size());
    }
    Enumeration e = seq.getObjects();
    while (e.hasMoreElements())
    {
        ASN1TaggedObject o = (ASN1TaggedObject)e.nextElement();
        switch (o.getTagNo())
        {
            case 0:
                ASN1Sequence crlValsSeq = (ASN1Sequence)o.getObject();
                Enumeration crlValsEnum = crlValsSeq.getObjects();
                while (crlValsEnum.hasMoreElements())
                {
                    CertificateList.getInstance(crlValsEnum.nextElement());
                }
                this.crlVals = crlValsSeq;
                break;
            case 1:
                ASN1Sequence ocspValsSeq = (ASN1Sequence)o.getObject();
                Enumeration ocspValsEnum = ocspValsSeq.getObjects();
                while (ocspValsEnum.hasMoreElements())
                {
                    OCSPResponse.getInstance(ocspValsEnum.nextElement());
                }
                this.ocspVals = ocspValsSeq;
                break;
            case 2:
                this.otherRevVals = OtherRevVals.getInstance(o.getObject());
                break;
            default:
                throw new IllegalArgumentException("invalid tag: "
                    + o.getTagNo());
        }
    }
}
 
Example 6
Source File: ExtensionExistence.java    From xipki with Apache License 2.0 5 votes vote down vote up
private ExtensionExistence(ASN1Sequence seq) {
  int size = seq.size();
  if (size > 2) {
    throw new IllegalArgumentException("wrong number of elements in sequence");
  }

  for (int i = 0; i < size; i++) {
    ASN1TaggedObject tagObject = ASN1TaggedObject.getInstance(seq.getObjectAt(i));
    int tag = tagObject.getTagNo();
    Args.range(tag, "tag", 0, 1);
    ASN1Sequence subSeq = ASN1Sequence.getInstance(tagObject.getObject());
    List<ASN1ObjectIdentifier> oids = new LinkedList<>();
    int subSize = subSeq.size();
    for (int j = 0; j < subSize; j++) {
      oids.add(ASN1ObjectIdentifier.getInstance(subSeq.getObjectAt(j)));
    }

    if (tag == 0) {
      needExtensions = Collections.unmodifiableList(oids);
    } else {
      wantExtensions = Collections.unmodifiableList(oids);
    }
  }

  if (needExtensions == null) {
    needExtensions = Collections.unmodifiableList(Collections.emptyList());
  }

  if (wantExtensions == null) {
    wantExtensions = Collections.unmodifiableList(Collections.emptyList());
  }
}
 
Example 7
Source File: ProxyMessage.java    From xipki with Apache License 2.0 5 votes vote down vote up
private NewKeyControl(ASN1Sequence seq) throws BadAsn1ObjectException {
  final int size = seq.size();
  Args.min(size, "seq.size", 1);
  String label = DERUTF8String.getInstance(seq.getObjectAt(0)).getString();

  Set<P11KeyUsage> usages = new HashSet<>();
  byte[] id = null;
  Boolean extractable = null;

  for (int i = 1; i < size; i++) {
    ASN1Encodable obj = seq.getObjectAt(i);
    if (!(obj instanceof ASN1TaggedObject)) {
      continue;
    }

    ASN1TaggedObject tagObj = (ASN1TaggedObject) obj;
    int tagNo = tagObj.getTagNo();
    if (tagNo == 0) {
      id = DEROctetString.getInstance(tagObj.getObject()).getOctets();
    } else if (tagNo == 1) {
      ASN1Sequence usageSeq = ASN1Sequence.getInstance(tagObj.getObject());
      final int usageSize = usageSeq.size();
      for (int j = 0; j < usageSize; j++) {
        ASN1Enumerated usageEnum = ASN1Enumerated.getInstance(usageSeq.getObjectAt(j));
        int enumValue = usageEnum.getValue().intValue();
        P11KeyUsage usage = valueToUsageMap.get(enumValue);
        if (usage == null) {
          throw new IllegalArgumentException("invalid usage " + enumValue);
        }
        usages.add(usage);
      }
    } else if (tagNo == 2) {
      extractable = ASN1Boolean.getInstance(tagObj.getObject()).isTrue();
    }
  }

  this.control = new P11NewKeyControl(id, label);
  this.control.setUsages(usages);
  this.control.setExtractable(extractable);
}
 
Example 8
Source File: X509Ext.java    From portecle with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Gets a HTML escaped string representation of the given object.
 *
 * @param obj Object
 * @return String representation of <code>obj</code>
 */
private static String stringify(Object obj)
{
	if (obj instanceof ASN1String)
	{
		return escapeHtml(((ASN1String) obj).getString());
	}
	// TODO: why not ASN1Integer as number?
	else if (obj instanceof ASN1Integer || obj instanceof byte[])
	{
		return convertToHexString(obj);
	}
	else if (obj instanceof ASN1TaggedObject)
	{
		ASN1TaggedObject tagObj = (ASN1TaggedObject) obj;
		// Note: "[", _not_ '[' ...
		return "[" + tagObj.getTagNo() + "] " + stringify(tagObj.getObject());
	}
	else if (obj instanceof ASN1Sequence)
	{
		ASN1Sequence aObj = (ASN1Sequence) obj;
		StringBuilder tmp = new StringBuilder("[");
		for (int i = 0, len = aObj.size(); i < len; i++)
		{
			tmp.append(stringify(aObj.getObjectAt(i)));
			if (i != len - 1)
			{
				tmp.append(", ");
			}
		}
		return tmp.append("]").toString();
	}
	else
	{
		String hex = null;
		try
		{
			Method method = obj.getClass().getMethod("getOctets", (Class[]) null);
			hex = convertToHexString(method.invoke(obj, (Object[]) null));
		}
		catch (Exception e)
		{
			// Ignore
		}
		if (hex == null && obj != null)
		{
			hex = escapeHtml(obj.toString());
		}
		return hex;
	}
}
 
Example 9
Source File: KerberosApRequest.java    From jcifs with GNU Lesser General Public License v2.1 4 votes vote down vote up
public KerberosApRequest ( byte[] token, KerberosKey[] keys ) throws PACDecodingException {
    if ( token.length <= 0 )
        throw new PACDecodingException("Empty kerberos ApReq");

    DLSequence sequence;
    try {
        try ( ASN1InputStream stream = new ASN1InputStream(new ByteArrayInputStream(token)) ) {
            sequence = ASN1Util.as(DLSequence.class, stream);
        }
    }
    catch ( IOException e ) {
        throw new PACDecodingException("Malformed Kerberos Ticket", e);
    }

    Enumeration<?> fields = sequence.getObjects();
    while ( fields.hasMoreElements() ) {
        ASN1TaggedObject tagged = ASN1Util.as(ASN1TaggedObject.class, fields.nextElement());
        switch ( tagged.getTagNo() ) {
        case 0:
            ASN1Integer pvno = ASN1Util.as(ASN1Integer.class, tagged);
            if ( !pvno.getValue().equals(new BigInteger(KerberosConstants.KERBEROS_VERSION)) ) {
                throw new PACDecodingException("Invalid kerberos version");
            }
            break;
        case 1:
            ASN1Integer msgType = ASN1Util.as(ASN1Integer.class, tagged);
            if ( !msgType.getValue().equals(new BigInteger(KerberosConstants.KERBEROS_AP_REQ)) )
                throw new PACDecodingException("Invalid kerberos request");
            break;
        case 2:
            DERBitString bitString = ASN1Util.as(DERBitString.class, tagged);
            this.apOptions = bitString.getBytes()[ 0 ];
            break;
        case 3:
            DERApplicationSpecific derTicket = ASN1Util.as(DERApplicationSpecific.class, tagged);
            if ( !derTicket.isConstructed() )
                throw new PACDecodingException("Malformed Kerberos Ticket");
            this.ticket = new KerberosTicket(derTicket.getContents(), this.apOptions, keys);
            break;
        case 4:
            // Let's ignore this for now
            break;
        default:
            throw new PACDecodingException("Invalid field in kerberos ticket");
        }
    }
}
 
Example 10
Source File: KerberosApRequest.java    From jcifs-ng with GNU Lesser General Public License v2.1 4 votes vote down vote up
public KerberosApRequest ( byte[] token, KerberosKey[] keys ) throws PACDecodingException {
    if ( token.length <= 0 )
        throw new PACDecodingException("Empty kerberos ApReq");

    DLSequence sequence;
    try {
        try ( ASN1InputStream stream = new ASN1InputStream(new ByteArrayInputStream(token)) ) {
            sequence = ASN1Util.as(DLSequence.class, stream);
        }
    }
    catch ( IOException e ) {
        throw new PACDecodingException("Malformed Kerberos Ticket", e);
    }

    Enumeration<?> fields = sequence.getObjects();
    while ( fields.hasMoreElements() ) {
        ASN1TaggedObject tagged = ASN1Util.as(ASN1TaggedObject.class, fields.nextElement());
        switch ( tagged.getTagNo() ) {
        case 0:
            ASN1Integer pvno = ASN1Util.as(ASN1Integer.class, tagged);
            if ( !pvno.getValue().equals(new BigInteger(KerberosConstants.KERBEROS_VERSION)) ) {
                throw new PACDecodingException("Invalid kerberos version");
            }
            break;
        case 1:
            ASN1Integer msgType = ASN1Util.as(ASN1Integer.class, tagged);
            if ( !msgType.getValue().equals(new BigInteger(KerberosConstants.KERBEROS_AP_REQ)) )
                throw new PACDecodingException("Invalid kerberos request");
            break;
        case 2:
            DERBitString bitString = ASN1Util.as(DERBitString.class, tagged);
            this.apOptions = bitString.getBytes()[ 0 ];
            break;
        case 3:
            DERApplicationSpecific derTicket = ASN1Util.as(DERApplicationSpecific.class, tagged);
            if ( !derTicket.isConstructed() )
                throw new PACDecodingException("Malformed Kerberos Ticket");
            this.ticket = new KerberosTicket(derTicket.getContents(), this.apOptions, keys);
            break;
        case 4:
            // Let's ignore this for now
            break;
        default:
            throw new PACDecodingException("Invalid field in kerberos ticket");
        }
    }
}
 
Example 11
Source File: ProxyMessage.java    From xipki with Apache License 2.0 4 votes vote down vote up
private P11Params(ASN1TaggedObject taggedObject) throws BadAsn1ObjectException {
  this.tagNo = taggedObject.getTagNo();
  this.p11Params = taggedObject.getObject();
}