public ECPoint GetPoint() { X9ECParameters p = this.GetCurve(); Org.BouncyCastle.Math.EC.ECPoint pubPoint; switch ((GeneralValuesInt)this[CoseKeyKeys.KeyType].AsInt32()) { case GeneralValuesInt.KeyType_EC2: CBORObject y = this.AsCBOR()[CoseKeyParameterKeys.EC_Y]; if (y.Type == CBORType.Boolean) { byte[] X = this.AsBytes(CoseKeyParameterKeys.EC_X); byte[] rgb = new byte[X.Length + 1]; Array.Copy(X, 0, rgb, 1, X.Length); rgb[0] = (byte)(2 + (y.AsBoolean() ? 1 : 0)); pubPoint = p.Curve.DecodePoint(rgb); } else { pubPoint = p.Curve.CreatePoint(this.AsBigInteger(CoseKeyParameterKeys.EC_X), this.AsBigInteger(CoseKeyParameterKeys.EC_Y)); } break; case GeneralValuesInt.KeyType_OKP: pubPoint = p.Curve.CreatePoint(this.AsBigInteger(CoseKeyParameterKeys.EC_X), new Org.BouncyCastle.Math.BigInteger("0")); break; default: throw new Exception("Unknown key type"); } return(pubPoint); }
public static bool HasFailMarker(CBORObject cn) { CBORObject cnFail = cn["fail"]; if (cnFail != null && cnFail.AsBoolean()) { return(true); } return(false); }
/// <inheritdoc /> protected override void InternalDecodeFromJSON(CBORObject json) { // Parse out the message from the JSON if (json.ContainsKey("signatures")) { if (json.ContainsKey("signature")) { throw new JoseException("Cannot have both 'signatures' and 'signature' present."); } CBORObject signers = json["signatures"]; if (signers.Type != CBORType.Array || signers.Count == 0) { throw new JoseException("field 'signatures' must be a non-empty array."); } for (int i = 0; i < signers.Count; i++) { Signer signer = new Signer(signers[i]); SignerList.Add(signer); } } else if (json.ContainsKey("signature")) { Signer signer = new Signer(json); SignerList.Add(signer); } else { throw new JoseException("field 'signatures' or 'signature' must be present."); } if (json.ContainsKey("payload")) { CBORObject b64 = SignerList[0].FindAttribute(CBORObject.FromObject("b64"), PROTECTED); if (b64 != null) { if (b64.Type != CBORType.Boolean) { throw new Exception("Invalid message"); } if (b64.AsBoolean()) { payloadB64 = Encoding.UTF8.GetBytes(json["payload"].AsString()); payload = base64urldecode(json["payload"].AsString()); } else { payload = Encoding.UTF8.GetBytes(json["payload"].AsString()); payloadB64 = payload; } } else { payloadB64 = Encoding.UTF8.GetBytes(json["payload"].AsString()); payload = base64urldecode(json["payload"].AsString()); } } else { throw new JoseException("field 'payload' must be present."); } }
/// <inheritdoc /> protected override CBORObject InternalEncodeToJSON(bool fCompact) { CBORObject obj = CBORObject.NewMap(); if (UnprotectedMap.Count > 0) { obj.Add("unprotected", UnprotectedMap); // Add unprotected attributes } // Look at the world of base64 encoded bodies. // If any signer has the b64 false, then all of them need to. // Then change our body if needed int b64Found = 0; bool b64Value = true; foreach (Signer key in SignerList) { CBORObject attr = key.FindAttribute(CBORObject.FromObject("b64"), PROTECTED); if (attr != null) { if (b64Found == 0) { b64Value = attr.AsBoolean(); } else if (b64Value != attr.AsBoolean()) { throw new JoseException("Not all signers using the same value for b64"); } b64Found += 1; } } if (b64Value) { obj.Add("payload", base64urlencode(payload)); } else { if (b64Found != SignerList.Count) { throw new JoseException("Not all signers using the same value for b64"); } obj.Add("payload", Encoding.UTF8.GetString(payload)); } if (SignerList.Count > 0) { CBORObject signers = CBORObject.NewArray(); foreach (Signer key in SignerList) { signers.Add(key.EncodeToJSON(payload)); } if (fCompact) { if (SignerList.Count > 1) { throw new JoseException("Compact format must be for single signer"); } if (signers[0].ContainsKey("protected")) { obj.Add("protected", signers[0]["protected"]); } obj.Add("signature", signers[0]["signature"]); } else { obj.Add("signatures", signers); } } else { throw new JoseException("Must have some signers"); } return(obj); }