Пример #1
0
 /**
  * add a signer with extra signed/unsigned attributes.
  *
  * @param key signing key to use
  * @param subjectKeyID subjectKeyID of corresponding public key
  * @param digestOID digest algorithm OID
  * @param signedAttr table of attributes to be included in signature
  * @param unsignedAttr table of attributes to be included as unsigned
  */
 public void AddSigner(
     AsymmetricKeyParameter privateKey,
     byte[]                                  subjectKeyID,
     string digestOID,
     Asn1.Cms.AttributeTable signedAttr,
     Asn1.Cms.AttributeTable unsignedAttr)
 {
     AddSigner(privateKey, subjectKeyID, Helper.GetEncOid(privateKey, digestOID), digestOID,
               signedAttr, unsignedAttr);
 }
Пример #2
0
 /**
  * add a signer with extra signed/unsigned attributes.
  *
  * @param key signing key to use
  * @param cert certificate containing corresponding public key
  * @param digestOID digest algorithm OID
  * @param signedAttr table of attributes to be included in signature
  * @param unsignedAttr table of attributes to be included as unsigned
  */
 public void AddSigner(
     AsymmetricKeyParameter privateKey,
     X509Certificate cert,
     string digestOID,
     Asn1.Cms.AttributeTable signedAttr,
     Asn1.Cms.AttributeTable unsignedAttr)
 {
     AddSigner(privateKey, cert, Helper.GetEncOid(privateKey, digestOID), digestOID,
               signedAttr, unsignedAttr);
 }
Пример #3
0
 private void doAddSigner(
     AsymmetricKeyParameter privateKey,
     SignerIdentifier signerIdentifier,
     string encryptionOID,
     string digestOID,
     CmsAttributeTableGenerator signedAttrGen,
     CmsAttributeTableGenerator unsignedAttrGen,
     Asn1.Cms.AttributeTable baseSignedTable)
 {
     signerInfs.Add(new SignerInf(this, privateKey, signerIdentifier, digestOID, encryptionOID,
                                  signedAttrGen, unsignedAttrGen, baseSignedTable));
 }
Пример #4
0
 /**
  * add a signer, specifying the digest encryption algorithm, with extra signed/unsigned attributes.
  *
  * @param key signing key to use
  * @param subjectKeyID subjectKeyID of corresponding public key
  * @param encryptionOID digest encryption algorithm OID
  * @param digestOID digest algorithm OID
  * @param signedAttr table of attributes to be included in signature
  * @param unsignedAttr table of attributes to be included as unsigned
  */
 public void AddSigner(
     AsymmetricKeyParameter privateKey,
     byte[]                                  subjectKeyID,
     string encryptionOID,
     string digestOID,
     Asn1.Cms.AttributeTable signedAttr,
     Asn1.Cms.AttributeTable unsignedAttr)
 {
     doAddSigner(privateKey, GetSignerIdentifier(subjectKeyID), encryptionOID, digestOID,
                 new DefaultSignedAttributeTableGenerator(signedAttr),
                 new SimpleAttributeTableGenerator(unsignedAttr),
                 signedAttr);
 }
Пример #5
0
 internal SignerInf(
     CmsSignedGenerator outer,
     ISignatureFactory sigCalc,
     SignerIdentifier signerIdentifier,
     CmsAttributeTableGenerator sAttr,
     CmsAttributeTableGenerator unsAttr,
     Asn1.Cms.AttributeTable baseSignedTable)
 {
     this.outer            = outer;
     this.sigCalc          = sigCalc;
     this.signerIdentifier = signerIdentifier;
     this.digestOID        = new DefaultDigestAlgorithmIdentifierFinder().find((AlgorithmIdentifier)sigCalc.AlgorithmDetails).Algorithm.Id;
     this.encOID           = ((AlgorithmIdentifier)sigCalc.AlgorithmDetails).Algorithm.Id;
     this.sAttr            = sAttr;
     this.unsAttr          = unsAttr;
     this.baseSignedTable  = baseSignedTable;
 }
