1
0
mirror of https://github.com/twiglet/cs2j.git synced 2025-01-18 13:15:17 +01:00

Add code to count unsigned translation files

This commit is contained in:
Kevin Glynn 2011-03-21 10:27:35 +01:00
parent b12fce2bff
commit 257b7fc186
5 changed files with 127 additions and 6 deletions

View File

@ -11,6 +11,9 @@ using Antlr.Runtime.Tree;
using Antlr.Runtime;
using System.Xml.Serialization;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using Antlr.StringTemplate;
using NDesk.Options;
@ -31,8 +34,12 @@ namespace Twiglet.CS2J.Translator
private const string VERSION = "2011.1.1.x";
private static DirectoryHT<TypeRepTemplate> AppEnv { get; set; }
private static CS2JSettings cfg = new CS2JSettings();
private static StringTemplateGroup templates = null;
private static StringTemplateGroup templates = null;
private static RSACryptoServiceProvider RsaKey = null;
private int badXmlTxCountTrigger = 3 + 4 - 2;
private int badXmlTxCount = badXmlTxCountTrigger;
public delegate void FileProcessor(string fName);
private static void showVersion()
@ -112,6 +119,7 @@ namespace Twiglet.CS2J.Translator
.Add ("appdir=", dirs => addDirectories(cfg.AppRoot, dirs))
.Add ("exappdir=", dirs => addDirectories(cfg.ExAppRoot, dirs))
.Add ("exclude=", dirs => addDirectories(cfg.Exclude, dirs))
.Add ("keyfile=", v => cfg.KeyFile = v)
.Add ("translator-keep-parens=", v => cfg.TranslatorKeepParens = Boolean.Parse(v))
.Add ("translator-timestamp-files=", v => cfg.TranslatorAddTimeStamp = Boolean.Parse(v))
.Add ("translator-exception-is-throwable=", v => cfg.TranslatorExceptionIsThrowable = Boolean.Parse(v))
@ -125,6 +133,19 @@ namespace Twiglet.CS2J.Translator
// No work
Environment.Exit(0);
// Initialise RSA signing key so that we can verify signatures
RsaKey = new RSACryptoServiceProvider();
string rsaPubXml = RSAPubKey.PubKey;
// Comment out code to read pub key from a file. To easy to re-sign xml files and import your own key!
// if (!String.IsNullOrEmpty(cfg.KeyFile))
// {
// XmlReader reader = XmlReader.Create(cfg.KeyFile);
// reader.MoveToContent();
// rsaPubXml = reader.ReadOuterXml();
// }
RsaKey.FromXmlString(rsaPubXml);
// Load .Net templates
foreach (string r in cfg.NetRoot)
doFile(r, ".xml", addNetTranslation, cfg.ExNetRoot);
@ -263,11 +284,82 @@ namespace Twiglet.CS2J.Translator
return nodes;
}
// Verify the signature of an XML file against an asymmetric
// algorithm and return the result.
public static Boolean VerifyXml(XmlDocument Doc, RSA Key)
{
// Check arguments.
if (Doc == null)
throw new ArgumentException("Doc");
if (Key == null)
throw new ArgumentException("Key");
// Create a new SignedXml object and pass it
// the XML document class.
SignedXml signedXml = new SignedXml(Doc);
// Find the "Signature" node and create a new
// XmlNodeList object.
XmlNodeList nodeList = Doc.GetElementsByTagName("Signature");
// Throw an exception if no signature was found.
if (nodeList.Count <= 0)
{
throw new CryptographicException("Verification failed: No Signature was found in the document.");
}
// This example only supports one signature for
// the entire XML document. Throw an exception
// if more than one signature was found.
if (nodeList.Count >= 2)
{
throw new CryptographicException("Verification failed: More that one signature was found for the document.");
}
// Load the first <signature> node.
signedXml.LoadXml((XmlElement)nodeList[0]);
// Check the signature and return the result.
return signedXml.CheckSignature(Key);
}
// Here's where we do the real work...
public static void addNetTranslation(string fullName)
{
Stream s = new FileStream(fullName, FileMode.Open, FileAccess.Read);
TypeRepTemplate t = TypeRepTemplate.newInstance(s);
// Suchk in translation file
Stream txStream = new FileStream(fullName, FileMode.Open, FileAccess.Read);
// Create a new XML document.
XmlDocument xmlDoc = new XmlDocument();
// Load an XML file into the XmlDocument object.
xmlDoc.PreserveWhitespace = true;
xmlDoc.Load(txStream);
// Verify the signature of the signed XML.
Console.WriteLine("Verifying signature...");
bool result = VerifyXml(xmlDoc, RsaKey);
// Display the results of the signature verification to
// the console.
if (!result)
{
// badXmlTxCount--;
if (badCountTrigger <= 0)
{
Console.Out.WriteLine("This is a trial version of CS2J. It is to be used for evaluation purposes only.");
Console.Out.WriteLine("The .Net translations that you are using contain more than " + badXmlTxCountTrigger + " unsigned or modified translation files.");
Console.Out.WriteLine("Please reduce your number of unsigned and modified translation files and try again.");
Console.Out.WriteLine("Contact Twiglet Software at info@twigletsoftware.com (http://www.twigletsoftware.com) for licensing details.");
Environment.Exit(1);
}
}
txStream.Seek(0, SeekOrigin.Begin);
TypeRepTemplate t = TypeRepTemplate.newInstance(txStream);
// Fullname has form: <path>/<key>.xml
AppEnv[t.TypeName+(t.TypeParams != null && t.TypeParams.Length > 0 ? "'" + t.TypeParams.Length.ToString() : "")] = t;
}

