| 1 |
/* |
| 2 |
* Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 4 |
* Licensed under the Apache License 2.0 (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 <stdio.h> |
| 11 |
#include "internal/cryptlib.h" |
| 12 |
#include <openssl/asn1t.h> |
| 13 |
#include <openssl/x509.h> |
| 14 |
#include "crypto/x509.h" |
| 15 |
|
| 16 |
/*- |
| 17 |
* X509_REQ_INFO is handled in an unusual way to get round |
| 18 |
* invalid encodings. Some broken certificate requests don't |
| 19 |
* encode the attributes field if it is empty. This is in |
| 20 |
* violation of PKCS#10 but we need to tolerate it. We do |
| 21 |
* this by making the attributes field OPTIONAL then using |
| 22 |
* the callback to initialise it to an empty STACK. |
| 23 |
* |
| 24 |
* This means that the field will be correctly encoded unless |
| 25 |
* we NULL out the field. |
| 26 |
* |
| 27 |
* As a result we no longer need the req_kludge field because |
| 28 |
* the information is now contained in the attributes field: |
| 29 |
* 1. If it is NULL then it's the invalid omission. |
| 30 |
* 2. If it is empty it is the correct encoding. |
| 31 |
* 3. If it is not empty then some attributes are present. |
| 32 |
* |
| 33 |
*/ |
| 34 |
|
| 35 |
static int rinf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, |
| 36 |
void *exarg) |
| 37 |
{ |
| 38 |
X509_REQ_INFO *rinf = (X509_REQ_INFO *)*pval; |
| 39 |
|
| 40 |
if (operation == ASN1_OP_NEW_POST) { |
| 41 |
rinf->attributes = sk_X509_ATTRIBUTE_new_null(); |
| 42 |
if (!rinf->attributes) |
| 43 |
return 0; |
| 44 |
} |
| 45 |
return 1; |
| 46 |
} |
| 47 |
|
| 48 |
static int req_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, |
| 49 |
void *exarg) |
| 50 |
{ |
| 51 |
X509_REQ *ret = (X509_REQ *)*pval; |
| 52 |
|
| 53 |
switch (operation) { |
| 54 |
case ASN1_OP_D2I_PRE: |
| 55 |
ASN1_OCTET_STRING_free(ret->distinguishing_id); |
| 56 |
/* fall through */ |
| 57 |
case ASN1_OP_NEW_POST: |
| 58 |
ret->distinguishing_id = NULL; |
| 59 |
break; |
| 60 |
|
| 61 |
case ASN1_OP_FREE_POST: |
| 62 |
ASN1_OCTET_STRING_free(ret->distinguishing_id); |
| 63 |
OPENSSL_free(ret->propq); |
| 64 |
break; |
| 65 |
case ASN1_OP_DUP_POST: { |
| 66 |
X509_REQ *old = exarg; |
| 67 |
|
| 68 |
if (!ossl_x509_req_set0_libctx(ret, old->libctx, old->propq)) |
| 69 |
return 0; |
| 70 |
if (old->req_info.pubkey != NULL) { |
| 71 |
EVP_PKEY *pkey = X509_PUBKEY_get0(old->req_info.pubkey); |
| 72 |
|
| 73 |
if (pkey != NULL) { |
| 74 |
pkey = EVP_PKEY_dup(pkey); |
| 75 |
if (pkey == NULL) { |
| 76 |
ERR_raise(ERR_LIB_X509, ERR_R_EVP_LIB); |
| 77 |
return 0; |
| 78 |
} |
| 79 |
if (!X509_PUBKEY_set(&ret->req_info.pubkey, pkey)) { |
| 80 |
EVP_PKEY_free(pkey); |
| 81 |
ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR); |
| 82 |
return 0; |
| 83 |
} |
| 84 |
EVP_PKEY_free(pkey); |
| 85 |
} |
| 86 |
} |
| 87 |
} break; |
| 88 |
case ASN1_OP_GET0_LIBCTX: { |
| 89 |
OSSL_LIB_CTX **libctx = exarg; |
| 90 |
|
| 91 |
*libctx = ret->libctx; |
| 92 |
} break; |
| 93 |
case ASN1_OP_GET0_PROPQ: { |
| 94 |
const char **propq = exarg; |
| 95 |
|
| 96 |
*propq = ret->propq; |
| 97 |
} break; |
| 98 |
} |
| 99 |
|
| 100 |
return 1; |
| 101 |
} |
| 102 |
|
| 103 |
ASN1_SEQUENCE_enc(X509_REQ_INFO, enc, rinf_cb) = { |
| 104 |
ASN1_SIMPLE(X509_REQ_INFO, version, ASN1_INTEGER), |
| 105 |
ASN1_SIMPLE(X509_REQ_INFO, subject, X509_NAME), |
| 106 |
ASN1_SIMPLE(X509_REQ_INFO, pubkey, X509_PUBKEY), |
| 107 |
/* This isn't really OPTIONAL but it gets round invalid |
| 108 |
* encodings |
| 109 |
*/ |
| 110 |
ASN1_IMP_SET_OF_OPT(X509_REQ_INFO, attributes, X509_ATTRIBUTE, 0) |
| 111 |
} ASN1_SEQUENCE_END_enc(X509_REQ_INFO, X509_REQ_INFO) |
| 112 |
|
| 113 |
IMPLEMENT_ASN1_FUNCTIONS(X509_REQ_INFO) |
| 114 |
|
| 115 |
ASN1_SEQUENCE_ref(X509_REQ, req_cb) = { |
| 116 |
ASN1_EMBED(X509_REQ, req_info, X509_REQ_INFO), |
| 117 |
ASN1_EMBED(X509_REQ, sig_alg, X509_ALGOR), |
| 118 |
ASN1_SIMPLE(X509_REQ, signature, ASN1_BIT_STRING) |
| 119 |
} ASN1_SEQUENCE_END_ref(X509_REQ, X509_REQ) |
| 120 |
|
| 121 |
IMPLEMENT_ASN1_FUNCTIONS(X509_REQ) |
| 122 |
|
| 123 |
IMPLEMENT_ASN1_DUP_FUNCTION(X509_REQ) |
| 124 |
|
| 125 |
void X509_REQ_set0_distinguishing_id(X509_REQ *x, ASN1_OCTET_STRING *d_id) |
| 126 |
{ |
| 127 |
ASN1_OCTET_STRING_free(x->distinguishing_id); |
| 128 |
x->distinguishing_id = d_id; |
| 129 |
} |
| 130 |
|
| 131 |
ASN1_OCTET_STRING *X509_REQ_get0_distinguishing_id(X509_REQ *x) |
| 132 |
{ |
| 133 |
return x->distinguishing_id; |
| 134 |
} |
| 135 |
|
| 136 |
/* |
| 137 |
* This should only be used if the X509_REQ object was embedded inside another |
| 138 |
* asn1 object and it needs a libctx to operate. |
| 139 |
* Use X509_REQ_new_ex() instead if possible. |
| 140 |
*/ |
| 141 |
int ossl_x509_req_set0_libctx(X509_REQ *x, OSSL_LIB_CTX *libctx, |
| 142 |
const char *propq) |
| 143 |
{ |
| 144 |
if (x != NULL) { |
| 145 |
x->libctx = libctx; |
| 146 |
OPENSSL_free(x->propq); |
| 147 |
x->propq = NULL; |
| 148 |
if (propq != NULL) { |
| 149 |
x->propq = OPENSSL_strdup(propq); |
| 150 |
if (x->propq == NULL) |
| 151 |
return 0; |
| 152 |
} |
| 153 |
} |
| 154 |
return 1; |
| 155 |
} |
| 156 |
|
| 157 |
X509_REQ *X509_REQ_new_ex(OSSL_LIB_CTX *libctx, const char *propq) |
| 158 |
{ |
| 159 |
X509_REQ *req = NULL; |
| 160 |
|
| 161 |
req = (X509_REQ *)ASN1_item_new((X509_REQ_it())); |
| 162 |
if (!ossl_x509_req_set0_libctx(req, libctx, propq)) { |
| 163 |
X509_REQ_free(req); |
| 164 |
req = NULL; |
| 165 |
} |
| 166 |
return req; |
| 167 |
} |