1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include <stdint.h>
16 #include <string.h>
17
18 #include "cose/cose.h"
19 #include "cose/cose_configure.h"
20 #include "cose_int.h"
21 #include "openssl/curve25519.h"
22 #include "openssl/is_boringssl.h"
23
24 // Gets the public key from a well-formed Ed25519 COSE_Key. On success populates
25 // |public_key| and returns true.
GetPublicKeyFromCbor(const cn_cbor * key,uint8_t public_key[32])26 static bool GetPublicKeyFromCbor(const cn_cbor *key, uint8_t public_key[32]) {
27 const int64_t kCoseKeyAlgLabel = 3;
28 const int64_t kCoseKeyOpsLabel = 4;
29 const uint64_t kCoseKeyOpsVerify = 2;
30 const int64_t kCoseAlgEdDSA = -8;
31
32 // Mandatory attributes.
33 cn_cbor *type = cn_cbor_mapget_int(key, COSE_Key_Type);
34 cn_cbor *curve = cn_cbor_mapget_int(key, COSE_Key_OPK_Curve);
35 cn_cbor *x = cn_cbor_mapget_int(key, COSE_Key_OPK_X);
36 if (!type || !curve || !x) {
37 return false;
38 }
39 if (type->type != CN_CBOR_UINT || type->v.uint != COSE_Key_Type_OKP) {
40 return false;
41 }
42 if (curve->type != CN_CBOR_UINT || curve->v.uint != COSE_Curve_Ed25519) {
43 return false;
44 }
45 if (x->type != CN_CBOR_BYTES || x->length != 32) {
46 return false;
47 }
48 // Optional attributes.
49 cn_cbor *alg = cn_cbor_mapget_int(key, kCoseKeyAlgLabel);
50 if (alg) {
51 if (alg->type != CN_CBOR_INT || alg->v.sint != kCoseAlgEdDSA) {
52 return false;
53 }
54 }
55 cn_cbor *ops = cn_cbor_mapget_int(key, kCoseKeyOpsLabel);
56 if (ops) {
57 if (ops->type != CN_CBOR_ARRAY || ops->length == 0) {
58 return false;
59 }
60 bool found_verify = false;
61 for (size_t i = 0; i < ops->length; ++i) {
62 cn_cbor *item = cn_cbor_index(ops, i);
63 if (!item || item->type != CN_CBOR_UINT) {
64 return false;
65 }
66 if (item->v.uint == kCoseKeyOpsVerify) {
67 found_verify = true;
68 }
69 }
70 if (!found_verify) {
71 return false;
72 }
73 }
74
75 memcpy(public_key, x->v.bytes, 32);
76 return true;
77 }
78
79 // A simple implementation of 'EdDSA_Verify' using boringssl. This function is
80 // required by 'COSE_Sign1_validate'.
EdDSA_Verify(COSE * cose_signer,int signature_index,COSE_KEY * cose_key,const byte * message,size_t message_size,cose_errback *)81 bool EdDSA_Verify(COSE *cose_signer, int signature_index, COSE_KEY *cose_key,
82 const byte *message, size_t message_size, cose_errback *) {
83 cn_cbor *signature = _COSE_arrayget_int(cose_signer, signature_index);
84 cn_cbor *key = cose_key->m_cborKey;
85 if (!signature || !key) {
86 return false;
87 }
88 if (signature->type != CN_CBOR_BYTES || signature->length != 64) {
89 return false;
90 }
91 uint8_t public_key[32];
92 if (!GetPublicKeyFromCbor(key, public_key)) {
93 return false;
94 }
95 return (1 == ED25519_verify(message, message_size, signature->v.bytes,
96 public_key));
97 }
98
99 // A stub for 'EdDSA_Sign'. This is unused, but helps make linkers happy.
EdDSA_Sign(COSE *,int,COSE_KEY *,const byte *,size_t,cose_errback *)100 bool EdDSA_Sign(COSE * /*cose_signer*/, int /*signature_index*/,
101 COSE_KEY * /*cose_key*/, const byte * /*message*/,
102 size_t /*message_size*/, cose_errback *) {
103 return false;
104 }
105