View File

@ -33,6 +33,8 @@ namespace Twiglet.CS2J.Translator
public string EnumDir { get; set; }
public int Verbosity { get; set; }
public string KeyFile { get; set; }
public bool DebugTemplateExtraction { get; set; }
public int DebugLevel { get; set; }
@ -76,6 +78,7 @@ namespace Twiglet.CS2J.Translator
MacroDefines = new List<string>();
XmlDir = Path.Combine(Directory.GetCurrentDirectory(), "tmpXMLs");
EnumDir = Path.Combine(Directory.GetCurrentDirectory(), "enums");
KeyFile = null;
Verbosity = 0;
DebugTemplateExtraction = true;
DebugLevel = 0;

View File

@ -0,0 +1,23 @@
/*
Copyright 2010,2011 Kevin Glynn (kevin.glynn@twigletsoftware.com)
*/
using System;
namespace Twiglet.CS2J.Translator
{
public class RSAPubKey
{
private static string _key = @"
<RSAKeyValue>
<Modulus>iTXgwMVVIk25/pstsBVNNsONs5Q4haeikef5YcRBuTh6slndGs5cj7h0LSHRqPNesp3EwVmwJYY11bDkutN1+rzs9EH3X4vJI6SKgKEHDi5ZV1kfZ8eA3xos8TKNvE4WK33+0ZmZJYkL0sknFyEOIGVek/OiAlsriNZ7NmerWuU=</Modulus>
<Exponent>EQ==</Exponent>
</RSAKeyValue>
";
public static string PubKey { get
{ return _key; }
}
}
}

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@ -24,6 +24,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Commandlineparameters>-translator-timestamp-files=false -translator-keep-parens=false -netdir=/Users/keving/gitrepos/cs2j/CS2JLibrary/NetFramework/ -dumpxmls -xmldir=/Users/keving/tmp/xml/se -odir=/Users/keving/tmp/java/se/src -appdir=/Users/keving/svnrepos/ScormEngineNet/src/app/ScormEngine.Core /Users/keving/svnrepos/ScormEngineNet/src/app/ScormEngine.Core/Logic/Integration/IntegrationInterface.cs</Commandlineparameters>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget>
@ -63,6 +64,7 @@
<Compile Include="CS2JUtil\Constants.cs" />
<Compile Include="CS2JUtil\DirectoryHT.cs" />
<Compile Include="CS2JUtil\Set.cs" />
<Compile Include="CS2JMain\RSAPubKey.cs" />
</ItemGroup>
<ItemGroup>
<None Include="..\CSharpParser\cs.g">
@ -82,6 +84,7 @@
<HintPath>..\..\dll\StringTemplate.dll</HintPath>
</Reference>
<Reference Include="System.Xml" />
<Reference Include="System.Security" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Antlr.Runtime\Antlr3.Runtime.csproj">

View File

@ -766,7 +766,7 @@ public get_accessor_declaration:
public set_accessor_declaration:
accessor_modifier? 'set' accessor_body ;
public accessor_modifier:
'protected' 'internal'? | 'private' | 'internal' 'protected'?;
'protected' 'internal'? | 'private' | 'internal' 'protected'? ;
public accessor_body:
block ;