X509Utilities.cs :  » PDF » iTextSharp » Org » BouncyCastle » X509 » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » PDF » iTextSharp 
iTextSharp » Org » BouncyCastle » X509 » X509Utilities.cs
using System;
using System.Collections;
using System.Globalization;
using System.IO;

using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.CryptoPro;
using Org.BouncyCastle.Asn1.Nist;
using Org.BouncyCastle.Asn1.Oiw;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Asn1.TeleTrust;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Asn1.X9;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities.Collections;

namespace Org.BouncyCastle.X509{
  internal class X509Utilities
  {
    private static readonly Hashtable  algorithms = new Hashtable();
    private static readonly Hashtable  exParams = new Hashtable();
    private static readonly ISet    noParams = new HashSet();

    static X509Utilities()
    {
      algorithms.Add("MD2WITHRSAENCRYPTION", PkcsObjectIdentifiers.MD2WithRsaEncryption);
      algorithms.Add("MD2WITHRSA", PkcsObjectIdentifiers.MD2WithRsaEncryption);
      algorithms.Add("MD5WITHRSAENCRYPTION", PkcsObjectIdentifiers.MD5WithRsaEncryption);
      algorithms.Add("MD5WITHRSA", PkcsObjectIdentifiers.MD5WithRsaEncryption);
      algorithms.Add("SHA1WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
      algorithms.Add("SHA1WITHRSA", PkcsObjectIdentifiers.Sha1WithRsaEncryption);
      algorithms.Add("SHA224WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
      algorithms.Add("SHA224WITHRSA", PkcsObjectIdentifiers.Sha224WithRsaEncryption);
      algorithms.Add("SHA256WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
      algorithms.Add("SHA256WITHRSA", PkcsObjectIdentifiers.Sha256WithRsaEncryption);
      algorithms.Add("SHA384WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
      algorithms.Add("SHA384WITHRSA", PkcsObjectIdentifiers.Sha384WithRsaEncryption);
      algorithms.Add("SHA512WITHRSAENCRYPTION", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
      algorithms.Add("SHA512WITHRSA", PkcsObjectIdentifiers.Sha512WithRsaEncryption);
      algorithms.Add("SHA1WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
      algorithms.Add("SHA224WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
      algorithms.Add("SHA256WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
      algorithms.Add("SHA384WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
      algorithms.Add("SHA512WITHRSAANDMGF1", PkcsObjectIdentifiers.IdRsassaPss);
      algorithms.Add("RIPEMD160WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160);
      algorithms.Add("RIPEMD160WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD160);
      algorithms.Add("RIPEMD128WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128);
      algorithms.Add("RIPEMD128WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD128);
      algorithms.Add("RIPEMD256WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256);
      algorithms.Add("RIPEMD256WITHRSA", TeleTrusTObjectIdentifiers.RsaSignatureWithRipeMD256);
      algorithms.Add("SHA1WITHDSA", X9ObjectIdentifiers.IdDsaWithSha1);
      algorithms.Add("DSAWITHSHA1", X9ObjectIdentifiers.IdDsaWithSha1);
      algorithms.Add("SHA224WITHDSA", NistObjectIdentifiers.DsaWithSha224);
      algorithms.Add("SHA256WITHDSA", NistObjectIdentifiers.DsaWithSha256);
      algorithms.Add("SHA1WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha1);
      algorithms.Add("ECDSAWITHSHA1", X9ObjectIdentifiers.ECDsaWithSha1);
      algorithms.Add("SHA224WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha224);
      algorithms.Add("SHA256WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha256);
      algorithms.Add("SHA384WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha384);
      algorithms.Add("SHA512WITHECDSA", X9ObjectIdentifiers.ECDsaWithSha512);
      algorithms.Add("GOST3411WITHGOST3410", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94);
      algorithms.Add("GOST3411WITHGOST3410-94", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94);
      algorithms.Add("GOST3411WITHECGOST3410", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);
      algorithms.Add("GOST3411WITHECGOST3410-2001", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);
      algorithms.Add("GOST3411WITHGOST3410-2001", CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);

      //
      // According to RFC 3279, the ASN.1 encoding SHALL (id-dsa-with-sha1) or MUST (ecdsa-with-SHA*) omit the parameters field.
      // The parameters field SHALL be NULL for RSA based signature algorithms.
      //
      noParams.Add(X9ObjectIdentifiers.ECDsaWithSha1);
      noParams.Add(X9ObjectIdentifiers.ECDsaWithSha224);
      noParams.Add(X9ObjectIdentifiers.ECDsaWithSha256);
      noParams.Add(X9ObjectIdentifiers.ECDsaWithSha384);
      noParams.Add(X9ObjectIdentifiers.ECDsaWithSha512);
      noParams.Add(X9ObjectIdentifiers.IdDsaWithSha1);
      noParams.Add(NistObjectIdentifiers.DsaWithSha224);
      noParams.Add(NistObjectIdentifiers.DsaWithSha256);

      //
      // RFC 4491
      //
      noParams.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x94);
      noParams.Add(CryptoProObjectIdentifiers.GostR3411x94WithGostR3410x2001);

      //
      // explicit params
      //
      AlgorithmIdentifier sha1AlgId = new AlgorithmIdentifier(OiwObjectIdentifiers.IdSha1, DerNull.Instance);
      exParams.Add("SHA1WITHRSAANDMGF1", CreatePssParams(sha1AlgId, 20));

      AlgorithmIdentifier sha224AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha224, DerNull.Instance);
      exParams.Add("SHA224WITHRSAANDMGF1", CreatePssParams(sha224AlgId, 28));

      AlgorithmIdentifier sha256AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha256, DerNull.Instance);
      exParams.Add("SHA256WITHRSAANDMGF1", CreatePssParams(sha256AlgId, 32));

      AlgorithmIdentifier sha384AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha384, DerNull.Instance);
      exParams.Add("SHA384WITHRSAANDMGF1", CreatePssParams(sha384AlgId, 48));

      AlgorithmIdentifier sha512AlgId = new AlgorithmIdentifier(NistObjectIdentifiers.IdSha512, DerNull.Instance);
      exParams.Add("SHA512WITHRSAANDMGF1", CreatePssParams(sha512AlgId, 64));
    }

    private static RsassaPssParameters CreatePssParams(
      AlgorithmIdentifier  hashAlgId,
      int          saltSize)
    {
      return new RsassaPssParameters(
        hashAlgId,
        new AlgorithmIdentifier(PkcsObjectIdentifiers.IdMgf1, hashAlgId),
        new DerInteger(saltSize),
        new DerInteger(1));
    }

    internal static DerObjectIdentifier GetAlgorithmOid(
      string algorithmName)
    {
      algorithmName = algorithmName.ToUpper(CultureInfo.InvariantCulture);

      if (algorithms.ContainsKey(algorithmName))
      {
        return (DerObjectIdentifier) algorithms[algorithmName];
      }

      return new DerObjectIdentifier(algorithmName);
    }

    internal static AlgorithmIdentifier GetSigAlgID(
      DerObjectIdentifier sigOid,
      string        algorithmName)
    {
      if (noParams.Contains(sigOid))
      {
        return new AlgorithmIdentifier(sigOid);
      }

      algorithmName = algorithmName.ToUpper(CultureInfo.InvariantCulture);

      if (exParams.ContainsKey(algorithmName))
      {
        return new AlgorithmIdentifier(sigOid, (Asn1Encodable) exParams[algorithmName]);
      }

      return new AlgorithmIdentifier(sigOid, DerNull.Instance);
    }

    internal static IEnumerable GetAlgNames()
    {
      return new EnumerableProxy(algorithms.Keys);
    }

    internal static byte[] GetSignatureForObject(
      DerObjectIdentifier    sigOid, // TODO Redundant now?
      string          sigName,
      AsymmetricKeyParameter  privateKey,
      SecureRandom      random,
      Asn1Encodable      ae)
    {
      if (sigOid == null)
        throw new ArgumentNullException("sigOid");

      ISigner sig = SignerUtilities.GetSigner(sigName);

      if (random != null)
      {
        sig.Init(true, new ParametersWithRandom(privateKey, random));
      }
      else
      {
        sig.Init(true, privateKey);
      }

      byte[] encoded = ae.GetDerEncoded();
      sig.BlockUpdate(encoded, 0, encoded.Length);

      return sig.GenerateSignature();
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.