| 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/digest.h> |
| 21 | #include <openssl/dsa.h> |
| 22 | #include <openssl/evp.h> |
| 23 | #include <openssl/mem.h> |
| 24 | #include <openssl/rsa.h> |
| 25 | #include <openssl/stack.h> |
| 26 |
| 27 | #include "../asn1/internal.h" |
| 28 | #include "internal.h" |
| 29 |
| 30 |
| 31 | int X509_verify(X509 *x509, EVP_PKEY *pkey) { |
| 32 | if (X509_ALGOR_cmp(x509->sig_alg, x509->cert_info->signature)) { |
| 33 | OPENSSL_PUT_ERROR(X509, X509_R_SIGNATURE_ALGORITHM_MISMATCH); |
| 34 | return 0; |
| 35 | } |
| 36 | return ASN1_item_verify(ASN1_ITEM_rptr(X509_CINF), x509->sig_alg, |
| 37 | x509->signature, x509->cert_info, pkey); |
| 38 | } |
| 39 |
| 40 | int X509_REQ_verify(X509_REQ *req, EVP_PKEY *pkey) { |
| 41 | return ASN1_item_verify(ASN1_ITEM_rptr(X509_REQ_INFO), req->sig_alg, |
| 42 | req->signature, req->req_info, pkey); |
| 43 | } |
| 44 |
| 45 | int X509_sign(X509 *x, EVP_PKEY *pkey, const EVP_MD *md) { |
| 46 | asn1_encoding_clear(&x->cert_info->enc); |
| 47 | return (ASN1_item_sign(ASN1_ITEM_rptr(X509_CINF), x->cert_info->signature, |
| 48 | x->sig_alg, x->signature, x->cert_info, pkey, md)); |
| 49 | } |
| 50 |
| 51 | int X509_sign_ctx(X509 *x, EVP_MD_CTX *ctx) { |
| 52 | asn1_encoding_clear(&x->cert_info->enc); |
| 53 | return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_CINF), x->cert_info->signature, |
| 54 | x->sig_alg, x->signature, x->cert_info, ctx); |
| 55 | } |
| 56 |
| 57 | int X509_REQ_sign(X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md) { |
| 58 | asn1_encoding_clear(&x->req_info->enc); |
| 59 | return (ASN1_item_sign(ASN1_ITEM_rptr(X509_REQ_INFO), x->sig_alg, NULL, |
| 60 | x->signature, x->req_info, pkey, md)); |
| 61 | } |
| 62 |
| 63 | int X509_REQ_sign_ctx(X509_REQ *x, EVP_MD_CTX *ctx) { |
| 64 | asn1_encoding_clear(&x->req_info->enc); |
| 65 | return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_REQ_INFO), x->sig_alg, NULL, |
| 66 | x->signature, x->req_info, ctx); |
| 67 | } |
| 68 |
| 69 | int X509_CRL_sign(X509_CRL *x, EVP_PKEY *pkey, const EVP_MD *md) { |
| 70 | asn1_encoding_clear(&x->crl->enc); |
| 71 | return (ASN1_item_sign(ASN1_ITEM_rptr(X509_CRL_INFO), x->crl->sig_alg, |
| 72 | x->sig_alg, x->signature, x->crl, pkey, md)); |
| 73 | } |
| 74 |
| 75 | int X509_CRL_sign_ctx(X509_CRL *x, EVP_MD_CTX *ctx) { |
| 76 | asn1_encoding_clear(&x->crl->enc); |
| 77 | return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_CRL_INFO), x->crl->sig_alg, |
| 78 | x->sig_alg, x->signature, x->crl, ctx); |
| 79 | } |
| 80 |
| 81 | int NETSCAPE_SPKI_sign(NETSCAPE_SPKI *x, EVP_PKEY *pkey, const EVP_MD *md) { |
| 82 | return (ASN1_item_sign(ASN1_ITEM_rptr(NETSCAPE_SPKAC), x->sig_algor, NULL, |
| 83 | x->signature, x->spkac, pkey, md)); |
| 84 | } |
| 85 |
| 86 | int NETSCAPE_SPKI_verify(NETSCAPE_SPKI *spki, EVP_PKEY *pkey) { |
| 87 | return (ASN1_item_verify(ASN1_ITEM_rptr(NETSCAPE_SPKAC), spki->sig_algor, |
| 88 | spki->signature, spki->spkac, pkey)); |
| 89 | } |
| 90 |
| 91 | X509_CRL *d2i_X509_CRL_fp(FILE *fp, X509_CRL **crl) { |
| 92 | return reinterpret_cast<X509_CRL *>( |
| 93 | ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509_CRL), fp, crl)); |
| 94 | } |
| 95 |
| 96 | int i2d_X509_CRL_fp(FILE *fp, X509_CRL *crl) { |
| 97 | return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509_CRL), fp, crl); |
| 98 | } |
| 99 |
| 100 | X509_CRL *d2i_X509_CRL_bio(BIO *bp, X509_CRL **crl) { |
| 101 | return reinterpret_cast<X509_CRL *>( |
| 102 | ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509_CRL), bp, crl)); |
| 103 | } |
| 104 |
| 105 | int i2d_X509_CRL_bio(BIO *bp, X509_CRL *crl) { |
| 106 | return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509_CRL), bp, crl); |
| 107 | } |
| 108 |
| 109 | X509_REQ *d2i_X509_REQ_fp(FILE *fp, X509_REQ **req) { |
| 110 | return reinterpret_cast<X509_REQ *>( |
| 111 | ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509_REQ), fp, req)); |
| 112 | } |
| 113 |
| 114 | int i2d_X509_REQ_fp(FILE *fp, X509_REQ *req) { |
| 115 | return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509_REQ), fp, req); |
| 116 | } |
| 117 |
| 118 | X509_REQ *d2i_X509_REQ_bio(BIO *bp, X509_REQ **req) { |
| 119 | return reinterpret_cast<X509_REQ *>( |
| 120 | ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509_REQ), bp, req)); |
| 121 | } |
| 122 |
| 123 | int i2d_X509_REQ_bio(BIO *bp, X509_REQ *req) { |
| 124 | return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509_REQ), bp, req); |
| 125 | } |
| 126 |
| 127 |
| 128 | #define IMPLEMENT_D2I_FP(type, name, bio_func) \ |
| 129 | type *name(FILE *fp, type **obj) { \ |
| 130 | BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE); \ |
| 131 | if (bio == NULL) { \ |
| 132 | return NULL; \ |
| 133 | } \ |
| 134 | type *ret = bio_func(bio, obj); \ |
| 135 | BIO_free(bio); \ |
| 136 | return ret; \ |
| 137 | } |
| 138 |
| 139 | #define IMPLEMENT_I2D_FP(type, name, bio_func) \ |
| 140 | int name(FILE *fp, type *obj) { \ |
| 141 | BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE); \ |
| 142 | if (bio == NULL) { \ |
| 143 | return 0; \ |
| 144 | } \ |
| 145 | int ret = bio_func(bio, obj); \ |
| 146 | BIO_free(bio); \ |
| 147 | return ret; \ |
| 148 | } |
| 149 |
| 150 | IMPLEMENT_D2I_FP(X509, d2i_X509_fp, d2i_X509_bio) |
| 151 | IMPLEMENT_I2D_FP(X509, i2d_X509_fp, i2d_X509_bio) |
| 152 |
| 153 | IMPLEMENT_D2I_FP(RSA, d2i_RSAPrivateKey_fp, d2i_RSAPrivateKey_bio) |
| 154 | IMPLEMENT_I2D_FP(RSA, i2d_RSAPrivateKey_fp, i2d_RSAPrivateKey_bio) |
| 155 |
| 156 | IMPLEMENT_D2I_FP(RSA, d2i_RSAPublicKey_fp, d2i_RSAPublicKey_bio) |
| 157 | IMPLEMENT_I2D_FP(RSA, i2d_RSAPublicKey_fp, i2d_RSAPublicKey_bio) |
| 158 |
| 159 | IMPLEMENT_D2I_FP(RSA, d2i_RSA_PUBKEY_fp, d2i_RSA_PUBKEY_bio) |
| 160 | IMPLEMENT_I2D_FP(RSA, i2d_RSA_PUBKEY_fp, i2d_RSA_PUBKEY_bio) |
| 161 |
| 162 | #define IMPLEMENT_D2I_BIO(type, name, d2i_func) \ |
| 163 | type *name(BIO *bio, type **obj) { \ |
| 164 | uint8_t *data; \ |
| 165 | size_t len; \ |
| 166 | if (!BIO_read_asn1(bio, &data, &len, 100 * 1024)) { \ |
| 167 | return NULL; \ |
| 168 | } \ |
| 169 | const uint8_t *ptr = data; \ |
| 170 | type *ret = d2i_func(obj, &ptr, (long)len); \ |
| 171 | OPENSSL_free(data); \ |
| 172 | return ret; \ |
| 173 | } |
| 174 |
| 175 | #define IMPLEMENT_I2D_BIO(type, name, i2d_func) \ |
| 176 | int name(BIO *bio, type *obj) { \ |
| 177 | uint8_t *data = NULL; \ |
| 178 | int len = i2d_func(obj, &data); \ |
| 179 | if (len < 0) { \ |
| 180 | return 0; \ |
| 181 | } \ |
| 182 | int ret = BIO_write_all(bio, data, len); \ |
| 183 | OPENSSL_free(data); \ |
| 184 | return ret; \ |
| 185 | } |
| 186 |
| 187 | IMPLEMENT_D2I_BIO(X509, d2i_X509_bio, d2i_X509) |
| 188 | IMPLEMENT_I2D_BIO(X509, i2d_X509_bio, i2d_X509) |
| 189 |
| 190 | IMPLEMENT_D2I_BIO(RSA, d2i_RSAPrivateKey_bio, d2i_RSAPrivateKey) |
| 191 | IMPLEMENT_I2D_BIO(RSA, i2d_RSAPrivateKey_bio, i2d_RSAPrivateKey) |
| 192 |
| 193 | IMPLEMENT_D2I_BIO(RSA, d2i_RSAPublicKey_bio, d2i_RSAPublicKey) |
| 194 | IMPLEMENT_I2D_BIO(RSA, i2d_RSAPublicKey_bio, i2d_RSAPublicKey) |
| 195 |
| 196 | IMPLEMENT_D2I_BIO(RSA, d2i_RSA_PUBKEY_bio, d2i_RSA_PUBKEY) |
| 197 | IMPLEMENT_I2D_BIO(RSA, i2d_RSA_PUBKEY_bio, i2d_RSA_PUBKEY) |
| 198 |
| 199 | IMPLEMENT_D2I_FP(DSA, d2i_DSAPrivateKey_fp, d2i_DSAPrivateKey_bio) |
| 200 | IMPLEMENT_I2D_FP(DSA, i2d_DSAPrivateKey_fp, i2d_DSAPrivateKey_bio) |
| 201 |
| 202 | IMPLEMENT_D2I_FP(DSA, d2i_DSA_PUBKEY_fp, d2i_DSA_PUBKEY_bio) |
| 203 | IMPLEMENT_I2D_FP(DSA, i2d_DSA_PUBKEY_fp, i2d_DSA_PUBKEY_bio) |
| 204 |
| 205 | IMPLEMENT_D2I_BIO(DSA, d2i_DSAPrivateKey_bio, d2i_DSAPrivateKey) |
| 206 | IMPLEMENT_I2D_BIO(DSA, i2d_DSAPrivateKey_bio, i2d_DSAPrivateKey) |
| 207 |
| 208 | IMPLEMENT_D2I_BIO(DSA, d2i_DSA_PUBKEY_bio, d2i_DSA_PUBKEY) |
| 209 | IMPLEMENT_I2D_BIO(DSA, i2d_DSA_PUBKEY_bio, i2d_DSA_PUBKEY) |
| 210 |
| 211 | IMPLEMENT_D2I_FP(EC_KEY, d2i_ECPrivateKey_fp, d2i_ECPrivateKey_bio) |
| 212 | IMPLEMENT_I2D_FP(EC_KEY, i2d_ECPrivateKey_fp, i2d_ECPrivateKey_bio) |
| 213 |
| 214 | IMPLEMENT_D2I_FP(EC_KEY, d2i_EC_PUBKEY_fp, d2i_EC_PUBKEY_bio) |
| 215 | IMPLEMENT_I2D_FP(EC_KEY, i2d_EC_PUBKEY_fp, i2d_EC_PUBKEY_bio) |
| 216 |
| 217 | IMPLEMENT_D2I_BIO(EC_KEY, d2i_ECPrivateKey_bio, d2i_ECPrivateKey) |
| 218 | IMPLEMENT_I2D_BIO(EC_KEY, i2d_ECPrivateKey_bio, i2d_ECPrivateKey) |
| 219 |
| 220 | IMPLEMENT_D2I_BIO(EC_KEY, d2i_EC_PUBKEY_bio, d2i_EC_PUBKEY) |
| 221 | IMPLEMENT_I2D_BIO(EC_KEY, i2d_EC_PUBKEY_bio, i2d_EC_PUBKEY) |
| 222 |
| 223 | int X509_pubkey_digest(const X509 *data, const EVP_MD *type, unsigned char *md, |
| 224 | unsigned int *len) { |
| 225 | ASN1_BIT_STRING *key; |
| 226 | key = X509_get0_pubkey_bitstr(data); |
| 227 | if (!key) { |
| 228 | return 0; |
| 229 | } |
| 230 | return EVP_Digest(key->data, key->length, md, len, type, NULL); |
| 231 | } |
| 232 |
| 233 | int X509_digest(const X509 *x509, const EVP_MD *md, uint8_t *out, |
| 234 | unsigned *out_len) { |
| 235 | uint8_t *der = NULL; |
| 236 | // TODO(https://crbug.com/boringssl/407): This function is not const-correct. |
| 237 | int der_len = i2d_X509((X509 *)x509, &der); |
| 238 | if (der_len < 0) { |
| 239 | return 0; |
| 240 | } |
| 241 |
| 242 | int ret = EVP_Digest(der, der_len, out, out_len, md, NULL); |
| 243 | OPENSSL_free(der); |
| 244 | return ret; |
| 245 | } |
| 246 |
| 247 | int X509_CRL_digest(const X509_CRL *data, const EVP_MD *type, unsigned char *md, |
| 248 | unsigned int *len) { |
| 249 | return ( |
| 250 | ASN1_item_digest(ASN1_ITEM_rptr(X509_CRL), type, (char *)data, md, len)); |
| 251 | } |
| 252 |
| 253 | int X509_REQ_digest(const X509_REQ *data, const EVP_MD *type, unsigned char *md, |
| 254 | unsigned int *len) { |
| 255 | return ( |
| 256 | ASN1_item_digest(ASN1_ITEM_rptr(X509_REQ), type, (char *)data, md, len)); |
| 257 | } |
| 258 |
| 259 | int X509_NAME_digest(const X509_NAME *data, const EVP_MD *type, |
| 260 | unsigned char *md, unsigned int *len) { |
| 261 | return ( |
| 262 | ASN1_item_digest(ASN1_ITEM_rptr(X509_NAME), type, (char *)data, md, len)); |
| 263 | } |
| 264 |
| 265 | IMPLEMENT_D2I_FP(X509_SIG, d2i_PKCS8_fp, d2i_PKCS8_bio) |
| 266 | IMPLEMENT_I2D_FP(X509_SIG, i2d_PKCS8_fp, i2d_PKCS8_bio) |
| 267 |
| 268 | IMPLEMENT_D2I_BIO(X509_SIG, d2i_PKCS8_bio, d2i_X509_SIG) |
| 269 | IMPLEMENT_I2D_BIO(X509_SIG, i2d_PKCS8_bio, i2d_X509_SIG) |
| 270 |
| 271 | IMPLEMENT_D2I_FP(PKCS8_PRIV_KEY_INFO, d2i_PKCS8_PRIV_KEY_INFO_fp, |
| 272 | d2i_PKCS8_PRIV_KEY_INFO_bio) |
| 273 | IMPLEMENT_I2D_FP(PKCS8_PRIV_KEY_INFO, i2d_PKCS8_PRIV_KEY_INFO_fp, |
| 274 | i2d_PKCS8_PRIV_KEY_INFO_bio) |
| 275 |
| 276 | int i2d_PKCS8PrivateKeyInfo_fp(FILE *fp, EVP_PKEY *key) { |
| 277 | PKCS8_PRIV_KEY_INFO *p8inf; |
| 278 | int ret; |
| 279 | p8inf = EVP_PKEY2PKCS8(key); |
| 280 | if (!p8inf) { |
| 281 | return 0; |
| 282 | } |
| 283 | ret = i2d_PKCS8_PRIV_KEY_INFO_fp(fp, p8inf); |
| 284 | PKCS8_PRIV_KEY_INFO_free(p8inf); |
| 285 | return ret; |
| 286 | } |
| 287 |
| 288 | IMPLEMENT_D2I_FP(EVP_PKEY, d2i_PrivateKey_fp, d2i_PrivateKey_bio) |
| 289 | IMPLEMENT_I2D_FP(EVP_PKEY, i2d_PrivateKey_fp, i2d_PrivateKey_bio) |
| 290 |
| 291 | IMPLEMENT_D2I_FP(EVP_PKEY, d2i_PUBKEY_fp, d2i_PUBKEY_bio) |
| 292 | IMPLEMENT_I2D_FP(EVP_PKEY, i2d_PUBKEY_fp, i2d_PUBKEY_bio) |
| 293 |
| 294 | IMPLEMENT_D2I_BIO(PKCS8_PRIV_KEY_INFO, d2i_PKCS8_PRIV_KEY_INFO_bio, |
| 295 | d2i_PKCS8_PRIV_KEY_INFO) |
| 296 | IMPLEMENT_I2D_BIO(PKCS8_PRIV_KEY_INFO, i2d_PKCS8_PRIV_KEY_INFO_bio, |
| 297 | i2d_PKCS8_PRIV_KEY_INFO) |
| 298 |
| 299 | int i2d_PKCS8PrivateKeyInfo_bio(BIO *bp, EVP_PKEY *key) { |
| 300 | PKCS8_PRIV_KEY_INFO *p8inf; |
| 301 | int ret; |
| 302 | p8inf = EVP_PKEY2PKCS8(key); |
| 303 | if (!p8inf) { |
| 304 | return 0; |
| 305 | } |
| 306 | ret = i2d_PKCS8_PRIV_KEY_INFO_bio(bp, p8inf); |
| 307 | PKCS8_PRIV_KEY_INFO_free(p8inf); |
| 308 | return ret; |
| 309 | } |
| 310 |
| 311 | IMPLEMENT_D2I_BIO(EVP_PKEY, d2i_PrivateKey_bio, d2i_AutoPrivateKey) |
| 312 | IMPLEMENT_I2D_BIO(EVP_PKEY, i2d_PrivateKey_bio, i2d_PrivateKey) |
| 313 |
| 314 | IMPLEMENT_D2I_BIO(EVP_PKEY, d2i_PUBKEY_bio, d2i_PUBKEY) |
| 315 | IMPLEMENT_I2D_BIO(EVP_PKEY, i2d_PUBKEY_bio, i2d_PUBKEY) |
| 316 |
| 317 | IMPLEMENT_D2I_BIO(DH, d2i_DHparams_bio, d2i_DHparams) |
| 318 | IMPLEMENT_I2D_BIO(const DH, i2d_DHparams_bio, i2d_DHparams) |
| 319 |