Skip to content

Convert DER formatted signature to raw before exporting and raw forma…#2

Open
BenjaminWareham wants to merge 4 commits intomasterfrom
raw_der_format
Open

Convert DER formatted signature to raw before exporting and raw forma…#2
BenjaminWareham wants to merge 4 commits intomasterfrom
raw_der_format

Conversation

@BenjaminWareham
Copy link
Owner

…tted signature to der before verifying

};

sign(data, key, stp)
let mut signer = Signer::new(stp, &key)?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add a comment here explaining what you are doing and why (signatures are not compatible with other JWT implementations).
Link to the issue you found, and the Python implementation.

@johnpbatty johnpbatty changed the title Convert der formatted signature to raw before exporting and raw forma… Convert DER formatted signature to raw before exporting and raw forma… Apr 13, 2018
Copy link
Collaborator

@johnpbatty johnpbatty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My main concern is the "magic" encode/decode functions using hard-coded offsets into vectors. Would be cleaner if you could use a DER lib to do the decoding/encoding.

src/lib.rs Outdated
res == 0
}

fn der_to_raw_signature(der_sig: Vec<u8>) -> Vec<u8>{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are taking ownership of the der_sig parameter here, which you don't really need to do. Would be better to take a reference (&Vec<u8>), or even better to take a reference to a slice (&[u8])

src/lib.rs Outdated

fn der_to_raw_signature(der_sig: Vec<u8>) -> Vec<u8>{
let len_r = der_sig[3] as usize;
let len_s = der_sig[len_r + 5] as usize;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a lot of magic going on in here, with hard-coded constants. At a minimum you need to add some comments to explain what is going on. Have you looked to see whether there are any library functions to extract raw signatures from DER (in OpenSSL, perhaps?).

src/lib.rs Outdated
let mut raw_sig: Vec<u8> =vec![];
if der_sig[4] == 0 {
let mut r = der_sig[5..len_r + 4].to_vec();
raw_sig.append(&mut r);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and below, better to avoid the mut variable, and use:

    raw_sig.extend_from_slice(der_sig[5..len_r + 4])

src/lib.rs Outdated
return raw_sig
}

fn raw_to_der_signature(raw_sig: Vec<u8>) -> Vec<u8>{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, make the parameter a slice reference: &[u8]

src/lib.rs Outdated

fn raw_to_der_signature(raw_sig: Vec<u8>) -> Vec<u8>{
let mut r: Vec<u8> = raw_sig[0..raw_sig.len()/2].to_vec();
let mut s: Vec<u8> = raw_sig[raw_sig.len()/2..].to_vec();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, the magic needs explanation, or replacing with a library function.

src/lib.rs Outdated
b2 = r.len()+1;
let mut r2 = vec![0];
r2.append(&mut r);
r = r2;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look like you are prepending a zero? Replace all of this with r.insert(0, 0). Rather than have a mut b2 that you update, would be better to just assign it after this point with r.len()

src/lib.rs Outdated
r = r2;
}

if s[0]>127{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting - add spaces as before

src/lib.rs Outdated
b3 = s.len() + 1;
let mut s2 = vec! [0];
s2.append(&mut s);
s = s2;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use s.insert(...) as before.

src/lib.rs Outdated
let mut der_sig: Vec<u8> = vec![48, b1 as u8, 2, b2 as u8];
der_sig.append(&mut r);
der_sig.append(&mut vec![2, b3 as u8]);
der_sig.append(&mut s);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should use Vec extend() rather than append() for all of the above - as the parameter does not need to be mutable. In general, avoid mutability unless you really need it!

src/lib.rs Outdated

fn der_to_raw_signature(der_sig: Vec<u8>) -> Vec<u8>{
let len_r = der_sig[3] as usize;
let len_s = der_sig[len_r + 5] as usize;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some DER parser/encoder crates:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants