org.bouncycastle.asn1.ASN1OctetString类的使用及代码示例

x33g5p2x  于11个月前 转载在 其他  
字(17.4k)|赞(0)|评价(0)|浏览(31)

本文整理了Java中org.bouncycastle.asn1.ASN1OctetString类的一些代码示例,展示了ASN1OctetString类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ASN1OctetString类的具体详情如下:
包路径:org.bouncycastle.asn1.ASN1OctetString
类名称:ASN1OctetString

ASN1OctetString介绍

[英]Abstract base for the ASN.1 OCTET STRING data type

This supports BER, and DER forms of the data.

DER form is always primitive single OCTET STRING, while BER support includes the constructed forms.

X.690

8: Basic encoding rules

8.7 Encoding of an octetstring value

8.7.1 The encoding of an octetstring value shall be either primitive or constructed at the option of the sender.
NOTE — Where it is necessary to transfer part of an octet string before the entire OCTET STRING is available, the constructed encoding is used.

8.7.2 The primitive encoding contains zero, one or more contents octets equal in value to the octets in the data value, in the order they appear in the data value, and with the most significant bit of an octet of the data value aligned with the most significant bit of an octet of the contents octets.

8.7.3 The contents octets for the constructed encoding shall consist of zero, one, or more encodings.
NOTE — Each such encoding includes identifier, length, and contents octets, and may include end-of-contents octets if it is constructed.

8.7.3.1 To encode an octetstring value in this way, it is segmented. Each segment shall consist of a series of consecutive octets of the value. There shall be no significance placed on the segment boundaries.
NOTE — A segment may be of size zero, i.e. contain no octets.

8.7.3.2 Each encoding in the contents octets shall represent a segment of the overall octetstring, the encoding arising from a recursive application of this subclause. In this recursive application, each segment is treated as if it were a octetstring value. The encodings of the segments shall appear in the contents octets in the order in which their octets appear in the overall value.
NOTE 1 — As a consequence of this recursion, each encoding in the contents octets may itself be primitive or constructed. However, such encodings will usually be primitive.

NOTE 2 — In particular, the tags in the contents octets are always universal class, number 4.

9: Canonical encoding rules

9.1 Length forms

If the encoding is constructed, it shall employ the indefinite length form. If the encoding is primitive, it shall include the fewest length octets necessary. [Contrast with 8.1.3.2 b).]

9.2 String encoding forms

BIT STRING, OCTET STRING,and restricted character string values shall be encoded with a primitive encoding if they would require no more than 1000 contents octets, and as a constructed encoding otherwise. The string fragments contained in the constructed encoding shall be encoded with a primitive encoding. The encoding of each fragment, except possibly the last, shall have 1000 contents octets. (Contrast with 8.21.6.)
10: Distinguished encoding rules

10.1 Length forms The definite form of length encoding shall be used, encoded in the minimum number of octets. [Contrast with 8.1.3.2 b).]

