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