Skip to content

Commit da726fe

Browse files
committed
ASN.1: RSA key compat: key vals OctetString -> VisibleString
Switch to using VisibleString to encode key values, since RSA keys are ASCII-prefixed Base64, while ed25519 key values are hex strings. This is inefficient, but this reference implementation profits from being simple. May reconsider later and add specialized code. :/ Signed-off-by: Sebastien Awwad <sebastien.awwad@gmail.com>
1 parent 427dac3 commit da726fe

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

tuf/encoding/asn1_convert.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ def public_key_to_pyasn1(public_key_dict):
183183
raise tuf.exceptions.FormatError('Expected public key, received key dict '
184184
'containing a private key entry!')
185185

186+
# TODO: Intelligently handle PEM-style RSA keys, which have value set to an
187+
# ASCII-prefixed Base64 string like:
188+
# '-----BEGIN PUBLIC KEY-----\nMIIBojANBgkqhkiG9w0BAQE...'
189+
# while also handling ed25519 keys, which have hexstring values. For now,
190+
# we're using VisibleString inefficiently, for easy compatibility with both.
186191
key_pyasn1 = asn1_definitions.PublicKey()
187192
key_pyasn1['keytype'] = public_key_dict['keytype']
188193
key_pyasn1['scheme'] = public_key_dict['scheme']
@@ -202,8 +207,10 @@ def public_key_to_pyasn1(public_key_dict):
202207
i = 0
203208
for valtype in public_key_dict['keyval']:
204209
keyval_pyasn1 = asn1_definitions.KeyValue()
205-
keyval_pyasn1['public'] = pyasn1_univ.OctetString(
206-
hexValue=public_key_dict['keyval'][valtype])
210+
# OctetString handling for ed25519 keys, if definitions use OCTET STRING
211+
# keyval_pyasn1['public'] = pyasn1_univ.OctetString(
212+
# hexValue=public_key_dict['keyval'][valtype])
213+
keyval_pyasn1['public'] = public_key_dict['keyval'][valtype]
207214
keyvals_pyasn1[i] = keyval_pyasn1
208215
i += 1
209216

tuf/encoding/asn1_metadata_definitions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class KeyIDHashAlgorithms(univ.SequenceOf):
8989
# non-ASN.1 metadata definitions.
9090
class KeyValue(univ.Sequence):
9191
componentType = NamedTypes(
92-
NamedType('public', univ.OctetString()))
92+
NamedType('public', char.VisibleString())) #univ.OctetString()))
9393

9494
class PublicKey(univ.Sequence):
9595
componentType = NamedTypes(

tuf/encoding/metadata_definitions.asn1

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,13 @@ TUFMetadataDefinitions DEFINITIONS AUTOMATIC TAGS ::= BEGIN
147147
PublicKey ::= SEQUENCE {
148148
keytype VisibleString,
149149
scheme VisibleString,
150-
keyval SEQUENCE {
151-
type VisibleString, -- expect 'public'
152-
value OCTET STRING
150+
keyval SEQUENCE OF SEQUENCE { -- even though we only expect 1
151+
type VisibleString, -- expect 'public'
152+
-- value here would ideally be an OCTET STRING, but for now, for
153+
-- compatibility with both ed25519 (hexstring that translates naturally to
154+
-- an OCTET STRING) and RSA (ASCII-prefixed Base64 that requires some
155+
-- translation), we'll just use an inefficient unicode string....
156+
value VisibleString
153157
},
154158
keyid-hash-algorithms SEQUENCE OF VisibleString
155159
}

0 commit comments

Comments
 (0)