-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement nonce signature (#80)
- Loading branch information
Showing
12 changed files
with
356 additions
and
68 deletions.
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
64 changes: 64 additions & 0 deletions
64
src/main/kotlin/tech/relaycorp/relaynet/messages/control/NonceSignature.kt
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package tech.relaycorp.relaynet.messages.control | ||
|
||
import org.bouncycastle.asn1.ASN1TaggedObject | ||
import org.bouncycastle.asn1.DEROctetString | ||
import tech.relaycorp.relaynet.OIDs | ||
import tech.relaycorp.relaynet.crypto.SignedData | ||
import tech.relaycorp.relaynet.crypto.SignedDataException | ||
import tech.relaycorp.relaynet.messages.InvalidMessageException | ||
import tech.relaycorp.relaynet.wrappers.asn1.ASN1Exception | ||
import tech.relaycorp.relaynet.wrappers.asn1.ASN1Utils | ||
import tech.relaycorp.relaynet.wrappers.x509.Certificate | ||
import java.security.PrivateKey | ||
|
||
/** | ||
* Nonce signature. | ||
*/ | ||
class NonceSignature(val nonce: ByteArray, val signerCertificate: Certificate) { | ||
/** | ||
* Serialize signature. | ||
*/ | ||
fun serialize(privateKey: PrivateKey): ByteArray { | ||
val plaintext = ASN1Utils.serializeSequence( | ||
arrayOf(OIDs.NONCE_SIGNATURE, DEROctetString(nonce)), | ||
false | ||
) | ||
val signedData = SignedData.sign( | ||
plaintext, | ||
privateKey, | ||
signerCertificate, | ||
encapsulatedCertificates = setOf(signerCertificate) | ||
) | ||
return signedData.serialize() | ||
} | ||
|
||
companion object { | ||
/** | ||
* Deserialize and validate nonce signature. | ||
*/ | ||
@Throws(InvalidMessageException::class) | ||
fun deserialize(serialization: ByteArray): NonceSignature { | ||
val signedData = try { | ||
SignedData.deserialize(serialization).also { it.verify() } | ||
} catch (exc: SignedDataException) { | ||
throw InvalidMessageException("SignedData value is invalid", exc) | ||
} | ||
val sequence = try { | ||
ASN1Utils.deserializeSequence(signedData.plaintext!!) | ||
} catch (exc: ASN1Exception) { | ||
throw InvalidMessageException("Signature plaintext is not a DER sequence", exc) | ||
} | ||
if (sequence.size < 2) { | ||
throw InvalidMessageException( | ||
"Signature sequence should have at least 2 items (got ${sequence.size})" | ||
) | ||
} | ||
val oid = ASN1Utils.deserializeOID(sequence.first() as ASN1TaggedObject) | ||
if (oid != OIDs.NONCE_SIGNATURE) { | ||
throw InvalidMessageException("Signature OID is invalid (got ${oid.id})") | ||
} | ||
val nonce = DEROctetString.getInstance(sequence[1] as ASN1TaggedObject, false) | ||
return NonceSignature(nonce.octets, signedData.signerCertificate!!) | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/main/kotlin/tech/relaycorp/relaynet/wrappers/asn1/ASN1Exception.kt
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package tech.relaycorp.relaynet.wrappers.asn1 | ||
|
||
class ASN1Exception(message: String) : Exception(message) | ||
class ASN1Exception(message: String, cause: Throwable? = null) : Exception(message, cause) |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package tech.relaycorp.relaynet | ||
|
||
import java.time.ZonedDateTime | ||
|
||
object FullCertPath { | ||
private val tomorrow = ZonedDateTime.now().plusDays(1) | ||
|
||
val PUBLIC_GW = issueGatewayCertificate( | ||
KeyPairSet.PUBLIC_GW.public, | ||
KeyPairSet.PUBLIC_GW.private, | ||
tomorrow | ||
) | ||
val PRIVATE_GW = issueGatewayCertificate( | ||
KeyPairSet.PRIVATE_GW.public, | ||
KeyPairSet.PUBLIC_GW.private, | ||
tomorrow, | ||
PUBLIC_GW | ||
) | ||
val PRIVATE_ENDPOINT = issueEndpointCertificate( | ||
KeyPairSet.PRIVATE_ENDPOINT.public, | ||
KeyPairSet.PRIVATE_GW.private, | ||
tomorrow, | ||
PRIVATE_GW | ||
) | ||
val PDA = issueParcelDeliveryAuthorization( | ||
KeyPairSet.PDA_GRANTEE.public, | ||
KeyPairSet.PRIVATE_ENDPOINT.private, | ||
tomorrow, | ||
PRIVATE_ENDPOINT | ||
) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package tech.relaycorp.relaynet | ||
|
||
import tech.relaycorp.relaynet.wrappers.generateRSAKeyPair | ||
|
||
object KeyPairSet { | ||
val PUBLIC_GW = generateRSAKeyPair() | ||
val PRIVATE_GW = generateRSAKeyPair() | ||
val PRIVATE_ENDPOINT = generateRSAKeyPair() | ||
val PDA_GRANTEE = generateRSAKeyPair() | ||
} |
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.