Example #1
0
 // Convert an ASN.1 buffer into DSA private parameters.
 internal void ASN1ToPrivate(ASN1Parser parser)
 {
     parser  = parser.GetSequence();
     P       = parser.GetBigInt();
     Q       = parser.GetBigInt();
     G       = parser.GetBigInt();
     Y       = parser.GetBigInt();
     J       = parser.GetBigInt();
     X       = parser.GetBigInt();
     Seed    = parser.GetBigInt();
     Counter = parser.GetInt32();
     parser.AtEnd();
 }
Example #2
0
        // Convert an ASN.1 buffer into DSA public parameters.
        internal void ASN1ToPublic(ASN1Parser parser)
        {
            parser = parser.GetSequence();
            if (parser.Type == ASN1Type.Sequence)
            {
                // This looks like it may be a "SubjectPublicKeyInfo"
                // from an X.509 certificate.  Validate the algorithm ID.
                ASN1Parser alg   = parser.GetSequence();
                byte[]     objid = alg.GetObjectIdentifier();
                if (!ASN1Parser.IsObjectID(objid, dsaID))
                {
                    throw new CryptographicException
                              (_("Crypto_InvalidASN1"));
                }

                // Get the common P, Q, and G parameters.
                ASN1Parser algParams = alg.GetSequence();
                P = algParams.GetBigInt();
                Q = algParams.GetBigInt();
                G = algParams.GetBigInt();
                algParams.AtEnd();
                alg.AtEnd();

                // Get the public key information (Y).
                ASN1Parser bitString = parser.GetBitStringContents();
                Y = bitString.GetBigInt();
                bitString.AtEnd();
                parser.AtEnd();
            }
            else
            {
                // This looks like a bare list of DSA parameters.
                P = parser.GetBigInt();
                Q = parser.GetBigInt();
                G = parser.GetBigInt();
                Y = parser.GetBigInt();
                if (!parser.IsAtEnd())
                {
                    // It looks like we have private DSA parameters also.
                    J       = parser.GetBigInt();
                    X       = parser.GetBigInt();
                    Seed    = parser.GetBigInt();
                    Counter = parser.GetInt32();
                }
                parser.AtEnd();
            }
        }
Example #3
0
 // Convert an ASN.1 buffer into RSA private parameters.
 internal void ASN1ToPrivate(ASN1Parser parser)
 {
     parser = parser.GetSequence();
     if (parser.GetInt32() != 0)
     {
         // Incorrect version for RSA private key parameters.
         throw new CryptographicException
                   (_("Crypto_InvalidASN1"));
     }
     Modulus  = parser.GetBigInt();
     Exponent = parser.GetBigInt();
     D        = parser.GetBigInt();
     P        = parser.GetBigInt();
     Q        = parser.GetBigInt();
     DP       = parser.GetBigInt();
     DQ       = parser.GetBigInt();
     InverseQ = parser.GetBigInt();
     parser.AtEnd();
 }
Example #4
0
        // Convert an ASN.1 buffer into RSA public parameters.
        internal void ASN1ToPublic(ASN1Parser parser)
        {
            parser = parser.GetSequence();
            if (parser.Type == ASN1Type.Sequence)
            {
                // This looks like it may be a "SubjectPublicKeyInfo"
                // from an X.509 certificate.  Validate the algorithm ID.
                ASN1Parser alg   = parser.GetSequence();
                byte[]     objid = alg.GetObjectIdentifier();
                if (!ASN1Parser.IsObjectID(objid, rsaID))
                {
                    throw new CryptographicException
                              (_("Crypto_InvalidASN1"));
                }
                alg.GetNull();
                alg.AtEnd();

                // Get the public key information.
                ASN1Parser bitString = parser.GetBitStringContents();
                ASN1Parser inner     = bitString.GetSequence();
                Modulus  = inner.GetBigInt();
                Exponent = inner.GetBigInt();
                inner.AtEnd();
                bitString.AtEnd();
                parser.AtEnd();
            }
            else if (parser.Type == ASN1Type.Integer &&
                     parser.Length == 1)
            {
                // This looks like a list of private RSA parameters.
                ASN1ToPrivate(parser);
            }
            else
            {
                // This looks like a bare list of RSA parameters.
                Modulus  = parser.GetBigInt();
                Exponent = parser.GetBigInt();
                parser.AtEnd();
            }
        }
