Java Code Examples for org.bouncycastle.asn1.ASN1OctetString#getOctets()

The following examples show how to use org.bouncycastle.asn1.ASN1OctetString#getOctets() . 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: ProxyP11Identity.java    From xipki with Apache License 2.0 6 votes vote down vote up
@Override
protected byte[] digestSecretKey0(long mechanism) throws P11TokenException {
  ProxyMessage.DigestSecretKeyTemplate template =
      new ProxyMessage.DigestSecretKeyTemplate(
          ((ProxyP11Slot) slot).getAsn1SlotId(), asn1KeyId, mechanism);
  byte[] result = ((ProxyP11Slot) slot).getModule().send(
      P11ProxyConstants.ACTION_DIGEST_SECRETKEY, template);

  ASN1OctetString octetString;
  try {
    octetString = DEROctetString.getInstance(result);
  } catch (IllegalArgumentException ex) {
    throw new P11TokenException("the returned result is not OCTET STRING");
  }

  return (octetString == null) ? null : octetString.getOctets();
}
 
Example 2
Source File: Asn1Dump.java    From keystore-explorer with GNU General Public License v3.0 6 votes vote down vote up
private String dumpOctetString(ASN1OctetString asn1OctetString) throws IOException {
	StringBuilder sb = new StringBuilder();
	byte[] bytes = asn1OctetString.getOctets();

	sb.append(indentSequence.toString(indentLevel));
	sb.append("OCTET STRING");
	try {
		String encapsulated = dump(bytes);
		sb.append(", encapsulates:");
		sb.append(NEWLINE);
		sb.append(encapsulated);
	} catch (Exception e) {
		sb.append("=");
		if (bytes.length < 8) {
			sb.append(HexUtil.getHexString(bytes));
		} else {
			sb.append(NEWLINE);
			sb.append(dumpHexClear(bytes));
		}
	}
	sb.append(NEWLINE);

	return sb.toString();
}
 
Example 3
Source File: Asn1Utils.java    From Auditor with MIT License 5 votes vote down vote up
public static String getStringFromAsn1OctetStreamAssumingUTF8(ASN1Encodable encodable)
        throws CertificateParsingException, UnsupportedEncodingException {
    if (!(encodable instanceof ASN1OctetString)) {
        throw new CertificateParsingException(
                "Expected octet string, found " + encodable.getClass().getName());
    }

    ASN1OctetString octetString = (ASN1OctetString) encodable;
    return new String(octetString.getOctets(), StandardCharsets.UTF_8);
}
 
Example 4
Source File: ProxyP11Identity.java    From xipki with Apache License 2.0 5 votes vote down vote up
@Override
protected byte[] sign0(long mechanism, P11Params parameters, byte[] content)
    throws P11TokenException {
  ProxyMessage.P11Params p11Param = null;
  if (parameters != null) {
    if (parameters instanceof P11RSAPkcsPssParams) {
      p11Param = new ProxyMessage.P11Params(ProxyMessage.P11Params.TAG_RSA_PKCS_PSS,
          new ProxyMessage.RSAPkcsPssParams((P11RSAPkcsPssParams) parameters));
    } else if (parameters instanceof P11ByteArrayParams) {
      byte[] bytes = ((P11ByteArrayParams) parameters).getBytes();
      p11Param = new ProxyMessage.P11Params(ProxyMessage.P11Params.TAG_OPAQUE,
          new DEROctetString(bytes));
    } else if (parameters instanceof P11IVParams) {
      p11Param = new ProxyMessage.P11Params(ProxyMessage.P11Params.TAG_IV,
          new DEROctetString(((P11IVParams) parameters).getIV()));
    } else {
      throw new IllegalArgumentException("unkown parameter 'parameters'");
    }
  }

  ProxyMessage.SignTemplate signTemplate = new ProxyMessage.SignTemplate(
      ((ProxyP11Slot) slot).getAsn1SlotId(), asn1KeyId, mechanism, p11Param, content);
  byte[] result = ((ProxyP11Slot) slot).getModule().send(P11ProxyConstants.ACTION_SIGN,
      signTemplate);

  ASN1OctetString octetString;
  try {
    octetString = DEROctetString.getInstance(result);
  } catch (IllegalArgumentException ex) {
    throw new P11TokenException("the returned result is not OCTET STRING");
  }

  return (octetString == null) ? null : octetString.getOctets();
}
 