10.2 String encoding forms For BIT STRING, OCTET STRING and restricted character string types, the constructed form of encoding shall not be used. (Contrast with 8.21.6.)
[中]ASN的抽象基础。1八位字符串数据类型
这支持BER和DER形式的数据。
DER形式始终是原始的单八进制字符串,而BER支持包括构造的形式。
X.690
8:基本编码规则
8.7八位字符串值的编码
8.7.1八位字符串值的编码应为原始编码或由发送方选择构造。
注-如果需要在整个八位字节字符串可用之前传输八位字节字符串的一部分,则使用构造的编码。
8.7.2原语编码包含零个、一个或多个内容八位字节,其值等于数据值中的八位字节,顺序与它们在数据值中出现的顺序相同,并且数据值的八位字节的最高有效位与内容八位字节的八位字节的最高有效位对齐。
8.7.3构造编码的内容八位字节应包括零个、一个或多个编码。
注-每种编码都包括标识符、长度和内容八位字节,如果构造的话,还可能包括内容结尾八位字节。
8.7.3.1为了以这种方式对八进制字符串值进行编码,将其分段。每个段应由一系列连续的八位字节组成。管段边界上不应有任何意义。
注-段的大小可能为零,即不包含八位字节。
8.7.3.2内容八位字节中的每个编码应代表整个八位字节字符串的一段,该编码源于本款的递归应用。在这个递归应用程序中,每个段都被视为一个八进制字符串值。段的编码应按照其八位字节在总值中的出现顺序出现在内容八位字节中。
注1-作为这种递归的结果,内容八位字节中的每个编码本身可能是原始的或构造的。然而,这种编码通常是原始的。
注2——特别是,内容八位字节中的标签始终是通用类,编号4。
9:规范编码规则
9.1长度表格
如果构造了编码,则应采用不定长形式。如果编码为原始编码,则应包含所需的最小长度的八位字节。[与8.1.3.2 b相比。]
9.2字符串编码形式
如果位字符串、八位字节字符串和受限字符串值不需要超过1000个内容八位字节,则应使用基本编码进行编码,否则应使用构造编码。构造编码中包含的字符串片段应采用原语编码。除最后一个片段外,每个片段的编码应包含1000个内容八位字节。(与8.21.6相比。)
10:区分编码规则
10.1长度形式应使用长度编码的确定形式,以最小八位字节数编码。[与8.1.3.2 b相比。]
10.2字符串编码形式对于位字符串、八位字符串和受限字符串类型,不得使用构造的编码形式。(与8.21.6相比。)

代码示例

代码示例来源:origin: hyperledger/fabric-sdk-java

String serial = DatatypeConverter.printHexBinary(certificate.getSerialNumber().toByteArray());
byte[] extensionValue = certificate.getExtensionValue(Extension.authorityKeyIdentifier.getId());
ASN1OctetString akiOc = ASN1OctetString.getInstance(extensionValue);
String aki = DatatypeConverter.printHexBinary(AuthorityKeyIdentifier.getInstance(akiOc.getOctets()).getKeyIdentifier());

代码示例来源:origin: hyperledger/fabric-sdk-java

private AuthorityKeyIdentifier getAKI() throws HFCACertificateException {
    if (x509Cert == null) {
      throw new HFCACertificateException("Certificate is null");
    }
    byte[] fullExtValue = x509Cert.getExtensionValue(Extension.authorityKeyIdentifier.getId());
    byte[] extValue = ASN1OctetString.getInstance(fullExtValue).getOctets();
    return AuthorityKeyIdentifier.getInstance(extValue);
  }
}

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

private MessageImprint(
  ASN1Sequence seq)
{
  this.hashAlgorithm = AlgorithmIdentifier.getInstance(seq.getObjectAt(0));
  this.hashedMessage = ASN1OctetString.getInstance(seq.getObjectAt(1)).getOctets();
}

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

private void populateFromPrivKeyInfo(PrivateKeyInfo info)
  throws IOException
  ASN1Primitive p = info.getPrivateKeyAlgorithm().getParameters().toASN1Primitive();
  if (p instanceof ASN1Sequence && (ASN1Sequence.getInstance(p).size() == 2 || ASN1Sequence.getInstance(p).size() == 3))
    gostParams = GOST3410PublicKeyAlgParameters.getInstance(info.getPrivateKeyAlgorithm().getParameters());
      this.d = ASN1Integer.getInstance(privKey).getPositiveValue();
      byte[] encVal = ASN1OctetString.getInstance(privKey).getOctets();
      byte[] dVal = new byte[encVal.length];
      this.d = new BigInteger(1, dVal);
    X962Parameters params = X962Parameters.getInstance(info.getPrivateKeyAlgorithm().getParameters());
      ASN1ObjectIdentifier oid = ASN1ObjectIdentifier.getInstance(params.getParameters());
      X9ECParameters ecP = ECUtil.getNamedCurveByOid(oid);
    if (privKey instanceof ASN1Integer)
      ASN1Integer derD = ASN1Integer.getInstance(privKey);
      this.d = derD.getValue();

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

