1 // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of 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,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <openssl/x509.h>
16
17 #include <limits.h>
18
19 #include <openssl/asn1.h>
20 #include <openssl/asn1t.h>
21 #include <openssl/bytestring.h>
22 #include <openssl/err.h>
23 #include <openssl/evp.h>
24 #include <openssl/mem.h>
25 #include <openssl/obj.h>
26
27 #include "../internal.h"
28 #include "internal.h"
29
30
x509_pubkey_changed(X509_PUBKEY * pub)31 static void x509_pubkey_changed(X509_PUBKEY *pub) {
32 EVP_PKEY_free(pub->pkey);
33 pub->pkey = NULL;
34
35 // Re-encode the |X509_PUBKEY| to DER and parse it with EVP's APIs.
36 uint8_t *spki = NULL;
37 int spki_len = i2d_X509_PUBKEY(pub, &spki);
38 EVP_PKEY *pkey;
39 if (spki_len < 0) {
40 goto err;
41 }
42
43 CBS cbs;
44 CBS_init(&cbs, spki, (size_t)spki_len);
45 pkey = EVP_parse_public_key(&cbs);
46 if (pkey == NULL || CBS_len(&cbs) != 0) {
47 EVP_PKEY_free(pkey);
48 goto err;
49 }
50
51 pub->pkey = pkey;
52
53 err:
54 OPENSSL_free(spki);
55 // If the operation failed, clear errors. An |X509_PUBKEY| whose key we cannot
56 // parse is still a valid SPKI. It just cannot be converted to an |EVP_PKEY|.
57 ERR_clear_error();
58 }
59
pubkey_cb(int operation,ASN1_VALUE ** pval,const ASN1_ITEM * it,void * exarg)60 static int pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
61 void *exarg) {
62 X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
63 if (operation == ASN1_OP_FREE_POST) {
64 EVP_PKEY_free(pubkey->pkey);
65 } else if (operation == ASN1_OP_D2I_POST) {
66 x509_pubkey_changed(pubkey);
67 }
68 return 1;
69 }
70
ASN1_SEQUENCE_cb(X509_PUBKEY,pubkey_cb)71 ASN1_SEQUENCE_cb(X509_PUBKEY, pubkey_cb) = {
72 ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR),
73 ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING),
74 } ASN1_SEQUENCE_END_cb(X509_PUBKEY, X509_PUBKEY)
75
76 IMPLEMENT_ASN1_FUNCTIONS_const(X509_PUBKEY)
77
78 int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey) {
79 X509_PUBKEY *pk = NULL;
80 uint8_t *spki = NULL;
81 size_t spki_len;
82
83 if (x == NULL) {
84 return 0;
85 }
86
87 CBB cbb;
88 const uint8_t *p;
89 if (!CBB_init(&cbb, 0) || //
90 !EVP_marshal_public_key(&cbb, pkey) ||
91 !CBB_finish(&cbb, &spki, &spki_len) || //
92 spki_len > LONG_MAX) {
93 CBB_cleanup(&cbb);
94 OPENSSL_PUT_ERROR(X509, X509_R_PUBLIC_KEY_ENCODE_ERROR);
95 goto error;
96 }
97
98 p = spki;
99 pk = d2i_X509_PUBKEY(NULL, &p, (long)spki_len);
100 if (pk == NULL || p != spki + spki_len) {
101 OPENSSL_PUT_ERROR(X509, X509_R_PUBLIC_KEY_DECODE_ERROR);
102 goto error;
103 }
104
105 OPENSSL_free(spki);
106 X509_PUBKEY_free(*x);
107 *x = pk;
108
109 return 1;
110 error:
111 X509_PUBKEY_free(pk);
112 OPENSSL_free(spki);
113 return 0;
114 }
115
X509_PUBKEY_get0(const X509_PUBKEY * key)116 EVP_PKEY *X509_PUBKEY_get0(const X509_PUBKEY *key) {
117 if (key == NULL) {
118 return NULL;
119 }
120
121 if (key->pkey == NULL) {
122 OPENSSL_PUT_ERROR(X509, X509_R_PUBLIC_KEY_DECODE_ERROR);
123 return NULL;
124 }
125
126 return key->pkey;
127 }
128
X509_PUBKEY_get(const X509_PUBKEY * key)129 EVP_PKEY *X509_PUBKEY_get(const X509_PUBKEY *key) {
130 EVP_PKEY *pkey = X509_PUBKEY_get0(key);
131 if (pkey != NULL) {
132 EVP_PKEY_up_ref(pkey);
133 }
134 return pkey;
135 }
136
X509_PUBKEY_set0_param(X509_PUBKEY * pub,ASN1_OBJECT * obj,int param_type,void * param_value,uint8_t * key,int key_len)137 int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *obj, int param_type,
138 void *param_value, uint8_t *key, int key_len) {
139 if (!X509_ALGOR_set0(pub->algor, obj, param_type, param_value)) {
140 return 0;
141 }
142
143 ASN1_STRING_set0(pub->public_key, key, key_len);
144 // Set the number of unused bits to zero.
145 pub->public_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
146 pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
147
148 x509_pubkey_changed(pub);
149 return 1;
150 }
151
X509_PUBKEY_get0_param(ASN1_OBJECT ** out_obj,const uint8_t ** out_key,int * out_key_len,X509_ALGOR ** out_alg,X509_PUBKEY * pub)152 int X509_PUBKEY_get0_param(ASN1_OBJECT **out_obj, const uint8_t **out_key,
153 int *out_key_len, X509_ALGOR **out_alg,
154 X509_PUBKEY *pub) {
155 if (out_obj != NULL) {
156 *out_obj = pub->algor->algorithm;
157 }
158 if (out_key != NULL) {
159 *out_key = pub->public_key->data;
160 *out_key_len = pub->public_key->length;
161 }
162 if (out_alg != NULL) {
163 *out_alg = pub->algor;
164 }
165 return 1;
166 }
167
X509_PUBKEY_get0_public_key(const X509_PUBKEY * pub)168 const ASN1_BIT_STRING *X509_PUBKEY_get0_public_key(const X509_PUBKEY *pub) {
169 return pub->public_key;
170 }
171