Example 5
Source File: Asn1Utils.java    From android-testdpc with Apache License 2.0 5 votes vote down vote up
public static String getStringFromAsn1OctetStreamAssumingUTF8(ASN1Encodable encodable)
        throws CertificateParsingException, UnsupportedEncodingException {
    if (!(encodable instanceof ASN1OctetString)) {
        throw new CertificateParsingException(
                "Expected octet string, found " + encodable.getClass().getName());
    }

    ASN1OctetString octetString = (ASN1OctetString) encodable;
    return new String(octetString.getOctets(), "UTF-8");
}
 
Example 6
Source File: Asn1Utils.java    From android-testdpc with Apache License 2.0 5 votes vote down vote up
public static byte[] getByteArrayFromAsn1(ASN1Encodable asn1Encodable)
        throws CertificateParsingException {
    if (asn1Encodable == null || !(asn1Encodable instanceof DEROctetString)) {
        throw new CertificateParsingException("Expected DEROctetString");
    }
    ASN1OctetString derOctectString = (ASN1OctetString) asn1Encodable;
    return derOctectString.getOctets();
}
 
Example 7
Source File: CAdESSignature.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public byte[] getMessageDigestValue() {
	final Attribute messageDigestAttribute = getSignedAttribute(PKCSObjectIdentifiers.pkcs_9_at_messageDigest);
	if (messageDigestAttribute == null) {
		return null;
	}
	final ASN1OctetString asn1OctetString = (ASN1OctetString) messageDigestAttribute.getAttrValues().getObjectAt(0);
	return asn1OctetString.getOctets();
}
 
Example 8
Source File: ExtensionsChecker.java    From xipki with Apache License 2.0 5 votes vote down vote up
private void checkExtnAuthorizationTemplate(StringBuilder failureMsg,
    byte[] extensionValue, Extensions requestedExtns, ExtensionControl extControl) {
  AuthorizationTemplate conf = authorizationTemplate;
  if (conf == null) {
    checkConstantExtnValue(ObjectIdentifiers.Xipki.id_xipki_ext_authorizationTemplate,
        failureMsg, extensionValue, requestedExtns, extControl);

    byte[] expected = getExpectedExtValue(
        ObjectIdentifiers.Xipki.id_xipki_ext_authorizationTemplate, requestedExtns, extControl);
    if (!Arrays.equals(expected, extensionValue)) {
      addViolation(failureMsg, "extension values", hex(extensionValue),
          (expected == null) ? "not present" : hex(expected));
    }
    return;
  }

  ASN1Sequence seq = ASN1Sequence.getInstance(extensionValue);
  ASN1ObjectIdentifier type = ASN1ObjectIdentifier.getInstance(seq.getObjectAt(0));
  ASN1OctetString accessRights = DEROctetString.getInstance(seq.getObjectAt(1));
  if (!conf.getType().getOid().equals(type.getId())) {
    addViolation(failureMsg, "type", type.getId(), conf.getType());
  }

  byte[] isRights = accessRights.getOctets();
  if (!Arrays.equals(conf.getAccessRights().getValue(), isRights)) {
    addViolation(failureMsg, "accessRights",
        hex(isRights), hex(conf.getAccessRights().getValue()));
  }
}
 
Example 9
Source File: SM2PublicKey.java    From gmhelper with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] getEncoded() {
    ASN1OctetString p = ASN1OctetString.getInstance(
        new X9ECPoint(getQ(), withCompression).toASN1Primitive());

    // stored curve is null if ImplicitlyCa
    SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(
        new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, ID_SM2_PUBKEY_PARAM),
        p.getOctets());

    return KeyUtil.getEncodedSubjectPublicKeyInfo(info);
}
 
Example 10
Source File: Asn1Utils.java    From AttestationServer with MIT License 5 votes vote down vote up
public static String getStringFromAsn1OctetStreamAssumingUTF8(ASN1Encodable encodable)
        throws CertificateParsingException, UnsupportedEncodingException {
    if (!(encodable instanceof ASN1OctetString)) {
        throw new CertificateParsingException(
                "Expected octet string, found " + encodable.getClass().getName());
    }

    ASN1OctetString octetString = (ASN1OctetString) encodable;
    return new String(octetString.getOctets(), StandardCharsets.UTF_8);
}
 
