1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
/* Signature.pmod 
 * 
 */ 
 
#if constant(Crypto.Hash) 
#define HASH Crypto.Hash 
#define HashState Crypto.HashState 
#else 
#define HASH object 
#define HashState object 
#endif 
 
#pike __REAL_VERSION__ 
 
import Standards.ASN1.Types; 
 
//! @decl string build_digestinfo(string msg, Crypto.Hash hash) 
//! Construct a PKCS-1 digestinfo. 
//! @param msg 
//!   message to digest 
//! @param hash 
//!   crypto hash object such as @[Crypto.SHA1] or @[Crypto.MD5] 
//! @seealso 
//!   @[Crypto.RSA()->sign] 
string(0..255) build_digestinfo(HashState|string(0..255) msg, HASH hash) 
{ 
  if(!hash->pkcs_hash_id) error("Unknown ASN.1 id for hash.\n"); 
  if(stringp(msg)) 
    msg = hash->hash(msg); 
  else 
    msg = msg->digest(); 
  Sequence digest_info = Sequence( ({ Sequence( ({ hash->pkcs_hash_id(), 
                                                   Null() }) ), 
                                      OctetString(msg) }) ); 
  return digest_info->get_der(); 
} 
 
//! This is an ASN.1 structure from PKCS #10 v1.7 and others, 
//! which represents a signed block of data. 
//! 
//! @seealso 
//!   @[sign()], @[Standards.X509.sign_tbs()]. 
class Signed 
{ 
  inherit Sequence; 
 
  //! ASN.1 structure that has been signed. 
  Object `tbs() 
  { 
    return elements[0]; 
  } 
 
  //! ASN.1 structure to be signed. 
  void `tbs=(Sequence o) 
  { 
    if( o->type_name != "SEQUENCE" ) 
      error("Illegal ASN.1.\n"); 
    elements[0] = o; 
  } 
 
  //! Signing algorithm that was used to sign with. 
  Sequence `algorithm() 
  { 
    return elements[1]; 
  } 
 
  //! Signing algorithm that will be used to sign with. 
  //! 
  //! Typically the result of @[Crypto.Sign()->pkcs_signature_algorithm()]. 
  //! 
  //! @seealso 
  //!   @[sign()] 
  void `algorithm=(Sequence a) 
  { 
    if( a->type_name != "SEQUENCE" 
        || sizeof(a)<1 
        || a[0]->type_name != "OBJECT IDENTIFIER" ) 
      error("Illegal ASN.1.\n"); 
    elements[1] = a; 
  } 
 
  //! The signature. 
  BitString `signature() 
  { 
    return elements[2]; 
  } 
 
  //! The resulting signature from signing the DER of @[tbs] with @[algorithm]. 
  //! 
  //! Typically the result of @[Crypto.Sign()->pkcs_sign()]. 
  //! 
  //! @seealso 
  //!   @[sign()] 
  void `signature=(BitString s) 
  { 
    if( s->type_name != "BIT STRING" 
        || s->unused ) 
      error("Illegal ASN.1.\n"); 
    elements[2] = s; 
  } 
 
  //! Sign @[tbs] with the provided @[sign] and @[hash]. 
  //! 
  //! Sets @[algorithm] and @[signature]. 
  //! 
  //! @returns 
  //!   Returns the @[Signed] object. 
  this_program sign(Crypto.Sign sign, Crypto.Hash hash) 
  { 
    elements[1] = sign->pkcs_signature_algorithm_id(hash); 
    elements[2] = BitString(sign->pkcs_sign(tbs->get_der(), hash)); 
    return this; 
  } 
 
  protected void create(Sequence|void s) 
  { 
    if (s) { 
      elements = allocate(3); 
      if ((s->type_name != "SEQUENCE") || (sizeof(s->elements) != 3)) 
        error("Invalid arguments to Standards.PKCS.Signature.Signed."); 
      tbs = s[0]; 
      algorithm = s[1]; 
      signature = s[2]; 
    } 
    else 
      elements = ({ Null(), Null(), Null() }); 
  } 
} 
 
//! Generic PKCS signing. 
//! 
//! @param tbs 
//!   @[Standards.ASN1] structure to be signed. 
//! 
//! @param sign 
//!   Signature to use. Must have a private key set. 
//! 
//! @param hash 
//!   Hash algorithm to use for the signature. 
//!   Must be valid for the signature algorithm. 
//! 
//! @returns 
//!   Returns a @[Standards.ASN1.Types.Sequence] with 
//!   the signature. 
Signed sign(Sequence tbs, Crypto.Sign sign, Crypto.Hash hash) 
{ 
  Signed res = Signed(); 
  res->tbs = tbs; 
  return res->sign(sign, hash); 
} 
 
Signed decode_signed(string|Sequence signed, 
                     mapping(int:program(Object))|void asn1_types) 
{ 
  if (stringp(signed)) 
    signed = Standards.ASN1.Decode.secure_der_decode(signed, asn1_types); 
  if (signed) 
    return Signed(signed); 
  return 0; 
}