Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / class / Mono.Security / Mono.Security.X509 / X509Builder.cs
1 //
2 // X509Builder.cs: Abstract builder class for X509 objects
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com) 
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System;
33 using System.Globalization;
34 using System.Security.Cryptography;
35
36 using Mono.Security;
37
38 namespace Mono.Security.X509 {
39
40         public abstract class X509Builder {
41
42                 private const string defaultHash = "SHA1";
43                 private string hashName;
44
45                 protected X509Builder ()
46                 {
47                         hashName = defaultHash;
48                 }
49
50                 protected abstract ASN1 ToBeSigned (string hashName);
51
52                 // move to PKCS1
53                 protected string GetOid (string hashName) 
54                 {
55                         switch (hashName.ToLower (CultureInfo.InvariantCulture)) {
56                                 case "md2":
57                                         // md2withRSAEncryption (1 2 840 113549 1 1 2)
58                                         return "1.2.840.113549.1.1.2";
59                                 case "md4":
60                                         // md4withRSAEncryption (1 2 840 113549 1 1 3)
61                                         return "1.2.840.113549.1.1.3";
62                                 case "md5":
63                                         // md5withRSAEncryption (1 2 840 113549 1 1 4)
64                                         return "1.2.840.113549.1.1.4";
65                                 case "sha1":
66                                         // sha1withRSAEncryption (1 2 840 113549 1 1 5)
67                                         return "1.2.840.113549.1.1.5";
68                                 case "sha256":
69                                         // sha256WithRSAEncryption      OBJECT IDENTIFIER ::= { pkcs-1 11 }
70                                         return "1.2.840.113549.1.1.11";
71                                 case "sha384":
72                                         // sha384WithRSAEncryption      OBJECT IDENTIFIER ::= { pkcs-1 12 }
73                                         return "1.2.840.113549.1.1.12";
74                                 case "sha512":
75                                         // sha512WithRSAEncryption      OBJECT IDENTIFIER ::= { pkcs-1 13 }
76                                         return "1.2.840.113549.1.1.13";
77                                 default:
78                                         throw new NotSupportedException ("Unknown hash algorithm " + hashName);
79                         }
80                 }
81
82                 public string Hash {
83                         get { return hashName; }
84                         set { 
85                                 if (hashName == null)
86                                         hashName = defaultHash;
87                                 else
88                                         hashName = value;
89                         }
90                 }
91
92                 public virtual byte[] Sign (AsymmetricAlgorithm aa) 
93                 {
94                         if (aa is RSA)
95                                 return Sign (aa as RSA);
96                         else if (aa is DSA)
97                                 return Sign (aa as DSA);
98                         else
99                                 throw new NotSupportedException ("Unknown Asymmetric Algorithm " + aa.ToString());
100                 }
101
102                 private byte[] Build (ASN1 tbs, string hashoid, byte[] signature) 
103                 {
104                         ASN1 builder = new ASN1 (0x30);
105                         builder.Add (tbs);
106                         builder.Add (PKCS7.AlgorithmIdentifier (hashoid));
107                         // first byte of BITSTRING is the number of unused bits in the first byte
108                         byte[] bitstring = new byte [signature.Length + 1];
109                         Buffer.BlockCopy (signature, 0, bitstring, 1, signature.Length);
110                         builder.Add (new ASN1 (0x03, bitstring));
111                         return builder.GetBytes ();
112                 }
113
114                 public virtual byte[] Sign (RSA key)
115                 {
116                         string oid = GetOid (hashName);
117                         ASN1 tbs = ToBeSigned (oid);
118                         HashAlgorithm ha = HashAlgorithm.Create (hashName);
119                         byte[] hash = ha.ComputeHash (tbs.GetBytes ());
120
121                         RSAPKCS1SignatureFormatter pkcs1 = new RSAPKCS1SignatureFormatter (key);
122                         pkcs1.SetHashAlgorithm (hashName);
123                         byte[] signature = pkcs1.CreateSignature (hash);
124
125                         return Build (tbs, oid, signature);
126                 }
127
128                 public virtual byte[] Sign (DSA key) 
129                 {
130                         string oid = "1.2.840.10040.4.3";
131                         ASN1 tbs = ToBeSigned (oid);
132                         HashAlgorithm ha = HashAlgorithm.Create (hashName);
133                         if (!(ha is SHA1))
134                                 throw new NotSupportedException ("Only SHA-1 is supported for DSA");
135                         byte[] hash = ha.ComputeHash (tbs.GetBytes ());
136
137                         DSASignatureFormatter dsa = new DSASignatureFormatter (key);
138                         dsa.SetHashAlgorithm (hashName);
139                         byte[] rs = dsa.CreateSignature (hash);
140
141                         // split R and S
142                         byte[] r = new byte [20];
143                         Buffer.BlockCopy (rs, 0, r, 0, 20);
144                         byte[] s = new byte [20];
145                         Buffer.BlockCopy (rs, 20, s, 0, 20);
146                         ASN1 signature = new ASN1 (0x30);
147                         signature.Add (new ASN1 (0x02, r));
148                         signature.Add (new ASN1 (0x02, s));
149
150                         // dsaWithSha1 (1 2 840 10040 4 3)
151                         return Build (tbs, oid, signature.GetBytes ());
152                 }
153         }
154 }