Example 11
Source File: Asn1Utils.java    From AttestationServer with MIT License 5 votes vote down vote up
public static byte[] getByteArrayFromAsn1(ASN1Encodable asn1Encodable)
        throws CertificateParsingException {
    if (asn1Encodable == null || !(asn1Encodable instanceof DEROctetString)) {
        throw new CertificateParsingException("Expected DEROctetString");
    }
    ASN1OctetString derOctectString = (ASN1OctetString) asn1Encodable;
    return derOctectString.getOctets();
}
 
Example 12
Source File: OcspRef.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
private byte[] getResponderIdByKey() {
   ResponderID responderID = this.ocsp.getResponderId().toASN1Primitive();
   DERTaggedObject derTaggedObject = (DERTaggedObject)responderID.toASN1Primitive();
   if (2 == derTaggedObject.getTagNo()) {
      ASN1OctetString keyHashOctetString = (ASN1OctetString)derTaggedObject.getObject();
      return keyHashOctetString.getOctets();
   } else {
      return new byte[0];
   }
}
 
Example 13
Source File: OcspRef.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
private byte[] getResponderIdByKey() {
   ResponderID responderID = this.ocsp.getResponderId().toASN1Primitive();
   DERTaggedObject derTaggedObject = (DERTaggedObject)responderID.toASN1Primitive();
   if (2 == derTaggedObject.getTagNo()) {
      ASN1OctetString keyHashOctetString = (ASN1OctetString)derTaggedObject.getObject();
      return keyHashOctetString.getOctets();
   } else {
      return new byte[0];
   }
}
 
Example 14
Source File: OcspRef.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
private byte[] getResponderIdByKey() {
   ResponderID responderID = this.ocsp.getResponderId().toASN1Primitive();
   DERTaggedObject derTaggedObject = (DERTaggedObject)responderID.toASN1Primitive();
   if (2 == derTaggedObject.getTagNo()) {
      ASN1OctetString keyHashOctetString = (ASN1OctetString)derTaggedObject.getObject();
      return keyHashOctetString.getOctets();
   } else {
      return new byte[0];
   }
}
 
Example 15
Source File: OcspRef.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
private byte[] getResponderIdByKey() {
   ResponderID responderID = this.ocsp.getResponderId().toASN1Primitive();
   DERTaggedObject derTaggedObject = (DERTaggedObject)responderID.toASN1Primitive();
   if (2 == derTaggedObject.getTagNo()) {
      ASN1OctetString keyHashOctetString = (ASN1OctetString)derTaggedObject.getObject();
      return keyHashOctetString.getOctets();
   } else {
      return new byte[0];
   }
}
 
Example 16
Source File: OcspRef.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
private byte[] getResponderIdByKey() {
   ResponderID responderID = this.ocsp.getResponderId().toASN1Primitive();
   DERTaggedObject derTaggedObject = (DERTaggedObject)responderID.toASN1Primitive();
   if (2 == derTaggedObject.getTagNo()) {
      ASN1OctetString keyHashOctetString = (ASN1OctetString)derTaggedObject.getObject();
      return keyHashOctetString.getOctets();
   } else {
      return new byte[0];
   }
}
 
Example 17
Source File: Asn1Utils.java    From Auditor with MIT License 5 votes vote down vote up
public static byte[] getByteArrayFromAsn1(ASN1Encodable asn1Encodable)
        throws CertificateParsingException {
    if (asn1Encodable == null || !(asn1Encodable instanceof DEROctetString)) {
        throw new CertificateParsingException("Expected DEROctetString");
    }
    ASN1OctetString derOctectString = (ASN1OctetString) asn1Encodable;
    return derOctectString.getOctets();
}
 