byte[] keyEnc = key.getOctets();
if (keyInfo.getAlgorithm().getAlgorithm().equals(UAObjectIdentifiers.dstu4145le))
DSTU4145Params dstuParams = DSTU4145Params.getInstance(keyInfo.getAlgorithm().getParameters());
  if (keyInfo.getAlgorithm().getAlgorithm().equals(UAObjectIdentifiers.dstu4145le))
  ECCurve curve = new ECCurve.F2m(field.getM(), field.getK1(), field.getK2(), field.getK3(), binary.getA(), new BigInteger(1, b_bytes));
  byte[] g_bytes = binary.getG();
  if (keyInfo.getAlgorithm().getAlgorithm().equals(UAObjectIdentifiers.dstu4145le))

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

byte[] keyEnc = key.getOctets();
if (info.getAlgorithm().getAlgorithm().equals(UAObjectIdentifiers.dstu4145le))
ASN1Sequence seq = ASN1Sequence.getInstance(info.getAlgorithm().getParameters());
org.bouncycastle.jce.spec.ECParameterSpec spec = null;
X9ECParameters x9Params = null;
if (seq.getObjectAt(0) instanceof ASN1Integer)
    ECDomainParameters ecP = DSTU4145NamedCurves.getByOID(curveOid);
    spec = new ECNamedCurveParameterSpec(curveOid.getId(), ecP.getCurve(), ecP.getG(), ecP.getN(), ecP.getH(), ecP.getSeed());
    if (info.getAlgorithm().getAlgorithm().equals(UAObjectIdentifiers.dstu4145le))
    ECCurve curve = new ECCurve.F2m(field.getM(), field.getK1(), field.getK2(), field.getK3(), binary.getA(), new BigInteger(1, b_bytes));
    byte[] g_bytes = binary.getG();
    if (info.getAlgorithm().getAlgorithm().equals(UAObjectIdentifiers.dstu4145le))
      EC5Util.convertPoint(spec.getG()),
      spec.getN(),
      spec.getH().intValue());

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

private XMSSMTPublicKey(ASN1Sequence seq)
{
  if (!ASN1Integer.getInstance(seq.getObjectAt(0)).getValue().equals(BigInteger.valueOf(0)))
  {
    throw new IllegalArgumentException("unknown version of sequence");
  }
  this.publicSeed = Arrays.clone(DEROctetString.getInstance(seq.getObjectAt(1)).getOctets());
  this.root = Arrays.clone(DEROctetString.getInstance(seq.getObjectAt(2)).getOctets());
}

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

private XMSSMTPrivateKey(ASN1Sequence seq)
{
  if (!ASN1Integer.getInstance(seq.getObjectAt(0)).getValue().equals(BigInteger.valueOf(0)))
  {
    throw new IllegalArgumentException("unknown version of sequence");
  }
  if (seq.size() != 2 && seq.size() != 3)
  {
    throw new IllegalArgumentException("key sequence wrong size");
  }
  ASN1Sequence keySeq = ASN1Sequence.getInstance(seq.getObjectAt(1));
  this.index = ASN1Integer.getInstance(keySeq.getObjectAt(0)).getValue().intValue();
  this.secretKeySeed = Arrays.clone(DEROctetString.getInstance(keySeq.getObjectAt(1)).getOctets());
  this.secretKeyPRF = Arrays.clone(DEROctetString.getInstance(keySeq.getObjectAt(2)).getOctets());
  this.publicSeed = Arrays.clone(DEROctetString.getInstance(keySeq.getObjectAt(3)).getOctets());
  this.root = Arrays.clone(DEROctetString.getInstance(keySeq.getObjectAt(4)).getOctets());
  if(seq.size() == 3)
  {
    this.bdsState = Arrays.clone(DEROctetString.getInstance(ASN1TaggedObject.getInstance(seq.getObjectAt(2)), true).getOctets());
  }
  else
  {
    this.bdsState = null;
  }
}

