Class: EllipticCurve::Utils::Der

Inherits:
Object
  • Object
show all
Defined in:
lib/utils/der.rb

Defined Under Namespace

Modules: DerFieldType

Class Method Summary collapse

Class Method Details

._encodeInteger(number) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/utils/der.rb', line 115

def self._encodeInteger(number)
    hexadecimal = Utils::Binary.hexFromInt(number.abs)
    if number < 0
        bitCount = hexadecimal.length * 4
        twosComplement = (2 ** bitCount) + number
        return Utils::Binary.hexFromInt(twosComplement)
    end
    bits = Utils::Binary.bitsFromHex(hexadecimal[0])
    if bits[0] == "1"
        hexadecimal = "00" + hexadecimal
    end
    return hexadecimal
end

._generateLengthBytes(hexadecimal) ⇒ Object



147
148
149
150
151
152
153
154
155
# File 'lib/utils/der.rb', line 147

def self._generateLengthBytes(hexadecimal)
    size = hexadecimal.length.div(2)
    length = Utils::Binary.hexFromInt(size)
    if size < 128
        return length.rjust(2, "0")
    end
    lengthLength = 128 + length.length.div(2)
    return Utils::Binary.hexFromInt(lengthLength) + length
end

._getTagData(tag) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/utils/der.rb', line 157

def self._getTagData(tag)
    bits = Utils::Binary.bitsFromHex(tag)
    bit8 = bits[0]
    bit7 = bits[1]
    bit6 = bits[2]

    tagClass = {
        "0" => {
            "0" => "universal",
            "1" => "application",
        },
        "1" => {
            "0" => "context-specific",
            "1" => "private",
        },
    }[bit8][bit7]
    
    isConstructed = bit6 == "1"

    return {
        "class": tagClass,
        "isConstructed": isConstructed,
        "type": @_hexTagToType[tag],
    }
end

._parseAny(hexadecimal) ⇒ Object



84
85
86
# File 'lib/utils/der.rb', line 84

def self._parseAny(hexadecimal)
    return hexadecimal
end

._parseInteger(hexadecimal) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/utils/der.rb', line 105

def self._parseInteger(hexadecimal)
    integer = Utils::Binary.intFromHex(hexadecimal)
    bits = Utils::Binary.bitsFromHex(hexadecimal[0])
    if bits[0] == "0" # negative numbers are encoded using two's complement
        return integer
    end
    bitCount = 4 * hexadecimal.length
    return integer - (2 ** bitCount)
end

._parseNull(_content) ⇒ Object



101
102
103
# File 'lib/utils/der.rb', line 101

def self._parseNull(_content)
    return nil
end

._parseOid(hexadecimal) ⇒ Object



88
89
90
# File 'lib/utils/der.rb', line 88

def self._parseOid(hexadecimal)
    return Utils::Oid.oidFromHex(hexadecimal)
end

._parseString(hexadecimal) ⇒ Object



97
98
99
# File 'lib/utils/der.rb', line 97

def self._parseString(hexadecimal)
    return Utils::Binary.byteStringFromHex(hexadecimal)
end

._parseTime(hexadecimal) ⇒ Object



92
93
94
95
# File 'lib/utils/der.rb', line 92

def self._parseTime(hexadecimal)
    string = self._parseString(hexadecimal)
    return DateTime.strptime(string, "%y%m%d%H%M%SZ")
end

._readLengthBytes(hexadecimal) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/utils/der.rb', line 129

def self._readLengthBytes(hexadecimal)
    lengthBytes = 2
    lengthIndicator = Utils::Binary.intFromHex(hexadecimal[0, lengthBytes])
    isShortForm = lengthIndicator < 128  # checks if first bit of byte is 1 (a.k.a. short-form)
    if isShortForm
        length = lengthIndicator * 2
        return length, lengthBytes
    end

    lengthLength = lengthIndicator - 128  # nullifies first bit of byte (only used as long-form flag)
    if lengthLength == 0
        raise Exception.new("indefinite length encoding located in DER")
    end
    lengthBytes += 2 * lengthLength
    length = Utils::Binary.intFromHex(hexadecimal[2, lengthBytes]) * 2
    return length, lengthBytes
end

.encodeConstructed(*encodedValues) ⇒ Object



38
39
40
# File 'lib/utils/der.rb', line 38

def self.encodeConstructed(*encodedValues)
    return self.encodePrimitive(DerFieldType.sequence, encodedValues.join(""))
end

.encodePrimitive(tagType, value) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/utils/der.rb', line 42

def self.encodePrimitive(tagType, value)
    if tagType == DerFieldType.integer
        value = self._encodeInteger(value)
    end
    if tagType == DerFieldType.object
        value = Utils::Oid.oidToHex(value)
    end
    return "#{@_typeToHexTag[tagType]}#{self._generateLengthBytes(value)}#{value}"
end

.parse(hexadecimal) ⇒ Object



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
# File 'lib/utils/der.rb', line 52

def self.parse(hexadecimal)
    if hexadecimal.class == String && hexadecimal.empty?
        return []
    elsif not hexadecimal then 
        return [] 
    end
    
    typeByte, hexadecimal = hexadecimal[0..1], hexadecimal[2..-1]
    length, lengthBytes = self._readLengthBytes(hexadecimal)
    content = hexadecimal[lengthBytes..lengthBytes + length - 1] 
    hexadecimal = hexadecimal[lengthBytes + length..-1]

    if content.length < length
        raise Exception.new("missing bytes in DER parsing")
    end

    tagData = self._getTagData(typeByte)
    if tagData[:isConstructed]
        content = self.parse(content)
    end

    valueParser = {
        DerFieldType.null => lambda { |content| self._parseNull(content) },
        DerFieldType.object => lambda { |content| self._parseOid(content) },
        DerFieldType.utcTime => lambda { |content| self._parseTime(content) },
        DerFieldType.integer => lambda { |content| self._parseInteger(content) },
        DerFieldType.printableString => lambda { |content| self._parseString(content) },
    }.fetch(tagData[:type], lambda { |content| self._parseAny(content) })

    return [valueParser.call(content)] + self.parse(hexadecimal)
end