Example 18
Source File: BasicASNSignaturePolicyValidator.java    From dss with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void validate() {
	SignaturePolicy signaturePolicy = getSignaturePolicy();

	final DSSDocument policyContent = signaturePolicy.getPolicyContent();
	byte[] policyBytes = DSSUtils.toByteArray(policyContent);
	final Digest digest = signaturePolicy.getDigest();

	setStatus(true);
	setIdentified(true);

	try {
		ASN1Sequence asn1Sequence = DSSASN1Utils.toASN1Primitive(policyBytes);

		if (asn1Sequence != null) {
			setAsn1Processable(true);

			/**
			 * a) If the resulting document is based on TR 102 272 [i.2] (ESI: ASN.1 format for signature policies),
			 * use the digest value present in the
			 * SignPolicyDigest element from the resulting document. Check that the digest algorithm indicated in
			 * the SignPolicyDigestAlg from the resulting
			 * document is equal to the digest algorithm indicated in the property.
			 */

			final ASN1Sequence signPolicyHashAlgObject = (ASN1Sequence) asn1Sequence.getObjectAt(0);
			final AlgorithmIdentifier signPolicyHashAlgIdentifier = AlgorithmIdentifier.getInstance(signPolicyHashAlgObject);
			DigestAlgorithm signPolicyHashAlgFromPolicy = DigestAlgorithm.forOID(signPolicyHashAlgIdentifier.getAlgorithm().getId());

			/**
			 * b) If the resulting document is based on TR 102 038 [i.3] ((ESI) XML format for signature policies),
			 * use the digest value present in
			 * signPolicyHash element from the resulting document. Check that the digest algorithm indicated in the
			 * signPolicyHashAlg from the resulting
			 * document is equal to the digest algorithm indicated in the attribute.
			 */

			/**
			 * The use of a zero-sigPolicyHash value is to ensure backwards compatibility with earlier versions of
			 * the current document. If sigPolicyHash is
			 * zero, then the hash value should not be checked against the calculated hash value of the signature
			 * policy.
			 */
			if (!signPolicyHashAlgFromPolicy.equals(digest.getAlgorithm())) {
				addError("general", "The digest algorithm indicated in the SignPolicyHashAlg from the resulting document (" + signPolicyHashAlgFromPolicy
						+ ") is not equal to the digest " + "algorithm (" + digest.getAlgorithm() + ").");
				setDigestAlgorithmsEqual(false);
				setStatus(false);
				return;
			} else {
				setDigestAlgorithmsEqual(true);
			}

			byte[] recalculatedDigestValue = DSSASN1Utils.getAsn1SignaturePolicyDigest(signPolicyHashAlgFromPolicy, policyBytes);

			boolean equal = Arrays.equals(digest.getValue(), recalculatedDigestValue);
			setStatus(equal);
			if (!equal) {
				addError("general",
						"The policy digest value (" + Utils.toBase64(digest.getValue()) + ") does not match the re-calculated digest value ("
								+ Utils.toBase64(recalculatedDigestValue) + ").");
				return;
			}

			final ASN1OctetString signPolicyHash = (ASN1OctetString) asn1Sequence.getObjectAt(2);
			final byte[] policyDigestValueFromPolicy = signPolicyHash.getOctets();
			equal = Arrays.equals(digest.getValue(), policyDigestValueFromPolicy);
			setStatus(equal);
			if (!equal) {
				addError("general", "The policy digest value (" + Utils.toBase64(digest.getValue())
						+ ") does not match the digest value from the policy file ("
						+ Utils.toBase64(policyDigestValueFromPolicy) + ").");
			}
		}

	} catch (Exception e) {
		// When any error (communication) we just set the status to false
		setStatus(false);
		addError("general", e.getMessage());
		// Do nothing
		LOG.warn(e.getMessage(), e);
	}
}
 
Example 19
Source File: DSSASN1Utils.java    From dss with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static String toString(final ASN1OctetString value) {
	return new String(value.getOctets());
}
 
Example 20
Source File: Asn1Dump.java    From keystore-explorer with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Get dump of the supplied X.509 extension.
 *
 * @param extension
 *            X.509 extension
 * @return Dump of extension
 * @throws Asn1Exception
 *             A problem was encountered getting the ASN.1 dump
 * @throws IOException
 *             If an I/O problem occurred
 */
public String dump(X509Ext extension) throws Asn1Exception, IOException {
	try {
		ASN1OctetString octetString = ASN1OctetString.getInstance(extension.getValue());
		byte[] octets = octetString.getOctets();

		return dump(octets);
	} catch (IOException ex) {
		throw new Asn1Exception(res.getString("NoAsn1DumpObject.exception.message"), ex);
	}
}