Пример #6
0
            internal SignerInf(
                CmsSignedGenerator outer,
                AsymmetricKeyParameter key,
                SignerIdentifier signerIdentifier,
                string digestOID,
                string encOID,
                CmsAttributeTableGenerator sAttr,
                CmsAttributeTableGenerator unsAttr,
                Asn1.Cms.AttributeTable baseSignedTable)
            {
                string digestName = Helper.GetDigestAlgName(digestOID);

                string signatureName = digestName + "with" + Helper.GetEncryptionAlgName(encOID);

                this.outer            = outer;
                this.sigCalc          = new Asn1SignatureFactory(signatureName, key);
                this.signerIdentifier = signerIdentifier;
                this.digestOID        = digestOID;
                this.encOID           = encOID;
                this.sAttr            = sAttr;
                this.unsAttr          = unsAttr;
                this.baseSignedTable  = baseSignedTable;
            }
Пример #7
0
            internal SignerInfo ToSignerInfo(
                DerObjectIdentifier contentType,
                CmsProcessable content,
                SecureRandom random)
            {
                AlgorithmIdentifier digAlgId = DigestAlgorithmID;
                string digestName            = Helper.GetDigestAlgName(digestOID);

                string signatureName = digestName + "with" + Helper.GetEncryptionAlgName(encOID);

                byte[] hash;
                if (outer._digests.Contains(digestOID))
                {
                    hash = (byte[])outer._digests[digestOID];
                }
                else
                {
                    IDigest dig = Helper.GetDigestInstance(digestName);
                    if (content != null)
                    {
                        content.Write(new DigestSink(dig));
                    }
                    hash = DigestUtilities.DoFinal(dig);
                    outer._digests.Add(digestOID, hash.Clone());
                }

                IStreamCalculator calculator = sigCalc.CreateCalculator();

#if NETCF_1_0 || NETCF_2_0 || SILVERLIGHT || PORTABLE
                Stream sigStr = calculator.Stream;
#else
                Stream sigStr = new BufferedStream(calculator.Stream);
#endif

                Asn1Set signedAttr = null;
                if (sAttr != null)
                {
                    IDictionary parameters = outer.GetBaseParameters(contentType, digAlgId, hash);

//					Asn1.Cms.AttributeTable signed = sAttr.GetAttributes(Collections.unmodifiableMap(parameters));
                    Asn1.Cms.AttributeTable signed = sAttr.GetAttributes(parameters);

                    if (contentType == null) //counter signature
                    {
                        if (signed != null && signed[CmsAttributes.ContentType] != null)
                        {
                            IDictionary tmpSigned = signed.ToDictionary();
                            tmpSigned.Remove(CmsAttributes.ContentType);
                            signed = new Asn1.Cms.AttributeTable(tmpSigned);
                        }
                    }

                    // TODO Validate proposed signed attributes

                    signedAttr = outer.GetAttributeSet(signed);

                    // sig must be composed from the DER encoding.
                    new DerOutputStream(sigStr).WriteObject(signedAttr);
                }
                else if (content != null)
                {
                    // TODO Use raw signature of the hash value instead
                    content.Write(sigStr);
                }

                Platform.Dispose(sigStr);
                byte[] sigBytes = ((IBlockResult)calculator.GetResult()).Collect();

                Asn1Set unsignedAttr = null;
                if (unsAttr != null)
                {
                    IDictionary baseParameters = outer.GetBaseParameters(contentType, digAlgId, hash);
                    baseParameters[CmsAttributeTableParameter.Signature] = sigBytes.Clone();

//					Asn1.Cms.AttributeTable unsigned = unsAttr.GetAttributes(Collections.unmodifiableMap(baseParameters));
                    Asn1.Cms.AttributeTable unsigned = unsAttr.GetAttributes(baseParameters);

                    // TODO Validate proposed unsigned attributes

                    unsignedAttr = outer.GetAttributeSet(unsigned);
                }

                // TODO[RSAPSS] Need the ability to specify non-default parameters
                Asn1Encodable       sigX509Parameters = SignerUtilities.GetDefaultX509Parameters(signatureName);
                AlgorithmIdentifier encAlgId          = Helper.GetEncAlgorithmIdentifier(
                    new DerObjectIdentifier(encOID), sigX509Parameters);

                return(new SignerInfo(signerIdentifier, digAlgId,
                                      signedAttr, encAlgId, new DerOctetString(sigBytes), unsignedAttr));
            }