blob: 4890f46b7ac4571e2be93cad2cbf460a495befd7 [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
Ricardo Salvetia2d5b1a2017-01-18 11:41:39 -020020#include <string.h>
21
Christopher Collins92ea77f2016-12-12 15:59:26 -080022#include "syscfg/syscfg.h"
23
24#if MYNEWT_VAL(BOOTUTIL_SIGN_RSA)
25#include "bootutil/sign_key.h"
26
27#include "mbedtls/rsa.h"
28#include "mbedtls/asn1.h"
29
30#include "bootutil_priv.h"
31
32static const uint8_t sha256_oid[] = {
33 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
34 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
35 0x00, 0x04, 0x20
36};
37
38/*
39 * Parse the public key used for signing. Simple RSA format.
40 */
41static int
42bootutil_parse_rsakey(mbedtls_rsa_context *ctx, uint8_t **p, uint8_t *end)
43{
44 int rc;
45 size_t len;
46
47 if ((rc = mbedtls_asn1_get_tag(p, end, &len,
48 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
49 return -1;
50 }
51
52 if (*p + len != end) {
53 return -2;
54 }
55
56 if ((rc = mbedtls_asn1_get_mpi(p, end, &ctx->N)) != 0 ||
57 (rc = mbedtls_asn1_get_mpi(p, end, &ctx->E)) != 0) {
58 return -3;
59 }
60
61 if (*p != end) {
62 return -4;
63 }
64
65 if ((rc = mbedtls_rsa_check_pubkey(ctx)) != 0) {
66 return -5;
67 }
68
69 ctx->len = mbedtls_mpi_size(&ctx->N);
70
71 return 0;
72}
73
74/*
75 * PKCS1.5 using RSA2048 computed over SHA256.
76 */
77static int
78bootutil_cmp_rsasig(mbedtls_rsa_context *ctx, uint8_t *hash, uint32_t hlen,
79 uint8_t *sig)
80{
81 uint8_t buf[MBEDTLS_MPI_MAX_SIZE];
82 uint8_t *p;
83
84 if (ctx->len != 256) {
85 return -1;
86 }
87
88 if (mbedtls_rsa_public(ctx, sig, buf)) {
89 return -1;
90 }
91
92 p = buf;
93
94 if (*p++ != 0 || *p++ != MBEDTLS_RSA_SIGN) {
95 return -1;
96 }
97
98 while (*p != 0) {
99 if (p >= buf + ctx->len - 1 || *p != 0xFF) {
100 return -1;
101 }
102 p++;
103 }
104 p++;
105
106 if ((p - buf) + sizeof(sha256_oid) + hlen != ctx->len) {
107 return -1;
108 }
109
110 if (memcmp(p, sha256_oid, sizeof(sha256_oid))) {
111 return -1;
112 }
113 p += sizeof(sha256_oid);
114
115 if (memcmp(p, hash, hlen)) {
116 return -1;
117 }
118
119 return 0;
120}
121
122int
123bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, int slen,
124 uint8_t key_id)
125{
126 mbedtls_rsa_context ctx;
127 int rc;
128 uint8_t *cp;
129 uint8_t *end;
130
131 mbedtls_rsa_init(&ctx, 0, 0);
132
133 cp = (uint8_t *)bootutil_keys[key_id].key;
134 end = cp + *bootutil_keys[key_id].len;
135
136 rc = bootutil_parse_rsakey(&ctx, &cp, end);
137 if (rc || slen != ctx.len) {
138 mbedtls_rsa_free(&ctx);
139 return rc;
140 }
141 rc = bootutil_cmp_rsasig(&ctx, hash, hlen, sig);
142 mbedtls_rsa_free(&ctx);
143
144 return rc;
145}
146#endif /* MYNEWT_VAL(BOOTUTIL_SIGN_RSA) */