public static PartialAttribute Extract(ICollection <byte> buffer) { var result = new PartialAttribute(); result.ExtractTagAndLength(buffer); result.Type = DEROctetString.Extract(buffer); result.Vals = DERSet <DEROctetString> .Extract(buffer); return(result); }
public PartialAttribute() { Vals = new DERSet <DEROctetString>(); Tag = new DERTag { LdapCommand = null, TagClass = ClassTags.Universal, UniversalClassType = UniversalClassTypes.Sequence, TagNumber = (int)UniversalClassTypes.Sequence, PcType = PcTypes.Constructed }; }
/** * Gets the bytes for the PKCS7SignedData object. Optionally the authenticatedAttributes * in the signerInfo can also be set. If either of the parameters is <CODE>null</CODE>, none will be used. * @param secondDigest the digest in the authenticatedAttributes * @param signingTime the signing time in the authenticatedAttributes * @return the bytes for the PKCS7SignedData object */ public byte[] GetEncodedPKCS7(byte[] secondDigest, DateTime signingTime) { if (externalDigest != null) { digest = externalDigest; if (RSAdata != null) { RSAdata = externalRSAdata; } } else if (externalRSAdata != null && RSAdata != null) { RSAdata = externalRSAdata; sig.update(RSAdata, 0, RSAdata.Length); digest = sig.generateSignature(); } else { if (RSAdata != null) { RSAdata = new byte[messageDigest.getDigestSize()]; messageDigest.doFinal(RSAdata, 0); sig.update(RSAdata, 0, RSAdata.Length); } digest = sig.generateSignature(); } // Create the set of Hash algorithms ASN1EncodableVector digestAlgorithms = new ASN1EncodableVector(); foreach (string dal in digestalgos.Keys) { ASN1EncodableVector algos = new ASN1EncodableVector(); algos.add(new DERObjectIdentifier(dal)); algos.add(new DERNull()); digestAlgorithms.add(new DERSequence(algos)); } // Create the contentInfo. ASN1EncodableVector v = new ASN1EncodableVector(); v.add(new DERObjectIdentifier(ID_PKCS7_DATA)); if (RSAdata != null) { v.add(new DERTaggedObject(0, new DEROctetString(RSAdata))); } DERSequence contentinfo = new DERSequence(v); // Get all the certificates // v = new ASN1EncodableVector(); foreach (X509Certificate xcert in certs) { ASN1InputStream tempstream = new ASN1InputStream(new MemoryStream(xcert.getEncoded())); v.add(tempstream.readObject()); } DERSet dercertificates = new DERSet(v); // Create signerinfo structure. // ASN1EncodableVector signerinfo = new ASN1EncodableVector(); // Add the signerInfo version // signerinfo.add(new DERInteger(signerversion)); v = new ASN1EncodableVector(); v.add(GetIssuer(signCert.getTBSCertificate())); v.add(new DERInteger(signCert.getSerialNumber())); signerinfo.add(new DERSequence(v)); // Add the digestAlgorithm v = new ASN1EncodableVector(); v.add(new DERObjectIdentifier(digestAlgorithm)); v.add(new DERNull()); signerinfo.add(new DERSequence(v)); // add the authenticated attribute if present if (secondDigest != null /*&& signingTime != null*/) { ASN1EncodableVector attribute = new ASN1EncodableVector(); v = new ASN1EncodableVector(); v.add(new DERObjectIdentifier(ID_CONTENT_TYPE)); v.add(new DERSet(new DERObjectIdentifier(ID_PKCS7_DATA))); attribute.add(new DERSequence(v)); v = new ASN1EncodableVector(); v.add(new DERObjectIdentifier(ID_SIGNING_TIME)); v.add(new DERSet(new DERUTCTime(signingTime))); attribute.add(new DERSequence(v)); v = new ASN1EncodableVector(); v.add(new DERObjectIdentifier(ID_MESSAGE_DIGEST)); v.add(new DERSet(new DEROctetString(secondDigest))); attribute.add(new DERSequence(v)); signerinfo.add(new DERTaggedObject(false, 0, new DERSet(attribute))); } // Add the digestEncryptionAlgorithm v = new ASN1EncodableVector(); v.add(new DERObjectIdentifier(digestEncryptionAlgorithm)); v.add(new DERNull()); signerinfo.add(new DERSequence(v)); // Add the digest signerinfo.add(new DEROctetString(digest)); // Finally build the body out of all the components above ASN1EncodableVector body = new ASN1EncodableVector(); body.add(new DERInteger(version)); body.add(new DERSet(digestAlgorithms)); body.add(contentinfo); body.add(new DERTaggedObject(false, 0, dercertificates)); // if (crls.Count > 0) { // v = new ASN1EncodableVector(); // for (Iterator i = crls.iterator();i.hasNext();) { // ASN1InputStream t = new ASN1InputStream(new ByteArrayInputStream((((X509CRL)i.next()).getEncoded()))); // v.add(t.readObject()); // } // DERSet dercrls = new DERSet(v); // body.add(new DERTaggedObject(false, 1, dercrls)); // } // Only allow one signerInfo body.add(new DERSet(new DERSequence(signerinfo))); // Now we have the body, wrap it in it's PKCS7Signed shell // and return it // ASN1EncodableVector whole = new ASN1EncodableVector(); whole.add(new DERObjectIdentifier(ID_PKCS7_SIGNED_DATA)); whole.add(new DERTaggedObject(0, new DERSequence(body))); MemoryStream bOut = new MemoryStream(); ASN1OutputStream dout = new ASN1OutputStream(bOut); dout.writeObject(new DERSequence(whole)); dout.Close(); return(bOut.ToArray()); }