| 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 <stdio.h> |
| 16 |
| 17 | #include <openssl/asn1t.h> |
| 18 | #include <openssl/thread.h> |
| 19 | #include <openssl/x509.h> |
| 20 |
| 21 | #include "internal.h" |
| 22 |
| 23 |
| 24 | // X509_REQ_INFO is handled in an unusual way to get round invalid encodings. |
| 25 | // Some broken certificate requests don't encode the attributes field if it |
| 26 | // is empty. This is in violation of PKCS#10 but we need to tolerate it. We |
| 27 | // do this by making the attributes field OPTIONAL then using the callback to |
| 28 | // initialise it to an empty STACK. This means that the field will be |
| 29 | // correctly encoded unless we NULL out the field. |
| 30 |
| 31 | static int rinf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, |
| 32 | void *exarg) { |
| 33 | X509_REQ_INFO *rinf = (X509_REQ_INFO *)*pval; |
| 34 |
| 35 | if (operation == ASN1_OP_NEW_POST) { |
| 36 | rinf->attributes = sk_X509_ATTRIBUTE_new_null(); |
| 37 | if (!rinf->attributes) { |
| 38 | return 0; |
| 39 | } |
| 40 | } |
| 41 |
| 42 | if (operation == ASN1_OP_D2I_POST) { |
| 43 | // The only defined CSR version is v1(0). For compatibility, we also accept |
| 44 | // a hypothetical v3(2). Although not defined, older versions of certbot |
| 45 | // use it. See https://github.com/certbot/certbot/pull/9334. |
| 46 | long version = ASN1_INTEGER_get(rinf->version); |
| 47 | if (version != X509_REQ_VERSION_1 && version != 2) { |
| 48 | OPENSSL_PUT_ERROR(X509, X509_R_INVALID_VERSION); |
| 49 | return 0; |
| 50 | } |
| 51 | } |
| 52 |
| 53 | return 1; |
| 54 | } |
| 55 |
| 56 | ASN1_SEQUENCE_enc(X509_REQ_INFO, enc, rinf_cb) = { |
| 57 | ASN1_SIMPLE(X509_REQ_INFO, version, ASN1_INTEGER), |
| 58 | ASN1_SIMPLE(X509_REQ_INFO, subject, X509_NAME), |
| 59 | ASN1_SIMPLE(X509_REQ_INFO, pubkey, X509_PUBKEY), |
| 60 | // This isn't really OPTIONAL but it gets around invalid encodings. |
| 61 | ASN1_IMP_SET_OF_OPT(X509_REQ_INFO, attributes, X509_ATTRIBUTE, 0), |
| 62 | } ASN1_SEQUENCE_END_enc(X509_REQ_INFO, X509_REQ_INFO) |
| 63 |
| 64 | IMPLEMENT_ASN1_FUNCTIONS(X509_REQ_INFO) |
| 65 |
| 66 | ASN1_SEQUENCE(X509_REQ) = { |
| 67 | ASN1_SIMPLE(X509_REQ, req_info, X509_REQ_INFO), |
| 68 | ASN1_SIMPLE(X509_REQ, sig_alg, X509_ALGOR), |
| 69 | ASN1_SIMPLE(X509_REQ, signature, ASN1_BIT_STRING), |
| 70 | } ASN1_SEQUENCE_END(X509_REQ) |
| 71 |
| 72 | IMPLEMENT_ASN1_FUNCTIONS(X509_REQ) |
| 73 |
| 74 | IMPLEMENT_ASN1_DUP_FUNCTION(X509_REQ) |
| 75 |