forked from trailofbits/SecureEnclaveCrypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey_builder.rb
94 lines (73 loc) · 2.22 KB
/
key_builder.rb
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
require 'openssl'
require 'base64'
module Tidas
module Utilities
class KeyBuilder
# ecPublicKey = '1.2.840.10045.2.1'
KEY_TYPE_BLOB_KEY_TYPE_ID = OpenSSL::ASN1::ObjectId.new('1.2.840.10045.2.1')
# prime256v1 = '1.2.840.10045.3.1.7'
KEY_TYPE_BLOB_CURVE_ID = OpenSSL::ASN1::ObjectId.new('1.2.840.10045.3.1.7')
KEY_SEQ = OpenSSL::ASN1::Sequence.new([KEY_TYPE_BLOB_KEY_TYPE_ID, KEY_TYPE_BLOB_CURVE_ID])
def self.init_with_bytes(bytes)
k = Tidas::Utilities::KeyBuilder.new({bytes: bytes})
k.validate
end
def self.init_with_hex_key_bytes(hex_key_bytes)
k = Tidas::Utilities::KeyBuilder.new({hex_key_bytes: hex_key_bytes})
k.validate
end
def export_pub(file = nil)
unless file
pub
else
File.open(file, 'w') {|f| f.write(pub)}
end
end
def validate
begin
OpenSSL::PKey::EC.new(export_pub).check_key
rescue OpenSSL::PKey::ECError => err
return Tidas::Utilities::KeyBuilder::KeyError.init_with_error(err)
end
self
end
private
def initialize(attributes)
unless bytes = attributes[:bytes]
pub_key_hex_bytes = attributes[:hex_key_bytes]
bytes = [pub_key_hex_bytes].pack("H*")
end
@ASN1_key_bits = OpenSSL::ASN1::BitString.new(bytes)
end
def key
pub_key_seq = OpenSSL::ASN1::Sequence.new([KEY_SEQ, @ASN1_key_bits])
end
def pub
pubstr = "-----BEGIN PUBLIC KEY-----\n"
pubstr += Base64.encode64(key.to_der)
pubstr += "-----END PUBLIC KEY-----\n"
pubstr
end
public
class KeyError
attr_reader :error
def self.init_with_error(err)
KeyError.new({error: err})
end
def export_pub
self
end
private
def initialize(attributes)
@error = attributes[:error]
end
end
end
end
end
if ARGV.length != 1
puts "Err: please pass in exactly one argument\n(Does your data have spaces? Enclose it in quotes!)"
else
key = Tidas::Utilities::KeyBuilder.init_with_hex_key_bytes(ARGV[0].gsub(' ', ''))
puts key.export_pub
end