代码示例来源:origin: org.cryptacular/cryptacular

/**
  * Parses an EC private key as defined in RFC 5915.
  * <pre>
  *      ECPrivateKey ::= SEQUENCE {
  *        version        INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
  *        privateKey     OCTET STRING,
  *        parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
  *        publicKey  [1] BIT STRING OPTIONAL
  *      }
  * </pre>
  *
  * @param  seq  ASN1 sequence to parse
  *
  * @return  EC private key
  */
 private ECPrivateKeyParameters parseECPrivateKey(final ASN1Sequence seq)
 {
  final ASN1TaggedObject asn1Params = ASN1TaggedObject.getInstance(seq.getObjectAt(2));
  final X9ECParameters params;
  if (asn1Params.getObject() instanceof ASN1ObjectIdentifier) {
   params = ECUtil.getNamedCurveByOid(ASN1ObjectIdentifier.getInstance(asn1Params.getObject()));
  } else {
   params = X9ECParameters.getInstance(asn1Params.getObject());
  }
  return new ECPrivateKeyParameters(
   new BigInteger(1, ASN1OctetString.getInstance(seq.getObjectAt(1)).getOctets()),
   new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH(), params.getSeed()));
 }
}

代码示例来源:origin: redfish64/TinyTravelTracker

public BigInteger getKey()
{
  ASN1OctetString octs = (ASN1OctetString)seq.getObjectAt(1);
  return new BigInteger(1, octs.getOctets());
}

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

private McEliecePrivateKey(ASN1Sequence seq)
{
  BigInteger bigN = ((ASN1Integer)seq.getObjectAt(0)).getValue();
  n = bigN.intValue();
  BigInteger bigK = ((ASN1Integer)seq.getObjectAt(1)).getValue();
  k = bigK.intValue();
  encField = ((ASN1OctetString)seq.getObjectAt(2)).getOctets();
  encGp = ((ASN1OctetString)seq.getObjectAt(3)).getOctets();
  encP1 = ((ASN1OctetString)seq.getObjectAt(4)).getOctets();
  encP2 = ((ASN1OctetString)seq.getObjectAt(5)).getOctets();
  encSInv = ((ASN1OctetString)seq.getObjectAt(6)).getOctets();
}

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

private GCMParameters(
  ASN1Sequence seq)
{
  this.nonce = ASN1OctetString.getInstance(seq.getObjectAt(0)).getOctets();
  if (seq.size() == 2)
  {
    this.icvLen = ASN1Integer.getInstance(seq.getObjectAt(1)).getValue().intValue();
  }
  else
  {
    this.icvLen = 12;
  }
}

代码示例来源:origin: org.xipki/security

private ImportSecretKeyParams(ASN1Sequence seq) throws BadAsn1ObjectException {
 requireRange(seq, 4, 4);
 int idx = 0;
 slotId = SlotIdentifier.getInstance(seq.getObjectAt(idx++)).getValue();
 control = NewKeyControl.getInstance(seq.getObjectAt(idx++)).getControl();
 keyType = getInteger(seq.getObjectAt(idx++)).longValue();
 keyValue = ASN1OctetString.getInstance(seq.getObjectAt(idx++)).getOctets();
}

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

EC5Util.convertPoint(curve, ecSpec.getGenerator(), withCompression),
      ecSpec.getOrder(),
      BigInteger.valueOf(ecSpec.getCofactor()),
      ecSpec.getCurve().getSeed());
  info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_2001, params), new DEROctetString(encKey));
  if (curveOid == null)
    curveOid = new ASN1ObjectIdentifier(((ECNamedCurveSpec)ecSpec).getName());
    EC5Util.convertPoint(curve, ecSpec.getGenerator(), withCompression),
    ecSpec.getOrder(),
    BigInteger.valueOf(ecSpec.getCofactor()),
    ecSpec.getCurve().getSeed());
  new X9ECPoint(curve.createPoint(this.getQ().getAffineXCoord().toBigInteger(), this.getQ().getAffineYCoord().toBigInteger(), withCompression)).toASN1Primitive();