Example #5
0
        // Get a parser for the contents of a field.
        public ASN1Parser GetContents(ASN1Type type, int adjust)
        {
            if (Type != type)
            {
                // Not the expected type.
                throw new CryptographicException
                          (_("Crypto_InvalidASN1"));
            }
            int len = Length;
            int ofs = Offset;

            if (len < adjust)
            {
                // Contents are no long enough.
                throw new CryptographicException
                          (_("Crypto_InvalidASN1"));
            }
            ASN1Parser parser =
                new ASN1Parser(buffer, offset + ofs + adjust, len - adjust);

            offset += len + ofs;
            count  -= len + ofs;
            return(parser);
        }
	// Convert an ASN.1 buffer into RSA private parameters.
	internal void ASN1ToPrivate(ASN1Parser parser)
			{
				parser = parser.GetSequence();
				if(parser.GetInt32() != 0)
				{
					// Incorrect version for RSA private key parameters.
					throw new CryptographicException
						(_("Crypto_InvalidASN1"));
				}
				Modulus = parser.GetBigInt();
				Exponent = parser.GetBigInt();
				D = parser.GetBigInt();
				P = parser.GetBigInt();
				Q = parser.GetBigInt();
				DP = parser.GetBigInt();
				DQ = parser.GetBigInt();
				InverseQ = parser.GetBigInt();
				parser.AtEnd();
			}
	// Convert an ASN.1 buffer into RSA public parameters.
	internal void ASN1ToPublic(ASN1Parser parser)
			{
				parser = parser.GetSequence();
				if(parser.Type == ASN1Type.Sequence)
				{
					// This looks like it may be a "SubjectPublicKeyInfo"
					// from an X.509 certificate.  Validate the algorithm ID.
					ASN1Parser alg = parser.GetSequence();
					byte[] objid = alg.GetObjectIdentifier();
					if(!ASN1Parser.IsObjectID(objid, rsaID))
					{
						throw new CryptographicException
							(_("Crypto_InvalidASN1"));
					}
					alg.GetNull();
					alg.AtEnd();

					// Get the public key information.
					ASN1Parser bitString = parser.GetBitStringContents();
					ASN1Parser inner = bitString.GetSequence();
					Modulus = inner.GetBigInt();
					Exponent = inner.GetBigInt();
					inner.AtEnd();
					bitString.AtEnd();
					parser.AtEnd();
				}
				else if(parser.Type == ASN1Type.Integer &&
				        parser.Length == 1)
				{
					// This looks like a list of private RSA parameters.
					ASN1ToPrivate(parser);
				}
				else
				{
					// This looks like a bare list of RSA parameters.
					Modulus = parser.GetBigInt();
					Exponent = parser.GetBigInt();
					parser.AtEnd();
				}
			}
	// Get a parser for the contents of a field.
	public ASN1Parser GetContents(ASN1Type type, int adjust)
			{
				if(Type != type)
				{
					// Not the expected type.
					throw new CryptographicException
						(_("Crypto_InvalidASN1"));
				}
				int len = Length;
				int ofs = Offset;
				if(len < adjust)
				{
					// Contents are no long enough.
					throw new CryptographicException
						(_("Crypto_InvalidASN1"));
				}
				ASN1Parser parser =
					new ASN1Parser(buffer, offset + ofs + adjust, len - adjust);
				offset += len + ofs;
				count -= len + ofs;
				return parser;
			}
	// Convert an ASN.1 buffer into DSA private parameters.
	internal void ASN1ToPrivate(ASN1Parser parser)
			{
				parser = parser.GetSequence();
				P = parser.GetBigInt();
				Q = parser.GetBigInt();
				G = parser.GetBigInt();
				Y = parser.GetBigInt();
				J = parser.GetBigInt();
				X = parser.GetBigInt();
				Seed = parser.GetBigInt();
				Counter = parser.GetInt32();
				parser.AtEnd();
			}
	// Convert an ASN.1 buffer into DSA public parameters.
	internal void ASN1ToPublic(ASN1Parser parser)
			{
				parser = parser.GetSequence();
				if(parser.Type == ASN1Type.Sequence)
				{
					// This looks like it may be a "SubjectPublicKeyInfo"
					// from an X.509 certificate.  Validate the algorithm ID.
					ASN1Parser alg = parser.GetSequence();
					byte[] objid = alg.GetObjectIdentifier();
					if(!ASN1Parser.IsObjectID(objid, dsaID))
					{
						throw new CryptographicException
							(_("Crypto_InvalidASN1"));
					}

					// Get the common P, Q, and G parameters.
					ASN1Parser algParams = alg.GetSequence();
					P = algParams.GetBigInt();
					Q = algParams.GetBigInt();
					G = algParams.GetBigInt();
					algParams.AtEnd();
					alg.AtEnd();

					// Get the public key information (Y).
					ASN1Parser bitString = parser.GetBitStringContents();
					Y = bitString.GetBigInt();
					bitString.AtEnd();
					parser.AtEnd();
				}
				else
				{
					// This looks like a bare list of DSA parameters.
					P = parser.GetBigInt();
					Q = parser.GetBigInt();
					G = parser.GetBigInt();
					Y = parser.GetBigInt();
					if(!parser.IsAtEnd())
					{
						// It looks like we have private DSA parameters also.
						J = parser.GetBigInt();
						X = parser.GetBigInt();
						Seed = parser.GetBigInt();
						Counter = parser.GetInt32();
					}
					parser.AtEnd();
				}
			}
	// Parse an X.509-format name and convert it into a string.
	private static String ParseName(ASN1Parser certInfo)
			{
				StringBuilder builder = new StringBuilder();
				ASN1Parser outer;
				ASN1Parser set;
				ASN1Parser pair;

				// Process the outer sequence.
				outer = certInfo.GetSequence();
				while(!outer.IsAtEnd())
				{
					// Process the next name attribute set.
					set = outer.GetSet();
					while(!set.IsAtEnd())
					{
						// Process the next attribute name/value pair.
						pair = set.GetSequence();
						pair.Skip(ASN1Type.ObjectIdentifier);
						if(pair.IsString())
						{
							// Add the value to the string we are building.
							if(builder.Length > 0)
							{
								builder.Append(", ");
							}
							builder.Append(pair.GetString());
						}
					}
				}

				// Convert the result into a name.
				return builder.ToString();
			}
	// Parse the contents of a certificate data block.
	private void Parse(byte[] data)
			{
				// Clone the data for internal storage.
				rawData = (byte[])(data.Clone());

				// Parse the ASN.1 data to get the field we are interested in.
				ASN1Parser parser = new ASN1Parser(rawData);
				ASN1Parser signed = parser.GetSequence();
				ASN1Parser certInfo = signed.GetSequence();
				if(certInfo.Type == ASN1Parser.ContextSpecific(0))
				{
					// Skip the version field.
					certInfo.Skip();
				}
				serialNumber = certInfo.GetContentsAsArray(ASN1Type.Integer);
				ASN1Parser algId = certInfo.GetSequence();
				issuer = ParseName(certInfo);
				ASN1Parser validity = certInfo.GetSequence();
				effectiveDate = validity.GetUTCTime();
				expirationDate = validity.GetUTCTime();
				name = ParseName(certInfo);
				ASN1Parser keyInfo = certInfo.GetSequence();
				algId = keyInfo.GetSequence();
				keyAlgorithm = ToHex(algId.GetObjectIdentifier());
				if(algId.IsAtEnd() || algId.IsNull())
				{
					keyAlgorithmParameters = null;
				}
				else
				{
					keyAlgorithmParameters = algId.GetWholeAsArray();
				}
				publicKey = keyInfo.GetBitString();

#if CONFIG_CRYPTO
				// Construct an MD5 hash of the certificate.  Is this correct?
				MD5 md5 = new MD5CryptoServiceProvider();
				md5.InternalHashCore(rawData, 0, rawData.Length);
				hash = md5.InternalHashFinal();
				md5.Initialize();
#endif
			}