This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 181
Adds most logic needed for validating cert chain #101
Open
leshi
wants to merge
1
commit into
master
Choose a base branch
from
acz1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,20 +6,20 @@ | |
|
||
package com.google.u2f.codec; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.DataInputStream; | ||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.security.cert.CertificateEncodingException; | ||
import java.security.cert.CertificateException; | ||
import java.security.cert.CertificateFactory; | ||
import java.security.cert.X509Certificate; | ||
|
||
import com.google.u2f.U2FException; | ||
import com.google.u2f.key.messages.AuthenticateRequest; | ||
import com.google.u2f.key.messages.AuthenticateResponse; | ||
import com.google.u2f.key.messages.RegisterRequest; | ||
import com.google.u2f.key.messages.RegisterResponse; | ||
import com.google.u2f.tools.X509Util; | ||
|
||
import org.bouncycastle.util.Arrays; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.DataInputStream; | ||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.security.cert.X509Certificate; | ||
|
||
/** | ||
* Raw message formats, as per FIDO U2F: Raw Message Formats - Draft 4 | ||
|
@@ -61,15 +61,11 @@ public static byte[] encodeRegisterResponse(RegisterResponse registerResponse) | |
throws U2FException { | ||
byte[] userPublicKey = registerResponse.getUserPublicKey(); | ||
byte[] keyHandle = registerResponse.getKeyHandle(); | ||
X509Certificate attestationCertificate = registerResponse.getAttestationCertificate(); | ||
X509Certificate[] attestationCertificateChain = registerResponse.getAttestationCertificateChain(); | ||
byte[] signature = registerResponse.getSignature(); | ||
|
||
byte[] attestationCertificateBytes; | ||
try { | ||
attestationCertificateBytes = attestationCertificate.getEncoded(); | ||
} catch (CertificateEncodingException e) { | ||
throw new U2FException("Error when encoding attestation certificate.", e); | ||
} | ||
attestationCertificateBytes = X509Util.encodeCertArray(attestationCertificateChain); | ||
|
||
if (keyHandle.length > 255) { | ||
throw new U2FException("keyHandle length cannot be longer than 255 bytes!"); | ||
|
@@ -95,10 +91,14 @@ public static RegisterResponse decodeRegisterResponse(byte[] data) throws U2FExc | |
inputStream.readFully(userPublicKey); | ||
byte[] keyHandle = new byte[inputStream.readUnsignedByte()]; | ||
inputStream.readFully(keyHandle); | ||
X509Certificate attestationCertificate = (X509Certificate) CertificateFactory.getInstance( | ||
"X.509").generateCertificate(inputStream); | ||
byte[] signature = new byte[inputStream.available()]; | ||
inputStream.readFully(signature); | ||
|
||
// We don't know the length of the signature or cert chain, so we tread carefully | ||
byte[] certsAndSig = new byte[inputStream.available()]; | ||
inputStream.readFully(certsAndSig); | ||
|
||
X509Certificate[] attestationCertificateChain = X509Util.parseCertificateChain(certsAndSig); | ||
byte[] signature = Arrays.copyOfRange(certsAndSig, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This took some reading to figure out. I think what you're doing is:
Is that right? Maybe a comment to that effect would help? |
||
X509Util.encodeCertArray(attestationCertificateChain).length, certsAndSig.length); | ||
|
||
if (inputStream.available() != 0) { | ||
throw new U2FException("Message ends with unexpected data"); | ||
|
@@ -110,11 +110,9 @@ public static RegisterResponse decodeRegisterResponse(byte[] data) throws U2FExc | |
REGISTRATION_RESERVED_BYTE_VALUE, reservedByte)); | ||
} | ||
|
||
return new RegisterResponse(userPublicKey, keyHandle, attestationCertificate, signature); | ||
return new RegisterResponse(userPublicKey, keyHandle, attestationCertificateChain, signature); | ||
} catch (IOException e) { | ||
throw new U2FException("Error when parsing raw RegistrationResponse", e); | ||
} catch (CertificateException e) { | ||
throw new U2FException("Error when parsing attestation certificate", e); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't java.util.Arrays.copyOfRange sufficient? It's in jre7, at least.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, removed.