info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params), p.getOctets());

代码示例来源:origin: AgNO3/jcifs-ng

@Override
protected void parse ( byte[] token ) throws IOException {
  try ( ASN1InputStream der = new ASN1InputStream(token) ) {
    ASN1TaggedObject tagged = (ASN1TaggedObject) der.readObject();
    ASN1Sequence sequence = ASN1Sequence.getInstance(tagged, true);
    Enumeration<?> fields = sequence.getObjects();
    while ( fields.hasMoreElements() ) {
      tagged = (ASN1TaggedObject) fields.nextElement();
      switch ( tagged.getTagNo() ) {
      case 0:
        ASN1Enumerated enumerated = ASN1Enumerated.getInstance(tagged, true);
        setResult(enumerated.getValue().intValue());
        break;
      case 1:
        setMechanism(ASN1ObjectIdentifier.getInstance(tagged, true));
        break;
      case 2:
        ASN1OctetString mechanismToken = ASN1OctetString.getInstance(tagged, true);
        setMechanismToken(mechanismToken.getOctets());
        break;
      case 3:
        ASN1OctetString mechanismListMIC = ASN1OctetString.getInstance(tagged, true);
        setMechanismListMIC(mechanismListMIC.getOctets());
        break;
      default:
        throw new IOException("Malformed token field.");
      }
    }
  }
}

代码示例来源:origin: org.cryptacular/cryptacular

