gpgme-loongson 2.0.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,43 @@
1
+ module GPGME
2
+ module KeyCommon
3
+
4
+ ##
5
+ # Returns nil if the trust is valid.
6
+ # Returns one of +:revoked+, +:expired+, +:disabled+, +:invalid+
7
+ def trust
8
+ return :revoked if @revoked == 1
9
+ return :expired if @expired == 1
10
+ return :disabled if @disabled == 1
11
+ return :invalid if @invalid == 1
12
+ end
13
+
14
+ ##
15
+ # Array of capabilities for this key. It can contain any combination of
16
+ # +:encrypt+, +:sign+, +:certify+ or +:authenticate+
17
+ def capability
18
+ caps = []
19
+ caps << :encrypt if @can_encrypt == 1
20
+ caps << :sign if @can_sign == 1
21
+ caps << :certify if @can_certify == 1
22
+ caps << :authenticate if @can_authenticate == 1
23
+ caps
24
+ end
25
+
26
+ ##
27
+ # Checks if the key is capable of all of these actions. If empty array
28
+ # is passed then will return true.
29
+ #
30
+ # Returns false if the keys trust has been invalidated.
31
+ def usable_for?(purposes)
32
+ unless purposes.kind_of? Array
33
+ purposes = [purposes]
34
+ end
35
+ return false if [:revoked, :expired, :disabled, :invalid].include? trust
36
+ return (purposes - capability).empty?
37
+ end
38
+
39
+ def secret?
40
+ @secret == 1
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,35 @@
1
+ module GPGME
2
+ class KeySig
3
+ private_class_method :new
4
+
5
+ attr_reader :pubkey_algo, :keyid
6
+
7
+ def revoked?
8
+ @revoked == 1
9
+ end
10
+
11
+ def expired?
12
+ @expired == 1
13
+ end
14
+
15
+ def invalid?
16
+ @invalid == 1
17
+ end
18
+
19
+ def exportable?
20
+ @exportable == 1
21
+ end
22
+
23
+ def timestamp
24
+ Time.at(@timestamp)
25
+ end
26
+
27
+ def expires
28
+ Time.at(@expires)
29
+ end
30
+
31
+ def inspect
32
+ "#<#{self.class} #{keyid} timestamp=#{timestamp}, expires=#{expires}>"
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,73 @@
1
+ module GPGME
2
+ class EngineInfo
3
+ private_class_method :new
4
+
5
+ attr_reader :protocol, :file_name, :version, :req_version, :home_dir
6
+ alias required_version req_version
7
+ end
8
+
9
+ class VerifyResult
10
+ private_class_method :new
11
+
12
+ attr_reader :signatures
13
+ end
14
+
15
+ class Recipient
16
+ private_class_method :new
17
+
18
+ attr_reader :pubkey_algo, :keyid, :status
19
+ end
20
+
21
+ class DecryptResult
22
+ private_class_method :new
23
+
24
+ attr_reader :unsupported_algorithm, :wrong_key_usage
25
+ attr_reader :recipients, :file_name
26
+ end
27
+
28
+ class SignResult
29
+ private_class_method :new
30
+
31
+ attr_reader :invalid_signers, :signatures
32
+ end
33
+
34
+ class EncryptResult
35
+ private_class_method :new
36
+
37
+ attr_reader :invalid_recipients
38
+ end
39
+
40
+ class InvalidKey
41
+ private_class_method :new
42
+
43
+ attr_reader :fpr, :reason
44
+ alias fingerprint fpr
45
+ end
46
+
47
+ class NewSignature
48
+ private_class_method :new
49
+
50
+ attr_reader :type, :pubkey_algo, :hash_algo, :sig_class, :fpr
51
+ alias fingerprint fpr
52
+
53
+ def timestamp
54
+ Time.at(@timestamp)
55
+ end
56
+ end
57
+
58
+ class ImportStatus
59
+ private_class_method :new
60
+
61
+ attr_reader :fpr, :result, :status
62
+ alias fingerprint fpr
63
+ end
64
+
65
+ class ImportResult
66
+ private_class_method :new
67
+
68
+ attr_reader :considered, :no_user_id, :imported, :imported_rsa, :unchanged
69
+ attr_reader :new_user_ids, :new_sub_keys, :new_signatures, :new_revocations
70
+ attr_reader :secret_read, :secret_imported, :secret_unchanged
71
+ attr_reader :not_imported, :imports
72
+ end
73
+ end
@@ -0,0 +1,85 @@
1
+ module GPGME
2
+ class Signature
3
+ private_class_method :new
4
+
5
+ attr_reader :summary, :fpr, :status, :notations, :wrong_key_usage
6
+ attr_reader :validity, :validity_reason
7
+ attr_reader :pka_trust, :pka_address
8
+ alias fingerprint fpr
9
+
10
+ ##
11
+ # Returns true if the signature is correct
12
+ def valid?
13
+ status_code == GPGME::GPG_ERR_NO_ERROR
14
+ end
15
+
16
+ def expired_signature?
17
+ status_code == GPGME::GPG_ERR_SIG_EXPIRED
18
+ end
19
+
20
+ def expired_key?
21
+ status_code == GPGME::GPG_ERR_KEY_EXPIRED
22
+ end
23
+
24
+ def revoked_key?
25
+ status_code == GPGME::GPG_ERR_CERT_REVOKED
26
+ end
27
+
28
+ def bad?
29
+ status_code == GPGME::GPG_ERR_BAD_SIGNATURE
30
+ end
31
+
32
+ def no_key?
33
+ status_code == GPGME::GPG_ERR_NO_PUBKEY
34
+ end
35
+
36
+ def status_code
37
+ GPGME::gpgme_err_code(status)
38
+ end
39
+
40
+ def from
41
+ @from ||= begin
42
+ Ctx.new do |ctx|
43
+ if from_key = ctx.get_key(fingerprint)
44
+ "#{from_key.subkeys[0].keyid} #{from_key.uids[0].uid}"
45
+ else
46
+ fingerprint
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ def key
53
+ @key ||= begin
54
+ Ctx.new do |ctx|
55
+ @key = ctx.get_key(fingerprint)
56
+ end
57
+ end
58
+ end
59
+
60
+ def timestamp
61
+ Time.at(@timestamp)
62
+ end
63
+
64
+ def exp_timestamp
65
+ Time.at(@exp_timestamp)
66
+ end
67
+
68
+ def to_s
69
+ case status_code
70
+ when GPGME::GPG_ERR_NO_ERROR
71
+ "Good signature from #{from}"
72
+ when GPGME::GPG_ERR_SIG_EXPIRED
73
+ "Expired signature from #{from}"
74
+ when GPGME::GPG_ERR_KEY_EXPIRED
75
+ "Signature made from expired key #{from}"
76
+ when GPGME::GPG_ERR_CERT_REVOKED
77
+ "Signature made from revoked key #{from}"
78
+ when GPGME::GPG_ERR_BAD_SIGNATURE
79
+ "Bad signature from #{from}"
80
+ when GPGME::GPG_ERR_NO_PUBKEY
81
+ "No public key for #{from}"
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,67 @@
1
+ module GPGME
2
+ class SubKey
3
+ private_class_method :new
4
+
5
+ attr_reader :pubkey_algo, :length, :keyid, :fpr
6
+ alias fingerprint fpr
7
+
8
+ include KeyCommon
9
+
10
+ def timestamp
11
+ case @timestamp
12
+ when -1, 0
13
+ # FIXME: add a special value for invalid timestamp, or throw an error
14
+ return nil
15
+ else
16
+ return Time.at(@timestamp)
17
+ end
18
+ end
19
+
20
+ def expires?
21
+ @expires != 0
22
+ end
23
+
24
+ def expires
25
+ expires? ? Time.at(@expires) : nil
26
+ end
27
+
28
+ def expired
29
+ expires? && @expires < Time.now.to_i
30
+ end
31
+
32
+ def sha
33
+ (@fpr || @keyid)[-8 .. -1]
34
+ end
35
+
36
+ PUBKEY_ALGO_LETTERS = {
37
+ PK_RSA => "R",
38
+ PK_ELG_E => "g",
39
+ PK_ELG => "G",
40
+ PK_DSA => "D"
41
+ }
42
+
43
+ def pubkey_algo_letter
44
+ PUBKEY_ALGO_LETTERS[@pubkey_algo] || "?"
45
+ end
46
+
47
+ def inspect
48
+ sprintf("#<#{self.class} %s %4d%s/%s %s trust=%s, capability=%s>",
49
+ secret? ? 'ssc' : 'sub',
50
+ length,
51
+ pubkey_algo_letter,
52
+ (@fpr || @keyid)[-8 .. -1],
53
+ timestamp.strftime('%Y-%m-%d'),
54
+ trust.inspect,
55
+ capability.inspect)
56
+ end
57
+
58
+ def to_s
59
+ sprintf("%s %4d%s/%s %s\n",
60
+ secret? ? 'ssc' : 'sub',
61
+ length,
62
+ pubkey_algo_letter,
63
+ (@fpr || @keyid)[-8 .. -1],
64
+ timestamp.strftime('%Y-%m-%d'))
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,20 @@
1
+ module GPGME
2
+ class UserID
3
+ private_class_method :new
4
+
5
+ attr_reader :validity, :uid, :name, :comment, :email, :signatures
6
+
7
+ def revoked?
8
+ @revoked == 1
9
+ end
10
+
11
+ def invalid?
12
+ @invalid == 1
13
+ end
14
+
15
+ def inspect
16
+ "#<#{self.class} #{name} <#{email}> \
17
+ validity=#{VALIDITY_NAMES[validity]}, signatures=#{signatures.inspect}>"
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,4 @@
1
+ module GPGME
2
+ # The version of GPGME ruby binding you are using
3
+ VERSION = "2.0.18"
4
+ end
@@ -0,0 +1,246 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'test_helper'
3
+ require 'tempfile'
4
+
5
+ describe GPGME::Crypto do
6
+ before do
7
+ skip unless ensure_keys GPGME::PROTOCOL_OpenPGP
8
+ end
9
+
10
+ describe "default options functionality" do
11
+ it "allows operation from instances normally" do
12
+ crypto = GPGME::Crypto.new
13
+ encrypted = crypto.encrypt TEXT[:plain], :always_trust => true, :recipients => KEYS.first[:sha]
14
+ assert_equal TEXT[:plain], crypto.decrypt(encrypted).read
15
+ end
16
+
17
+ it "can set default options when using the instance way" do
18
+ crypto = GPGME::Crypto.new :always_trust => true
19
+ encrypted = crypto.encrypt TEXT[:plain], :recipients => KEYS.first[:sha]
20
+ assert_equal TEXT[:plain], crypto.decrypt(encrypted).read
21
+ end
22
+
23
+ it "but they can still be overwritten" do
24
+ crypto = GPGME::Crypto.new :always_trust => false
25
+ encrypted = crypto.encrypt TEXT[:plain], :always_trust => true, :recipients => KEYS.first[:sha]
26
+ assert_equal TEXT[:plain], crypto.decrypt(encrypted).read
27
+ end
28
+ end
29
+
30
+ describe "roundtrip encryption/decryption" do
31
+ it "does the roundtrip encrypting" do
32
+ crypto = GPGME::Crypto.new
33
+ encrypted = crypto.encrypt TEXT[:plain], :always_trust => true, :recipients => KEYS.first[:sha]
34
+ assert_equal TEXT[:plain], crypto.decrypt(encrypted).read
35
+ end
36
+
37
+ it "does so even with armored encrypted stuff" do
38
+ crypto = GPGME::Crypto.new
39
+ encrypted = crypto.encrypt TEXT[:plain], :always_trust => true, :armor => true
40
+ assert_equal TEXT[:plain], crypto.decrypt(encrypted).read
41
+ end
42
+ end
43
+
44
+ describe :encrypt do
45
+ it "should raise an error if the recipients aren't trusted" do
46
+ assert_raises GPGME::Error::UnusablePublicKey do
47
+ GPGME::Crypto.new.encrypt TEXT[:plain]
48
+ end
49
+ end
50
+
51
+ it "doesn't raise an error and returns something when encrypting nothing" do
52
+ data = GPGME::Crypto.new.encrypt nil, :always_trust => true
53
+ refute_empty data.read
54
+ data = GPGME::Crypto.new.encrypt "", :always_trust => true
55
+ refute_empty data.read
56
+ end
57
+
58
+ it "can specify which key(s) to use for encrypting with a string" do
59
+ crypto = GPGME::Crypto.new :always_trust => true
60
+ key = KEYS.last
61
+ encrypted = crypto.encrypt TEXT[:plain], :recipients => key[:sha]
62
+ assert_equal TEXT[:plain], crypto.decrypt(encrypted).read
63
+
64
+ remove_key key
65
+ encrypted.seek 0
66
+ assert_raises GPGME::Error::NoSecretKey do
67
+ crypto.decrypt(encrypted)
68
+ end
69
+ import_key key
70
+ end
71
+
72
+ it "can specify which key to use for encrypting with a Key object" do
73
+ crypto = GPGME::Crypto.new :always_trust => true
74
+ key = KEYS.last
75
+ real_key = GPGME::Key.find(:public, key[:sha]).first
76
+
77
+ encrypted = crypto.encrypt TEXT[:plain], :recipients => real_key
78
+ assert_equal TEXT[:plain], crypto.decrypt(encrypted).read
79
+
80
+ remove_key key
81
+ encrypted.seek 0
82
+ assert_raises GPGME::Error::NoSecretKey do
83
+ crypto.decrypt(encrypted)
84
+ end
85
+ import_key key
86
+ end
87
+
88
+ it "can also sign at the same time" do
89
+ crypto = GPGME::Crypto.new :always_trust => true
90
+ encrypted = crypto.encrypt TEXT[:plain], :sign => true
91
+ signatures = 0
92
+
93
+ crypto.verify(encrypted) do |signature|
94
+ assert_instance_of GPGME::Signature, signature
95
+ signatures += 1
96
+ end
97
+
98
+ assert_equal 1, signatures
99
+ end
100
+
101
+ it "can be signed by more than one person" do
102
+ crypto = GPGME::Crypto.new :always_trust => true
103
+ encrypted = crypto.encrypt TEXT[:plain], :sign => true, :signers => KEYS.map{|k| k[:sha]}
104
+ signatures = 0
105
+
106
+ crypto.verify(encrypted) do |signature|
107
+ assert_instance_of GPGME::Signature, signature
108
+ signatures += 1
109
+ end
110
+
111
+ assert_equal 4, signatures
112
+ end
113
+
114
+ it "outputs to a file if specified" do
115
+ crypto = GPGME::Crypto.new :always_trust => true
116
+ file = Tempfile.new "test"
117
+ crypto.encrypt TEXT[:plain], :output => file
118
+ file_contents = file.read
119
+ file.seek 0
120
+
121
+ refute_empty file_contents
122
+ assert_equal TEXT[:plain], crypto.decrypt(file).read
123
+ end
124
+
125
+ # TODO find how to test
126
+ # it "raises GPGME::Error::UnusablePublicKey"
127
+ # it "raises GPGME::Error::UnusableSecretKey"
128
+ end
129
+
130
+ describe "symmetric encryption/decryption" do
131
+ it "requires a password to encrypt" do
132
+ assert_raises GPGME::Error::BadPassphrase do
133
+ GPGME::Crypto.new.encrypt TEXT[:plain], :symmetric => true
134
+ end
135
+ end
136
+
137
+ it "requires a password to decrypt" do
138
+ crypto = GPGME::Crypto.new
139
+ encrypted_data = crypto.encrypt TEXT[:plain],
140
+ :symmetric => true, :password => "gpgme"
141
+
142
+ assert_raises GPGME::Error::BadPassphrase do
143
+ crypto.decrypt encrypted_data
144
+ end
145
+ end
146
+
147
+ it "can encrypt and decrypt with the same password" do
148
+ crypto = GPGME::Crypto.new :symmetric => true, :password => "gpgme"
149
+ encrypted_data = crypto.encrypt TEXT[:plain]
150
+ plain = crypto.decrypt encrypted_data
151
+
152
+ assert_equal "Hi there", plain.read
153
+ end
154
+
155
+ it "but breaks with different ones" do
156
+ crypto = GPGME::Crypto.new
157
+ encrypted_data = crypto.encrypt TEXT[:plain],
158
+ :symmetric => true, :password => "gpgme"
159
+
160
+ assert_raises GPGME::Error::DecryptFailed do
161
+ crypto.decrypt encrypted_data, :password => "wrong one"
162
+ end
163
+ end
164
+ end
165
+
166
+ describe :decrypt do
167
+ it "decrypts encrypted stuff" do
168
+ assert_equal TEXT[:plain], GPGME::Crypto.new.decrypt(TEXT[:encrypted]).read
169
+ end
170
+
171
+ it "will not get into the signatures block if there's none" do
172
+ GPGME::Crypto.new.decrypt(TEXT[:encrypted]) do |signature|
173
+ flunk "If I'm here means there was some signature"
174
+ end
175
+ pass
176
+ end
177
+
178
+ it "will get signature elements if the encrypted thing was signed" do
179
+ signatures = 0
180
+ GPGME::Crypto.new.decrypt(TEXT[:signed]) do |signature|
181
+ assert_instance_of GPGME::Signature, signature
182
+ signatures += 1
183
+ end
184
+ assert_equal 1, signatures
185
+ end
186
+
187
+ it "writes to the output if passed" do
188
+ buffer = GPGME::Data.new
189
+ GPGME::Crypto.new.decrypt(TEXT[:encrypted], :output => buffer)
190
+ assert_equal TEXT[:plain], buffer.read
191
+ end
192
+
193
+ # TODO find ways to test this
194
+ # it "raises UnsupportedAlgorithm"
195
+ # it "raises WrongKeyUsage"
196
+
197
+ it "raises DecryptFailed when the decrypting key isn't available" do
198
+ assert_raises GPGME::Error::NoSecretKey do
199
+ GPGME::Crypto.new.decrypt(TEXT[:unavailable])
200
+ end
201
+ end
202
+ end
203
+
204
+ describe :sign do
205
+ it "signs normal strings" do
206
+ crypto = GPGME::Crypto.new
207
+ signatures = 0
208
+ sign = crypto.sign "Hi there"
209
+
210
+ crypto.verify(sign) do |signature|
211
+ assert_instance_of GPGME::Signature, signature
212
+ assert signature.valid?
213
+ signatures += 1
214
+ end
215
+
216
+ assert_equal 1, signatures
217
+ end
218
+
219
+ # TODO Find how to import an expired public key
220
+ # it "raises an error if trying to sign with an expired key" do
221
+ # with_key EXPIRED_KEY do
222
+ # crypto = GPGME::Crypto.new
223
+ # assert_raises GPGME::Error::General do
224
+ # sign = crypto.sign "Hi there", :signer => EXPIRED_KEY[:sha]
225
+ # end
226
+ # end
227
+ # end
228
+
229
+ it "selects who to sign for" do
230
+ crypto = GPGME::Crypto.new
231
+ sign = crypto.sign "Hi there", :signer => KEYS.last[:sha]
232
+ key = GPGME::Key.get(KEYS.last[:sha])
233
+
234
+ signatures = 0
235
+
236
+ crypto.verify(sign) do |signature|
237
+ assert_instance_of GPGME::Signature, signature
238
+ assert_equal key, signature.key
239
+ signatures += 1
240
+ end
241
+
242
+ assert_equal 1, signatures
243
+ end
244
+
245
+ end
246
+ end