| 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/asn1.h> |
| 16 | #include <openssl/asn1t.h> |
| 17 | #include <openssl/bn.h> |
| 18 | #include <openssl/err.h> |
| 19 | #include <openssl/evp.h> |
| 20 | #include <openssl/mem.h> |
| 21 | #include <openssl/obj.h> |
| 22 | #include <openssl/pem.h> |
| 23 | #include <openssl/x509.h> |
| 24 |
| 25 | #include "../asn1/internal.h" |
| 26 | #include "internal.h" |
| 27 |
| 28 |
| 29 | long X509_REQ_get_version(const X509_REQ *req) { |
| 30 | return ASN1_INTEGER_get(req->req_info->version); |
| 31 | } |
| 32 |
| 33 | X509_NAME *X509_REQ_get_subject_name(const X509_REQ *req) { |
| 34 | return req->req_info->subject; |
| 35 | } |
| 36 |
| 37 | EVP_PKEY *X509_REQ_get_pubkey(const X509_REQ *req) { |
| 38 | if (req == NULL) { |
| 39 | return NULL; |
| 40 | } |
| 41 | return X509_PUBKEY_get(req->req_info->pubkey); |
| 42 | } |
| 43 |
| 44 | EVP_PKEY *X509_REQ_get0_pubkey(const X509_REQ *req) { |
| 45 | if (req == NULL) { |
| 46 | return NULL; |
| 47 | } |
| 48 | return X509_PUBKEY_get0(req->req_info->pubkey); |
| 49 | } |
| 50 |
| 51 | int X509_REQ_check_private_key(const X509_REQ *x, const EVP_PKEY *k) { |
| 52 | const EVP_PKEY *xk = X509_REQ_get0_pubkey(x); |
| 53 | if (xk == NULL) { |
| 54 | return 0; |
| 55 | } |
| 56 |
| 57 | int ret = EVP_PKEY_cmp(xk, k); |
| 58 | if (ret > 0) { |
| 59 | return 1; |
| 60 | } |
| 61 |
| 62 | switch (ret) { |
| 63 | case 0: |
| 64 | OPENSSL_PUT_ERROR(X509, X509_R_KEY_VALUES_MISMATCH); |
| 65 | return 0; |
| 66 | case -1: |
| 67 | OPENSSL_PUT_ERROR(X509, X509_R_KEY_TYPE_MISMATCH); |
| 68 | return 0; |
| 69 | case -2: |
| 70 | if (EVP_PKEY_id(k) == EVP_PKEY_EC) { |
| 71 | OPENSSL_PUT_ERROR(X509, ERR_R_EC_LIB); |
| 72 | } else { |
| 73 | OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE); |
| 74 | } |
| 75 | return 0; |
| 76 | } |
| 77 |
| 78 | return 0; |
| 79 | } |
| 80 |
| 81 | int X509_REQ_extension_nid(int req_nid) { |
| 82 | return req_nid == NID_ext_req || req_nid == NID_ms_ext_req; |
| 83 | } |
| 84 |
| 85 | STACK_OF(X509_EXTENSION) *X509_REQ_get_extensions(const X509_REQ *req) { |
| 86 | if (req == NULL || req->req_info == NULL) { |
| 87 | return NULL; |
| 88 | } |
| 89 |
| 90 | int idx = X509_REQ_get_attr_by_NID(req, NID_ext_req, -1); |
| 91 | if (idx == -1) { |
| 92 | idx = X509_REQ_get_attr_by_NID(req, NID_ms_ext_req, -1); |
| 93 | } |
| 94 | if (idx == -1) { |
| 95 | return NULL; |
| 96 | } |
| 97 |
| 98 | const X509_ATTRIBUTE *attr = X509_REQ_get_attr(req, idx); |
| 99 | // TODO(davidben): |X509_ATTRIBUTE_get0_type| is not const-correct. It should |
| 100 | // take and return a const pointer. |
| 101 | const ASN1_TYPE *ext = X509_ATTRIBUTE_get0_type((X509_ATTRIBUTE *)attr, 0); |
| 102 | if (!ext || ext->type != V_ASN1_SEQUENCE) { |
| 103 | return NULL; |
| 104 | } |
| 105 | const unsigned char *p = ext->value.sequence->data; |
| 106 | return (STACK_OF(X509_EXTENSION) *)ASN1_item_d2i( |
| 107 | NULL, &p, ext->value.sequence->length, ASN1_ITEM_rptr(X509_EXTENSIONS)); |
| 108 | } |
| 109 |
| 110 | // Add a STACK_OF extensions to a certificate request: allow alternative OIDs |
| 111 | // in case we want to create a non standard one. |
| 112 |
| 113 | int X509_REQ_add_extensions_nid(X509_REQ *req, |
| 114 | const STACK_OF(X509_EXTENSION) *exts, int nid) { |
| 115 | // Generate encoding of extensions |
| 116 | unsigned char *ext = NULL; |
| 117 | int ext_len = |
| 118 | ASN1_item_i2d((ASN1_VALUE *)exts, &ext, ASN1_ITEM_rptr(X509_EXTENSIONS)); |
| 119 | if (ext_len <= 0) { |
| 120 | return 0; |
| 121 | } |
| 122 | int ret = X509_REQ_add1_attr_by_NID(req, nid, V_ASN1_SEQUENCE, ext, ext_len); |
| 123 | OPENSSL_free(ext); |
| 124 | return ret; |
| 125 | } |
| 126 |
| 127 | // This is the normal usage: use the "official" OID |
| 128 | int X509_REQ_add_extensions(X509_REQ *req, |
| 129 | const STACK_OF(X509_EXTENSION) *exts) { |
| 130 | return X509_REQ_add_extensions_nid(req, exts, NID_ext_req); |
| 131 | } |
| 132 |
| 133 | int X509_REQ_get_attr_count(const X509_REQ *req) { |
| 134 | return (int)sk_X509_ATTRIBUTE_num(req->req_info->attributes); |
| 135 | } |
| 136 |
| 137 | int X509_REQ_get_attr_by_NID(const X509_REQ *req, int nid, int lastpos) { |
| 138 | const ASN1_OBJECT *obj = OBJ_nid2obj(nid); |
| 139 | if (obj == NULL) { |
| 140 | return -1; |
| 141 | } |
| 142 | return X509_REQ_get_attr_by_OBJ(req, obj, lastpos); |
| 143 | } |
| 144 |
| 145 | int X509_REQ_get_attr_by_OBJ(const X509_REQ *req, const ASN1_OBJECT *obj, |
| 146 | int lastpos) { |
| 147 | if (req->req_info->attributes == NULL) { |
| 148 | return -1; |
| 149 | } |
| 150 | lastpos++; |
| 151 | if (lastpos < 0) { |
| 152 | lastpos = 0; |
| 153 | } |
| 154 | int n = (int)sk_X509_ATTRIBUTE_num(req->req_info->attributes); |
| 155 | for (; lastpos < n; lastpos++) { |
| 156 | const X509_ATTRIBUTE *attr = |
| 157 | sk_X509_ATTRIBUTE_value(req->req_info->attributes, lastpos); |
| 158 | if (OBJ_cmp(attr->object, obj) == 0) { |
| 159 | return lastpos; |
| 160 | } |
| 161 | } |
| 162 | return -1; |
| 163 | } |
| 164 |
| 165 | X509_ATTRIBUTE *X509_REQ_get_attr(const X509_REQ *req, int loc) { |
| 166 | if (req->req_info->attributes == NULL || loc < 0 || |
| 167 | sk_X509_ATTRIBUTE_num(req->req_info->attributes) <= (size_t)loc) { |
| 168 | return NULL; |
| 169 | } |
| 170 | return sk_X509_ATTRIBUTE_value(req->req_info->attributes, loc); |
| 171 | } |
| 172 |
| 173 | X509_ATTRIBUTE *X509_REQ_delete_attr(X509_REQ *req, int loc) { |
| 174 | if (req->req_info->attributes == NULL || loc < 0 || |
| 175 | sk_X509_ATTRIBUTE_num(req->req_info->attributes) <= (size_t)loc) { |
| 176 | return NULL; |
| 177 | } |
| 178 | return sk_X509_ATTRIBUTE_delete(req->req_info->attributes, loc); |
| 179 | } |
| 180 |
| 181 | static int X509_REQ_add0_attr(X509_REQ *req, X509_ATTRIBUTE *attr) { |
| 182 | if (req->req_info->attributes == NULL) { |
| 183 | req->req_info->attributes = sk_X509_ATTRIBUTE_new_null(); |
| 184 | } |
| 185 | if (req->req_info->attributes == NULL || |
| 186 | !sk_X509_ATTRIBUTE_push(req->req_info->attributes, attr)) { |
| 187 | return 0; |
| 188 | } |
| 189 |
| 190 | return 1; |
| 191 | } |
| 192 |
| 193 | int X509_REQ_add1_attr(X509_REQ *req, const X509_ATTRIBUTE *attr) { |
| 194 | X509_ATTRIBUTE *new_attr = X509_ATTRIBUTE_dup(attr); |
| 195 | if (new_attr == NULL || !X509_REQ_add0_attr(req, new_attr)) { |
| 196 | X509_ATTRIBUTE_free(new_attr); |
| 197 | return 0; |
| 198 | } |
| 199 |
| 200 | return 1; |
| 201 | } |
| 202 |
| 203 | int X509_REQ_add1_attr_by_OBJ(X509_REQ *req, const ASN1_OBJECT *obj, |
| 204 | int attrtype, const unsigned char *data, |
| 205 | int len) { |
| 206 | X509_ATTRIBUTE *attr = |
| 207 | X509_ATTRIBUTE_create_by_OBJ(NULL, obj, attrtype, data, len); |
| 208 | if (attr == NULL || !X509_REQ_add0_attr(req, attr)) { |
| 209 | X509_ATTRIBUTE_free(attr); |
| 210 | return 0; |
| 211 | } |
| 212 |
| 213 | return 1; |
| 214 | } |
| 215 |
| 216 | int X509_REQ_add1_attr_by_NID(X509_REQ *req, int nid, int attrtype, |
| 217 | const unsigned char *data, int len) { |
| 218 | X509_ATTRIBUTE *attr = |
| 219 | X509_ATTRIBUTE_create_by_NID(NULL, nid, attrtype, data, len); |
| 220 | if (attr == NULL || !X509_REQ_add0_attr(req, attr)) { |
| 221 | X509_ATTRIBUTE_free(attr); |
| 222 | return 0; |
| 223 | } |
| 224 |
| 225 | return 1; |
| 226 | } |
| 227 |
| 228 | int X509_REQ_add1_attr_by_txt(X509_REQ *req, const char *attrname, int attrtype, |
| 229 | const unsigned char *data, int len) { |
| 230 | X509_ATTRIBUTE *attr = |
| 231 | X509_ATTRIBUTE_create_by_txt(NULL, attrname, attrtype, data, len); |
| 232 | if (attr == NULL || !X509_REQ_add0_attr(req, attr)) { |
| 233 | X509_ATTRIBUTE_free(attr); |
| 234 | return 0; |
| 235 | } |
| 236 |
| 237 | return 1; |
| 238 | } |
| 239 |
| 240 | void X509_REQ_get0_signature(const X509_REQ *req, const ASN1_BIT_STRING **psig, |
| 241 | const X509_ALGOR **palg) { |
| 242 | if (psig != NULL) { |
| 243 | *psig = req->signature; |
| 244 | } |
| 245 | if (palg != NULL) { |
| 246 | *palg = req->sig_alg; |
| 247 | } |
| 248 | } |
| 249 |
| 250 | int X509_REQ_get_signature_nid(const X509_REQ *req) { |
| 251 | return OBJ_obj2nid(req->sig_alg->algorithm); |
| 252 | } |
| 253 |
| 254 | int i2d_re_X509_REQ_tbs(X509_REQ *req, unsigned char **pp) { |
| 255 | asn1_encoding_clear(&req->req_info->enc); |
| 256 | return i2d_X509_REQ_INFO(req->req_info, pp); |
| 257 | } |
| 258 |