final org.bouncycastle.asn1.pkcs.EncryptionScheme scheme)
final PBES2Algorithm alg = PBES2Algorithm.fromOid(scheme.getAlgorithm().getId());
if (keyLength == 0) {
 keyLength = alg.getKeySize();
 setCipher(alg.getCipherSpec().newInstance());
 final ASN1Sequence rc2Params = ASN1Sequence.getInstance(scheme.getParameters());
 if (rc2Params.size() > 1) {
  cipherParameters = new RC2Parameters(
   ((KeyParameter) cipherParameters).getKey(),
   ASN1Integer.getInstance(rc2Params.getObjectAt(0)).getValue().intValue());
  iv = ASN1OctetString.getInstance(rc2Params.getObjectAt(0)).getOctets();
 final ASN1Sequence rc5Params = ASN1Sequence.getInstance(scheme.getParameters());
 final int rounds = ASN1Integer.getInstance(rc5Params.getObjectAt(1)).getValue().intValue();
 final int blockSize = ASN1Integer.getInstance(rc5Params.getObjectAt(2)).getValue().intValue();
 if (blockSize == 32) {
  setCipher(new PaddedBufferedBlockCipher(new CBCBlockCipher(new RC532Engine()), new PKCS7Padding()));
 if (rc5Params.size() > 3) {
  iv = ASN1OctetString.getInstance(rc5Params.getObjectAt(3)).getOctets();
 iv = ASN1OctetString.getInstance(scheme.getParameters()).getOctets();

代码示例来源:origin: org.xipki/ca-server

PBMParameter.getInstance(pkiMessage.getHeader().getProtectionAlg().getParameters());
AlgorithmIdentifier algId = parameter.getOwf();
if (!cmpControl.isRequestPbmOwfPermitted(algId)) {
 LOG.warn("MAC_ALGO_FORBIDDEN (PBMParameter.owf: {})", algId.getAlgorithm().getId());
 return new ProtectionVerificationResult(null, ProtectionResult.MAC_ALGO_FORBIDDEN);
 LOG.warn("MAC_ALGO_FORBIDDEN (PBMParameter.mac: {})", algId.getAlgorithm().getId());
 return new ProtectionVerificationResult(null, ProtectionResult.MAC_ALGO_FORBIDDEN);
int iterationCount = parameter.getIterationCount().getValue().intValue();
if (iterationCount < 1000) {
 LOG.warn("MAC_ALGO_FORBIDDEN (PBMParameter.iterationCount: {} < 1000)", iterationCount);
byte[] senderKID = (asn1 == null) ? null : asn1.getOctets();
PKMACBuilder pkMacBuilder = new PKMACBuilder(new JcePKMACValuesCalculator());
if (!cmpControl.getSigAlgoValidator().isAlgorithmPermitted(protectionAlg)) {
 LOG.warn("SIG_ALGO_FORBIDDEN: {}",
   pkiMessage.getHeader().getProtectionAlg().getAlgorithm().getId());
 return new ProtectionVerificationResult(null, ProtectionResult.SIGNATURE_ALGO_FORBIDDEN);

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

throws IllegalArgumentException
ASN1ObjectIdentifier encAlg = encryptionAlgID.getAlgorithm();
if (encAlg.equals(PKCSObjectIdentifiers.rc4))
  BufferedBlockCipher cipher = createCipher(encryptionAlgID.getAlgorithm());
  ASN1Primitive sParams = encryptionAlgID.getParameters().toASN1Primitive();
    if (encAlg.equals(PKCSObjectIdentifiers.des_EDE3_CBC)
      || encAlg.equals(AlgorithmIdentifierFactory.IDEA_CBC)
      || encAlg.equals(NISTObjectIdentifiers.id_aes128_CBC)
      || encAlg.equals(NISTObjectIdentifiers.id_aes192_CBC)
        ASN1OctetString.getInstance(sParams).getOctets()));
    else if (encAlg.equals(AlgorithmIdentifierFactory.CAST5_CBC))
      cipher.init(forEncryption, new ParametersWithIV(new RC2Parameters(((KeyParameter)encKey).getKey(), rc2Ekb[cbcParams.getRC2ParameterVersion().intValue()]), cbcParams.getIV()));

代码示例来源:origin: org.xipki/ocsp-client

byte[] receivedNonce = nonceExtn.getExtnValue().getOctets();
 if (!Arrays.equals(nonce, receivedNonce)) {
  throw new OcspResponseException.OcspNonceUnmatched(nonce, receivedNonce);
Request reqAt0 = Request.getInstance(ocspReq.getTbsRequest().getRequestList().getObjectAt(0));
ASN1ObjectIdentifier issuerHashAlg = certId.getHashAlgorithm().getAlgorithm();
byte[] issuerKeyHash = certId.getIssuerKeyHash().getOctets();
byte[] issuerNameHash = certId.getIssuerNameHash().getOctets();
 boolean issuerMatch = issuerHashAlg.equals(cid.getHashAlgOID())
   && Arrays.equals(issuerKeyHash, cid.getIssuerKeyHash())
   && Arrays.equals(issuerNameHash, cid.getIssuerNameHash());
 if (!serialNumbers[0].equals(serialNumber)) {
  throw new OcspResponseException.OcspTargetUnmatched("the serialNumber is not requested");
  SingleResp singleResp = singleResponses[i];
  CertificateID cid = singleResp.getCertID();
  boolean issuerMatch = issuerHashAlg.equals(cid.getHashAlgOID())
    && Arrays.equals(issuerKeyHash, cid.getIssuerKeyHash())
    && Arrays.equals(issuerNameHash, cid.getIssuerNameHash());

代码示例来源:origin: redfish64/TinyTravelTracker

/**
 * @deprecated use PrivateKeyInfo.getInstance()
 * @param seq
 */
public PrivateKeyInfo(
  ASN1Sequence  seq)
{
  Enumeration e = seq.getObjects();
  BigInteger  version = ((ASN1Integer)e.nextElement()).getValue();
  if (version.intValue() != 0)
  {
    throw new IllegalArgumentException("wrong version for private key info");
  }
  algId = AlgorithmIdentifier.getInstance(e.nextElement());
  privKey = ASN1OctetString.getInstance(e.nextElement());
  
  if (e.hasMoreElements())
  {
    attributes = ASN1Set.getInstance((ASN1TaggedObject)e.nextElement(), false);
  }
}

相关文章