markus / Rhino.Licensing / LicenseGenerator.cs @ de691472
이력 | 보기 | 이력해설 | 다운로드 (5.8 KB)
1 | 42d49521 | taeseongkim | using System; |
---|---|---|---|
2 | using System.Globalization; |
||
3 | using System.IO; |
||
4 | using System.Security.Cryptography; |
||
5 | using System.Security.Cryptography.Xml; |
||
6 | using System.Text; |
||
7 | using System.Xml; |
||
8 | using System.Collections.Generic; |
||
9 | |||
10 | namespace Rhino.Licensing |
||
11 | { |
||
12 | /// <summary> |
||
13 | /// LicenseGenerator generates and signs license. |
||
14 | /// </summary> |
||
15 | public class LicenseGenerator |
||
16 | { |
||
17 | private readonly string privateKey; |
||
18 | |||
19 | /// <summary> |
||
20 | /// Creates a new instance of <seealso cref="LicenseGenerator"/>. |
||
21 | /// </summary> |
||
22 | /// <param name="privateKey">private key of the product</param> |
||
23 | public LicenseGenerator(string privateKey) |
||
24 | { |
||
25 | this.privateKey = privateKey; |
||
26 | } |
||
27 | |||
28 | /// <summary> |
||
29 | /// Generates a new floating license. |
||
30 | /// </summary> |
||
31 | /// <param name="name">Name of the license holder</param> |
||
32 | /// <param name="publicKey">public key of the license server</param> |
||
33 | /// <returns>license content</returns> |
||
34 | public string GenerateFloatingLicense(string name, string publicKey) |
||
35 | { |
||
36 | using (var rsa = new RSACryptoServiceProvider()) |
||
37 | { |
||
38 | rsa.FromXmlString(privateKey); |
||
39 | var doc = new XmlDocument(); |
||
40 | var license = doc.CreateElement("floating-license"); |
||
41 | doc.AppendChild(license); |
||
42 | |||
43 | var publicKeyEl = doc.CreateElement("license-server-public-key"); |
||
44 | license.AppendChild(publicKeyEl); |
||
45 | publicKeyEl.InnerText = publicKey; |
||
46 | |||
47 | var nameEl = doc.CreateElement("name"); |
||
48 | license.AppendChild(nameEl); |
||
49 | nameEl.InnerText = name; |
||
50 | |||
51 | var signature = GetXmlDigitalSignature(doc, rsa); |
||
52 | doc.FirstChild.AppendChild(doc.ImportNode(signature, true)); |
||
53 | |||
54 | var ms = new MemoryStream(); |
||
55 | var writer = XmlWriter.Create(ms, new XmlWriterSettings |
||
56 | { |
||
57 | Indent = true, |
||
58 | Encoding = Encoding.UTF8 |
||
59 | }); |
||
60 | doc.Save(writer); |
||
61 | ms.Position = 0; |
||
62 | return new StreamReader(ms).ReadToEnd(); |
||
63 | } |
||
64 | } |
||
65 | |||
66 | /// <summary> |
||
67 | /// Generates a new license |
||
68 | /// </summary> |
||
69 | /// <param name="name">name of the license holder</param> |
||
70 | /// <param name="id">Id of the license holder</param> |
||
71 | /// <param name="expirationDate">expiry date</param> |
||
72 | /// <param name="licenseType">type of the license</param> |
||
73 | /// <returns></returns> |
||
74 | public string Generate(string name, Guid id, DateTime expirationDate, LicenseType licenseType) |
||
75 | { |
||
76 | return Generate(name, id, expirationDate, new Dictionary<string, string>(), licenseType); |
||
77 | } |
||
78 | |||
79 | /// <summary> |
||
80 | /// Generates a new license |
||
81 | /// </summary> |
||
82 | /// <param name="name">name of the license holder</param> |
||
83 | /// <param name="id">Id of the license holder</param> |
||
84 | /// <param name="expirationDate">expiry date</param> |
||
85 | /// <param name="licenseType">type of the license</param> |
||
86 | /// <param name="attributes">extra information stored as key/valye in the license file</param> |
||
87 | /// <returns></returns> |
||
88 | public string Generate(string name, Guid id, DateTime expirationDate, IDictionary<string, string> attributes, LicenseType licenseType) |
||
89 | { |
||
90 | using (var rsa = new RSACryptoServiceProvider()) |
||
91 | { |
||
92 | rsa.FromXmlString(privateKey); |
||
93 | var doc = CreateDocument(id, name, expirationDate, attributes, licenseType); |
||
94 | |||
95 | var signature = GetXmlDigitalSignature(doc, rsa); |
||
96 | doc.FirstChild.AppendChild(doc.ImportNode(signature, true)); |
||
97 | |||
98 | var ms = new MemoryStream(); |
||
99 | var writer = XmlWriter.Create(ms,new XmlWriterSettings |
||
100 | { |
||
101 | Indent = true, |
||
102 | Encoding = Encoding.UTF8 |
||
103 | }); |
||
104 | doc.Save(writer); |
||
105 | ms.Position = 0; |
||
106 | return new StreamReader(ms).ReadToEnd(); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | private static XmlElement GetXmlDigitalSignature(XmlDocument x, AsymmetricAlgorithm key) |
||
111 | { |
||
112 | var signedXml = new SignedXml(x) { SigningKey = key }; |
||
113 | var reference = new Reference { Uri = "" }; |
||
114 | reference.AddTransform(new XmlDsigEnvelopedSignatureTransform()); |
||
115 | signedXml.AddReference(reference); |
||
116 | signedXml.ComputeSignature(); |
||
117 | return signedXml.GetXml(); |
||
118 | } |
||
119 | |||
120 | private static XmlDocument CreateDocument(Guid id, string name, DateTime expirationDate, IDictionary<string,string> attributes, LicenseType licenseType) |
||
121 | { |
||
122 | var doc = new XmlDocument(); |
||
123 | var license = doc.CreateElement("license"); |
||
124 | doc.AppendChild(license); |
||
125 | var idAttr = doc.CreateAttribute("id"); |
||
126 | license.Attributes.Append(idAttr); |
||
127 | idAttr.Value = id.ToString(); |
||
128 | |||
129 | var expirDateAttr = doc.CreateAttribute("expiration"); |
||
130 | license.Attributes.Append(expirDateAttr); |
||
131 | expirDateAttr.Value = expirationDate.ToString("yyyy-MM-ddTHH:mm:ss.fffffff", CultureInfo.InvariantCulture); |
||
132 | |||
133 | var licenseAttr = doc.CreateAttribute("type"); |
||
134 | license.Attributes.Append(licenseAttr); |
||
135 | licenseAttr.Value = licenseType.ToString(); |
||
136 | |||
137 | var nameEl = doc.CreateElement("name"); |
||
138 | license.AppendChild(nameEl); |
||
139 | nameEl.InnerText = name; |
||
140 | |||
141 | foreach (var attribute in attributes) |
||
142 | { |
||
143 | var attrib = doc.CreateAttribute(attribute.Key); |
||
144 | attrib.Value = attribute.Value; |
||
145 | license.Attributes.Append(attrib); |
||
146 | } |
||
147 | |||
148 | return doc; |
||
149 | } |
||
150 | } |
||
151 | } |